コード例 #1
0
  @Override
  public PsiFileSystemItem findRoot(final Project project, @NotNull final VirtualFile file) {
    final ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
    VirtualFile contentRootForFile = index.getContentRootForFile(file);

    return contentRootForFile != null
        ? PsiManager.getInstance(project).findDirectory(contentRootForFile)
        : null;
  }
コード例 #2
0
 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);
 }
コード例 #3
0
 public boolean isContentOrSourceRoot() {
   if (myVDirectory != null) {
     final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
     final VirtualFile contentRoot = fileIndex.getContentRootForFile(myVDirectory);
     final VirtualFile sourceRoot = fileIndex.getSourceRootForFile(myVDirectory);
     if (myVDirectory.equals(contentRoot) || myVDirectory.equals(sourceRoot)) {
       return true;
     }
   }
   return false;
 }
コード例 #4
0
 public String getFQName() {
   final ProjectFileIndex index = ProjectRootManager.getInstance(myProject).getFileIndex();
   VirtualFile directory = myVDirectory;
   VirtualFile contentRoot = index.getContentRootForFile(directory);
   if (Comparing.equal(directory, contentRoot)) {
     return "";
   }
   if (contentRoot == null) {
     return "";
   }
   return VfsUtilCore.getRelativePath(directory, contentRoot, '/');
 }
コード例 #5
0
ファイル: MvcFramework.java プロジェクト: Zeratyl-SK/consulo
  @Nullable
  public VirtualFile findAppDirectory(@Nullable PsiElement element) {
    if (element == null) return null;

    PsiFile containingFile = element.getContainingFile().getOriginalFile();
    VirtualFile file = containingFile.getVirtualFile();
    if (file == null) return null;

    ProjectFileIndex index =
        ProjectRootManager.getInstance(containingFile.getProject()).getFileIndex();

    VirtualFile root = index.getContentRootForFile(file);
    if (root == null) return null;

    return root.findChild(getApplicationDirectoryName());
  }
コード例 #6
0
  public DirectoryNode(
      VirtualFile aDirectory,
      Project project,
      boolean compactPackages,
      boolean showFQName,
      VirtualFile baseDir,
      final VirtualFile[] contentRoots) {
    super(project);
    myVDirectory = aDirectory;
    final ProjectRootManager projectRootManager = ProjectRootManager.getInstance(project);
    final ProjectFileIndex index = projectRootManager.getFileIndex();
    String dirName = aDirectory.getName();
    if (showFQName) {
      final VirtualFile contentRoot = index.getContentRootForFile(myVDirectory);
      if (contentRoot != null) {
        if (Comparing.equal(myVDirectory, contentRoot)) {
          myFQName = dirName;
        } else {
          final VirtualFile sourceRoot = index.getSourceRootForFile(myVDirectory);
          if (Comparing.equal(myVDirectory, sourceRoot)) {
            myFQName = VfsUtilCore.getRelativePath(myVDirectory, contentRoot, '/');
          } else if (sourceRoot != null) {
            myFQName = VfsUtilCore.getRelativePath(myVDirectory, sourceRoot, '/');
          } else {
            myFQName = VfsUtilCore.getRelativePath(myVDirectory, contentRoot, '/');
          }
        }

        if (contentRoots.length > 1
            && ProjectRootsUtil.isModuleContentRoot(myVDirectory, project)) {
          myFQName = getContentRootName(baseDir, myFQName);
        }
      } else {
        myFQName = FilePatternPackageSet.getLibRelativePath(myVDirectory, index);
      }
      dirName = myFQName;
    } else {
      if (contentRoots.length > 1 && ProjectRootsUtil.isModuleContentRoot(myVDirectory, project)) {
        dirName = getContentRootName(baseDir, dirName);
      }
    }
    myDirName = dirName;
    myCompactPackages = compactPackages;
  }
コード例 #7
0
  public static String getReferencePath(Project project, VirtualFile file) {
    final LogicalRoot logicalRoot =
        LogicalRootsManager.getLogicalRootsManager(project).findLogicalRoot(file);
    if (logicalRoot != null) {
      return getRelativePath(file, logicalRoot.getVirtualFile());
    }

    ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    VirtualFile sourceRoot = fileIndex.getSourceRootForFile(file);
    if (sourceRoot != null) {
      return getRelativePath(file, sourceRoot);
    }

    VirtualFile root = fileIndex.getContentRootForFile(file);
    if (root != null) {
      return getRelativePath(file, root);
    }

    return file.getPath();
  }
