Java Code Examples for com.intellij.openapi.roots.ProjectFileIndex#isInLibraryClasses()

The following examples show how to use com.intellij.openapi.roots.ProjectFileIndex#isInLibraryClasses() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ProjectFileIndexSampleAction.java    From intellij-sdk-docs with Apache License 2.0 6 votes vote down vote up
@Override
public void actionPerformed(@NotNull final AnActionEvent event) {
  Project project = event.getProject();
  final Editor editor = event.getData(CommonDataKeys.EDITOR);
  if (project == null || editor == null) return;
  Document document = editor.getDocument();
  FileDocumentManager fileDocumentManager = FileDocumentManager.getInstance();
  VirtualFile virtualFile = fileDocumentManager.getFile(document);
  ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
  if (virtualFile != null) {
    Module module = projectFileIndex.getModuleForFile(virtualFile);
    String moduleName;
    moduleName = module != null ? module.getName() : "No module defined for file";

    VirtualFile moduleContentRoot = projectFileIndex.getContentRootForFile(virtualFile);
    boolean isLibraryFile = projectFileIndex.isLibraryClassFile(virtualFile);
    boolean isInLibraryClasses = projectFileIndex.isInLibraryClasses(virtualFile);
    boolean isInLibrarySource = projectFileIndex.isInLibrarySource(virtualFile);
    Messages.showInfoMessage("Module: " + moduleName + "\n" +
                             "Module content root: " + moduleContentRoot + "\n" +
                             "Is library file: " + isLibraryFile + "\n" +
                             "Is in library classes: " + isInLibraryClasses +
                             ", Is in library source: " + isInLibrarySource,
                             "Main File Info for" + virtualFile.getName());
  }
}
 
Example 2
Source File: UsageScopeGroupingRule.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public UsageGroup groupUsage(@Nonnull Usage usage) {
  if (!(usage instanceof PsiElementUsage)) {
    return null;
  }
  PsiElementUsage elementUsage = (PsiElementUsage)usage;

  PsiElement element = elementUsage.getElement();
  VirtualFile virtualFile = PsiUtilCore.getVirtualFile(element);

  if (virtualFile == null) {
    return null;
  }
  ProjectFileIndex fileIndex = ProjectRootManager.getInstance(element.getProject()).getFileIndex();
  boolean isInLib = fileIndex.isInLibraryClasses(virtualFile) || fileIndex.isInLibrarySource(virtualFile);
  if (isInLib) return LIBRARY;
  boolean isInTest = TestSourcesFilter.isTestSources(virtualFile, element.getProject());
  return isInTest ? TEST : PRODUCTION;
}
 
Example 3
Source File: PackageViewPane.java    From consulo with Apache License 2.0 6 votes vote down vote up
private Module[] getModulesFor(PsiDirectory dir) {
  final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
  final VirtualFile vFile = dir.getVirtualFile();
  final Set<Module> modules = new HashSet<Module>();
  final Module module = fileIndex.getModuleForFile(vFile);
  if (module != null) {
    modules.add(module);
  }
  if (fileIndex.isInLibrarySource(vFile) || fileIndex.isInLibraryClasses(vFile)) {
    final List<OrderEntry> orderEntries = fileIndex.getOrderEntriesForFile(vFile);
    if (orderEntries.isEmpty()) {
      return Module.EMPTY_ARRAY;
    }
    for (OrderEntry entry : orderEntries) {
      modules.add(entry.getOwnerModule());
    }
  }
  return modules.toArray(new Module[modules.size()]);
}
 
Example 4
Source File: PantsProjectPaneSelectInTarget.java    From intellij-pants-plugin with Apache License 2.0 5 votes vote down vote up
private boolean canSelect(final VirtualFile vFile) {
  if (vFile != null && vFile.isValid()) {
    ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
    if (projectFileIndex.isInLibraryClasses(vFile) || projectFileIndex.isInLibrarySource(vFile)) {
      return true;
    }
    final Optional<VirtualFile> buildRoot = PantsUtil.findBuildRoot(myProject.getBaseDir());

    return buildRoot.isPresent() && VfsUtil.isAncestor(buildRoot.get(), vFile, false);
  }

  return false;
}
 
