Search in sources :

Example 1 with LocalInspectionToolSession

use of com.intellij.codeInspection.LocalInspectionToolSession in project intellij-community by JetBrains.

the class PyImportOptimizer method processFile.

@Override
@NotNull
public Runnable processFile(@NotNull final PsiFile file) {
    final LocalInspectionToolSession session = new LocalInspectionToolSession(file, 0, file.getTextLength());
    final PyUnresolvedReferencesInspection.Visitor visitor = new PyUnresolvedReferencesInspection.Visitor(null, session, Collections.emptyList());
    file.accept(new PyRecursiveElementVisitor() {

        @Override
        public void visitElement(PsiElement node) {
            super.visitElement(node);
            node.accept(visitor);
        }
    });
    return () -> {
        visitor.optimizeImports();
        if (file instanceof PyFile) {
            new ImportSorter((PyFile) file).run();
        }
    };
}
Also used : LocalInspectionToolSession(com.intellij.codeInspection.LocalInspectionToolSession) PyUnresolvedReferencesInspection(com.jetbrains.python.inspections.unresolvedReference.PyUnresolvedReferencesInspection) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with LocalInspectionToolSession

use of com.intellij.codeInspection.LocalInspectionToolSession in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoUnusedVariableInspection method buildGoVisitor.

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

        @Override
        public void visitVarDefinition(@NotNull GoVarDefinition o) {
            if (o.isBlank())
                return;
            GoCompositeElement varSpec = PsiTreeUtil.getParentOfType(o, GoVarSpec.class, GoTypeSwitchGuard.class);
            GoVarDeclaration decl = PsiTreeUtil.getParentOfType(o, GoVarDeclaration.class);
            if (shouldValidate(decl) && (varSpec != null || decl != null)) {
                PsiReference reference = o.getReference();
                PsiElement resolve = reference != null ? reference.resolve() : null;
                if (resolve != null)
                    return;
                boolean foundReference = !ReferencesSearch.search(o, o.getUseScope()).forEach(reference1 -> {
                    ProgressManager.checkCanceled();
                    PsiElement element = reference1.getElement();
                    if (element == null)
                        return true;
                    PsiElement parent = element.getParent();
                    if (parent instanceof GoLeftHandExprList) {
                        PsiElement grandParent = parent.getParent();
                        if (grandParent instanceof GoAssignmentStatement && ((GoAssignmentStatement) grandParent).getAssignOp().getAssign() != null) {
                            GoFunctionLit fn = PsiTreeUtil.getParentOfType(element, GoFunctionLit.class);
                            if (fn == null || !PsiTreeUtil.isAncestor(GoVarProcessor.getScope(o), fn, true)) {
                                return true;
                            }
                        }
                    }
                    if (parent instanceof GoShortVarDeclaration) {
                        int op = ((GoShortVarDeclaration) parent).getVarAssign().getStartOffsetInParent();
                        if (element.getStartOffsetInParent() < op) {
                            return true;
                        }
                    }
                    return false;
                });
                if (!foundReference) {
                    reportError(o, holder);
                }
            }
        }
    };
}
Also used : ProgressManager(com.intellij.openapi.progress.ProgressManager) ReferencesSearch(com.intellij.psi.search.searches.ReferencesSearch) GoDeleteVarDefinitionQuickFix(com.goide.quickfix.GoDeleteVarDefinitionQuickFix) com.goide.psi(com.goide.psi) LocalInspectionToolSession(com.intellij.codeInspection.LocalInspectionToolSession) PsiReference(com.intellij.psi.PsiReference) GoVarProcessor(com.goide.psi.impl.GoVarProcessor) GoInspectionBase(com.goide.inspections.GoInspectionBase) Nullable(org.jetbrains.annotations.Nullable) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) GoRenameToBlankQuickFix(com.goide.quickfix.GoRenameToBlankQuickFix) PsiElement(com.intellij.psi.PsiElement) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) NotNull(org.jetbrains.annotations.NotNull) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) PsiReference(com.intellij.psi.PsiReference) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with LocalInspectionToolSession

use of com.intellij.codeInspection.LocalInspectionToolSession in project intellij-community by JetBrains.

the class HighlightSeverityTest method testErrorLikeUnusedSymbol.

public void testErrorLikeUnusedSymbol() throws Exception {
    enableInspectionTool(new LocalInspectionTool() {

        @NotNull
        @Override
        public String getShortName() {
            return getDisplayName();
        }

        @NotNull
        @Override
        public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
            return new JavaElementVisitor() {

                @Override
                public void visitIdentifier(PsiIdentifier identifier) {
                    if (identifier.getText().equals("k")) {
                        holder.registerProblem(identifier, "Variable 'k' is never used");
                    }
                }
            };
        }

        @NotNull
        @Override
        public HighlightDisplayLevel getDefaultLevel() {
            return HighlightDisplayLevel.ERROR;
        }

        @Nls
        @NotNull
        @Override
        public String getDisplayName() {
            return "x";
        }

        @Nls
        @NotNull
        @Override
        public String getGroupDisplayName() {
            return getDisplayName();
        }
    });
    doTest(BASE_PATH + "/" + getTestName(false) + ".java", true, false);
}
Also used : HighlightDisplayLevel(com.intellij.codeHighlighting.HighlightDisplayLevel) JavaElementVisitor(com.intellij.psi.JavaElementVisitor) Nls(org.jetbrains.annotations.Nls) NonNls(org.jetbrains.annotations.NonNls) LocalInspectionToolSession(com.intellij.codeInspection.LocalInspectionToolSession) PsiIdentifier(com.intellij.psi.PsiIdentifier) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) LocalInspectionTool(com.intellij.codeInspection.LocalInspectionTool) NotNull(org.jetbrains.annotations.NotNull) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder)

