com.intellij.psi.stubs.StubIndex Java Examples

The following examples show how to use com.intellij.psi.stubs.StubIndex. 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: PhpIndexUtil.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
private static Collection<PhpClass> getPhpClassInsideNamespace(Project project, PhpIndex phpIndex, String namespaceName, int maxDeep) {

        final Collection<PhpClass> phpClasses = new ArrayList<>();

        if(maxDeep-- <= 0) {
            return phpClasses;
        }

        StubIndex.getInstance().processElements(PhpNamespaceIndex.KEY, namespaceName.toLowerCase(), project, phpIndex.getSearchScope(), PhpNamespace.class, phpNamespace -> {
            phpClasses.addAll(PsiTreeUtil.getChildrenOfTypeAsList(phpNamespace.getStatements(), PhpClass.class));
            return true;
        });

        for(String ns: phpIndex.getChildNamespacesByParentName(namespaceName + "\\")) {
            phpClasses.addAll(getPhpClassInsideNamespace(project, phpIndex, namespaceName + "\\" + ns, maxDeep));
        }

        return phpClasses;
    }
 
Example #2
Source File: CSharpSymbolNameContributor.java    From consulo-csharp with Apache License 2.0 6 votes vote down vote up
@Override
public void processElementsWithName(
		@Nonnull String name, @Nonnull Processor<NavigationItem> navigationItemProcessor, @Nonnull FindSymbolParameters findSymbolParameters)
{
	Project project = findSymbolParameters.getProject();
	IdFilter idFilter = findSymbolParameters.getIdFilter();
	GlobalSearchScope searchScope = findSymbolParameters.getSearchScope();

	StubIndex.getInstance().processElements(CSharpIndexKeys.METHOD_INDEX, name, project, searchScope, idFilter,
			DotNetLikeMethodDeclaration.class, (Processor) navigationItemProcessor);
	StubIndex.getInstance().processElements(CSharpIndexKeys.EVENT_INDEX, name, project, searchScope, idFilter,
			DotNetEventDeclaration.class,  (Processor) navigationItemProcessor);
	StubIndex.getInstance().processElements(CSharpIndexKeys.PROPERTY_INDEX, name, project, searchScope, idFilter,
			DotNetPropertyDeclaration.class, (Processor) navigationItemProcessor);
	StubIndex.getInstance().processElements(CSharpIndexKeys.FIELD_INDEX, name, project, searchScope, idFilter,
			DotNetFieldDeclaration.class, (Processor) navigationItemProcessor);
}
 
Example #3
Source File: ResolveEngine.java    From intellij-neos with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
private static String findNamespaceByAlias(Project project, String alias) {
    Collection<FusionNamespaceDeclaration> namespaces = StubIndex.getElements(
            FusionNamespaceDeclarationIndex.KEY,
            alias,
            project,
            GlobalSearchScope.projectScope(project),
            FusionNamespaceDeclaration.class);

    if (!namespaces.isEmpty()) {
        FusionNamespace namespace = namespaces.iterator().next().getNamespace();
        if (namespace != null) {
            return namespace.getText();
        }
    }

    return null;
}
 
Example #4
Source File: BashPsiUtils.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
public static void collectIncludedFiles(PsiFile file, Set<PsiFile> files, boolean followNestedFiles) {
    String filePath = file.getVirtualFile().getPath();

    Collection<BashIncludeCommand> commands = StubIndex.getElements(BashIncludeCommandIndex.KEY, filePath, file.getProject(), GlobalSearchScope.fileScope(file), BashIncludeCommand.class);
    for (BashIncludeCommand command : commands) {
        PsiFile includedFile = findIncludedFile(command);
        if (includedFile != null) {
            boolean followFile = followNestedFiles && !files.contains(includedFile);
            files.add(includedFile);

            if (followFile) {
                collectIncludedFiles(includedFile, files, true);
            }
        }
    }
}
 
Example #5
Source File: BlazeGoTestLocator.java    From intellij with Apache License 2.0 6 votes vote down vote up
/**
 * @param path for function "TestFoo" in target "//foo/bar:baz" would be "//foo/bar:baz::TestFoo".
 *     See {@link BlazeGoTestEventsHandler#testLocationUrl}.
 */
