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

The following examples show how to use com.intellij.psi.search.GlobalSearchScope#fileScope() . 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: CodeGenerationUtils.java    From JHelper with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static void removeUnusedCode(PsiFile file) {
	while (true) {
		Collection<PsiElement> toDelete = new ArrayList<>();
		Project project = file.getProject();
		SearchScope scope = GlobalSearchScope.fileScope(project, file.getVirtualFile());
		file.acceptChildren(new DeletionMarkingVisitor(toDelete, scope));
		if (toDelete.isEmpty()) {
			break;
		}
		WriteCommandAction.writeCommandAction(project).run(
				() -> {
					for (PsiElement element : toDelete) {
						element.delete();
					}
				}
		);

	}
}
 
Example 2
Source File: GraphQLPsiSearchHelper.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
public GraphQLPsiSearchHelper(@NotNull final Project project) {
    myProject = project;
    mySettings = GraphQLSettings.getSettings(project);
    psiManager = PsiManager.getInstance(myProject);
    graphQLInjectionSearchHelper = ServiceManager.getService(GraphQLInjectionSearchHelper.class);
    injectedLanguageManager = InjectedLanguageManager.getInstance(myProject);
    graphQLConfigManager = GraphQLConfigManager.getService(myProject);
    pluginDescriptor = PluginManager.getPlugin(PluginId.getId("com.intellij.lang.jsgraphql"));

    final PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(myProject);
    defaultProjectFile = (GraphQLFile) psiFileFactory.createFileFromText("Default schema file", GraphQLLanguage.INSTANCE, "");

    GlobalSearchScope defaultProjectFileScope = GlobalSearchScope.fileScope(defaultProjectFile);
    GlobalSearchScope builtInSchemaScope = GlobalSearchScope.fileScope(project, getBuiltInSchema().getVirtualFile());
    GlobalSearchScope builtInRelaySchemaScope = GlobalSearchScope.fileScope(project, getRelayModernDirectivesSchema().getVirtualFile());
    allBuiltInSchemaScopes = builtInSchemaScope
            .union(new ConditionalGlobalSearchScope(builtInRelaySchemaScope, mySettings::isEnableRelayModernFrameworkSupport))
            .union(defaultProjectFileScope)
    ;

    final FileType[] searchScopeFileTypes = GraphQLFindUsagesUtil.getService().getIncludedFileTypes().toArray(FileType.EMPTY_ARRAY);
    searchScope = GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.projectScope(myProject), searchScopeFileTypes).union(allBuiltInSchemaScopes);
    project.getMessageBus().connect().subscribe(PsiManagerImpl.ANY_PSI_CHANGE_TOPIC, new AnyPsiChangeListener.Adapter() {
        @Override
        public void beforePsiChanged(boolean isPhysical) {
            // clear the cache on each PSI change
            fileNameToSchemaScope.clear();
        }
    });
}
 
Example 3
Source File: BashResolveUtil.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
public static GlobalSearchScope varDefSearchScope(BashVar reference, boolean withIncludedFiles) {
    PsiFile referenceFile = BashPsiUtils.findFileContext(reference);
    if (!withIncludedFiles) {
        return GlobalSearchScope.fileScope(referenceFile.getProject(), referenceFile.getVirtualFile());
    }

    Set<VirtualFile> result = Sets.newLinkedHashSet();
    result.add(referenceFile.getVirtualFile());

    int referenceFileOffset = BashPsiUtils.getFileTextOffset(reference);
    BashFunctionDef referenceFunctionContainer = BashPsiUtils.findNextVarDefFunctionDefScope(reference);

    for (BashIncludeCommand command : BashPsiUtils.findIncludeCommands(referenceFile, null)) {
        boolean includeIsInFunction = BashPsiUtils.findNextVarDefFunctionDefScope(command) != null;

        //either one of var or include command is in a function or the var is used after the include command
        if (referenceFunctionContainer != null || includeIsInFunction || (referenceFileOffset > BashPsiUtils.getFileTextEndOffset(command))) {
            BashFileReference fileReference = command.getFileReference();
            PsiFile includedFile = fileReference != null ? fileReference.findReferencedFile() : null;
            if (includedFile != null) {
                result.add(includedFile.getVirtualFile());

                //also, add all files included in the valid include command's file
                for (PsiFile file : BashPsiUtils.findIncludedFiles(includedFile, true)) {
                    result.add(file.getVirtualFile());
                }
            }
        }
    }

    return GlobalSearchScope.filesScope(referenceFile.getProject(), result);
}
 
Example 4
Source File: BashSearchScopes.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
public static GlobalSearchScope moduleScope(PsiFile file) {
    VirtualFile virtualFile = file.getVirtualFile();
    if (virtualFile == null) {
        return GlobalSearchScope.EMPTY_SCOPE;
    }

    Module module = ProjectRootManager.getInstance(file.getProject()).getFileIndex().getModuleForFile(virtualFile);
    if (module == null) {
        return GlobalSearchScope.fileScope(file);
    }

    //the module scope returned by getModuleScope() just contains the files in the configured source and test source directories,
    //module content scope includes all files in the module directory
    return module.getModuleContentScope();
}
 
Example 5
Source File: BashElementSharedImpl.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
public static GlobalSearchScope getElementGlobalSearchScope(BashPsiElement element, Project project) {
    PsiFile psiFile = BashPsiUtils.findFileContext(element);
    GlobalSearchScope currentFileScope = GlobalSearchScope.fileScope(psiFile);

    Set<PsiFile> includedFiles = FileInclusionManager.findIncludedFiles(psiFile, true, true);
    Collection<VirtualFile> files = Collections2.transform(includedFiles, psiToVirtualFile());

    return currentFileScope.uniteWith(GlobalSearchScope.filesScope(project, files));
}
 
