public static boolean isRoot(PsiFileSystemItem directory) {
   if (directory == null) return true;
   VirtualFile vFile = directory.getVirtualFile();
   if (vFile == null) return true;
   ProjectFileIndex fileIndex = ProjectFileIndex.SERVICE.getInstance(directory.getProject());
   return Comparing.equal(fileIndex.getClassRootForFile(vFile), vFile)
       || Comparing.equal(fileIndex.getContentRootForFile(vFile), vFile)
       || Comparing.equal(fileIndex.getSourceRootForFile(vFile), vFile);
 }
Example #2
0
 private PsiDirectory findOurSourceRoot(PsiFileSystemItem item) {
   for (VirtualFile sourceRoot : ModuleRootManager.getInstance(myIdeaModule).getSourceRoots()) {
     String rootPath = sourceRoot.toString();
     String itemPath = item.getVirtualFile().toString();
     if (itemPath.startsWith(rootPath)) {
       return PsiManager.getInstance(myIdeaModule.getProject()).findDirectory(sourceRoot);
     }
   }
   return null;
 }
 public void processIncludingFiles(
     PsiFile context, Processor<Pair<VirtualFile, FileIncludeInfo>> processor) {
   context = context.getOriginalFile();
   VirtualFile contextFile = context.getVirtualFile();
   if (contextFile == null) return;
   MultiMap<VirtualFile, FileIncludeInfoImpl> infoList =
       FileIncludeIndex.getIncludingFileCandidates(
           context.getName(), GlobalSearchScope.allScope(myProject));
   for (VirtualFile candidate : infoList.keySet()) {
     PsiFile psiFile = myPsiManager.findFile(candidate);
     if (psiFile == null || context.equals(psiFile)) continue;
     for (FileIncludeInfo info : infoList.get(candidate)) {
       PsiFileSystemItem item = resolveFileInclude(info, psiFile);
       if (item != null && contextFile.equals(item.getVirtualFile())) {
         if (!processor.process(Pair.create(candidate, info))) {
           return;
         }
       }
     }
   }
 }
 @Nullable
 public static List<File> asFileList(final PsiElement[] elements) {
   final List<File> result = new ArrayList<File>();
   for (PsiElement element : elements) {
     final PsiFileSystemItem psiFile;
     if (element instanceof PsiFileSystemItem) {
       psiFile = (PsiFileSystemItem) element;
     } else if (element instanceof PsiDirectoryContainer) {
       final PsiDirectory[] directories = ((PsiDirectoryContainer) element).getDirectories();
       psiFile = directories[0];
     } else {
       psiFile = element.getContainingFile();
     }
     if (psiFile != null) {
       VirtualFile vFile = psiFile.getVirtualFile();
       if (vFile != null && vFile.getFileSystem() instanceof LocalFileSystem) {
         result.add(new File(vFile.getPath()));
       }
     }
   }
   return result.isEmpty() ? null : result;
 }
  @Nullable
  public BundleManifest getManifest(@NotNull PsiFileSystemItem item) {
    VirtualFile file = item.getVirtualFile();
    if (file != null) {
      List<OrderEntry> entries = myIndex.getOrderEntriesForFile(file);
      if (entries.size() == 1 && entries.get(0) instanceof JdkOrderEntry) {
        return new JdkBundleManifest();
      }

      Module module = myIndex.getModuleForFile(file);
      if (module != null) {
        return getManifest(module);
      }

      VirtualFile libRoot = myIndex.getClassRootForFile(file);
      if (libRoot != null) {
        return getManifest(libRoot);
      }
    }

    return null;
  }
  @Nullable
  @Override
  public PsiElement[] getGotoDeclarationTargets(
      @Nullable PsiElement sourceElement, int offset, Editor editor) {
    ArrayList<PsiElement> result = new ArrayList<PsiElement>();
    if (sourceElement != null)
      for (PsiReference reference : sourceElement.getReferences())
        if (reference instanceof PsiPolyVariantReference)
          for (ResolveResult resolveResult :
              ((PsiPolyVariantReference) reference).multiResolve(false))
            result.add(resolveResult.getElement());
        else result.add(reference.resolve());

    // add shadowed variables declaration
    if (sourceElement instanceof PerlVariableNameElement) {
      PsiElement variable = sourceElement.getParent();

      if (variable instanceof PerlVariable) {
        PsiElement variableContainer = sourceElement.getParent().getParent();
        PsiFile myFile = sourceElement.getContainingFile();

        if (myFile instanceof PerlFileElement
            && (variableContainer instanceof PsiPerlVariableDeclarationLexical
                || variableContainer instanceof PsiPerlVariableDeclarationGlobal)) {
          PerlVariable shadowedVariable =
              ((PerlFileElement) myFile).getLexicalDeclaration((PerlVariable) variable);
          if (shadowedVariable != null && !result.contains(shadowedVariable))
            result.add(shadowedVariable);
        }
      }
    }
    // additional procesing for subname
    else if (sourceElement instanceof PerlSubNameElement) {
      PsiElement elementParent = sourceElement.getParent();

      // suppress declaration if there is a definition and declaration
      if (result.size() == 2
          && !(elementParent instanceof PerlSubDefinition
              || elementParent instanceof PerlSubDeclaration))
        if (result.get(0).getOriginalElement() instanceof PerlSubDeclaration
            && result.get(1).getOriginalElement() instanceof PerlSubDefinition) result.remove(0);

    }
    // string content to file jump
    else if (sourceElement instanceof PerlStringContentElement
        && ((PerlStringContentElement) sourceElement).looksLikePath()) {
      String tokenText = sourceElement.getText().replaceAll("\\\\", "/").replaceAll("/+", "/");
      Project project = sourceElement.getProject();

      String fileName = ((PerlStringContentElement) sourceElement).getContentFileName();

      for (String file : FilenameIndex.getAllFilenames(project))
        if (file.contains(fileName)) {
          // fixme somehow if includeDirectories is true - no files found
          for (PsiFileSystemItem fileItem :
              FilenameIndex.getFilesByName(project, file, GlobalSearchScope.allScope(project))) {
            String canonicalPath = fileItem.getVirtualFile().getCanonicalPath();
            if (canonicalPath != null)
              if (canonicalPath.contains(tokenText + ".")) // higer priority
              result.add(0, fileItem);
              else if (canonicalPath.contains(tokenText)) result.add(fileItem);
          }
          for (PsiFileSystemItem fileItem :
              FilenameIndex.getFilesByName(
                  project, file, GlobalSearchScope.allScope(project), true)) {
            String canonicalPath = fileItem.getVirtualFile().getCanonicalPath();
            if (canonicalPath != null) if (canonicalPath.contains(tokenText)) result.add(fileItem);
          }
        }
    }

    return result.toArray(new PsiElement[result.size()]);
  }