Ejemplo n.º 1
0
  @Nullable
  @Override
  public PsiPackage resolvePackage(
      @NotNull PsiPackageManager packageManager,
      @NotNull VirtualFile virtualFile,
      @NotNull Class<? extends ModuleExtension> extensionClass,
      String qualifiedName) {
    ProjectFileIndex fileIndexFacade =
        ProjectFileIndex.SERVICE.getInstance(packageManager.getProject());
    PsiManager psiManager = PsiManager.getInstance(packageManager.getProject());
    if (fileIndexFacade.isInLibraryClasses(virtualFile)) {

      List<OrderEntry> orderEntriesForFile = fileIndexFacade.getOrderEntriesForFile(virtualFile);
      for (OrderEntry orderEntry : orderEntriesForFile) {
        Module ownerModule = orderEntry.getOwnerModule();
        ModuleExtension extension = ModuleUtilCore.getExtension(ownerModule, extensionClass);
        if (extension != null) {
          for (PsiPackageSupportProvider p : PsiPackageSupportProvider.EP_NAME.getExtensions()) {
            if (p.isSupported(extension)) {
              return p.createPackage(psiManager, packageManager, extensionClass, qualifiedName);
            }
          }
        }
      }
    }
    return null;
  }
  @Nullable
  static String getRelativePath(final VirtualFile virtualFile, final Project project) {
    String url = virtualFile.getPresentableUrl();
    if (project == null) {
      return url;
    }
    VirtualFile root =
        ProjectFileIndex.SERVICE.getInstance(project).getContentRootForFile(virtualFile);
    if (root != null) {
      return root.getName()
          + File.separatorChar
          + VfsUtilCore.getRelativePath(virtualFile, root, File.separatorChar);
    }

    final VirtualFile baseDir = project.getBaseDir();
    if (baseDir != null) {
      //noinspection ConstantConditions
      final String projectHomeUrl = baseDir.getPresentableUrl();
      if (url.startsWith(projectHomeUrl)) {
        final String cont = url.substring(projectHomeUrl.length());
        if (cont.isEmpty()) return null;
        url = "..." + cont;
      }
    }
    return url;
  }
Ejemplo n.º 3
0
        @Override
        public void caretPositionChanged(CaretEvent event) {

          if (project.isDisposed()) {
            return;
          }

          VirtualFile file =
              FileDocumentManager.getInstance().getFile(event.getEditor().getDocument());

          // Make sure file exists
          if (file == null) {
            return;
          }

          // Make sure file is in the project
          if (!ProjectFileIndex.SERVICE.getInstance(project).isInSource(file)) {
            return;
          }

          int offset = event.getEditor().logicalPositionToOffset(event.getNewPosition());

          // Get path relative to project root (e.g. src/Sample.java)
          Path basePath = Paths.get(project.getBasePath());
          Path absoluteFilePath = Paths.get(file.getPath());
          String relativeFilePath = basePath.relativize(absoluteFilePath).toString();

          CursorMovement cursorMovement = new CursorMovement(-1, relativeFilePath, offset);

          for (EditorEvent editorEvent : events) {
            editorEvent.sendCursorMovement(cursorMovement);
          }
        }
 private void processFileOrDirectoryChange(final VirtualFile file) {
   if (!ProjectFileIndex.SERVICE.getInstance(myProject).isInContent(file)) return;
   if (!file.isDirectory()) {
     processFileChange(file);
   } else {
     processDirectoryChange(file);
   }
 }
 private VcsRootProblemNotifier(@NotNull Project project) {
   myProject = project;
   mySettings = VcsConfiguration.getInstance(myProject);
   myChangeListManager = ChangeListManager.getInstance(project);
   myProjectFileIndex = ProjectFileIndex.SERVICE.getInstance(myProject);
   myVcsManager = ProjectLevelVcsManager.getInstance(project);
   myReportedUnregisteredRoots = new HashSet<>(mySettings.IGNORED_UNREGISTERED_ROOTS);
 }
  public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement element) {
    if (ApplicationManager.getApplication().isUnitTestMode()) {
      return null;
    }

    if (!(element instanceof PsiMethod) && !(element instanceof PsiClass)) {
      return null;
    }

    if (!PsiUtil.isPluginProject(element.getProject())) {
      return null;
    }

    final VirtualFile file = PsiUtilCore.getVirtualFile(element);
    if (file == null
        || !ProjectFileIndex.SERVICE
            .getInstance(element.getProject())
            .isInTestSourceContent(file)) {
      return null;
    }
    if (element instanceof PsiMethod) {
      final PsiMethod method = (PsiMethod) element;
      if (isTestMethod(method)) {
        return new LineMarkerInfo<PsiMethod>(
            method,
            method.getModifierList().getTextRange(),
            PlatformIcons.TEST_SOURCE_FOLDER,
            Pass.UPDATE_ALL,
            null,
            new TestDataNavigationHandler(),
            GutterIconRenderer.Alignment.LEFT);
      }
    } else {
      final PsiClass psiClass = (PsiClass) element;
      final String basePath = getTestDataBasePath(psiClass);
      if (basePath != null) {
        PsiModifierList modifierList = psiClass.getModifierList();
        assert modifierList != null;
        return new LineMarkerInfo<PsiClass>(
            psiClass,
            modifierList.getTextRange(),
            PlatformIcons.TEST_SOURCE_FOLDER,
            Pass.UPDATE_ALL,
            new TooltipProvider(basePath),
            new GutterIconNavigationHandler<PsiClass>() {
              @Override
              public void navigate(MouseEvent e, PsiClass elt) {
                final VirtualFile baseDir = VfsUtil.findFileByIoFile(new File(basePath), true);
                if (baseDir != null) {
                  new OpenFileDescriptor(psiClass.getProject(), baseDir).navigate(true);
                }
              }
            },
            GutterIconRenderer.Alignment.LEFT);
      }
    }
    return null;
  }
 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);
 }
 @NotNull
 public static GlobalSearchScope getMaximalScope(@NotNull FindUsagesHandler handler) {
   PsiElement element = handler.getPsiElement();
   Project project = element.getProject();
   PsiFile file = element.getContainingFile();
   if (file != null
       && ProjectFileIndex.SERVICE
           .getInstance(project)
           .isInContent(file.getViewProvider().getVirtualFile())) {
     return GlobalSearchScope.projectScope(project);
   }
   return GlobalSearchScope.allScope(project);
 }