コード例 #8
0
 @Nullable
 public static String getTestDataBasePath(PsiClass psiClass) {
   final PsiAnnotation annotation =
       AnnotationUtil.findAnnotationInHierarchy(
           psiClass, Collections.singleton(TEST_DATA_PATH_ANNOTATION_QUALIFIED_NAME));
   if (annotation != null) {
     final PsiAnnotationMemberValue value =
         annotation.findAttributeValue(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME);
     if (value instanceof PsiExpression) {
       final Project project = value.getProject();
       final PsiConstantEvaluationHelper evaluationHelper =
           JavaPsiFacade.getInstance(project).getConstantEvaluationHelper();
       final Object constantValue = evaluationHelper.computeConstantExpression(value, false);
       if (constantValue instanceof String) {
         String path = (String) constantValue;
         if (path.contains(CONTENT_ROOT_VARIABLE)) {
           final ProjectFileIndex fileIndex =
               ProjectRootManager.getInstance(project).getFileIndex();
           final VirtualFile file = psiClass.getContainingFile().getVirtualFile();
           if (file == null) {
             return null;
           }
           final VirtualFile contentRoot = fileIndex.getContentRootForFile(file);
           if (contentRoot == null) return null;
           path = path.replace(CONTENT_ROOT_VARIABLE, contentRoot.getPath());
         }
         if (path.contains(PROJECT_ROOT_VARIABLE)) {
           final VirtualFile baseDir = project.getBaseDir();
           if (baseDir == null) {
             return null;
           }
           path = path.replace(PROJECT_ROOT_VARIABLE, baseDir.getPath());
         }
         return path;
       }
     }
   }
   return null;
 }
コード例 #9
0
  @NotNull
  public static VirtualFile getTargetDirectoryFor(
      @NotNull Project project,
      @NotNull VirtualFile sourceFile,
      @Nullable String targetFile,
      @Nullable String targetPackage,
      boolean returnRoot) {
    boolean hasPackage = StringUtil.isNotEmpty(targetPackage);
    ProjectRootManager rootManager = ProjectRootManager.getInstance(project);
    ProjectFileIndex fileIndex = ProjectFileIndex.SERVICE.getInstance(project);
    Collection<VirtualFile> files =
        targetFile == null
            ? Collections.<VirtualFile>emptyList()
            : FilenameIndex.getVirtualFilesByName(
                project, targetFile, ProjectScope.getAllScope(project));

    VirtualFile existingFile = null;
    for (VirtualFile file : files) {
      String existingFilePackage = fileIndex.getPackageNameByDirectory(file.getParent());
      if (!hasPackage || existingFilePackage == null || targetPackage.equals(existingFilePackage)) {
        existingFile = file;
        break;
      }
    }

    VirtualFile existingFileRoot =
        existingFile == null
            ? null
            : fileIndex.isInSourceContent(existingFile)
                ? fileIndex.getSourceRootForFile(existingFile)
                : fileIndex.isInContent(existingFile)
                    ? fileIndex.getContentRootForFile(existingFile)
                    : null;

    boolean preferGenRoot =
        sourceFile.getFileType() == BnfFileType.INSTANCE
            || sourceFile.getFileType() == JFlexFileType.INSTANCE;
    boolean preferSourceRoot = hasPackage && !preferGenRoot;
    VirtualFile[] sourceRoots = rootManager.getContentSourceRoots();
    VirtualFile[] contentRoots = rootManager.getContentRoots();
    final VirtualFile virtualRoot =
        existingFileRoot != null
            ? existingFileRoot
            : preferSourceRoot && fileIndex.isInSource(sourceFile)
                ? fileIndex.getSourceRootForFile(sourceFile)
                : fileIndex.isInContent(sourceFile)
                    ? fileIndex.getContentRootForFile(sourceFile)
                    : getFirstElement(
                        preferSourceRoot && sourceRoots.length > 0 ? sourceRoots : contentRoots);
    if (virtualRoot == null) {
      fail(project, sourceFile, "Unable to guess target source root");
      throw new ProcessCanceledException();
    }
    try {
      String genDirName = Options.GEN_DIR.get();
      boolean newGenRoot = !fileIndex.isInSourceContent(virtualRoot);
      final String relativePath =
          (hasPackage && newGenRoot
                  ? genDirName + "/" + targetPackage
                  : hasPackage ? targetPackage : newGenRoot ? genDirName : "")
              .replace('.', '/');
      if (relativePath.isEmpty()) {
        return virtualRoot;
      } else {
        VirtualFile result =
            new WriteAction<VirtualFile>() {
              @Override
              protected void run(@NotNull Result<VirtualFile> result) throws Throwable {
                result.setResult(VfsUtil.createDirectoryIfMissing(virtualRoot, relativePath));
              }
            }.execute().throwException().getResultObject();
        VfsUtil.markDirtyAndRefresh(false, true, true, result);
        return returnRoot && newGenRoot
            ? ObjectUtils.assertNotNull(virtualRoot.findChild(genDirName))
            : returnRoot ? virtualRoot : result;
      }
    } catch (ProcessCanceledException ex) {
      throw ex;
    } catch (Exception ex) {
      fail(project, sourceFile, ex.getMessage());
      throw new ProcessCanceledException();
    }
  }
