Java Code Examples for com.intellij.psi.search.GlobalSearchScope#contains()

The following examples show how to use com.intellij.psi.search.GlobalSearchScope#contains() . These examples are extracted from open source projects. 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 Project: intellij-haxe   File: HaxeProjectModel.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
public List<HaxeModel> resolve(FullyQualifiedInfo info, @Nullable GlobalSearchScope searchScope) {
  if (info == null) return null;
  HaxeModel resolvedValue;
  List<HaxeModel> result = new ArrayList<>();
  for (HaxeSourceRootModel root : getRoots()) {
    if (searchScope == null || !searchScope.contains(root.root)) {
      continue;
    }
    resolvedValue = root.resolve(info);
    if (resolvedValue != null) result.add(resolvedValue);
  }

  if (result.isEmpty()) {
    resolvedValue = getStdPackage().resolve(info);
    if (resolvedValue != null) result.add(resolvedValue);
  }

  return result;
}
 
Example 2
Source Project: consulo   File: ResourceFileUtil.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
public static VirtualFile findResourceFileInScope(final String resourceName,
                                                  final Project project,
                                                  final GlobalSearchScope scope) {
  int index = resourceName.lastIndexOf('/');
  String packageName = index >= 0 ? resourceName.substring(0, index).replace('/', '.') : "";
  final String fileName = index >= 0 ? resourceName.substring(index+1) : resourceName;
  Query<VirtualFile> directoriesByPackageName = DirectoryIndex.getInstance(project).getDirectoriesByPackageName(packageName, true);
  for (VirtualFile virtualFile : directoriesByPackageName) {
    final VirtualFile child = virtualFile.findChild(fileName);
    if(child != null && scope.contains(child)) {
      return child;
    }
  }
  return null;
}
 
Example 3
Source Project: consulo   File: PsiFileReferenceHelper.java    License: Apache License 2.0 6 votes vote down vote up
static Collection<PsiFileSystemItem> getContextsForModule(@Nonnull Module module, @Nonnull String packageName, @Nullable GlobalSearchScope scope) {
  List<PsiFileSystemItem> result = null;
  Query<VirtualFile> query = DirectoryIndex.getInstance(module.getProject()).getDirectoriesByPackageName(packageName, false);
  PsiManager manager = null;

  for (VirtualFile file : query) {
    if (scope != null && !scope.contains(file)) continue;
    if (result == null) {
      result = new ArrayList<>();
      manager = PsiManager.getInstance(module.getProject());
    }
    PsiDirectory psiDirectory = manager.findDirectory(file);
    if (psiDirectory != null) result.add(psiDirectory);
  }

  return result != null ? result : Collections.emptyList();
}
 
Example 4
Source Project: litho   File: ComponentFinder.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public PsiClass[] getClasses(PsiPackage psiPackage, GlobalSearchScope scope) {
  if (!scope.contains(dummyFile)) return PsiClass.EMPTY_ARRAY;

  // We don't create own package, but provide additional classes to existing one
  final String packageQN = psiPackage.getQualifiedName();
  return Arrays.stream(ComponentsCacheService.getInstance(project).getAllComponents())
      .filter(cls -> StringUtil.getPackageName(cls.getQualifiedName()).equals(packageQN))
      .toArray(PsiClass[]::new);
}
 
Example 5
Source Project: litho   File: ComponentFinder.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
private PsiClass findClassInternal(
    String qualifiedName, GlobalSearchScope scope, Project project) {
  if (!scope.contains(dummyFile)) return null;

  if (!StringUtil.isJavaIdentifier(StringUtil.getShortName(qualifiedName))) return null;

  final PsiClass componentFromCache =
      ComponentsCacheService.getInstance(project).getComponent(qualifiedName);

  return componentFromCache;
}
 
