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