コード例 #10
0
  private boolean isInScopeOf(DebugProcessImpl debugProcess, String className) {
    final SourcePosition position = getSourcePosition();
    if (position != null) {
      final VirtualFile breakpointFile = position.getFile().getVirtualFile();
      final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
      if (breakpointFile != null
          && fileIndex.isUnderSourceRootOfType(breakpointFile, JavaModuleSourceRootTypes.SOURCES)) {
        if (debugProcess.getSearchScope().contains(breakpointFile)) {
          return true;
        }
        // apply filtering to breakpoints from content sources only, not for sources attached to
        // libraries
        final Collection<VirtualFile> candidates =
            findClassCandidatesInSourceContent(className, debugProcess.getSearchScope(), fileIndex);
        if (LOG.isDebugEnabled()) {
          LOG.debug(
              "Found "
                  + (candidates == null ? "null" : candidates.size())
                  + " candidate containing files for class "
                  + className);
        }
        if (candidates == null) {
          // If no candidates are found in scope then assume that class is loaded dynamically and
          // allow breakpoint
          return true;
        }

        // breakpointFile is not in scope here and there are some candidates in scope
        // for (VirtualFile classFile : candidates) {
        //  if (LOG.isDebugEnabled()) {
        //    LOG.debug("Breakpoint file: " + breakpointFile.getPath()+ "; candidate file: " +
        // classFile.getPath());
        //  }
        //  if (breakpointFile.equals(classFile)) {
        //    return true;
        //  }
        // }
        if (LOG.isDebugEnabled()) {
          final GlobalSearchScope scope = debugProcess.getSearchScope();
          final boolean contains = scope.contains(breakpointFile);
          final Project project = getProject();
          final List<VirtualFile> files =
              ContainerUtil.map(
                  JavaFullClassNameIndex.getInstance().get(className.hashCode(), project, scope),
                  new Function<PsiClass, VirtualFile>() {
                    @Override
                    public VirtualFile fun(PsiClass aClass) {
                      return aClass.getContainingFile().getVirtualFile();
                    }
                  });
          final List<VirtualFile> allFiles =
              ContainerUtil.map(
                  JavaFullClassNameIndex.getInstance()
                      .get(className.hashCode(), project, new EverythingGlobalScope(project)),
                  new Function<PsiClass, VirtualFile>() {
                    @Override
                    public VirtualFile fun(PsiClass aClass) {
                      return aClass.getContainingFile().getVirtualFile();
                    }
                  });
          final VirtualFile contentRoot = fileIndex.getContentRootForFile(breakpointFile);
          final Module module = fileIndex.getModuleForFile(breakpointFile);

          LOG.debug(
              "Did not find '"
                  + className
                  + "' in "
                  + scope
                  + "; contains="
                  + contains
                  + "; contentRoot="
                  + contentRoot
                  + "; module = "
                  + module
                  + "; all files in index are: "
                  + files
                  + "; all possible files are: "
                  + allFiles);
        }

        return false;
      }
    }
    return true;
  }