Ejemplo n.º 9
0
        @Override
        public void beforeDocumentChange(DocumentEvent event) {

          if (project.isDisposed()) {
            return;
          }

          if (!isListening) {
            System.out.println("Ignoring change.");
            return;
          }

          VirtualFile file = FileDocumentManager.getInstance().getFile(event.getDocument());

          // Make sure file exists
          if (file == null) {
            return;
          }

          // Make sure file is in the project
          if (!ProjectFileIndex.SERVICE.getInstance(project).isInSource(file)) {
            return;
          }

          String dummyIdentifier = "IntellijIdeaRulezzz";

          if (!event.getNewFragment().toString().contains(dummyIdentifier)) {
            if (!event.isWholeTextReplaced()) {

              // Get path relative to project root (e.g. src/Sample.java)
              Path basePath = Paths.get(project.getBasePath());
              Path absoluteFilePath = Paths.get(file.getPath());
              String relativeFilePath = basePath.relativize(absoluteFilePath).toString();

              String documentText = event.getDocument().getText();
              String oldFragment = event.getOldFragment().toString();
              String newFragment = event.getNewFragment().toString();
              int offset = event.getOffset();

              IOPatcher patcher = new IOPatcher();
              LinkedList<Patch> patches =
                  patcher.makePatchAsList(documentText, oldFragment, newFragment, offset);

              UserEdit edit = new UserEdit(0, relativeFilePath, patches);

              for (EditorEvent editorEvent : events) {
                editorEvent.sendChange(edit);
              }
            }
          }
        }
 private void collectFiles(final Set<VirtualFile> srcSet, final BitSet processed) {
   final ProjectFileIndex projectIndex = ProjectFileIndex.SERVICE.getInstance(myProject);
   projectIndex.iterateContent(
       new ContentIterator() {
         @Override
         public boolean processFile(VirtualFile fileOrDir) {
           int fileId = ((VirtualFileWithId) fileOrDir).getId();
           if (!processed.get(fileId) && projectIndex.isInSourceContent(fileOrDir)) {
             srcSet.add(fileOrDir);
           }
           return true;
         }
       });
 }
 private static void addSourceDirectoriesFromLibraries(
     @NotNull Project project,
     @NotNull VirtualFile directory,
     @NotNull Collection<VirtualFile> outSourceRoots) {
   ProjectFileIndex index = ProjectFileIndex.SERVICE.getInstance(project);
   VirtualFile classRoot = index.getClassRootForFile(directory);
   if (classRoot == null) return;
   String relativePath = VfsUtilCore.getRelativePath(directory, classRoot);
   if (relativePath == null) return;
   for (OrderEntry orderEntry : index.getOrderEntriesForFile(directory)) {
     for (VirtualFile sourceRoot : orderEntry.getFiles(OrderRootType.SOURCES)) {
       VirtualFile sourceFile = sourceRoot.findFileByRelativePath(relativePath);
       if (sourceFile != null) {
         outSourceRoots.add(sourceFile);
       }
     }
   }
 }
  @Nullable
  static HighlightInfo checkFileLocation(@NotNull PsiJavaModule element, @NotNull PsiFile file) {
    VirtualFile vFile = file.getVirtualFile();
    if (vFile != null) {
      VirtualFile root =
          ProjectFileIndex.SERVICE.getInstance(file.getProject()).getSourceRootForFile(vFile);
      if (root != null && !root.equals(vFile.getParent())) {
        String message = JavaErrorMessages.message("module.file.wrong.location");
        HighlightInfo info =
            HighlightInfo.newHighlightInfo(HighlightInfoType.WARNING)
                .range(range(element))
                .description(message)
                .create();
        QuickFixAction.registerQuickFixAction(
            info,
            new MoveFileFix(vFile, root, QuickFixBundle.message("move.file.to.source.root.text")));
        return info;
      }
    }

    return null;
  }
  @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();
    }
  }
