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);
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 : ReferencesSearch.search(element, searchScope)) {
PsiElement referenceElement = reference.getElement();
if(!isParentFor(element, referenceElement)) {
return;
}
}
toDelete.add(element);
}
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:
I've tried to extend that Inspection to log every item its builder visit:
But nothing is visited.
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?
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:)
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:
you may want to see how com.intellij.codeInspection.actions.CleanupInspectionIntention (Fix all problems) does the same task.
Anna