Example 5
Source File: ProjectRootsUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isLibraryRoot(final VirtualFile directoryFile, final Project project) {
  final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();
  if (projectFileIndex.isInLibraryClasses(directoryFile)) {
    final VirtualFile parent = directoryFile.getParent();
    return parent == null || !projectFileIndex.isInLibraryClasses(parent);
  }
  return false;
}
 
Example 6
Source File: ProjectRootsUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean isOutsideSourceRoot(@Nullable PsiFile psiFile) {
  if (psiFile == null) return false;
  if (psiFile instanceof PsiCodeFragment) return false;
  final VirtualFile file = psiFile.getVirtualFile();
  if (file == null) return false;
  final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(psiFile.getProject()).getFileIndex();
  return !projectFileIndex.isInSource(file) && !projectFileIndex.isInLibraryClasses(file);
}
 
Example 7
Source File: FilePatternPackageSet.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static String getLibRelativePath(final VirtualFile virtualFile, final ProjectFileIndex index) {
  StringBuilder relativePath = new StringBuilder(100);
  VirtualFile directory = virtualFile;
  while (directory != null && index.isInLibraryClasses(directory)) {
    relativePath.insert(0, '/');
    relativePath.insert(0, directory.getName());
    directory = directory.getParent();
  }
  return relativePath.toString();
}
 
Example 8
Source File: ProjectProductionScope.java    From consulo with Apache License 2.0 5 votes vote down vote up
public ProjectProductionScope() {
  super(IdeBundle.message("predefined.scope.production.name"), new AbstractPackageSet("project:*..*") {
    @Override
    public boolean contains(VirtualFile file, NamedScopesHolder holder) {
      final ProjectFileIndex index = ProjectRootManager.getInstance(holder.getProject()).getFileIndex();
      return file != null
             && !index.isInTestSourceContent(file)
             && !index.isInLibraryClasses(file)
             && !index.isInLibrarySource(file);
    }
  });
}
 
Example 9
Source File: FindInProjectUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void addSourceDirectoriesFromLibraries(@Nonnull Project project, @Nonnull VirtualFile directory, @Nonnull Collection<? super VirtualFile> outSourceRoots) {
  ProjectFileIndex index = ProjectFileIndex.SERVICE.getInstance(project);
  // if we already are in the sources, search just in this directory only
  if (!index.isInLibraryClasses(directory)) return;
  VirtualFile classRoot = index.getClassRootForFile(directory);
  if (classRoot == null) return;
  String relativePath = VfsUtilCore.getRelativePath(directory, classRoot);
  if (relativePath == null) return;

  Collection<VirtualFile> otherSourceRoots = new THashSet<>();

  // if we are in the library sources, return (to search in this directory only)
  // otherwise, if we outside sources or in a jar directory, add directories from other source roots
  searchForOtherSourceDirs:
  for (OrderEntry entry : index.getOrderEntriesForFile(directory)) {
    if (entry instanceof LibraryOrderEntry) {
      Library library = ((LibraryOrderEntry)entry).getLibrary();
      if (library == null) continue;
      // note: getUrls() returns jar directories too
      String[] sourceUrls = library.getUrls(OrderRootType.SOURCES);
      for (String sourceUrl : sourceUrls) {
        if (VfsUtilCore.isEqualOrAncestor(sourceUrl, directory.getUrl())) {
          // already in this library sources, no need to look for another source root
          otherSourceRoots.clear();
          break searchForOtherSourceDirs;
        }
        // otherwise we may be inside the jar file in a library which is configured as a jar directory
        // in which case we have no way to know whether this is a source jar or classes jar - so try to locate the source jar
      }
    }
    for (VirtualFile sourceRoot : entry.getFiles(OrderRootType.SOURCES)) {
      VirtualFile sourceFile = sourceRoot.findFileByRelativePath(relativePath);
      if (sourceFile != null) {
        otherSourceRoots.add(sourceFile);
      }
    }
  }
  outSourceRoots.addAll(otherSourceRoots);
}
 