Example 6
Source Project: litho   File: ComponentShortNamesCache.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public PsiClass[] getClassesByName(String name, GlobalSearchScope scope) {
  if (!scope.contains(dummyFile)) return PsiClass.EMPTY_ARRAY;

  return Arrays.stream(ComponentsCacheService.getInstance(project).getAllComponents())
      .filter(
          cls -> {
            final String shortName = StringUtil.getShortName(cls.getQualifiedName());
            return shortName.equals(name);
          })
      .toArray(PsiClass[]::new);
}
 
Example 7
Source Project: intellij   File: PyIssueParserProvider.java    License: Apache License 2.0 5 votes vote down vote up
/** Used to sort search results, in order: {project, library, sdk, no virtual file} */
private static int rankResult(PsiFile file, GlobalSearchScope projectScope, Sdk sdk) {
  VirtualFile vf = file.getVirtualFile();
  if (vf == null) {
    return 3;
  }
  if (projectScope.contains(vf)) {
    return 0;
  }
  return PythonSdkType.isStdLib(vf, sdk) ? 2 : 1;
}
 
Example 8
/**
 * Processes all named elements that match the specified word, e.g. the declaration of a type name
 */
public void processElementsWithWord(PsiElement scopedElement, String word, Processor<PsiNamedElement> processor) {
    try {
        final GlobalSearchScope schemaScope = getSchemaScope(scopedElement);

        processElementsWithWordUsingIdentifierIndex(schemaScope, word, processor);

        // also include the built-in schemas
        final PsiRecursiveElementVisitor builtInFileVisitor = new PsiRecursiveElementVisitor() {
            @Override
            public void visitElement(PsiElement element) {
                if (element instanceof PsiNamedElement && word.equals(element.getText())) {
                    if (!processor.process((PsiNamedElement) element)) {
                        return; // done processing
                    }
                }
                super.visitElement(element);
            }
        };

        // spec schema
        getBuiltInSchema().accept(builtInFileVisitor);

        // relay schema if enabled
        final PsiFile relayModernDirectivesSchema = getRelayModernDirectivesSchema();
        if (schemaScope.contains(relayModernDirectivesSchema.getVirtualFile())) {
            relayModernDirectivesSchema.accept(builtInFileVisitor);
        }

        // finally, look in the current scratch file
        if (GraphQLFileType.isGraphQLScratchFile(myProject, getVirtualFile(scopedElement.getContainingFile()))) {
            scopedElement.getContainingFile().accept(builtInFileVisitor);
        }

    } catch (IndexNotReadyException e) {
        // can't search yet (e.g. during project startup)
    }
}
 
Example 9
/**
 * Process built-in GraphQL PsiFiles that are not the spec schema
 *
 * @param schemaScope the search scope to use for limiting the schema definitions
 * @param consumer    a consumer that will be invoked for each injected GraphQL PsiFile
 */
public void processAdditionalBuiltInPsiFiles(GlobalSearchScope schemaScope, Consumer<PsiFile> consumer) {
    final PsiFile relayModernDirectivesSchema = getRelayModernDirectivesSchema();
    if (schemaScope.contains(relayModernDirectivesSchema.getVirtualFile())) {
        consumer.accept(relayModernDirectivesSchema);
    }
}
 
Example 10
Source Project: consulo   File: SourceScope.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
private GlobalSearchScope findScopeFor(final VirtualFile file) {
  if (myMainScope.contains(file)) return myMainScope;
  //noinspection ForLoopReplaceableByForEach
  for (int i = 0, size = myScopes.size(); i < size; i++) {
    GlobalSearchScope scope = myScopes.get(i);
    if (scope.contains(file)) return scope;
  }
  return null;
}
 
Example 11
Source Project: consulo   File: PsiPackageBase.java    License: Apache License 2.0 5 votes vote down vote up
@Override
@Nonnull
public PsiDirectory[] getDirectories(@Nonnull GlobalSearchScope scope) {
  List<PsiDirectory> result = null;
  final boolean includeLibrarySources = scope.isForceSearchingInLibrarySources();
  final Collection<PsiDirectory> directories = getAllDirectories(includeLibrarySources);
  for (final PsiDirectory directory : directories) {
    if (scope.contains(directory.getVirtualFile())) {
      if (result == null) result = new ArrayList<PsiDirectory>();
      result.add(directory);
    }
  }
  return result == null ? PsiDirectory.EMPTY_ARRAY : result.toArray(new PsiDirectory[result.size()]);
}
 
