@Override
  public Module getModule() {
    if (!isValid()) return null;
    VirtualFile virtualFile = getFile();
    if (virtualFile == null) return null;

    ProjectRootManager projectRootManager = ProjectRootManager.getInstance(getProject());
    ProjectFileIndex fileIndex = projectRootManager.getFileIndex();
    return fileIndex.getModuleForFile(virtualFile);
  }
  private void initLivePackageNameToDirMap() {
    final VirtualFile baseDir = myPubspecYamlFile == null ? null : myPubspecYamlFile.getParent();
    if (myPubspecYamlFile == null || baseDir == null) return;

    final String name = PubspecYamlUtil.getDartProjectName(myPubspecYamlFile);
    final VirtualFile libFolder = baseDir.findChild(PubspecYamlUtil.LIB_DIR_NAME);

    if (name != null && libFolder != null && libFolder.isDirectory()) {
      myLivePackageNameToDirMap.put(name, libFolder);
    }

    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();

    PubspecYamlUtil.processPathPackages(
        myPubspecYamlFile,
        new PairConsumer<String, VirtualFile>() {
          @Override
          public void consume(
              @NotNull final String packageName, @NotNull final VirtualFile packageDir) {
            if (fileIndex.isInContent(packageDir)) {
              myLivePackageNameToDirMap.put(packageName, packageDir);
            }
          }
        });
  }
  private void initPubListPackageDirsMap(final @NotNull VirtualFile contextFile) {
    final Module module = ModuleUtilCore.findModuleForFile(contextFile, myProject);

    final List<OrderEntry> orderEntries =
        module != null
            ? Arrays.asList(ModuleRootManager.getInstance(module).getOrderEntries())
            : ProjectRootManager.getInstance(myProject)
                .getFileIndex()
                .getOrderEntriesForFile(contextFile);
    for (OrderEntry orderEntry : orderEntries) {
      if (orderEntry instanceof LibraryOrderEntry
          && LibraryTablesRegistrar.PROJECT_LEVEL.equals(
              ((LibraryOrderEntry) orderEntry).getLibraryLevel())
          && PubListPackageDirsAction.PUB_LIST_PACKAGE_DIRS_LIB_NAME.equals(
              ((LibraryOrderEntry) orderEntry).getLibraryName())) {
        final LibraryEx library = (LibraryEx) ((LibraryOrderEntry) orderEntry).getLibrary();
        final LibraryProperties properties = library == null ? null : library.getProperties();

        if (properties instanceof DartListPackageDirsLibraryProperties) {
          myPubListPackageDirsMap.putAll(
              ((DartListPackageDirsLibraryProperties) properties).getPackageNameToDirsMap());
          return;
        }
      }
    }
  }
  private static THashSet<String> collectFoldersToExclude(
      final Module module, final VirtualFile pubspecYamlFile) {
    final THashSet<String> newExcludedPackagesUrls = new THashSet<String>();
    final ProjectFileIndex fileIndex =
        ProjectRootManager.getInstance(module.getProject()).getFileIndex();
    final VirtualFile root = pubspecYamlFile.getParent();

    final VirtualFile binFolder = root.findChild("bin");
    if (binFolder != null && binFolder.isDirectory() && fileIndex.isInContent(binFolder)) {
      newExcludedPackagesUrls.add(binFolder.getUrl() + "/packages");
    }

    appendPackagesFolders(newExcludedPackagesUrls, root.findChild("benchmark"), fileIndex);
    appendPackagesFolders(newExcludedPackagesUrls, root.findChild("example"), fileIndex);
    appendPackagesFolders(newExcludedPackagesUrls, root.findChild("test"), fileIndex);
    appendPackagesFolders(newExcludedPackagesUrls, root.findChild("tool"), fileIndex);
    appendPackagesFolders(newExcludedPackagesUrls, root.findChild("web"), fileIndex);

    // Folder packages/ThisProject (where ThisProject is the name specified in pubspec.yaml) is a
    // symlink to local 'lib' folder. Exclude it in order not to have duplicates. Resolve goes to
    // local 'lib' folder.
    // Empty 'ThisProject (link to 'lib' folder)' node is added to Project Structure by
    // DartTreeStructureProvider
    final VirtualFile libFolder = root.findChild("lib");
    if (libFolder != null && libFolder.isDirectory()) {
      final String pubspecName = PubspecYamlUtil.getPubspecName(pubspecYamlFile);
      if (pubspecName != null) {
        newExcludedPackagesUrls.add(root.getUrl() + "/packages/" + pubspecName);
      }
    }

    return newExcludedPackagesUrls;
  }
 @Override
 public Object[] getDirectoryCachedValueDependencies(PsiPackage psiPackage) {
   return new Object[] {
     PsiModificationTracker.OUT_OF_CODE_BLOCK_MODIFICATION_COUNT,
     ProjectRootManager.getInstance(psiPackage.getProject())
   };
 }
  public AddModuleDependencyFix(
      Module currentModule, VirtualFile classVFile, PsiClass[] classes, PsiReference reference) {
    final PsiElement psiElement = reference.getElement();
    final Project project = psiElement.getProject();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();

    for (PsiClass aClass : classes) {
      if (!facade.getResolveHelper().isAccessible(aClass, psiElement, aClass)) continue;
      PsiFile psiFile = aClass.getContainingFile();
      if (psiFile == null) continue;
      VirtualFile virtualFile = psiFile.getVirtualFile();
      if (virtualFile == null) continue;
      final Module classModule = fileIndex.getModuleForFile(virtualFile);
      if (classModule != null
          && classModule != currentModule
          && !ModuleRootManager.getInstance(currentModule).isDependsOn(classModule)) {
        myModules.add(classModule);
      }
    }
    myCurrentModule = currentModule;
    myClassVFile = classVFile;
    myClasses = classes;
    myReference = reference;
  }
  @NotNull
  private static DartLibInfo collectPackagesLibraryRoots(
      @NotNull final Project project, @NotNull final DartSdk sdk) {
    final DartLibInfo libInfo = new DartLibInfo(false);

    final Collection<VirtualFile> pubspecYamlFiles =
        FilenameIndex.getVirtualFilesByName(
            project, PUBSPEC_YAML, GlobalSearchScope.projectScope(project));
    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();

    for (VirtualFile pubspecFile : pubspecYamlFiles) {
      final VirtualFile dotPackagesFile =
          pubspecFile.getParent().findChild(DotPackagesFileUtil.DOT_PACKAGES);
      final Module module =
          dotPackagesFile == null ? null : fileIndex.getModuleForFile(dotPackagesFile);

      if (dotPackagesFile != null
          && !dotPackagesFile.isDirectory()
          && module != null
          && DartSdkGlobalLibUtil.isDartSdkEnabled(module)) {
        final Map<String, String> packagesMap = DotPackagesFileUtil.getPackagesMap(dotPackagesFile);
        if (packagesMap != null) {
          for (Map.Entry<String, String> entry : packagesMap.entrySet()) {
            final String packageName = entry.getKey();
            final String packagePath = entry.getValue();
            if (isPathOutsideProjectContent(fileIndex, packagePath)) {
              libInfo.addPackage(packageName, packagePath);
            }
          }
        }
      }
    }

    return libInfo;
  }
  @Override
  public void annotateExternally(
      @NotNull final PsiModifierListOwner listOwner,
      @NotNull final String annotationFQName,
      @NotNull final PsiFile fromFile,
      final PsiNameValuePair[] value) {
    final Project project = myPsiManager.getProject();
    final PsiFile containingFile = listOwner.getContainingFile();
    if (!(containingFile instanceof PsiJavaFile)) {
      return;
    }
    final String packageName = ((PsiJavaFile) containingFile).getPackageName();
    final VirtualFile virtualFile = containingFile.getVirtualFile();
    LOG.assertTrue(virtualFile != null);
    final List<OrderEntry> entries =
        ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile);
    if (entries.isEmpty()) {
      return;
    }
    for (final OrderEntry entry : entries) {
      if (entry instanceof ModuleOrderEntry) continue;
      VirtualFile[] virtualFiles = AnnotationOrderRootType.getFiles(entry);
      virtualFiles = filterByReadOnliness(virtualFiles);

      if (virtualFiles.length > 0) {
        chooseRootAndAnnotateExternally(
            listOwner,
            annotationFQName,
            fromFile,
            project,
            packageName,
            virtualFile,
            virtualFiles,
            value);
      } else {
        if (ApplicationManager.getApplication().isUnitTestMode()
            || ApplicationManager.getApplication().isHeadlessEnvironment()) {
          return;
        }
        SwingUtilities.invokeLater(
            new Runnable() {
              @Override
              public void run() {
                setupRootAndAnnotateExternally(
                    entry,
                    project,
                    listOwner,
                    annotationFQName,
                    fromFile,
                    packageName,
                    virtualFile,
                    value);
              }
            });
      }
      break;
    }
  }
