Search in sources :

Example 6 with GoFile

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));
            }
        }
    };
}
Also used : GoFunctionDeclaration(com.goide.psi.GoFunctionDeclaration) GoFile(com.goide.psi.GoFile) GoRenameToBlankQuickFix(com.goide.quickfix.GoRenameToBlankQuickFix) TextRange(com.intellij.openapi.util.TextRange) GoDeleteQuickFix(com.goide.quickfix.GoDeleteQuickFix) NotNull(org.jetbrains.annotations.NotNull) GoVisitor(com.goide.psi.GoVisitor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with GoFile

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();
}
Also used : GoFile(com.goide.psi.GoFile)

Example 8 with GoFile

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();
}
Also used : GoFile(com.goide.psi.GoFile) PsiDirectory(com.intellij.psi.PsiDirectory) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 9 with GoFile

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);
    }
}
Also used : GoFile(com.goide.psi.GoFile) Project(com.intellij.openapi.project.Project)

Example 10 with GoFile

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

Aggregations

GoFile (com.goide.psi.GoFile)31 PsiFile (com.intellij.psi.PsiFile)20 PsiElement (com.intellij.psi.PsiElement)12 Project (com.intellij.openapi.project.Project)8 VirtualFile (com.intellij.openapi.vfs.VirtualFile)8 PsiDirectory (com.intellij.psi.PsiDirectory)8 Module (com.intellij.openapi.module.Module)6 NotNull (org.jetbrains.annotations.NotNull)6 TargetMap (com.google.idea.blaze.base.ideinfo.TargetMap)4 BlazeProjectData (com.google.idea.blaze.base.model.BlazeProjectData)4 MockBlazeProjectDataManager (com.google.idea.blaze.base.model.MockBlazeProjectDataManager)4 WorkspacePath (com.google.idea.blaze.base.model.primitives.WorkspacePath)4 WorkspaceLanguageSettings (com.google.idea.blaze.base.sync.projectview.WorkspaceLanguageSettings)4 WorkspacePathResolverImpl (com.google.idea.blaze.base.sync.workspace.WorkspacePathResolverImpl)4 Nullable (javax.annotation.Nullable)4 Test (org.junit.Test)4 GoPackageClause (com.goide.psi.GoPackageClause)3 Nullable (org.jetbrains.annotations.Nullable)3 GoFunctionDeclaration (com.goide.psi.GoFunctionDeclaration)2 GoFunctionOrMethodDeclaration (com.goide.psi.GoFunctionOrMethodDeclaration)2