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