예제 #9
0
  @Override
  public boolean contains(@NotNull VirtualFile file) {
    final ProjectFileIndex index = ProjectRootManager.getInstance(getProject()).getFileIndex();
    if (!index.isInLibrarySource(file) && !index.isInLibraryClasses(file)) {
      return false;
    }

    return someChildContainsFile(file, false);
  }
  public static void removeDuplicatingClasses(
      final Module module,
      @NotNull final String packageName,
      @NotNull String className,
      @Nullable File classFile,
      String sourceRootPath) {
    if (sourceRootPath == null) {
      return;
    }
    VirtualFile sourceRoot = LocalFileSystem.getInstance().findFileByPath(sourceRootPath);
    if (sourceRoot == null) {
      return;
    }
    final Project project = module.getProject();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
    final String interfaceQualifiedName = packageName + '.' + className;
    PsiClass[] classes =
        facade.findClasses(interfaceQualifiedName, GlobalSearchScope.moduleScope(module));
    final ProjectFileIndex projectFileIndex =
        ProjectRootManager.getInstance(project).getFileIndex();
    for (PsiClass c : classes) {
      PsiFile psiFile = c.getContainingFile();
      if (className.equals(FileUtil.getNameWithoutExtension(psiFile.getName()))) {
        VirtualFile virtualFile = psiFile.getVirtualFile();
        if (virtualFile != null
            && projectFileIndex.getSourceRootForFile(virtualFile) == sourceRoot) {
          final String path = virtualFile.getPath();
          File f = new File(path);

          try {
            f = f.getCanonicalFile();
            classFile = classFile != null ? classFile.getCanonicalFile() : null;
            if (f != null && !f.equals(classFile) && f.exists()) {
              if (f.delete()) {
                virtualFile.refresh(true, false);
              } else {
                ApplicationManager.getApplication()
                    .invokeLater(
                        new Runnable() {
                          public void run() {
                            Messages.showErrorDialog(
                                project, "Can't delete file " + path, CommonBundle.getErrorTitle());
                          }
                        },
                        project.getDisposed());
              }
            }
          } catch (IOException e) {
            LOG.info(e);
          }
        }
      }
    }
  }
  private boolean isExcludeRoot(VirtualFile file) {
    VirtualFile parent = file.getParent();
    if (parent == null) return false;

    Module module = myProjectRootManager.getFileIndex().getModuleForFile(parent);
    if (module == null) return false;
    VirtualFile[] excludeRoots = ModuleRootManager.getInstance(module).getExcludeRoots();
    for (VirtualFile root : excludeRoots) {
      if (root.equals(file)) return true;
    }
    return false;
  }
  @Override
  public OrderEntry getLibraryEntry() {
    if (!isValid()) return null;
    PsiFile psiFile = getPsiFile();
    VirtualFile virtualFile = getFile();
    if (virtualFile == null) return null;

    ProjectRootManager projectRootManager = ProjectRootManager.getInstance(getProject());
    ProjectFileIndex fileIndex = projectRootManager.getFileIndex();

    if (psiFile instanceof PsiCompiledElement || fileIndex.isInLibrarySource(virtualFile)) {
      List<OrderEntry> orders = fileIndex.getOrderEntriesForFile(virtualFile);
      for (OrderEntry order : orders) {
        if (order instanceof LibraryOrderEntry || order instanceof JdkOrderEntry) {
          return order;
        }
      }
    }

    return null;
  }
 @NotNull
 @Override
 public Collection<String> getNonTrivialPackagePrefixes() {
   Set<String> names = myNontrivialPackagePrefixes;
   if (names == null) {
     names = new HashSet<>();
     final ProjectRootManager rootManager = ProjectRootManager.getInstance(myManager.getProject());
     final List<VirtualFile> sourceRoots =
         rootManager.getModuleSourceRoots(JavaModuleSourceRootTypes.SOURCES);
     final ProjectFileIndex fileIndex = rootManager.getFileIndex();
     for (final VirtualFile sourceRoot : sourceRoots) {
       if (sourceRoot.isDirectory()) {
         final String packageName = fileIndex.getPackageNameByDirectory(sourceRoot);
         if (packageName != null && !packageName.isEmpty()) {
           names.add(packageName);
         }
       }
     }
     myNontrivialPackagePrefixes = names;
   }
   return names;
 }
    @Override
    public void run() {
      Project project = myModule.getProject();
      if (GoSdkService.getInstance(project).isGoModule(myModule)) {
        synchronized (myLastHandledGoPathSourcesRoots) {
          Collection<VirtualFile> goPathSourcesRoots =
              GoSdkUtil.getGoPathSources(project, myModule);
          Set<VirtualFile> excludeRoots =
              ContainerUtil.newHashSet(ProjectRootManager.getInstance(project).getContentRoots());
          ProgressIndicatorProvider.checkCanceled();
          if (!myLastHandledGoPathSourcesRoots.equals(goPathSourcesRoots)
              || !myLastHandledExclusions.equals(excludeRoots)) {
            Collection<VirtualFile> includeRoots =
                gatherIncludeRoots(goPathSourcesRoots, excludeRoots);
            ApplicationManager.getApplication()
                .invokeLater(
                    () -> {
                      if (!myModule.isDisposed()
                          && GoSdkService.getInstance(project).isGoModule(myModule)) {
                        attachLibraries(includeRoots, excludeRoots);
                      }
                    });

            myLastHandledGoPathSourcesRoots.clear();
            myLastHandledGoPathSourcesRoots.addAll(goPathSourcesRoots);

            myLastHandledExclusions.clear();
            myLastHandledExclusions.addAll(excludeRoots);

            List<String> paths = ContainerUtil.map(goPathSourcesRoots, VirtualFile::getPath);
            myWatchedRequests.clear();
            myWatchedRequests.addAll(LocalFileSystem.getInstance().addRootsToWatch(paths, true));
          }
        }
      } else {
        synchronized (myLastHandledGoPathSourcesRoots) {
          LocalFileSystem.getInstance().removeWatchedRoots(myWatchedRequests);
          myLastHandledGoPathSourcesRoots.clear();
          myLastHandledExclusions.clear();
          ApplicationManager.getApplication()
              .invokeLater(
                  () -> {
                    if (!myModule.isDisposed()
                        && GoSdkService.getInstance(project).isGoModule(myModule)) {
                      removeLibraryIfNeeded();
                    }
                  });
        }
      }
    }
  public static void excludePackagesFolders(
      final Module module, final VirtualFile pubspecYamlFile) {
    final VirtualFile root = pubspecYamlFile.getParent();
    final VirtualFile contentRoot =
        root == null
            ? null
            : ProjectRootManager.getInstance(module.getProject())
                .getFileIndex()
                .getContentRootForFile(root);
    if (contentRoot == null) return;

    // http://pub.dartlang.org/doc/glossary.html#entrypoint-directory
    // Entrypoint directory: A directory inside your package that is allowed to contain Dart
    // entrypoints.
    // Pub will ensure all of these directories get a “packages” directory, which is needed for
    // “package:” imports to work.
    // Pub has a whitelist of these directories: benchmark, bin, example, test, tool, and web.
    // Any subdirectories of those (except bin) may also contain entrypoints.
    //
    // the same can be seen in the pub tool source code: [repo
    // root]/sdk/lib/_internal/pub/lib/src/entrypoint.dart

    final Collection<String> oldExcludedPackagesUrls =
        ContainerUtil.filter(
            ModuleRootManager.getInstance(module).getExcludeRootUrls(),
            new Condition<String>() {
              final String rootUrlPrefix = root.getUrl() + '/';

              public boolean value(final String url) {
                if (!url.startsWith(rootUrlPrefix)) return false;

                if (url.endsWith("/packages")) return true;

                // excluded subfolder of 'packages' folder
                final int lastSlashIndex = url.lastIndexOf('/');
                if (lastSlashIndex > 0 && url.substring(0, lastSlashIndex).endsWith("/packages"))
                  return true;

                return false;
              }
            });

    final THashSet<String> newExcludedPackagesUrls =
        collectFoldersToExclude(module, pubspecYamlFile);

    if (oldExcludedPackagesUrls.size() != newExcludedPackagesUrls.size()
        || !newExcludedPackagesUrls.containsAll(oldExcludedPackagesUrls)) {
      updateExcludedFolders(module, contentRoot, oldExcludedPackagesUrls, newExcludedPackagesUrls);
    }
  }