@SuppressWarnings("rawtypes")
private static List<Location> findTestFunction(Project project, String path) {
  String[] parts = path.split(SmRunnerUtils.TEST_NAME_PARTS_SPLITTER);
  if (parts.length != 2) {
    return ImmutableList.of();
  }
  String labelString = parts[0];
  String functionName = parts[1];
  TargetIdeInfo target = getGoTestTarget(project, labelString);
  if (target == null) {
    return ImmutableList.of();
  }
  List<VirtualFile> goFiles = getGoFiles(project, target);
  if (goFiles.isEmpty()) {
    return ImmutableList.of();
  }
  GlobalSearchScope scope = FilesScope.filesScope(project, goFiles);
  Collection<GoFunctionDeclaration> functions =
      StubIndex.getElements(
          GoFunctionIndex.KEY, functionName, project, scope, GoFunctionDeclaration.class);
  return functions.stream().map(PsiLocation::new).collect(Collectors.toList());
}
 
Example #6
Source File: BashPsiUtils.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the commands of file which include the other file.
 *
 * @param file
 * @param filterByFile
 * @return The list of commands, may be empty but wont be null
 */
public static List<BashIncludeCommand> findIncludeCommands(PsiFile file, @Nullable final PsiFile filterByFile) {
    String filePath = file.getVirtualFile().getPath();

    List<BashIncludeCommand> result = Lists.newLinkedList();

    Collection<BashIncludeCommand> commands = StubIndex.getElements(BashIncludeCommandIndex.KEY, filePath, file.getProject(), GlobalSearchScope.fileScope(file), BashIncludeCommand.class);
    for (BashIncludeCommand command : commands) {
        if (filterByFile == null || filterByFile.equals(findIncludedFile(command))) {
            result.add(command);
        }
    }

    return result;
}
 
Example #7
Source File: CSharpSymbolNameContributor.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public void processNames(@Nonnull Processor<String> stringProcessor, @Nonnull GlobalSearchScope searchScope, @Nullable IdFilter idFilter)
{
	StubIndex.getInstance().processAllKeys(CSharpIndexKeys.METHOD_INDEX, stringProcessor, searchScope, idFilter);
	StubIndex.getInstance().processAllKeys(CSharpIndexKeys.EVENT_INDEX, stringProcessor, searchScope, idFilter);
	StubIndex.getInstance().processAllKeys(CSharpIndexKeys.PROPERTY_INDEX, stringProcessor, searchScope, idFilter);
	StubIndex.getInstance().processAllKeys(CSharpIndexKeys.FIELD_INDEX, stringProcessor, searchScope, idFilter);
}
 
Example #8
Source File: CSharpTypeNameContributor.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public void processElementsWithName(@Nonnull String name, @Nonnull final Processor<NavigationItem> navigationItemProcessor, @Nonnull FindSymbolParameters findSymbolParameters)
{
	Project project = findSymbolParameters.getProject();
	IdFilter idFilter = findSymbolParameters.getIdFilter();
	Processor temp = navigationItemProcessor;
	GlobalSearchScope searchScope = findSymbolParameters.getSearchScope();

	StubIndex.getInstance().processElements(CSharpIndexKeys.TYPE_INDEX, name, project, searchScope, idFilter, CSharpTypeDeclaration.class, temp);
	StubIndex.getInstance().processElements(CSharpIndexKeys.DELEGATE_METHOD_BY_NAME_INDEX, name, project, searchScope, idFilter, CSharpMethodDeclaration.class, temp);
}
 
Example #9
Source File: CSharpShortNameSearcher.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Override
public void collectTypes(@Nonnull String s, @Nonnull GlobalSearchScope searchScope, @Nullable IdFilter filter, @Nonnull final Processor<DotNetTypeDeclaration> processor)
{
	StubIndex.getInstance().processElements(CSharpIndexKeys.TYPE_INDEX, s, myProject, searchScope, filter, CSharpTypeDeclaration.class, processor);

	StubIndex.getInstance().processElements(CSharpIndexKeys.DELEGATE_METHOD_BY_NAME_INDEX, s, myProject, searchScope, filter, CSharpMethodDeclaration.class, methodDeclaration ->
	{
		ProgressManager.checkCanceled();

		CSharpTypeDeclaration typeFromDelegate = CSharpLambdaResolveResultUtil.createTypeFromDelegate(methodDeclaration, DotNetGenericExtractor.EMPTY);
		return processor.process(typeFromDelegate);
	});
}
 
