How do I run a particular inspection?

I want to run a particular inspection and apply a quick fix from it. It shouldn't ask anything from user.

How do I do that?

Comment actions Permalink

I am trying to use OCUnusedGlobalDeclarationInspection

I've tried to use checkFile(PsiFile, InspectionManager, boolean isOnTheFly) but that method is not overriden from LocalInspectionTool and always return null.
I see that the only public method that is declared in this particular Inspection class is buildVisitor, but I can't really understand how to use returned result.

Then I found method `processFile` and it seems to be the thing I need, but it doesn't found anything despite that when I run inspection from the IDE it found that exact problem.
My current code:

OCInspections.UnusedImportStatement inspection = new OCInspections.UnusedImportStatement();
InspectionManager manager = InspectionManager.getInstance(project);

List<ProblemDescriptor> problems = inspection.processFile(psiOutputFile, manager);
System.err.println(problems.size()); //prints 0

I've tried to extend that Inspection to log every item its builder visit:

OCInspections.UnusedImportStatement inspection = new OCInspections.UnusedImportStatement() {      @Nullable      @Override      public UnusedVisitor buildVisitor() {           @NotNull final UnusedVisitor unusedVisitor = super.buildVisitor();           return new UnusedVisitor(){                @Override                public void visitOCElement(OCElement elem) {                     System.err.println("visitOCElement$");                     unusedVisitor.visitOCElement(elem);                }                @Override                public void visitOCFile(OCFile file) {                     System.err.println("visitOCFile$");                     unusedVisitor.visitOCFile(file);                }
//all others visit methods in the same way.

But nothing is visited.

Comment actions Permalink

Many times the inspections rely on other annotators to run first. It is hard to say since the appCode source isn't available.

Are you trying to run the inspection on a file that is open in the editor?

Comment actions Permalink

No, it could be not open.

Actually I'll found the solution for my particular case(I'll add it a bit later) but If one have a good solution I'll glad to see that:)

Comment actions Permalink

As I said, I'll post a solution.

So what I basicly do: check all references(thats tool for that) and if some reference is not child of us, then we are not unused.

So, essentially you run some Visitor(you may run recursive one, I use a simple one, that for some element runs recoursively[it depends on what you're searching for]) and calls function like that:

     private void removeIfNoReference(OCElement element) { //Could be PsiElement, bit I need more specific one to traverse the tree.           for (PsiReference reference :, searchScope)) {                PsiElement referenceElement = reference.getElement();                if(!isParentFor(element, referenceElement)) {                     return;                }           }           toDelete.add(element);      }

     private static boolean isParentFor(OCElement potentialParent, PsiElement potentialChild) {           while (potentialChild != null) {                //noinspection ObjectEquality                if(potentialChild == potentialParent) {                     return true;                }                potentialChild = potentialChild.getParent();           }           return false;      }

Comment actions Permalink

you may want to see how com.intellij.codeInspection.actions.CleanupInspectionIntention (Fix all problems) does the same task.



Please sign in to leave a comment.