Example 10
Source File: PackageViewLibrariesNode.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public boolean contains(@Nonnull final VirtualFile file) {
  ProjectFileIndex index = ProjectRootManager.getInstance(getProject()).getFileIndex();
  if (!index.isInLibrarySource(file) && !index.isInLibraryClasses(file)) return false;

  return someChildContainsFile(file, false);
}
 
Example 11
Source File: PackagesPaneSelectInTarget.java    From consulo with Apache License 2.0 5 votes vote down vote up
private boolean isInLibraryContentOnly(final VirtualFile vFile) {
  if (vFile == null) {
    return false;
  }
  ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
  return (projectFileIndex.isInLibraryClasses(vFile) || projectFileIndex.isInLibrarySource(vFile)) && !projectFileIndex.isInSourceContent(vFile);
}
 
Example 12
Source File: CSharpDocumentationProvider.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@RequiredReadAction
private static String generateQuickTypeDeclarationInfo(DotNetTypeDeclaration element, boolean isFullDocumentation)
{
	StringBuilder builder = new StringBuilder();

	if(isFullDocumentation)
	{
		PsiFile containingFile = element.getContainingFile();
		final ProjectFileIndex fileIndex = ProjectRootManager.getInstance(element.getProject()).getFileIndex();
		VirtualFile vFile = containingFile == null ? null : containingFile.getVirtualFile();
		if(vFile != null && (fileIndex.isInLibrarySource(vFile) || fileIndex.isInLibraryClasses(vFile)))
		{
			final List<OrderEntry> orderEntries = fileIndex.getOrderEntriesForFile(vFile);
			if(orderEntries.size() > 0)
			{
				final OrderEntry orderEntry = orderEntries.get(0);
				builder.append("[").append(StringUtil.escapeXml(orderEntry.getPresentableName())).append("] ");
			}
		}
		else
		{
			final Module module = containingFile == null ? null : ModuleUtil.findModuleForPsiElement(containingFile);
			if(module != null)
			{
				builder.append('[').append(module.getName()).append("] ");
			}
		}
	}

	String presentableParentQName = element.getPresentableParentQName();
	if(!StringUtil.isEmpty(presentableParentQName))
	{
		builder.append(presentableParentQName);
	}

	if(builder.length() > 0)
	{
		builder.append("<br>");
	}

	appendModifiers(element, builder);

	appendTypeDeclarationType(element, builder);

	builder.append(" ");

	appendName(element, builder, isFullDocumentation);

	return builder.toString();
}
 
Example 13
Source File: CoverageEngine.java    From consulo with Apache License 2.0 4 votes vote down vote up
public boolean isInLibraryClasses(Project project, VirtualFile file) {
  final ProjectFileIndex projectFileIndex = ProjectRootManager.getInstance(project).getFileIndex();

  return projectFileIndex.isInLibraryClasses(file);
}
 