예제 #16
0
  @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());
  }
  public PsiVFSListener(Project project) {
    myProject = project;
    myFileTypeManager = FileTypeManager.getInstance();
    myProjectRootManager = ProjectRootManager.getInstance(project);
    myManager = (PsiManagerImpl) PsiManager.getInstance(project);
    myFileManager = (FileManagerImpl) myManager.getFileManager();

    myConnection = project.getMessageBus().connect(project);

    StartupManager.getInstance(project)
        .registerPreStartupActivity(
            new Runnable() {
              @Override
              public void run() {
                final BulkVirtualFileListenerAdapter adapter =
                    new BulkVirtualFileListenerAdapter(PsiVFSListener.this);
                myConnection.subscribe(
                    VirtualFileManager.VFS_CHANGES,
                    new BulkFileListener() {
                      @Override
                      public void before(@NotNull List<? extends VFileEvent> events) {
                        adapter.before(events);
                      }

                      @Override
                      public void after(@NotNull List<? extends VFileEvent> events) {
                        myReportedUnloadedPsiChange = false;
                        adapter.after(events);
                        myReportedUnloadedPsiChange = false;
                      }
                    });
                myConnection.subscribe(ProjectTopics.PROJECT_ROOTS, new MyModuleRootListener());
                myConnection.subscribe(
                    FileTypeManager.TOPIC,
                    new FileTypeListener.Adapter() {
                      @Override
                      public void fileTypesChanged(@NotNull FileTypeEvent e) {
                        myFileManager.processFileTypesChanged();
                      }
                    });
                myConnection.subscribe(
                    AppTopics.FILE_DOCUMENT_SYNC, new MyFileDocumentManagerAdapter());
                myFileManager.markInitialized();
              }
            });
  }
  private boolean hasAcceptablePackage(@NotNull VirtualFile vFile) {
    if (vFile.getFileType() == JavaClassFileType.INSTANCE) {
      // See IDEADEV-5626
      final VirtualFile root =
          ProjectRootManager.getInstance(myManager.getProject())
              .getFileIndex()
              .getClassRootForFile(vFile);
      VirtualFile parent = vFile.getParent();
      final PsiNameHelper nameHelper = PsiNameHelper.getInstance(myManager.getProject());
      while (parent != null && !Comparing.equal(parent, root)) {
        if (!nameHelper.isIdentifier(parent.getName())) return false;
        parent = parent.getParent();
      }
    }

    return true;
  }
  FindInProjectTask(
      @NotNull final FindModel findModel,
      @NotNull final Project project,
      @Nullable final PsiDirectory psiDirectory) {
    myFindModel = findModel;
    myProject = project;
    myPsiDirectory = psiDirectory;
    myPsiManager = PsiManager.getInstance(project);

    final String moduleName = findModel.getModuleName();
    myModule =
        moduleName == null
            ? null
            : ApplicationManager.getApplication()
                .runReadAction(
                    new Computable<Module>() {
                      @Override
                      public Module compute() {
                        return ModuleManager.getInstance(project).findModuleByName(moduleName);
                      }
                    });
    myFileIndex =
        myModule == null
            ? ProjectRootManager.getInstance(project).getFileIndex()
            : ModuleRootManager.getInstance(myModule).getFileIndex();

    final String filter = findModel.getFileFilter();
    final Pattern pattern = FindInProjectUtil.createFileMaskRegExp(filter);

    //noinspection unchecked
    myFileMask =
        pattern == null
            ? Condition.TRUE
            : new Condition<VirtualFile>() {
              @Override
              public boolean value(VirtualFile file) {
                return file != null && pattern.matcher(file.getName()).matches();
              }
            };

    final ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator();
    myProgress = progress != null ? progress : new EmptyProgressIndicator();
  }
 public LibraryRuntimeClasspathScope(final Project project, final List<Module> modules) {
   super(project);
   myIndex = ProjectRootManager.getInstance(project).getFileIndex();
   final Set<Sdk> processedSdk = new THashSet<Sdk>();
   final Set<Library> processedLibraries = new THashSet<Library>();
   final Set<Module> processedModules = new THashSet<Module>();
   final Condition<OrderEntry> condition =
       new Condition<OrderEntry>() {
         @Override
         public boolean value(OrderEntry orderEntry) {
           if (orderEntry instanceof ModuleOrderEntry) {
             final Module module = ((ModuleOrderEntry) orderEntry).getModule();
             return module != null && !processedModules.contains(module);
           }
           return true;
         }
       };
   for (Module module : modules) {
     buildEntries(module, processedModules, processedLibraries, processedSdk, condition);
   }
 }
 @Override
 @NotNull
 protected List<VirtualFile> getExternalAnnotationsRoots(@NotNull VirtualFile libraryFile) {
   final List<OrderEntry> entries =
       ProjectRootManager.getInstance(myPsiManager.getProject())
           .getFileIndex()
           .getOrderEntriesForFile(libraryFile);
   List<VirtualFile> result = new ArrayList<VirtualFile>();
   for (OrderEntry entry : entries) {
     if (entry instanceof ModuleOrderEntry) {
       continue;
     }
     final String[] externalUrls = AnnotationOrderRootType.getUrls(entry);
     for (String url : externalUrls) {
       VirtualFile root = VirtualFileManager.getInstance().findFileByUrl(url);
       if (root != null) {
         result.add(root);
       }
     }
   }
   return result;
 }