Example #10
Source File: HaskellChooseByNameContributor.java    From intellij-haskforce with Apache License 2.0 5 votes vote down vote up
@NotNull
@Override
public NavigationItem[] getItemsByName(String name, String pattern, Project project, boolean includeNonProjectItems) {
    GlobalSearchScope scope = includeNonProjectItems ? GlobalSearchScope.allScope(project) : GlobalSearchScope.projectScope(project);
    Collection<HaskellNamedElement> result = StubIndex.getElements(HaskellAllNameIndex.KEY, name, project, scope, HaskellNamedElement.class);
    List<NavigationItem> items = ContainerUtil.newArrayListWithCapacity(result.size());
    for (HaskellNamedElement element : result) {
        items.add(element);
    }
    return items.toArray(new NavigationItem[items.size()]);
}
 
Example #11
Source File: FileInclusionManager.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
/**
 * Finds all files which include the given file.
 * The bash files of the module are checked if they include the file.
 *
 * @param project The project
 * @param file    The file for which the includers should be found.
 * @return
 */
@NotNull
public static Set<BashFile> findIncluders(@NotNull Project project, @NotNull PsiFile file) {
    if (DumbService.isDumb(project)) {
        return Collections.emptySet();
    }

    GlobalSearchScope searchScope = BashSearchScopes.moduleScope(file);

    String filename = file.getName();
    if (StringUtils.isEmpty(filename)) {
        return Collections.emptySet();
    }

    Collection<BashIncludeCommand> includeCommands = StubIndex.getElements(BashIncludedFilenamesIndex.KEY, filename, project, BashSearchScopes.bashOnly(searchScope), BashIncludeCommand.class);
    if (includeCommands == null || includeCommands.isEmpty()) {
        return Collections.emptySet();
    }

    Set<BashFile> includers = Sets.newLinkedHashSet();
    for (BashIncludeCommand command : includeCommands) {
        BashFile includer = (BashFile) BashPsiUtils.findFileContext(command);

        if (!file.equals(includer)) {
            includers.add(includer);
        }
    }

    return includers;
}
 
Example #12
Source File: PrototypeProvider.java    From intellij-neos with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    Collection<String> keys = StubIndex.getInstance().getAllKeys(FusionPrototypeDeclarationIndex.KEY, parameters.getPosition().getProject());
    Project project = parameters.getPosition().getProject();

    for (String key : keys) {
        Collection<FusionPrototypeSignature> prototypes = StubIndex.getElements(FusionPrototypeDeclarationIndex.KEY, key, project, GlobalSearchScope.projectScope(project), FusionPrototypeSignature.class );
        for (FusionPrototypeSignature signature : prototypes) {
            if (signature.getType() != null) {
                result.addElement(LookupElementBuilder.create(signature.getType().getText()).withIcon(FusionIcons.PROTOTYPE));
            }
        }
    }
}
 
Example #13
Source File: ResolveEngine.java    From intellij-neos with GNU General Public License v3.0 4 votes vote down vote up
public static List<PsiElement> getPrototypeDefinitions(Project project, String name, @Nullable String namespace) {
    String instanceAliasNamespace = null;
    if (namespace != null) {
        instanceAliasNamespace = findNamespaceByAlias(project, namespace);
    }

    // find all prototypes that have the name of this instance
    List<PsiElement> result = new ArrayList<>();
    Collection<FusionPrototypeSignature> possiblePrototypes = StubIndex.getElements(
            FusionPrototypeDeclarationIndex.KEY,
            name,
            project,
            GlobalSearchScope.projectScope(project),
            FusionPrototypeSignature.class);

    // check for each prototype if the namespace matches by resolving aliases
    for (FusionPrototypeSignature possiblePrototype : possiblePrototypes) {
        FusionType prototypeType = possiblePrototype.getType();
        if (prototypeType != null) {
            PsiElement prototypeNamespace = prototypeType.getObjectTypeNamespace();
            if (prototypeNamespace != null) {
                // check if prototype has default namespace
                if (namespace == null) {
                    if (prototypeNamespace.getText().equals("TYPO3.Neos")
                            || prototypeNamespace.getText().equals("Neos.Neos")) {
                        result.add(possiblePrototype);
                    }
                    continue;
                }

                String prototypeNs = prototypeType.getObjectTypeNamespace().getText();
                if (prototypeNs.equals(namespace) || prototypeNs.equals(instanceAliasNamespace)) {
                    result.add(possiblePrototype);
                } else {
                    prototypeNs = findNamespaceByAlias(project, prototypeNs);
                    if (namespace.equals(prototypeNs)) {
                        result.add(possiblePrototype);
                    }
                }
            } else if (namespace == null
                    || (namespace.equals("TYPO3.Neos")
                    || namespace.equals("Neos.Neos"))) {

                result.add(possiblePrototype);
            }
        }
    }

    // If one of the results is a prototype inheritance, return it as the only result
    for (PsiElement resultPrototype : result) {
        if (resultPrototype instanceof FusionPrototypeSignature
                && ((FusionPrototypeSignature)resultPrototype).isInheritanceDefinition()) {
            result.clear();
            result.add(resultPrototype);
            return result;
        }
    }

    return result;
}
 
