@Nullable
  public static VirtualFile getDirectory(@NotNull final FindModel findModel) {
    String directoryName = findModel.getDirectoryName();
    if (findModel.isProjectScope() || StringUtil.isEmpty(directoryName)) {
      return null;
    }

    String path = directoryName.replace(File.separatorChar, '/');
    VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(path);
    if (virtualFile == null || !virtualFile.isDirectory()) {
      virtualFile = null;
      for (LocalFileProvider provider :
          ((VirtualFileManagerEx) VirtualFileManager.getInstance()).getLocalFileProviders()) {
        VirtualFile file = provider.findLocalVirtualFileByPath(path);
        if (file != null && file.isDirectory()) {
          if (file.getChildren().length > 0) {
            virtualFile = file;
            break;
          }
          if (virtualFile == null) {
            virtualFile = file;
          }
        }
      }
    }
    return virtualFile;
  }
示例#2
0
 private static void retainOnlyJarsAndDirectories(List<VirtualFile> woSdk) {
   for (Iterator<VirtualFile> iterator = woSdk.iterator(); iterator.hasNext(); ) {
     VirtualFile file = iterator.next();
     final VirtualFile local = ArchiveVfsUtil.getVirtualFileForJar(file);
     final boolean dir = file.isDirectory();
     final String name = file.getName();
     if (LOG.isDebugEnabled()) {
       LOG.debug(
           "Considering: "
               + file.getPath()
               + "; local="
               + local
               + "; dir="
               + dir
               + "; name="
               + name);
     }
     if (dir || local != null) {
       continue;
     }
     if (name.endsWith(".jar")) {
       continue;
     }
     LOG.debug("Removing");
     iterator.remove();
   }
 }
 public static boolean isSupportedFileType(@NotNull VirtualFile virtualFile) {
   if (virtualFile.isDirectory()) return true;
   if (virtualFile.getFileType() == StdFileTypes.JAVA) return true;
   if (virtualFile.getFileType() == StdFileTypes.XML
       && !ProjectCoreUtil.isProjectOrWorkspaceFile(virtualFile)) return true;
   if ("groovy".equals(virtualFile.getExtension())) return true;
   return false;
 }
 @Nullable
 public final <T extends DomElement> DomFileElementImpl<T> getFileElement(XmlFile file) {
   if (file == null) return null;
   if (!(file.getFileType() instanceof DomSupportEnabled)) return null;
   final VirtualFile virtualFile = file.getVirtualFile();
   if (virtualFile != null && virtualFile.isDirectory()) return null;
   return this.<T>getOrCreateCachedValueProvider(file).getFileElement();
 }
 private void processFileOrDirectoryChange(final VirtualFile file) {
   if (!ProjectFileIndex.SERVICE.getInstance(myProject).isInContent(file)) return;
   if (!file.isDirectory()) {
     processFileChange(file);
   } else {
     processDirectoryChange(file);
   }
 }
示例#6
0
  public static void addAvailableScripts(
      final Collection<String> result, @Nullable final VirtualFile root) {
    if (root == null || !root.isDirectory()) {
      return;
    }

    final VirtualFile scripts = root.findChild("scripts");

    if (scripts == null || !scripts.isDirectory()) {
      return;
    }

    for (VirtualFile child : scripts.getChildren()) {
      if (isScriptFile(child)) {
        result.add(GroovyNamesUtil.camelToSnake(child.getNameWithoutExtension()));
      }
    }
  }
 // return true if continue to process sub-directories of the {@code file}, false if the file is
 // already processed
 private boolean doCountAndMarkUnresolved(
     @NotNull VirtualFile file, @NotNull Set<VirtualFile> result) {
   if (file.isDirectory()) {
     fileIsResolved.set(getAbsId(file));
     return result.add(file);
   }
   if (toResolve(file, myProject)) {
     result.add(file);
     fileIsResolved.clear(getAbsId(file));
   }
   return true;
 }
  public static void setDirectoryName(@NotNull FindModel model, @NotNull DataContext dataContext) {
    PsiElement psiElement = null;
    Project project = CommonDataKeys.PROJECT.getData(dataContext);

    if (project != null && !DumbServiceImpl.getInstance(project).isDumb()) {
      try {
        psiElement = CommonDataKeys.PSI_ELEMENT.getData(dataContext);
      } catch (IndexNotReadyException ignore) {
      }
    }

    String directoryName = null;

    if (psiElement instanceof PsiDirectory) {
      directoryName = ((PsiDirectory) psiElement).getVirtualFile().getPresentableUrl();
    }

    if (directoryName == null && psiElement instanceof PsiDirectoryContainer) {
      final PsiDirectory[] directories = ((PsiDirectoryContainer) psiElement).getDirectories();
      directoryName =
          directories.length == 1 ? directories[0].getVirtualFile().getPresentableUrl() : null;
    }

    Module module = LangDataKeys.MODULE_CONTEXT.getData(dataContext);
    if (module != null) {
      model.setModuleName(module.getName());
    }

    Editor editor = CommonDataKeys.EDITOR.getData(dataContext);
    if (model.getModuleName() == null || editor == null) {
      model.setDirectoryName(directoryName);
      model.setProjectScope(
          directoryName == null && module == null && !model.isCustomScope() || editor != null);
      if (directoryName != null) {
        model.setCustomScope(false); // to select "Directory: " radio button
      }

      // for convenience set directory name to directory of current file, note that we doesn't
      // change default projectScope
      if (directoryName == null) {
        VirtualFile virtualFile = CommonDataKeys.VIRTUAL_FILE.getData(dataContext);
        if (virtualFile != null && !virtualFile.isDirectory())
          virtualFile = virtualFile.getParent();
        if (virtualFile != null) model.setDirectoryName(virtualFile.getPresentableUrl());
      }
    }
  }
  private void countAndMarkUnresolved(
      @NotNull VirtualFile file, @NotNull final Set<VirtualFile> result, final boolean inDbOnly) {
    if (file.isDirectory()) {
      VfsUtilCore.visitChildrenRecursively(
          file,
          new VirtualFileVisitor() {
            @Override
            public boolean visitFile(@NotNull VirtualFile file) {
              return doCountAndMarkUnresolved(file, result);
            }

            @Nullable
            @Override
            public Iterable<VirtualFile> getChildrenIterable(@NotNull VirtualFile file) {
              return inDbOnly ? ((NewVirtualFile) file).iterInDbChildren() : null;
            }
          });
    } else {
      doCountAndMarkUnresolved(file, result);
    }
  }