예제 #22
0
  public static void customizeElementLabel(final PsiElement element, final JLabel label) {
    if (element != null) {
      PsiFile file = element.getContainingFile();
      VirtualFile vfile = file == null ? null : file.getVirtualFile();

      if (vfile == null) {
        label.setText("");
        label.setIcon(null);

        return;
      }

      final ProjectFileIndex fileIndex =
          ProjectRootManager.getInstance(element.getProject()).getFileIndex();
      final Module module = fileIndex.getModuleForFile(vfile);

      if (module != null) {
        label.setText(module.getName());
        label.setIcon(AllIcons.Nodes.Module);
      } else {
        final List<OrderEntry> entries = fileIndex.getOrderEntriesForFile(vfile);

        OrderEntry entry = null;

        for (OrderEntry order : entries) {
          if (order instanceof LibraryOrderEntry || order instanceof SdkOrderEntry) {
            entry = order;
            break;
          }
        }

        if (entry != null) {
          label.setText(entry.getPresentableName());
          label.setIcon(AllIcons.Nodes.PpLibFolder);
        }
      }
    }
  }
예제 #23
0
 public BackendCompilerWrapper(
     TranslatingCompiler translatingCompiler,
     Chunk<Module> chunk,
     @NotNull final Project project,
     @NotNull List<VirtualFile> filesToCompile,
     @NotNull CompileContextEx compileContext,
     @NotNull BackendCompiler compiler,
     TranslatingCompiler.OutputSink sink) {
   myTranslatingCompiler = translatingCompiler;
   myChunk = chunk;
   myProject = project;
   myCompiler = compiler;
   myCompileContext = compileContext;
   myFilesToCompile = filesToCompile;
   mySink = sink;
   myProjectFileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
   CompileStatistics stat = compileContext.getUserData(CompileStatistics.KEY);
   if (stat == null) {
     stat = new CompileStatistics();
     compileContext.putUserData(CompileStatistics.KEY, stat);
   }
   myStatistics = stat;
 }
  @Override
  @NotNull
  public AnnotationPlace chooseAnnotationsPlace(@NotNull final PsiElement element) {
    if (!element.isPhysical()) return AnnotationPlace.IN_CODE; // element just created
    if (!element.getManager().isInProject(element)) return AnnotationPlace.EXTERNAL;
    final Project project = myPsiManager.getProject();
    final PsiFile containingFile = element.getContainingFile();
    final VirtualFile virtualFile = containingFile.getVirtualFile();
    LOG.assertTrue(virtualFile != null);
    final List<OrderEntry> entries =
        ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile);
    if (!entries.isEmpty()) {
      for (OrderEntry entry : entries) {
        if (!(entry instanceof ModuleOrderEntry)) {
          if (AnnotationOrderRootType.getUrls(entry).length > 0) {
            return AnnotationPlace.EXTERNAL;
          }
          break;
        }
      }
    }
    final MyExternalPromptDialog dialog =
        ApplicationManager.getApplication().isUnitTestMode()
                || ApplicationManager.getApplication().isHeadlessEnvironment()
            ? null
            : new MyExternalPromptDialog(project);
    if (dialog != null && dialog.isToBeShown()) {
      final PsiElement highlightElement =
          element instanceof PsiNameIdentifierOwner
              ? ((PsiNameIdentifierOwner) element).getNameIdentifier()
              : element.getNavigationElement();
      LOG.assertTrue(highlightElement != null);
      final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
      final List<RangeHighlighter> highlighters = new ArrayList<RangeHighlighter>();
      final boolean highlight =
          editor != null
              && editor.getDocument()
                  == PsiDocumentManager.getInstance(project).getDocument(containingFile);
      try {
        if (highlight) { // do not highlight for batch inspections
          final EditorColorsManager colorsManager = EditorColorsManager.getInstance();
          final TextAttributes attributes =
              colorsManager.getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
          final TextRange textRange = highlightElement.getTextRange();
          HighlightManager.getInstance(project)
              .addRangeHighlight(
                  editor,
                  textRange.getStartOffset(),
                  textRange.getEndOffset(),
                  attributes,
                  true,
                  highlighters);
          final LogicalPosition logicalPosition =
              editor.offsetToLogicalPosition(textRange.getStartOffset());
          editor.getScrollingModel().scrollTo(logicalPosition, ScrollType.CENTER);
        }

        dialog.show();
        if (dialog.getExitCode() == 2) {
          return AnnotationPlace.EXTERNAL;
        } else if (dialog.getExitCode() == 1) {
          return AnnotationPlace.NOWHERE;
        }

      } finally {
        if (highlight) {
          HighlightManager.getInstance(project)
              .removeSegmentHighlighter(editor, highlighters.get(0));
        }
      }
    } else if (dialog != null) {
      dialog.close(DialogWrapper.OK_EXIT_CODE);
    }
    return AnnotationPlace.IN_CODE;
  }