Example #14
Source File: ModuleFqnIndex.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
@NotNull
@Override
public Collection<PsiModule> get(@NotNull final Integer integer, @NotNull final Project project, @NotNull final GlobalSearchScope scope) {
    return StubIndex.getElements(getKey(), integer, project, scope, PsiModule.class);
}
 
Example #15
Source File: VariantFqnIndex.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
@NotNull
@Override
public Collection<PsiVariantDeclaration> get(@NotNull final Integer integer, @NotNull final Project project, @NotNull final GlobalSearchScope scope) {
    return StubIndex.getElements(getKey(), integer, project, scope, PsiVariantDeclaration.class);
}
 
Example #16
Source File: ExceptionFqnIndex.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
@NotNull
@Override
public Collection<PsiException> get(@NotNull final Integer integer, @NotNull final Project project, @NotNull final GlobalSearchScope scope) {
    return StubIndex.getElements(getKey(), integer, project, scope, PsiException.class);
}
 
Example #17
Source File: LetFqnIndex.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
@NotNull
@Override
public Collection<PsiLet> get(@NotNull final Integer integer, @NotNull final Project project, @NotNull final GlobalSearchScope scope) {
    return StubIndex.getElements(getKey(), integer, project, /*new JavaSourceFilterScope(scope) TODO*/scope, PsiLet.class);
}
 
Example #18
Source File: CSharpTypeNameContributor.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Override
public void processNames(@Nonnull Processor<String> stringProcessor, @Nonnull GlobalSearchScope searchScope, @Nullable IdFilter idFilter)
{
	StubIndex.getInstance().processAllKeys(CSharpIndexKeys.TYPE_INDEX, stringProcessor, searchScope, idFilter);
	StubIndex.getInstance().processAllKeys(CSharpIndexKeys.DELEGATE_METHOD_BY_NAME_INDEX, stringProcessor, searchScope, idFilter);
}
 
Example #19
Source File: ValFqnIndex.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
@NotNull
@Override
public Collection<PsiVal> get(@NotNull final Integer integer, @NotNull final Project project, @NotNull final GlobalSearchScope scope) {
    return StubIndex.getElements(getKey(), integer, project, /*new JavaSourceFilterScope(scope) TODO*/scope, PsiVal.class);
}
 
Example #20
Source File: CSharpShortNameSearcher.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Override
public void collectTypeNames(@Nonnull Processor<String> processor, @Nonnull GlobalSearchScope searchScope, @Nullable IdFilter filter)
{
	StubIndex.getInstance().processAllKeys(CSharpIndexKeys.TYPE_INDEX, processor, searchScope, filter);
	StubIndex.getInstance().processAllKeys(CSharpIndexKeys.DELEGATE_METHOD_BY_NAME_INDEX, processor, searchScope, filter);
}
 
Example #21
Source File: CSharpNamespaceResolveContext.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@RequiredReadAction
public List<CSharpResolveContext> collectResolveContexts()
{
	String qName = myNamespaceAsElement.getPresentableQName();
	if(qName == null)
	{
		return Collections.emptyList();
	}

	Project project = myNamespaceAsElement.getProject();
	String indexableName = DotNetNamespaceStubUtil.getIndexableNamespace(qName);

	Set<DotNetTypeDeclaration> result = new THashSet<>();

	StubIndex.getInstance().processElements(CSharpIndexKeys.TYPE_WITH_EXTENSION_METHODS_INDEX, indexableName, project, myResolveScope, DotNetTypeDeclaration.class, Processors
			.cancelableCollectProcessor(result));

	Set<String> processed = new THashSet<>();

	List<CSharpResolveContext> list = new SmartList<>();
	for(DotNetTypeDeclaration typeDeclaration : result)
	{
		ProgressManager.checkCanceled();

		PsiElement wrappedDeclaration = ToNativeElementTransformers.transform(typeDeclaration);

		if(typeDeclaration instanceof CSharpTypeDeclaration && typeDeclaration.hasModifier(CSharpModifier.PARTIAL))
		{
			String vmQName = typeDeclaration.getVmQName();
			if(processed.contains(vmQName))
			{
				continue;
			}
			processed.add(vmQName);
		}

		CSharpResolveContext context = CSharpResolveContextUtil.createContext(DotNetGenericExtractor.EMPTY, myResolveScope, wrappedDeclaration);

		list.add(context);
	}
	return list.isEmpty() ? Collections.emptyList() : list;
}
 