示例#10
0
 private static boolean isScriptFile(VirtualFile virtualFile) {
   return !virtualFile.isDirectory() && isScriptFileName(virtualFile.getName());
 }
  @Override
  public void fileMoved(@NotNull VirtualFileMoveEvent event) {
    // let PushedFilePropertiesUpdater process all pending vfs events and update file properties
    // before we issue PSI events
    PushedFilePropertiesUpdater.getInstance(myProject).processPendingEvents();

    final VirtualFile vFile = event.getFile();

    final PsiDirectory oldParentDir = myFileManager.findDirectory(event.getOldParent());
    final PsiDirectory newParentDir = myFileManager.findDirectory(event.getNewParent());
    if (oldParentDir == null && newParentDir == null) return;

    final PsiElement oldElement =
        vFile.isDirectory()
            ? myFileManager.getCachedDirectory(vFile)
            : myFileManager.getCachedPsiFileInner(vFile);
    myFileManager.removeInvalidFilesAndDirs(true);
    final PsiElement newElement;
    final FileViewProvider newViewProvider;
    if (!vFile.isDirectory()) {
      newViewProvider = myFileManager.createFileViewProvider(vFile, true);
      newElement = newViewProvider.getPsi(myFileManager.findViewProvider(vFile).getBaseLanguage());
    } else {
      newElement = myFileManager.findDirectory(vFile);
      newViewProvider = null;
    }

    if (oldElement == null && newElement == null) return;

    ApplicationManager.getApplication()
        .runWriteAction(
            new ExternalChangeAction() {
              @Override
              public void run() {
                PsiTreeChangeEventImpl treeEvent = new PsiTreeChangeEventImpl(myManager);
                if (oldElement == null) {
                  myFileManager.setViewProvider(vFile, newViewProvider);
                  treeEvent.setParent(newParentDir);
                  treeEvent.setChild(newElement);
                  myManager.childAdded(treeEvent);
                } else {
                  if (newElement == null) {
                    myFileManager.setViewProvider(vFile, null);
                    treeEvent.setParent(oldParentDir);
                    treeEvent.setChild(oldElement);
                    myManager.childRemoved(treeEvent);
                  } else {
                    if (newElement instanceof PsiDirectory
                        || FileManagerImpl.areViewProvidersEquivalent(
                            newViewProvider, ((PsiFile) oldElement).getViewProvider())) {
                      treeEvent.setOldParent(oldParentDir);
                      treeEvent.setNewParent(newParentDir);
                      treeEvent.setChild(oldElement);
                      myManager.childMoved(treeEvent);
                    } else {
                      myFileManager.setViewProvider(vFile, newViewProvider);
                      PsiTreeChangeEventImpl treeRemoveEvent =
                          new PsiTreeChangeEventImpl(myManager);
                      treeRemoveEvent.setParent(oldParentDir);
                      treeRemoveEvent.setChild(oldElement);
                      myManager.childRemoved(treeRemoveEvent);
                      PsiTreeChangeEventImpl treeAddEvent = new PsiTreeChangeEventImpl(myManager);
                      treeAddEvent.setParent(newParentDir);
                      treeAddEvent.setChild(newElement);
                      myManager.childAdded(treeAddEvent);
                    }
                  }
                }
              }
            });
  }
  @Override
  public void propertyChanged(@NotNull final VirtualFilePropertyEvent event) {
    final String propertyName = event.getPropertyName();
    final VirtualFile vFile = event.getFile();

    final FileViewProvider oldFileViewProvider = myFileManager.findCachedViewProvider(vFile);
    final PsiFile oldPsiFile;
    if (oldFileViewProvider instanceof SingleRootFileViewProvider) {
      oldPsiFile =
          ((SingleRootFileViewProvider) oldFileViewProvider)
              .getCachedPsi(oldFileViewProvider.getBaseLanguage());
    } else {
      oldPsiFile = null;
    }

    VirtualFile parent = vFile.getParent();
    final PsiDirectory parentDir =
        oldPsiFile != null && parent != null
            ? myFileManager.findDirectory(parent)
            : getCachedDirectory(parent);

    if (oldFileViewProvider
            != null // there is no need to rebuild if there were no PSI in the first place
        && FileContentUtilCore.FORCE_RELOAD_REQUESTOR.equals(event.getRequestor())) {
      myFileManager.forceReload(vFile);
      return;
    }

    // do not suppress reparse request for light files
    if (parentDir == null) {
      boolean fire = VirtualFile.PROP_NAME.equals(propertyName) && vFile.isDirectory();
      if (fire) {
        PsiDirectory psiDir = myFileManager.getCachedDirectory(vFile);
        fire = psiDir != null;
      }
      if (!fire && !VirtualFile.PROP_WRITABLE.equals(propertyName)) {
        handleVfsChangeWithoutPsi(vFile);
        return;
      }
    }

    ((SmartPointerManagerImpl) SmartPointerManager.getInstance(myManager.getProject()))
        .fastenBelts(vFile, 0, null);
    ApplicationManager.getApplication()
        .runWriteAction(
            new ExternalChangeAction() {
              @Override
              public void run() {
                PsiTreeChangeEventImpl treeEvent = new PsiTreeChangeEventImpl(myManager);
                treeEvent.setParent(parentDir);

                if (VirtualFile.PROP_NAME.equals(propertyName)) {
                  if (vFile.isDirectory()) {
                    PsiDirectory psiDir = myFileManager.getCachedDirectory(vFile);
                    if (psiDir != null) {
                      if (myFileTypeManager.isFileIgnored(vFile)) {
                        myFileManager.removeFilesAndDirsRecursively(vFile);

                        treeEvent.setChild(psiDir);
                        myManager.childRemoved(treeEvent);
                      } else {
                        treeEvent.setElement(psiDir);
                        treeEvent.setPropertyName(PsiTreeChangeEvent.PROP_DIRECTORY_NAME);
                        treeEvent.setOldValue(event.getOldValue());
                        treeEvent.setNewValue(event.getNewValue());
                        myManager.propertyChanged(treeEvent);
                      }
                    } else {
                      PsiDirectory psiDir1 = myFileManager.findDirectory(vFile);
                      if (psiDir1 != null) {
                        treeEvent.setChild(psiDir1);
                        myManager.childAdded(treeEvent);
                      }
                    }
                  } else {
                    final FileViewProvider fileViewProvider =
                        myFileManager.createFileViewProvider(vFile, true);
                    final PsiFile newPsiFile =
                        fileViewProvider.getPsi(fileViewProvider.getBaseLanguage());
                    if (oldPsiFile != null) {
                      if (newPsiFile == null) {
                        myFileManager.setViewProvider(vFile, null);

                        treeEvent.setChild(oldPsiFile);
                        myManager.childRemoved(treeEvent);
                      } else if (!FileManagerImpl.areViewProvidersEquivalent(
                          fileViewProvider, oldFileViewProvider)) {
                        myFileManager.setViewProvider(vFile, fileViewProvider);

                        treeEvent.setOldChild(oldPsiFile);
                        treeEvent.setNewChild(newPsiFile);
                        myManager.childReplaced(treeEvent);
                      } else {
                        if (oldPsiFile instanceof PsiFileImpl) {
                          ((PsiFileImpl) oldPsiFile).clearCaches();
                        }
                        treeEvent.setElement(oldPsiFile);
                        treeEvent.setPropertyName(PsiTreeChangeEvent.PROP_FILE_NAME);
                        treeEvent.setOldValue(event.getOldValue());
                        treeEvent.setNewValue(event.getNewValue());
                        myManager.propertyChanged(treeEvent);
                      }
                    } else if (newPsiFile != null) {
                      myFileManager.setViewProvider(vFile, fileViewProvider);
                      if (parentDir != null) {
                        treeEvent.setChild(newPsiFile);
                        myManager.childAdded(treeEvent);
                      }
                    }
                  }
                } else if (VirtualFile.PROP_WRITABLE.equals(propertyName)) {
                  if (oldPsiFile == null) return;

                  treeEvent.setElement(oldPsiFile);
                  treeEvent.setPropertyName(PsiTreeChangeEvent.PROP_WRITABLE);
                  treeEvent.setOldValue(event.getOldValue());
                  treeEvent.setNewValue(event.getNewValue());
                  myManager.propertyChanged(treeEvent);
                } else if (VirtualFile.PROP_ENCODING.equals(propertyName)) {
                  if (oldPsiFile == null) return;

                  treeEvent.setElement(oldPsiFile);
                  treeEvent.setPropertyName(VirtualFile.PROP_ENCODING);
                  treeEvent.setOldValue(event.getOldValue());
                  treeEvent.setNewValue(event.getNewValue());
                  myManager.propertyChanged(treeEvent);
                }
              }
            });
  }