Search in sources :

Example 11 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoTestFunctionCompletionProvider method addCompletions.

@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
    Project project = parameters.getPosition().getProject();
    PsiFile file = parameters.getOriginalFile();
    PsiDirectory containingDirectory = file.getContainingDirectory();
    if (file instanceof GoFile && containingDirectory != null) {
        CompletionResultSet resultSet = result.withPrefixMatcher(new CamelHumpMatcher(result.getPrefixMatcher().getPrefix(), false));
        Collection<String> allPackageFunctionNames = collectAllFunctionNames(containingDirectory);
        Set<String> allTestFunctionNames = collectAllTestNames(allPackageFunctionNames, project, (GoFile) file);
        String fileNameWithoutTestPrefix = StringUtil.trimEnd(file.getName(), GoConstants.TEST_SUFFIX_WITH_EXTENSION) + ".go";
        GlobalSearchScope packageScope = GoPackageUtil.packageScope(containingDirectory, ((GoFile) file).getCanonicalPackageName());
        GlobalSearchScope scope = new GoUtil.ExceptTestsScope(packageScope);
        IdFilter idFilter = GoIdFilter.getFilesFilter(scope);
        for (String functionName : allPackageFunctionNames) {
            GoFunctionIndex.process(functionName, project, scope, idFilter, declaration -> {
                addVariants(declaration, functionName, fileNameWithoutTestPrefix, allTestFunctionNames, resultSet);
                return false;
            });
        }
        Collection<String> methodKeys = ContainerUtil.newTroveSet();
        StubIndex.getInstance().processAllKeys(GoMethodIndex.KEY, new CancellableCollectProcessor<>(methodKeys), scope, idFilter);
        for (String key : methodKeys) {
            Processor<GoMethodDeclaration> processor = declaration -> {
                GoMethodDeclarationStubElementType.calcTypeText(declaration);
                String typeText = key.substring(Math.min(key.indexOf('.') + 1, key.length()));
                String methodName = declaration.getName();
                if (methodName != null) {
                    if (!declaration.isPublic() || declaration.isBlank()) {
                        return true;
                    }
                    String lookupString = !typeText.isEmpty() ? StringUtil.capitalize(typeText) + "_" + methodName : methodName;
                    addVariants(declaration, lookupString, fileNameWithoutTestPrefix, allTestFunctionNames, resultSet);
                }
                return true;
            };
            GoMethodIndex.process(key, project, scope, idFilter, processor);
        }
    }
}
Also used : com.goide.psi(com.goide.psi) Document(com.intellij.openapi.editor.Document) GoMethodIndex(com.goide.stubs.index.GoMethodIndex) GotestGenerateAction(com.goide.runconfig.testing.frameworks.gotest.GotestGenerateAction) IdFilter(com.intellij.util.indexing.IdFilter) StubIndex(com.intellij.psi.stubs.StubIndex) ContainerUtil(com.intellij.util.containers.ContainerUtil) GoTestFunctionType(com.goide.runconfig.testing.GoTestFunctionType) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) GoMethodDeclarationStubElementType(com.goide.stubs.types.GoMethodDeclarationStubElementType) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) GoElementFactory(com.goide.psi.impl.GoElementFactory) CamelHumpMatcher(com.intellij.codeInsight.completion.impl.CamelHumpMatcher) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) ProcessingContext(com.intellij.util.ProcessingContext) GoFunctionIndex(com.goide.stubs.index.GoFunctionIndex) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) StringUtil(com.intellij.openapi.util.text.StringUtil) Collection(java.util.Collection) GoUtil(com.goide.util.GoUtil) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Set(java.util.Set) UniqueNameGenerator(com.intellij.util.text.UniqueNameGenerator) com.intellij.codeInsight.completion(com.intellij.codeInsight.completion) GoIdFilter(com.goide.stubs.index.GoIdFilter) CodeStyleManager(com.intellij.psi.codeStyle.CodeStyleManager) Processor(com.intellij.util.Processor) GoConstants(com.goide.GoConstants) PsiDirectory(com.intellij.psi.PsiDirectory) GoPackageUtil(com.goide.sdk.GoPackageUtil) NotNull(org.jetbrains.annotations.NotNull) IdFilter(com.intellij.util.indexing.IdFilter) GoIdFilter(com.goide.stubs.index.GoIdFilter) CamelHumpMatcher(com.intellij.codeInsight.completion.impl.CamelHumpMatcher) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile)