Example 6
Source File: IdIndex.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean hasIdentifierInFile(@Nonnull PsiFile file, @Nonnull String name) {
  PsiUtilCore.ensureValid(file);
  if (file.getVirtualFile() == null || DumbService.isDumb(file.getProject())) {
    return StringUtil.contains(file.getViewProvider().getContents(), name);
  }

  GlobalSearchScope scope = GlobalSearchScope.fileScope(file);
  return !FileBasedIndex.getInstance().getContainingFiles(NAME, new IdIndexEntry(name, true), scope).isEmpty();
}
 
Example 7
Source File: BashResolveUtil.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
public static PsiElement resolve(BashVar bashVar, boolean dumbMode, ResolveProcessor processor) {
    if (bashVar == null || !bashVar.isPhysical()) {
        return null;
    }

    final String varName = bashVar.getReferenceName();
    if (varName == null) {
        return null;
    }

    PsiFile psiFile = BashPsiUtils.findFileContext(bashVar);
    VirtualFile virtualFile = psiFile.getVirtualFile();

    String filePath = virtualFile != null ? virtualFile.getPath() : null;
    Project project = bashVar.getProject();

    ResolveState resolveState = ResolveState.initial();

    GlobalSearchScope fileScope = GlobalSearchScope.fileScope(psiFile);

    Collection<BashVarDef> varDefs;
    if (dumbMode || isScratchFile(virtualFile) || isNotIndexedFile(project, virtualFile)) {
        varDefs = PsiTreeUtil.collectElementsOfType(psiFile, BashVarDef.class);
    } else {
        varDefs = StubIndex.getElements(BashVarDefIndex.KEY, varName, project, fileScope, BashVarDef.class);
    }

    for (BashVarDef varDef : varDefs) {
        ProgressManager.checkCanceled();

        processor.execute(varDef, resolveState);
    }

    if (!dumbMode && filePath != null) {
        Collection<BashIncludeCommand> includeCommands = StubIndex.getElements(BashIncludeCommandIndex.KEY, filePath, project, fileScope, BashIncludeCommand.class);
        if (!includeCommands.isEmpty()) {
            boolean varIsInFunction = BashPsiUtils.findNextVarDefFunctionDefScope(bashVar) != null;

            for (BashIncludeCommand command : includeCommands) {
                ProgressManager.checkCanceled();

                boolean includeIsInFunction = BashPsiUtils.findNextVarDefFunctionDefScope(command) != null;

                //either one of var or include command is in a function or the var is used after the include command
                if (varIsInFunction || includeIsInFunction || (BashPsiUtils.getFileTextOffset(bashVar) > BashPsiUtils.getFileTextEndOffset(command))) {
                    try {
                        resolveState = resolveState.put(Keys.resolvingIncludeCommand, command);

                        command.processDeclarations(processor, resolveState, command, bashVar);
                    } finally {
                        resolveState = resolveState.put(Keys.resolvingIncludeCommand, null);
                    }
                }
            }
        }
    }

    processor.prepareResults();

    return processor.getBestResult(false, bashVar);
}
 
Example 8
Source File: BashElementSharedImpl.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
public static SearchScope getElementUseScope(BashPsiElement element, Project project) {
    //all files which include this element's file belong to the requested scope
    //bash files can call other bash files, thus the scope needs to be the module scope at minumum
    //fixme can this be optimized?
    PsiFile currentFile = BashPsiUtils.findFileContext(element);
    if (currentFile == null) {
        //no other fallback possible here
        return GlobalSearchScope.projectScope(project);
    }

    Set<BashFile> includers = FileInclusionManager.findIncluders(project, currentFile);
    Set<PsiFile> included = FileInclusionManager.findIncludedFiles(currentFile, true, true);

    //find all files which reference the source file
    Set<PsiFile> referencingScriptFiles = Sets.newLinkedHashSet();
    if (element instanceof BashFile) {
        String searchedName = ((BashFile) element).getName();
        if (searchedName != null) {
            Collection<BashCommand> commands = StubIndex.getElements(
                    BashCommandNameIndex.KEY,
                    searchedName,
                    project,
                    GlobalSearchScope.projectScope(project), //module scope isn't working as expected because it doesn't include non-src dirs
                    BashCommand.class);
            if (commands != null) {
                for (BashCommand command : commands) {
                    referencingScriptFiles.add(BashPsiUtils.findFileContext(command));
                }
            }
        }
    }

    if (includers.isEmpty() && included.isEmpty() && referencingScriptFiles.isEmpty()) {
        //we should return a local search scope if we only have local references
        //not return a local scope because then inline renaming is not possible
        return GlobalSearchScope.fileScope(currentFile);
    }

    //fixme improve this
    Set<PsiFile> union = Sets.newLinkedHashSet();
    union.addAll(included);
    union.addAll(includers);
    union.addAll(referencingScriptFiles);

    Collection<VirtualFile> virtualFiles = Collections2.transform(union, psiToVirtualFile());
    return GlobalSearchScope.fileScope(currentFile).union(GlobalSearchScope.filesScope(project, virtualFiles));
}
 
Example 9
Source File: ScratchResolveScopeEnlarger.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public SearchScope getAdditionalResolveScope(@Nonnull VirtualFile file, Project project) {
  return ScratchUtil.isScratch(file) ? GlobalSearchScope.fileScope(project, file) : null;
}