Example 14
Source File: BaseAnalysisActionDialog.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
protected JComponent createCenterPanel() {
  myTitledSeparator.setText(myAnalysisNoon);

  //include test option
  myInspectTestSource.setSelected(myAnalysisOptions.ANALYZE_TEST_SOURCES);

  //module scope if applicable
  myModuleButton.setText(AnalysisScopeBundle.message("scope.option.module.with.mnemonic", myModuleName));
  boolean useModuleScope = false;
  if (myModuleName != null) {
    useModuleScope = myAnalysisOptions.SCOPE_TYPE == AnalysisScope.MODULE;
    myModuleButton.setSelected(myRememberScope && useModuleScope);
  }

  myModuleButton.setVisible(myModuleName != null && ModuleManager.getInstance(myProject).getModules().length > 1);

  boolean useUncommitedFiles = false;
  final ChangeListManager changeListManager = ChangeListManager.getInstance(myProject);
  final boolean hasVCS = !changeListManager.getAffectedFiles().isEmpty();
  if (hasVCS){
    useUncommitedFiles = myAnalysisOptions.SCOPE_TYPE == AnalysisScope.UNCOMMITTED_FILES;
    myUncommitedFilesButton.setSelected(myRememberScope && useUncommitedFiles);
  }
  myUncommitedFilesButton.setVisible(hasVCS);

  DefaultComboBoxModel model = new DefaultComboBoxModel();
  model.addElement(ALL);
  final List<? extends ChangeList> changeLists = changeListManager.getChangeListsCopy();
  for (ChangeList changeList : changeLists) {
    model.addElement(changeList.getName());
  }
  myChangeLists.setModel(model);
  myChangeLists.setEnabled(myUncommitedFilesButton.isSelected());
  myChangeLists.setVisible(hasVCS);

  //file/package/directory/module scope
  if (myFileName != null) {
    myFileButton.setText(myFileName);
    myFileButton.setMnemonic(myFileName.charAt(getSelectedScopeMnemonic()));
  } else {
    myFileButton.setVisible(false);
  }

  VirtualFile file = PsiUtilBase.getVirtualFile(myContext);
  ProjectFileIndex fileIndex = ProjectRootManager.getInstance(myProject).getFileIndex();
  boolean searchInLib = file != null && (fileIndex.isInLibraryClasses(file) || fileIndex.isInLibrarySource(file));

  String preselect = StringUtil.isEmptyOrSpaces(myAnalysisOptions.CUSTOM_SCOPE_NAME)
                     ? FindSettings.getInstance().getDefaultScopeName()
                     : myAnalysisOptions.CUSTOM_SCOPE_NAME;
  if (searchInLib && GlobalSearchScope.projectScope(myProject).getDisplayName().equals(preselect)) {
    preselect = GlobalSearchScope.allScope(myProject).getDisplayName();
  }
  if (GlobalSearchScope.allScope(myProject).getDisplayName().equals(preselect) && myAnalysisOptions.SCOPE_TYPE == AnalysisScope.CUSTOM) {
    myAnalysisOptions.CUSTOM_SCOPE_NAME = preselect;
    searchInLib = true;
  }

  //custom scope
  myCustomScopeButton.setSelected(myRememberScope && myAnalysisOptions.SCOPE_TYPE == AnalysisScope.CUSTOM);

  myScopeCombo.init(myProject, searchInLib, true, preselect);

  //correct selection
  myProjectButton.setSelected(myRememberScope && myAnalysisOptions.SCOPE_TYPE == AnalysisScope.PROJECT || myFileName == null);
  myFileButton.setSelected(myFileName != null &&
                           (!myRememberScope ||
                           myAnalysisOptions.SCOPE_TYPE != AnalysisScope.PROJECT && !useModuleScope && myAnalysisOptions.SCOPE_TYPE != AnalysisScope.CUSTOM && !useUncommitedFiles));

  myScopeCombo.setEnabled(myCustomScopeButton.isSelected());

  final ActionListener radioButtonPressed = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
      onScopeRadioButtonPressed();
    }
  };
  final Enumeration<AbstractButton> enumeration = myGroup.getElements();
  while (enumeration.hasMoreElements()) {
    enumeration.nextElement().addActionListener(radioButtonPressed);
  }

  //additional panel - inspection profile chooser
  JPanel wholePanel = new JPanel(new BorderLayout());
  wholePanel.add(myPanel, BorderLayout.NORTH);
  final JComponent additionalPanel = getAdditionalActionSettings(myProject);
  if (additionalPanel!= null){
    wholePanel.add(additionalPanel, BorderLayout.CENTER);
  }
  new RadioUpDownListener(myProjectButton, myModuleButton, myUncommitedFilesButton, myFileButton, myCustomScopeButton);
  return wholePanel;
}
 
Example 15
Source File: PackageViewLibrariesNode.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public boolean someChildContainsFile(VirtualFile file) {
  ProjectFileIndex index = ProjectRootManager.getInstance(getProject()).getFileIndex();
  if (!index.isInLibrarySource(file) && !index.isInLibraryClasses(file)) return false;
  return super.someChildContainsFile(file);    
}