Example 4 with LocalInspectionToolSession

use of com.intellij.codeInspection.LocalInspectionToolSession in project intellij-community by JetBrains.

the class InspectionLIfeCycleTest method testInspectionFinishedCalledOnce.

public void testInspectionFinishedCalledOnce() throws Exception {
    String text = "class LQF {\n" + "    int f;\n" + "    public void me() {\n" + "        <caret>\n" + "    }\n" + "}";
    configureFromFileText("x.java", text);
    final AtomicInteger startedCount = new AtomicInteger();
    final AtomicInteger finishedCount = new AtomicInteger();
    final Key<Object> KEY = Key.create("just key");
    LocalInspectionTool tool = new LocalInspectionTool() {

        @Nls
        @NotNull
        @Override
        public String getGroupDisplayName() {
            return "fegna";
        }

        @Nls
        @NotNull
        @Override
        public String getDisplayName() {
            return getGroupDisplayName();
        }

        @NotNull
        @Override
        public String getShortName() {
            return getGroupDisplayName();
        }

        @NotNull
        @Override
        public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) {
            return new PsiElementVisitor() {
            };
        }

        @Override
        public void inspectionStarted(@NotNull LocalInspectionToolSession session, boolean isOnTheFly) {
            startedCount.incrementAndGet();
            session.putUserData(KEY, session);
        }

        @Override
        public void inspectionFinished(@NotNull LocalInspectionToolSession session, @NotNull ProblemsHolder problemsHolder) {
            finishedCount.incrementAndGet();
            assertEmpty(problemsHolder.getResults());
            assertSame(session, session.getUserData(KEY));
        }
    };
    enableInspectionTool(tool);
    List<HighlightInfo> infos = highlightErrors();
    assertEmpty(infos);
    assertEquals(1, startedCount.get());
    assertEquals(1, finishedCount.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LocalInspectionToolSession(com.intellij.codeInspection.LocalInspectionToolSession) HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) LocalInspectionTool(com.intellij.codeInspection.LocalInspectionTool) NotNull(org.jetbrains.annotations.NotNull) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder)

Example 5 with LocalInspectionToolSession

use of com.intellij.codeInspection.LocalInspectionToolSession in project intellij-community by JetBrains.

the class PyMakeFunctionFromMethodQuickFix method removeFormerImport.

private static void removeFormerImport(@NotNull final PsiFile usageFile, boolean addImport) {
    if (usageFile instanceof PyFile && addImport) {
        final LocalInspectionToolSession session = new LocalInspectionToolSession(usageFile, 0, usageFile.getTextLength());
        final PyUnresolvedReferencesInspection.Visitor visitor = new PyUnresolvedReferencesInspection.Visitor(null, session, Collections.<String>emptyList());
        usageFile.accept(new PyRecursiveElementVisitor() {

            @Override
            public void visitPyElement(PyElement node) {
                super.visitPyElement(node);
                node.accept(visitor);
            }
        });
        visitor.optimizeImports();
    }
}
Also used : LocalInspectionToolSession(com.intellij.codeInspection.LocalInspectionToolSession) PyUnresolvedReferencesInspection(com.jetbrains.python.inspections.unresolvedReference.PyUnresolvedReferencesInspection)

Aggregations

LocalInspectionToolSession (com.intellij.codeInspection.LocalInspectionToolSession)5 NotNull (org.jetbrains.annotations.NotNull)4 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)3 LocalInspectionTool (com.intellij.codeInspection.LocalInspectionTool)2 PsiElement (com.intellij.psi.PsiElement)2 PsiElementVisitor (com.intellij.psi.PsiElementVisitor)2 PyUnresolvedReferencesInspection (com.jetbrains.python.inspections.unresolvedReference.PyUnresolvedReferencesInspection)2 GoInspectionBase (com.goide.inspections.GoInspectionBase)1 com.goide.psi (com.goide.psi)1 GoVarProcessor (com.goide.psi.impl.GoVarProcessor)1 GoDeleteVarDefinitionQuickFix (com.goide.quickfix.GoDeleteVarDefinitionQuickFix)1 GoRenameToBlankQuickFix (com.goide.quickfix.GoRenameToBlankQuickFix)1 HighlightDisplayLevel (com.intellij.codeHighlighting.HighlightDisplayLevel)1 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 ProblemHighlightType (com.intellij.codeInspection.ProblemHighlightType)1 ProgressManager (com.intellij.openapi.progress.ProgressManager)1 JavaElementVisitor (com.intellij.psi.JavaElementVisitor)1 PsiIdentifier (com.intellij.psi.PsiIdentifier)1 PsiReference (com.intellij.psi.PsiReference)1 ReferencesSearch (com.intellij.psi.search.searches.ReferencesSearch)1