예제 #25
0
  @Nullable
  public static List<LocalQuickFix> registerFixes(
      @NotNull final QuickFixActionRegistrar registrar, @NotNull final PsiReference reference) {
    final PsiElement psiElement = reference.getElement();
    @NonNls
    final String shortReferenceName = reference.getRangeInElement().substring(psiElement.getText());

    Project project = psiElement.getProject();
    PsiFile containingFile = psiElement.getContainingFile();
    if (containingFile == null) return null;

    final VirtualFile classVFile = containingFile.getVirtualFile();
    if (classVFile == null) return null;

    final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(project).getFileIndex();
    final Module currentModule = fileIndex.getModuleForFile(classVFile);
    if (currentModule == null) return null;

    final List<LocalQuickFix> providedFixes =
        findFixes(
            new Function<MissingDependencyFixProvider, List<LocalQuickFix>>() {
              @Override
              public List<LocalQuickFix> fun(MissingDependencyFixProvider provider) {
                return provider.registerFixes(registrar, reference);
              }
            });
    if (providedFixes != null) {
      return providedFixes;
    }

    List<LocalQuickFix> result = new ArrayList<LocalQuickFix>();
    JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project);
    String fullReferenceText = reference.getCanonicalText();
    for (ExternalLibraryResolver resolver : ExternalLibraryResolver.EP_NAME.getExtensions()) {
      final ExternalClassResolveResult resolveResult =
          resolver.resolveClass(shortReferenceName, isReferenceToAnnotation(psiElement));
      OrderEntryFix fix = null;
      if (resolveResult != null
          && psiFacade.findClass(
                  resolveResult.getQualifiedClassName(),
                  currentModule.getModuleWithDependenciesAndLibrariesScope(true))
              == null) {
        fix =
            new AddExternalLibraryToDependenciesQuickFix(
                currentModule,
                resolveResult.getLibrary(),
                reference,
                resolveResult.getQualifiedClassName());
      } else if (!fullReferenceText.equals(shortReferenceName)) {
        ExternalLibraryDescriptor descriptor = resolver.resolvePackage(fullReferenceText);
        if (descriptor != null) {
          fix =
              new AddExternalLibraryToDependenciesQuickFix(
                  currentModule, descriptor, reference, null);
        }
      }
      if (fix != null) {
        registrar.register(fix);
        result.add(fix);
      }
    }
    if (!result.isEmpty()) {
      return result;
    }

    Set<Object> librariesToAdd = new THashSet<Object>();
    final JavaPsiFacade facade = JavaPsiFacade.getInstance(psiElement.getProject());
    PsiClass[] classes =
        PsiShortNamesCache.getInstance(project)
            .getClassesByName(shortReferenceName, GlobalSearchScope.allScope(project));
    List<PsiClass> allowedDependencies = filterAllowedDependencies(psiElement, classes);
    if (allowedDependencies.isEmpty()) {
      return result;
    }
    classes = allowedDependencies.toArray(new PsiClass[allowedDependencies.size()]);
    OrderEntryFix moduleDependencyFix =
        new AddModuleDependencyFix(currentModule, classVFile, classes, reference);

    final PsiClass[] finalClasses = classes;
    final OrderEntryFix finalModuleDependencyFix = moduleDependencyFix;
    final OrderEntryFix providedModuleDependencyFix =
        provideFix(
            new Function<MissingDependencyFixProvider, OrderEntryFix>() {
              @Override
              public OrderEntryFix fun(MissingDependencyFixProvider provider) {
                return provider.getAddModuleDependencyFix(
                    reference, finalModuleDependencyFix, currentModule, classVFile, finalClasses);
              }
            });
    moduleDependencyFix = ObjectUtils.notNull(providedModuleDependencyFix, moduleDependencyFix);

    registrar.register(moduleDependencyFix);
    result.add(moduleDependencyFix);
    for (final PsiClass aClass : classes) {
      if (!facade.getResolveHelper().isAccessible(aClass, psiElement, aClass)) continue;
      PsiFile psiFile = aClass.getContainingFile();
      if (psiFile == null) continue;
      VirtualFile virtualFile = psiFile.getVirtualFile();
      if (virtualFile == null) continue;
      ModuleFileIndex moduleFileIndex = ModuleRootManager.getInstance(currentModule).getFileIndex();
      for (OrderEntry orderEntry : fileIndex.getOrderEntriesForFile(virtualFile)) {
        if (orderEntry instanceof LibraryOrderEntry) {
          final LibraryOrderEntry libraryEntry = (LibraryOrderEntry) orderEntry;
          final Library library = libraryEntry.getLibrary();
          if (library == null) continue;
          VirtualFile[] files = library.getFiles(OrderRootType.CLASSES);
          if (files.length == 0) continue;
          final VirtualFile jar = files[0];

          if (jar == null
              || libraryEntry.isModuleLevel() && !librariesToAdd.add(jar)
              || !librariesToAdd.add(library)) continue;
          OrderEntry entryForFile = moduleFileIndex.getOrderEntryForFile(virtualFile);
          if (entryForFile != null
              && !(entryForFile instanceof ExportableOrderEntry
                  && ((ExportableOrderEntry) entryForFile).getScope() == DependencyScope.TEST
                  && !ModuleRootManager.getInstance(currentModule)
                      .getFileIndex()
                      .isInTestSourceContent(classVFile))) {
            continue;
          }
          final OrderEntryFix platformFix =
              new OrderEntryFix() {
                @Override
                @NotNull
                public String getText() {
                  return QuickFixBundle.message(
                      "orderEntry.fix.add.library.to.classpath", libraryEntry.getPresentableName());
                }

                @Override
                @NotNull
                public String getFamilyName() {
                  return QuickFixBundle.message("orderEntry.fix.family.add.library.to.classpath");
                }

                @Override
                public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
                  return !project.isDisposed()
                      && !currentModule.isDisposed()
                      && libraryEntry.isValid();
                }

                @Override
                public void invoke(
                    @NotNull final Project project, @Nullable final Editor editor, PsiFile file) {
                  OrderEntryUtil.addLibraryToRoots(libraryEntry, currentModule);
                  if (editor != null) {
                    DumbService.getInstance(project)
                        .withAlternativeResolveEnabled(
                            new Runnable() {
                              @Override
                              public void run() {
                                new AddImportAction(project, reference, editor, aClass).execute();
                              }
                            });
                  }
                }
              };

          final OrderEntryFix providedFix =
              provideFix(
                  new Function<MissingDependencyFixProvider, OrderEntryFix>() {
                    @Override
                    public OrderEntryFix fun(MissingDependencyFixProvider provider) {
                      return provider.getAddLibraryToClasspathFix(
                          reference, platformFix, currentModule, libraryEntry, aClass);
                    }
                  });
          final OrderEntryFix fix = ObjectUtils.notNull(providedFix, platformFix);
          registrar.register(fix);
          result.add(fix);
        }
      }
    }
    return result;
  }
  // currently only one level here..
  public boolean contains(@NotNull String name, @NotNull final VirtualFile vFile) {
    final ProjectFileIndex projectFileIndex =
        ProjectRootManager.getInstance(myProject).getFileIndex();
    final Set<Boolean> find = new HashSet<Boolean>();
    final ContentIterator contentIterator =
        new ContentIterator() {
          public boolean processFile(VirtualFile fileOrDir) {
            if (fileOrDir != null && fileOrDir.getPath().equals(vFile.getPath())) {
              find.add(Boolean.TRUE);
            }
            return true;
          }
        };

    Collection<TreeItem<Pair<AbstractUrl, String>>> urls = getFavoritesListRootUrls(name);
    for (TreeItem<Pair<AbstractUrl, String>> pair : urls) {
      AbstractUrl abstractUrl = pair.getData().getFirst();
      if (abstractUrl == null) {
        continue;
      }
      final Object[] path = abstractUrl.createPath(myProject);
      if (path == null || path.length < 1 || path[0] == null) {
        continue;
      }
      Object element = path[path.length - 1];
      if (element instanceof SmartPsiElementPointer) {
        final VirtualFile virtualFile =
            PsiUtilBase.getVirtualFile(((SmartPsiElementPointer) element).getElement());
        if (virtualFile == null) continue;
        if (vFile.getPath().equals(virtualFile.getPath())) {
          return true;
        }
        if (!virtualFile.isDirectory()) {
          continue;
        }
        projectFileIndex.iterateContentUnderDirectory(virtualFile, contentIterator);
      }

      if (element instanceof PsiElement) {
        final VirtualFile virtualFile = PsiUtilBase.getVirtualFile((PsiElement) element);
        if (virtualFile == null) continue;
        if (vFile.getPath().equals(virtualFile.getPath())) {
          return true;
        }
        if (!virtualFile.isDirectory()) {
          continue;
        }
        projectFileIndex.iterateContentUnderDirectory(virtualFile, contentIterator);
      }
      if (element instanceof Module) {
        ModuleRootManager.getInstance((Module) element)
            .getFileIndex()
            .iterateContent(contentIterator);
      }
      if (element instanceof LibraryGroupElement) {
        final boolean inLibrary =
            ModuleRootManager.getInstance(((LibraryGroupElement) element).getModule())
                    .getFileIndex()
                    .isInContent(vFile)
                && projectFileIndex.isInLibraryClasses(vFile);
        if (inLibrary) {
          return true;
        }
      }
      if (element instanceof NamedLibraryElement) {
        NamedLibraryElement namedLibraryElement = (NamedLibraryElement) element;
        final VirtualFile[] files =
            namedLibraryElement.getOrderEntry().getRootFiles(OrderRootType.CLASSES);
        if (files != null && ArrayUtil.find(files, vFile) > -1) {
          return true;
        }
      }
      if (element instanceof ModuleGroup) {
        ModuleGroup group = (ModuleGroup) element;
        final Collection<Module> modules = group.modulesInGroup(myProject, true);
        for (Module module : modules) {
          ModuleRootManager.getInstance(module).getFileIndex().iterateContent(contentIterator);
        }
      }

      for (FavoriteNodeProvider provider :
          Extensions.getExtensions(FavoriteNodeProvider.EP_NAME, myProject)) {
        if (provider.elementContainsFile(element, vFile)) {
          return true;
        }
      }

      if (!find.isEmpty()) {
        return true;
      }
    }
    return false;
  }
 public ProjectAndLibrariesScope(Project project, boolean searchOutsideRootModel) {
   super(project);
   myProjectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
   mySearchOutsideRootModel = searchOutsideRootModel;
 }
 public LibraryRuntimeClasspathScope(Project project, LibraryOrderEntry entry) {
   super(project);
   myIndex = ProjectRootManager.getInstance(project).getFileIndex();
   Collections.addAll(myEntries, entry.getFiles(BinariesOrderRootType.getInstance()));
 }
 @Nullable
 private static Sdk getRelevantJdk(@NotNull Project project, @Nullable Module module) {
   Sdk projectJdk = ProjectRootManager.getInstance(project).getProjectSdk();
   Sdk moduleJdk = module == null ? null : ModuleRootManager.getInstance(module).getSdk();
   return moduleJdk == null ? projectJdk : moduleJdk;
 }