use of com.goide.psi.GoFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoUnusedFunctionInspection method buildGoVisitor.
@NotNull
@Override
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
return new GoVisitor() {
@Override
public void visitFunctionDeclaration(@NotNull GoFunctionDeclaration o) {
if (o.isBlank())
return;
GoFile file = o.getContainingFile();
String name = o.getName();
if (!canRun(name))
return;
if (GoConstants.MAIN.equals(file.getPackageName()) && GoConstants.MAIN.equals(name))
return;
if (GoConstants.INIT.equals(name))
return;
if (GoTestFinder.isTestFile(file) && GoTestFunctionType.fromName(name) != null)
return;
if (ReferencesSearch.search(o, o.getUseScope()).findFirst() == null) {
PsiElement id = o.getIdentifier();
TextRange range = TextRange.from(id.getStartOffsetInParent(), id.getTextLength());
holder.registerProblem(o, "Unused function <code>#ref</code> #loc", ProblemHighlightType.LIKE_UNUSED_SYMBOL, range, new GoDeleteQuickFix("Delete function", GoFunctionDeclaration.class), new GoRenameToBlankQuickFix(o));
}
}
};
}
use of com.goide.psi.GoFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoPackageClauseStubTest method testStub.
public void testStub() {
GoFile file = (GoFile) myFixture.addFileToProject("bar/bar.go", "package bar; import `foo`; func _() { println(CONST_NAME) }");
failOnFileLoading();
file.getPackageName();
}
use of com.goide.psi.GoFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoTestFinder method findTestsForClass.
@NotNull
@Override
public Collection<PsiElement> findTestsForClass(@NotNull PsiElement element) {
PsiFile file = InjectedLanguageUtil.getTopLevelFile(element);
if (file instanceof GoFile) {
PsiDirectory directory = file.getContainingDirectory();
PsiFile testFile = directory.findFile(FileUtil.getNameWithoutExtension(file.getName()) + GoConstants.TEST_SUFFIX_WITH_EXTENSION);
if (testFile != null) {
return ContainerUtil.newSmartList(testFile);
}
}
return Collections.emptyList();
}
use of com.goide.psi.GoFile in project go-lang-idea-plugin by go-lang-plugin-org.
the class GoImportPackageQuickFix method perform.
private void perform(@NotNull PsiFile file, @Nullable String pathToImport) {
if (file instanceof GoFile && pathToImport != null) {
Project project = file.getProject();
CommandProcessor.getInstance().executeCommand(project, () -> ApplicationManager.getApplication().runWriteAction(() -> {
if (!isAvailable())
return;
if (((GoFile) file).getImportedPackagesMap().containsKey(pathToImport))
return;
((GoFile) file).addImport(pathToImport, null);
}), "Add import", null);
}
}
use of com.goide.psi.GoFile 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));
}
Aggregations