Search in sources :

Example 1 with GoFunctionDeclaration

use of com.goide.psi.GoFunctionDeclaration 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 2 with GoFunctionDeclaration

use of com.goide.psi.GoFunctionDeclaration in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoTestRunLineMarkerProvider method getInfo.

@Nullable
@Override
public Info getInfo(PsiElement e) {
    if (e != null && e.getNode().getElementType() == GoTypes.IDENTIFIER) {
        PsiElement parent = e.getParent();
        PsiFile file = e.getContainingFile();
        if (!GoTestFinder.isTestFile(file)) {
            return null;
        }
        if (GoRunUtil.isPackageContext(e)) {
            return new Info(AllIcons.RunConfigurations.TestState.Run_run, TOOLTIP_PROVIDER, ExecutorAction.getActions(0));
        } else if (parent instanceof GoFunctionOrMethodDeclaration) {
            GoTestFunctionType functionType = GoTestFunctionType.fromName(((GoFunctionOrMethodDeclaration) parent).getName());
            if (functionType != null) {
                if (parent instanceof GoFunctionDeclaration) {
                    return getInfo(GoTestLocator.PROTOCOL + "://" + ((GoFunctionDeclaration) parent).getName(), e.getProject());
                } else if (parent instanceof GoMethodDeclaration) {
                    GoReceiver receiver = ((GoMethodDeclaration) parent).getReceiver();
                    PsiElement receiverIdentifier = receiver != null ? receiver.getIdentifier() : null;
                    String receiverText = receiverIdentifier != null ? receiverIdentifier.getText() + "." : "";
                    return getInfo(GoTestLocator.PROTOCOL + "://" + receiverText + ((GoMethodDeclaration) parent).getName(), e.getProject());
                }
            }
        }
    }
    return null;
}
Also used : GoFunctionDeclaration(com.goide.psi.GoFunctionDeclaration) GoMethodDeclaration(com.goide.psi.GoMethodDeclaration) GoReceiver(com.goide.psi.GoReceiver) PsiFile(com.intellij.psi.PsiFile) GoFunctionOrMethodDeclaration(com.goide.psi.GoFunctionOrMethodDeclaration) TestStateInfo(com.intellij.execution.testframework.sm.runner.states.TestStateInfo) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with GoFunctionDeclaration

use of com.goide.psi.GoFunctionDeclaration in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoRunLineMarkerProvider method getInfo.

@Nullable
@Override
public Info getInfo(PsiElement e) {
    if (e != null && e.getNode().getElementType() == GoTypes.IDENTIFIER) {
        PsiElement parent = e.getParent();
        PsiFile file = e.getContainingFile();
        if (GoRunUtil.isMainGoFile(file) && parent instanceof GoFunctionDeclaration) {
            if (GoConstants.MAIN.equals(((GoFunctionDeclaration) parent).getName())) {
                return new Info(AllIcons.RunConfigurations.TestState.Run, TOOLTIP_PROVIDER, ExecutorAction.getActions(0));
            }
        }
    }
    return null;
}
Also used : GoFunctionDeclaration(com.goide.psi.GoFunctionDeclaration) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with GoFunctionDeclaration

use of com.goide.psi.GoFunctionDeclaration in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoEmptySignatureQuickFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    GoFunctionDeclaration function = ObjectUtils.tryCast(startElement, GoFunctionDeclaration.class);
    GoSignature signature = function != null ? function.getSignature() : null;
    if (signature == null)
        return;
    signature.replace(GoElementFactory.createFunctionSignatureFromText(project, ""));
}
Also used : GoFunctionDeclaration(com.goide.psi.GoFunctionDeclaration) GoSignature(com.goide.psi.GoSignature)

Aggregations

GoFunctionDeclaration (com.goide.psi.GoFunctionDeclaration)4 PsiElement (com.intellij.psi.PsiElement)3 PsiFile (com.intellij.psi.PsiFile)2 Nullable (org.jetbrains.annotations.Nullable)2 GoFile (com.goide.psi.GoFile)1 GoFunctionOrMethodDeclaration (com.goide.psi.GoFunctionOrMethodDeclaration)1 GoMethodDeclaration (com.goide.psi.GoMethodDeclaration)1 GoReceiver (com.goide.psi.GoReceiver)1 GoSignature (com.goide.psi.GoSignature)1 GoVisitor (com.goide.psi.GoVisitor)1 GoDeleteQuickFix (com.goide.quickfix.GoDeleteQuickFix)1 GoRenameToBlankQuickFix (com.goide.quickfix.GoRenameToBlankQuickFix)1 TestStateInfo (com.intellij.execution.testframework.sm.runner.states.TestStateInfo)1 TextRange (com.intellij.openapi.util.TextRange)1 NotNull (org.jetbrains.annotations.NotNull)1