Example 12 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoTestFunctionCompletionProvider method collectAllFunctionNames.

@NotNull
private static Set<String> collectAllFunctionNames(@NotNull PsiDirectory directory) {
    GlobalSearchScope packageScope = GoPackageUtil.packageScope(directory, null);
    IdFilter packageIdFilter = GoIdFilter.getFilesFilter(packageScope);
    Set<String> result = ContainerUtil.newHashSet();
    StubIndex.getInstance().processAllKeys(GoFunctionIndex.KEY, new CancellableCollectProcessor<String>(result) {

        @Override
        protected boolean accept(String s) {
            return !"_".equals(s) && StringUtil.isCapitalized(s);
        }
    }, packageScope, packageIdFilter);
    return result;
}
Also used : IdFilter(com.intellij.util.indexing.IdFilter) GoIdFilter(com.goide.stubs.index.GoIdFilter) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) NotNull(org.jetbrains.annotations.NotNull)

Example 13 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoImportPackageQuickFix method getImportPathVariantsToImport.

@NotNull
public static List<String> getImportPathVariantsToImport(@NotNull String packageName, @NotNull PsiElement context) {
    PsiFile contextFile = context.getContainingFile();
    Set<String> imported = contextFile instanceof GoFile ? ((GoFile) contextFile).getImportedPackagesMap().keySet() : Collections.emptySet();
    Project project = context.getProject();
    PsiDirectory parentDirectory = contextFile != null ? contextFile.getParent() : null;
    String testTargetPackage = GoTestFinder.getTestTargetPackage(contextFile);
    Module module = contextFile != null ? ModuleUtilCore.findModuleForPsiElement(contextFile) : null;
    boolean vendoringEnabled = GoVendoringUtil.isVendoringEnabled(module);
    GlobalSearchScope scope = GoUtil.goPathResolveScope(context);
    Collection<GoFile> packages = StubIndex.getElements(GoPackagesIndex.KEY, packageName, project, scope, GoFile.class);
    return sorted(skipNulls(map2Set(packages, file -> {
        if (parentDirectory != null && parentDirectory.isEquivalentTo(file.getParent())) {
            if (testTargetPackage == null || !testTargetPackage.equals(file.getPackageName())) {
                return null;
            }
        }
        if (!GoPsiImplUtil.canBeAutoImported(file, false, module)) {
            return null;
        }
        String importPath = file.getImportPath(vendoringEnabled);
        return !imported.contains(importPath) ? importPath : null;
    })), new MyImportsComparator(context, vendoringEnabled));
}
Also used : GoFile(com.goide.psi.GoFile) Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull)

Example 14 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoDocumentationProvider method getDocumentationElementForLink.

@Override
public PsiElement getDocumentationElementForLink(PsiManager psiManager, String link, PsiElement context) {
    if (context != null && !DumbService.isDumb(psiManager.getProject())) {
        // it's important to ask module on file, otherwise module won't be found for elements in libraries files [zolotov]
        Module module = ModuleUtilCore.findModuleForPsiElement(context.getContainingFile());
        int hash = link.indexOf('#');
        String importPath = hash >= 0 ? link.substring(0, hash) : link;
        Project project = psiManager.getProject();
        VirtualFile directory = GoPackageUtil.findByImportPath(importPath, project, module);
        PsiDirectory psiDirectory = directory != null ? psiManager.findDirectory(directory) : null;
        String anchor = hash >= 0 ? link.substring(Math.min(hash + 1, link.length())) : null;
        if (StringUtil.isNotEmpty(anchor)) {
            GlobalSearchScope scope = psiDirectory != null ? GoPackageUtil.packageScope(psiDirectory, null) : GlobalSearchScope.projectScope(project);
            IdFilter idFilter = GoIdFilter.getFilesFilter(scope);
            GoNamedElement element = ContainerUtil.getFirstItem(StubIndex.getElements(GoAllPublicNamesIndex.ALL_PUBLIC_NAMES, anchor, project, scope, idFilter, GoNamedElement.class));
            if (element != null) {
                return element;
            }
            return ContainerUtil.getFirstItem(StubIndex.getElements(GoAllPrivateNamesIndex.ALL_PRIVATE_NAMES, anchor, project, scope, idFilter, GoNamedElement.class));
        } else {
            return psiDirectory;
        }
    }
    return super.getDocumentationElementForLink(psiManager, link, context);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) IdFilter(com.intellij.util.indexing.IdFilter) GoIdFilter(com.goide.stubs.index.GoIdFilter) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module)

