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

The following examples show how to use com.intellij.openapi.roots.ProjectFileIndex#isInLibrarySource() . 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: 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 6
Source File: CheckinHandlerUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static boolean isFileUnderSourceRoot(@Nonnull Project project, @Nonnull VirtualFile file) {
  ProjectFileIndex index = ProjectRootManager.getInstance(project).getFileIndex();
  if (InternalStdFileTypes.JAVA == file.getFileType()) {
    return index.isInSource(file) && !index.isInLibrarySource(file);
  } else {
    return index.isInContent(file) && !index.isInLibrarySource(file) ;
  }
}
 
Example 7
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 8
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 9
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 10
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 11
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);    
}