Example #22
Source File: FileInclusionManager.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
@NotNull
public static Set<PsiFile> findIncludedFiles(@NotNull PsiFile sourceFile, boolean diveDeep, boolean bashOnly) {
    if (!(sourceFile instanceof BashFile)) {
        return Collections.emptySet();
    }

    if (!sourceFile.isPhysical()) {
        return Collections.emptySet();
    }

    if (DumbService.isDumb(sourceFile.getProject())) {
        return Collections.emptySet();
    }

    Project project = sourceFile.getProject();

    Set<PsiFile> includersTodo = Sets.newLinkedHashSet(Collections.singletonList(sourceFile));
    Set<PsiFile> includersDone = Sets.newLinkedHashSet();

    Set<PsiFile> allIncludedFiles = Sets.newHashSet();

    while (!includersTodo.isEmpty()) {
        Iterator<PsiFile> iterator = includersTodo.iterator();

        PsiFile file = iterator.next();
        iterator.remove();

        includersDone.add(file);

        VirtualFile virtualFile = file.getVirtualFile();
        if (virtualFile == null) {
            continue;
        }

        String filePath = virtualFile.getPath();

        Collection<BashIncludeCommand> commands = StubIndex.getElements(BashIncludeCommandIndex.KEY, filePath, project, BashSearchScopes.bashOnly(BashSearchScopes.moduleScope(file)), BashIncludeCommand.class);
        if (commands.isEmpty()) {
            continue;
        }

        for (BashIncludeCommand command : commands) {
            BashFileReference fileReference = command.getFileReference();
            if (fileReference != null && fileReference.isStatic()) {
                PsiFile referencedFile = fileReference.findReferencedFile();
                if (bashOnly && !(referencedFile instanceof BashFile)) {
                    continue;
                }

                if (referencedFile != null) {
                    allIncludedFiles.add(referencedFile);

                    if (!includersDone.contains(referencedFile)) {
                        //the include commands of this command have to be collected, too
                        includersTodo.add(referencedFile);
                    }
                }
            }
        }

        if (!diveDeep) {
            //the first iteration is the original source
            break;
        }
    }

    return allIncludedFiles;
}
 
Example #23
Source File: HaskellChooseByNameContributor.java    From intellij-haskforce with Apache License 2.0 4 votes vote down vote up
@NotNull
@Override
public String[] getNames(Project project, boolean includeNonProjectItems) {
    return ArrayUtil.toStringArray(StubIndex.getInstance().getAllKeys(HaskellAllNameIndex.KEY, project));
}
 
Example #24
Source File: UnityScriptGotoClassContributor.java    From consulo-unity3d with Apache License 2.0 4 votes vote down vote up
@Override
public void processElementsWithName(@Nonnull String name, @Nonnull final Processor<NavigationItem> processor, @Nonnull FindSymbolParameters parameters)
{
	StubIndex.getInstance().processElements(UnityScriptIndexKeys.FILE_BY_NAME_INDEX, name, parameters.getProject(), parameters.getSearchScope(), parameters.getIdFilter(), JSFile.class,
			new Processor<JSFile>()
	{
		@Override
		public boolean process(final JSFile file)
		{
			return processor.process(new FakePsiElement()
			{
				@Override
				public String getName()
				{
					return FileUtil.getNameWithoutExtension(file.getName());
				}

				@Nonnull
				@Override
				public Project getProject()
				{
					return file.getProject();
				}

				@Nullable
				@Override
				public Image getIcon()
				{
					IconDescriptor descriptor = new IconDescriptor(AllIcons.Nodes.Class);
					descriptor.addLayerIcon(Unity3dIcons.Js);
					descriptor.setRightIcon(AllIcons.Nodes.C_public);
					return descriptor.toIcon();
				}

				@Override
				public PsiElement getParent()
				{
					return file;
				}
			});
		}
	});
}
 