Example 15 with GlobalSearchScope

use of com.intellij.psi.search.GlobalSearchScope in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoDuplicateFunctionOrMethodInspection method buildGoVisitor.

@NotNull
@Override
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
    return new GoVisitor() {

        @Override
        public void visitMethodDeclaration(@NotNull GoMethodDeclaration method) {
            if (method.isBlank())
                return;
            String methodName = method.getName();
            if (methodName == null)
                return;
            String typeText = GoMethodDeclarationStubElementType.calcTypeText(method);
            if (typeText == null)
                return;
            GoFile file = method.getContainingFile();
            GlobalSearchScope scope = GoPackageUtil.packageScope(file);
            IdFilter idFilter = GoIdFilter.getFilesFilter(scope);
            Module module = ModuleUtilCore.findModuleForPsiElement(file);
            String key = file.getPackageName() + "." + typeText;
            GoMethodIndex.process(key, file.getProject(), scope, idFilter, declaration -> {
                ProgressManager.checkCanceled();
                if (!method.isEquivalentTo(declaration)) {
                    if (Comparing.equal(declaration.getName(), methodName) && GoPsiImplUtil.allowed(declaration.getContainingFile(), file, module)) {
                        PsiElement identifier = method.getNameIdentifier();
                        holder.registerProblem(identifier == null ? method : identifier, "Duplicate method name");
                        return false;
                    }
                }
                return true;
            });
        }

        @Override
        public void visitFunctionDeclaration(@NotNull GoFunctionDeclaration func) {
            if (func.isBlank())
                return;
            String funcName = func.getName();
            if (funcName == null)
                return;
            if (INIT.equals(funcName) && zeroArity(func))
                return;
            GoFile file = func.getContainingFile();
            boolean isMainFunction = MAIN.equals(funcName) && MAIN.equals(file.getPackageName()) && zeroArity(func);
            Module module = ModuleUtilCore.findModuleForPsiElement(file);
            GlobalSearchScope scope = GoPackageUtil.packageScope(file);
            IdFilter idFilter = GoIdFilter.getFilesFilter(scope);
            GoFunctionIndex.process(funcName, file.getProject(), scope, idFilter, declaration -> {
                ProgressManager.checkCanceled();
                if (!func.isEquivalentTo(declaration) && GoPsiImplUtil.allowed(declaration.getContainingFile(), file, module)) {
                    if (!isMainFunction || Comparing.equal(declaration.getContainingFile(), file)) {
                        PsiElement identifier = func.getNameIdentifier();
                        holder.registerProblem(identifier == null ? func : identifier, "Duplicate function name");
                        return false;
                    }
                }
                return true;
            });
        }
    };
}
Also used : IdFilter(com.intellij.util.indexing.IdFilter) GoIdFilter(com.goide.stubs.index.GoIdFilter) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) Module(com.intellij.openapi.module.Module) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)485 Project (com.intellij.openapi.project.Project)145 NotNull (org.jetbrains.annotations.NotNull)134 VirtualFile (com.intellij.openapi.vfs.VirtualFile)106 Module (com.intellij.openapi.module.Module)101 Nullable (org.jetbrains.annotations.Nullable)78 PsiClass (com.intellij.psi.PsiClass)53 ArrayList (java.util.ArrayList)37 PsiFile (com.intellij.psi.PsiFile)32 PsiElement (com.intellij.psi.PsiElement)31 THashSet (gnu.trove.THashSet)22 SearchScope (com.intellij.psi.search.SearchScope)19 PsiDirectory (com.intellij.psi.PsiDirectory)18 ContainerUtil (com.intellij.util.containers.ContainerUtil)18 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)17 ProjectFileIndex (com.intellij.openapi.roots.ProjectFileIndex)17 com.intellij.psi (com.intellij.psi)17 List (java.util.List)17 StringUtil (com.intellij.openapi.util.text.StringUtil)15 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)15