Search in sources :

Example 31 with PsiElementVisitor

use of com.intellij.psi.PsiElementVisitor in project intellij-community by JetBrains.

the class MisspelledHeaderInspection method buildVisitor.

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

        @Override
        public void visitElement(PsiElement element) {
            if (element instanceof Header) {
                Header header = (Header) element;
                String headerName = header.getName();
                SortedSet<Suggestion> matches = new TreeSet<>();
                addMatches(headerName, CUSTOM_HEADERS, matches);
                addMatches(headerName, myRepository.getAllHeaderNames(), matches);
                Suggestion bestMatch = ContainerUtil.getFirstItem(matches);
                if (bestMatch != null && headerName.equals(bestMatch.getWord())) {
                    return;
                }
                List<LocalQuickFix> fixes = new ArrayList<>();
                for (Suggestion match : matches) {
                    fixes.add(new HeaderRenameQuickFix(header, match.getWord()));
                    if (fixes.size() == MAX_SUGGESTIONS)
                        break;
                }
                if (bestMatch == null || bestMatch.getMetrics() > TYPO_DISTANCE) {
                    fixes.add(new CustomHeaderQuickFix(header, CUSTOM_HEADERS));
                }
                holder.registerProblem(header.getNameElement(), ManifestBundle.message("inspection.header.message"), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, fixes.toArray(new LocalQuickFix[fixes.size()]));
            }
        }

        private void addMatches(String headerName, Collection<String> headers, SortedSet<Suggestion> matches) {
            for (String candidate : headers) {
                int distance = EditDistance.optimalAlignment(headerName, candidate, false);
                if (distance <= MAX_DISTANCE) {
                    matches.add(new Suggestion(candidate, distance));
                }
            }
        }
    };
}
Also used : LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) Suggestion(com.intellij.spellchecker.engine.Suggestion) Header(org.jetbrains.lang.manifest.psi.Header) AbstractCollection(com.intellij.util.xmlb.annotations.AbstractCollection) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 32 with PsiElementVisitor

use of com.intellij.psi.PsiElementVisitor 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 33 with PsiElementVisitor

use of com.intellij.psi.PsiElementVisitor 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 34 with PsiElementVisitor

use of com.intellij.psi.PsiElementVisitor in project intellij-community by JetBrains.

the class SSBasedInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    final Map<Configuration, MatchContext> compiledOptions = SSBasedInspectionCompiledPatternsCache.getCompiledOptions(myConfigurations, holder.getProject());
    if (compiledOptions.isEmpty())
        return super.buildVisitor(holder, isOnTheFly);
    return new PsiElementVisitor() {

        final Matcher matcher = new Matcher(holder.getManager().getProject());

        final PairProcessor<MatchResult, Configuration> processor = (matchResult, configuration) -> {
            PsiElement element = matchResult.getMatch();
            String name = configuration.getName();
            LocalQuickFix fix = createQuickFix(holder.getManager().getProject(), matchResult, configuration);
            holder.registerProblem(holder.getManager().createProblemDescriptor(element, name, fix, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly));
            return true;
        };

        @Override
        public void visitElement(PsiElement element) {
            synchronized (LOCK) {
                if (LexicalNodesFilter.getInstance().accepts(element))
                    return;
                final SsrFilteringNodeIterator matchedNodes = new SsrFilteringNodeIterator(element);
                for (Map.Entry<Configuration, MatchContext> entry : compiledOptions.entrySet()) {
                    Configuration configuration = entry.getKey();
                    MatchContext context = entry.getValue();
                    if (MatcherImpl.checkIfShouldAttemptToMatch(context, matchedNodes)) {
                        final int nodeCount = context.getPattern().getNodeCount();
                        try {
                            matcher.processMatchesInElement(context, configuration, new CountingNodeIterator(nodeCount, matchedNodes), processor);
                        } catch (StructuralSearchException e) {
                            if (myProblemsReported.add(configuration.getName())) {
                                // don't overwhelm the user with messages
                                Notifications.Bus.notify(new Notification(SSRBundle.message("structural.search.title"), SSRBundle.message("template.problem", configuration.getName()), e.getMessage(), NotificationType.ERROR), element.getProject());
                            }
                        }
                        matchedNodes.reset();
                    }
                }
            }
        }
    };
}
Also used : ReplaceConfiguration(com.intellij.structuralsearch.plugin.replace.ui.ReplaceConfiguration) Configuration(com.intellij.structuralsearch.plugin.ui.Configuration) Matcher(com.intellij.structuralsearch.Matcher) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) PairProcessor(com.intellij.util.PairProcessor) Notification(com.intellij.notification.Notification) StructuralSearchException(com.intellij.structuralsearch.StructuralSearchException) CountingNodeIterator(com.intellij.dupLocator.iterators.CountingNodeIterator) MatchContext(com.intellij.structuralsearch.impl.matcher.MatchContext) SsrFilteringNodeIterator(com.intellij.structuralsearch.impl.matcher.iterators.SsrFilteringNodeIterator) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 35 with PsiElementVisitor

use of com.intellij.psi.PsiElementVisitor in project intellij-community by JetBrains.

the class GlobalMatchingVisitor method getVisitorForElement.

@Nullable
private PsiElementVisitor getVisitorForElement(PsiElement element) {
    Language language = element.getLanguage();
    PsiElementVisitor visitor = myLanguage2MatchingVisitor.get(language);
    if (visitor == null) {
        visitor = createMatchingVisitor(language);
        myLanguage2MatchingVisitor.put(language, visitor);
    }
    return visitor;
}
Also used : Language(com.intellij.lang.Language) PsiElementVisitor(com.intellij.psi.PsiElementVisitor) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

PsiElementVisitor (com.intellij.psi.PsiElementVisitor)60 PsiElement (com.intellij.psi.PsiElement)54 NotNull (org.jetbrains.annotations.NotNull)49 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)39 BasePhpElementVisitor (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpElementVisitor)37 BasePhpInspection (com.kalessil.phpStorm.phpInspectionsEA.openApi.BasePhpInspection)37 com.jetbrains.php.lang.psi.elements (com.jetbrains.php.lang.psi.elements)27 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)21 Project (com.intellij.openapi.project.Project)19 MessagesPresentationUtil (com.kalessil.phpStorm.phpInspectionsEA.utils.MessagesPresentationUtil)19 PhpTokenTypes (com.jetbrains.php.lang.lexer.PhpTokenTypes)17 Set (java.util.Set)17 com.kalessil.phpStorm.phpInspectionsEA.utils (com.kalessil.phpStorm.phpInspectionsEA.utils)16 HashSet (java.util.HashSet)16 OptionsComponent (com.kalessil.phpStorm.phpInspectionsEA.options.OptionsComponent)15 javax.swing (javax.swing)15 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)14 PhpType (com.jetbrains.php.lang.psi.resolve.types.PhpType)14 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)12 ProblemHighlightType (com.intellij.codeInspection.ProblemHighlightType)12