Example #25
Source File: UnityScriptGotoClassContributor.java    From consulo-unity3d with Apache License 2.0 4 votes vote down vote up
@Override
public void processNames(@Nonnull Processor<String> processor, @Nonnull GlobalSearchScope scope, @Nullable IdFilter filter)
{
	StubIndex.getInstance().processAllKeys(UnityScriptIndexKeys.FILE_BY_NAME_INDEX, processor, scope, filter);
}
 
Example #26
Source File: SmartFunctionReference.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
public PsiElement resolveInner() {
    final String referencedName = cmd.getReferencedCommandName();
    if (referencedName == null) {
        return null;
    }

    final ResolveProcessor processor = new BashFunctionProcessor(referencedName);

    Project project = cmd.getProject();
    PsiFile currentFile = cmd.getContainingFile();

    GlobalSearchScope allFiles = FileInclusionManager.includedFilesUnionScope(currentFile);
    Collection<BashFunctionDef> functionDefs = StubIndex.getElements(BashFunctionNameIndex.KEY, referencedName, project, allFiles, BashFunctionDef.class);

    ResolveState initial = ResolveState.initial();
    for (BashFunctionDef functionDef : functionDefs) {
        processor.execute(functionDef, initial);
    }

    //find include commands which are relevant for the start element
    if (!processor.hasResults()) {
        Set<BashFile> includingFiles = FileInclusionManager.findIncluders(project, currentFile);

        List<GlobalSearchScope> scopes = Lists.newLinkedList();
        for (BashFile file : includingFiles) {
            scopes.add(GlobalSearchScope.fileScope(file));
        }

        if (!scopes.isEmpty()) {
            GlobalSearchScope scope = GlobalSearchScope.union(scopes.toArray(new GlobalSearchScope[scopes.size()]));

            functionDefs = StubIndex.getElements(BashFunctionNameIndex.KEY, referencedName, project, scope, BashFunctionDef.class);

            for (BashFunctionDef def : functionDefs) {
                processor.execute(def, initial);
            }
        }
    }

    processor.prepareResults();

    return processor.hasResults() ? processor.getBestResult(true, cmd) : null;
}
 
Example #27
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 #28
Source File: ParameterFqnIndex.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
@NotNull
@Override
public Collection<PsiParameter> get(@NotNull final Integer integer, @NotNull final Project project, @NotNull final GlobalSearchScope scope) {
    return StubIndex.getElements(getKey(), integer, project, /*new JavaSourceFilterScope(scope) TODO*/scope, PsiParameter.class);
}
 
Example #29
Source File: BashResolveUtil.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
/**
 * @return true if the definition of this variable is not child of a conditional command or loop.
 */
public static boolean hasStaticVarDefPath(BashVar bashVar) {
    BashReference reference = bashVar.getNeighborhoodReference();
    if (reference == null) {
        return false;
    }

    PsiElement closestDef = reference.resolve();
    if (closestDef == null) {
        return false;
    }

    // if the closest def is in a different def scope, then we can't handle that
    // (e.g. var is top-level, def is in a function or var is in a function and def in another function, etc.)
    BashFunctionDef varScope = BashPsiUtils.findNextVarDefFunctionDefScope(bashVar);
    BashFunctionDef defScope = BashPsiUtils.findNextVarDefFunctionDefScope(closestDef);
    if (varScope == null && defScope != null) {
        return false;
    }

    // we can't handle different functions as scope
    if (varScope != null && !varScope.isEquivalentTo(defScope)) {
        return false;
    }

    // atm we can't handle different files
    PsiFile psiFile = bashVar.getContainingFile();
    if (varScope == null && !psiFile.isEquivalentTo(closestDef.getContainingFile())) {
        return false;
    }

    Collection<BashVarDef> allDefs = StubIndex.getElements(BashVarDefIndex.KEY, bashVar.getReferenceName(), bashVar.getProject(), GlobalSearchScope.fileScope(psiFile), BashVarDef.class);
    for (BashVarDef candidateDef : allDefs) {
        ProgressManager.checkCanceled();

        // skip var defs which are not in our own def scope
        BashFunctionDef scope = BashPsiUtils.findNextVarDefFunctionDefScope(candidateDef);
        if (varScope != null && !varScope.isEquivalentTo(scope)) {
            continue;
        }

        // it's not a static path if the var def is in a conditional block or loop and if our var is not
        PsiElement parent = PsiTreeUtil.findFirstParent(candidateDef, psi -> psi instanceof BashConditionalBlock || psi instanceof BashLoop);
        if (parent != null && !PsiTreeUtil.isAncestor(parent, bashVar, true)) {
            return false;
        }
    }

    return true;
}
 
Example #30
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);
}