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);
}
}
}
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;
}
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));
}
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);
}
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;
});
}
};
}
Aggregations