Example 12
Source Project: consulo   File: PlatformPackageUtil.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
private static PsiDirectory getWritableModuleDirectory(@Nonnull Query<VirtualFile> vFiles,
                                                       GlobalSearchScope scope,
                                                       PsiManager manager) {
  for (VirtualFile vFile : vFiles) {
    if (!scope.contains(vFile)) continue;
    PsiDirectory directory = manager.findDirectory(vFile);
    if (directory != null && directory.isValid() && directory.isWritable()) {
      return directory;
    }
  }
  return null;
}
 
Example 13
Source Project: CppTools   File: CppReferencesSearcher.java    License: Apache License 2.0 4 votes vote down vote up
private boolean doFindRefsInCppCode(final PsiFile psiFile, PsiElement target, ReferencesSearch.SearchParameters params,
                                    GlobalSearchScope globalScope, final Processor<PsiReference> processor) {
  final Project project = psiFile.getProject();
  final String commandName = project.getUserData(ourKey);
  final int offset;
  VirtualFile file;

  if (target instanceof CppFile) {
    offset = FindUsagesCommand.MAGIC_FILE_OFFSET;
    file = ((CppFile)target).getVirtualFile();
  } else {
    VirtualFile actionStartFile = null;
    int actionStartOffset = -1;
    
    final PsiFile actionStartPsiFile = project.getUserData(ourFileKey);
    if (actionStartPsiFile != null) {
      actionStartFile = actionStartPsiFile.getVirtualFile();
      final Integer integer = project.getUserData(ourOffsetKey);
      if (integer != null) actionStartOffset = integer.intValue();
    }
    
    if (actionStartOffset != -1) {
      offset = actionStartOffset;
      file = actionStartFile;
    } else {
      file = psiFile.getVirtualFile();
      offset = target.getTextOffset();
    }
  }

  final FindUsagesCommand findUsagesCommand = new FindUsagesCommand(
    file.getPath(),
    offset,
    RENAME_ACTION_TEXT.equals(commandName)
  );
  findUsagesCommand.setDoInfiniteBlockingWithCancelledCheck(true);

  findUsagesCommand.post(psiFile.getProject());

  if (!findUsagesCommand.hasReadyResult()) return true;

  final int count = findUsagesCommand.getUsageCount();
  if (count == 0) return true;

  final boolean scopeIsLocal = params.getScope() instanceof LocalSearchScope;
  final Set<VirtualFile> localScope = scopeIsLocal ? new HashSet<VirtualFile>() : null;

  if (scopeIsLocal) {
    for(PsiElement e: ((LocalSearchScope)params.getScope()).getScope()) {
      localScope.add(e.getContainingFile().getVirtualFile());
    }
  }

  for(final FileUsage fu:findUsagesCommand.getUsagesList().files) {
    final VirtualFile usagefile = fu.findVirtualFile();
    if ((globalScope != null && !globalScope.contains(usagefile)) ||
        localScope != null && !localScope.contains(usagefile)
      ) {
      continue;
    }


    Runnable runnable = new Runnable() {
      public void run() {
        final PsiFile usageFile = psiFile.getManager().findFile( usagefile );

        for(final OurUsage u:fu.usageList) {
          final PsiElement psiElement = usageFile.findElementAt(u.getStart());

          if (psiElement != null) {
            final PsiElement parentElement = psiElement.getParent();
            if (parentElement instanceof CppElement || parentElement instanceof CppKeyword /*operator*/) {
              final PsiReference reference = parentElement.getReference();
              if (reference != null) processor.process( reference );
            }
          }
        }
      }
    };

    ApplicationManager.getApplication().runReadAction(runnable);
  }
  return false;
}