Ejemplo n.º 14
0
  @NotNull
  private Collection<PsiFile> collectFilesInScope(
      @NotNull final Set<PsiFile> alreadySearched, final boolean skipIndexed) {
    SearchScope customScope = myFindModel.getCustomScope();
    final GlobalSearchScope globalCustomScope = toGlobal(customScope);

    final ProjectFileIndex fileIndex = ProjectFileIndex.SERVICE.getInstance(myProject);
    final boolean hasTrigrams = hasTrigrams(myFindModel.getStringToFind());

    class EnumContentIterator implements ContentIterator {
      final Set<PsiFile> myFiles = new LinkedHashSet<PsiFile>();

      @Override
      public boolean processFile(@NotNull final VirtualFile virtualFile) {
        ApplicationManager.getApplication()
            .runReadAction(
                new Runnable() {
                  @Override
                  public void run() {
                    ProgressManager.checkCanceled();
                    if (virtualFile.isDirectory()
                        || !virtualFile.isValid()
                        || !myFileMask.value(virtualFile)
                        || globalCustomScope != null && !globalCustomScope.contains(virtualFile)) {
                      return;
                    }

                    if (skipIndexed
                        && isCoveredByIndex(virtualFile)
                        && (fileIndex.isInContent(virtualFile)
                            || fileIndex.isInLibraryClasses(virtualFile)
                            || fileIndex.isInLibrarySource(virtualFile))) {
                      return;
                    }

                    PsiFile psiFile = myPsiManager.findFile(virtualFile);
                    if (psiFile != null
                        && !(psiFile instanceof PsiBinaryFile)
                        && !alreadySearched.contains(psiFile)) {
                      PsiFile sourceFile = (PsiFile) psiFile.getNavigationElement();
                      if (sourceFile != null) psiFile = sourceFile;
                      if (!psiFile.getFileType().isBinary()) {
                        myFiles.add(psiFile);
                      }
                    }
                  }

                  final FileBasedIndexImpl fileBasedIndex =
                      (FileBasedIndexImpl) FileBasedIndex.getInstance();

                  private boolean isCoveredByIndex(VirtualFile file) {
                    FileType fileType = file.getFileType();
                    if (hasTrigrams) {
                      return TrigramIndex.isIndexable(fileType)
                          && fileBasedIndex.isIndexingCandidate(file, TrigramIndex.INDEX_ID);
                    }
                    return IdIndex.isIndexable(fileType)
                        && fileBasedIndex.isIndexingCandidate(file, IdIndex.NAME);
                  }
                });
        return true;
      }

      @NotNull
      private Collection<PsiFile> getFiles() {
        return myFiles;
      }
    }

    final EnumContentIterator iterator = new EnumContentIterator();

    if (customScope instanceof LocalSearchScope) {
      for (VirtualFile file : getLocalScopeFiles((LocalSearchScope) customScope)) {
        iterator.processFile(file);
      }
    } else if (customScope
        instanceof
        Iterable) { // GlobalSearchScope can span files out of project roots e.g. FileScope /
                    // FilesScope
      //noinspection unchecked
      for (VirtualFile file : (Iterable<VirtualFile>) customScope) {
        iterator.processFile(file);
      }
    } else if (myPsiDirectory != null) {
      ApplicationManager.getApplication()
          .runReadAction(
              new Runnable() {
                @Override
                public void run() {
                  if (myPsiDirectory.isValid()) {
                    addFilesUnderDirectory(myPsiDirectory, iterator);
                  }
                }
              });

      myFileIndex.iterateContentUnderDirectory(myPsiDirectory.getVirtualFile(), iterator);
    } else {
      boolean success = myFileIndex.iterateContent(iterator);
      if (success && globalCustomScope != null && globalCustomScope.isSearchInLibraries()) {
        final VirtualFile[] librarySources =
            ApplicationManager.getApplication()
                .runReadAction(
                    new Computable<VirtualFile[]>() {
                      @Override
                      public VirtualFile[] compute() {
                        OrderEnumerator enumerator =
                            myModule == null
                                ? OrderEnumerator.orderEntries(myProject)
                                : OrderEnumerator.orderEntries(myModule);
                        return enumerator
                            .withoutModuleSourceEntries()
                            .withoutDepModules()
                            .getSourceRoots();
                      }
                    });
        iterateAll(librarySources, globalCustomScope, iterator);
      }
    }
    return iterator.getFiles();
  }
 private boolean isInRootModel(@NotNull VirtualFile file) {
   ProjectFileIndex index = ProjectFileIndex.SERVICE.getInstance(myProject);
   return index.isInContent(file)
       || index.isInLibraryClasses(file)
       || index.isInLibrarySource(file);
 }
 private UnitProcessor(Project project, HierarchyService hierarchyService) {
   myProject = project;
   myHierarchyService = hierarchyService;
   myProjectIndex = ProjectFileIndex.SERVICE.getInstance(myProject);
 }