Find usages of member variables
I am a total plugin dev newb so have mercy on me...I am wanting to find where member variables are used in a file. I am guessing each usage has its own PsiElement?
I have been able to list all the member variables in a class (and I am pretty happy I got this far). However, how do I go about finding everywhere they are used in a file? (I eventually want to prepend some text to them) A nudge in the right direction would be appreciated.
My current code:
final Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
if (editor != null) {
final PsiJavaFile javaFile = (PsiJavaFile) PsiUtil.getPsiFileInEditor(editor, project);
if (javaFile != null) {
final PsiElement element = javaFile.findElementAt(editor.getCaretModel().getOffset());
PsiClass currentClass = PsiTreeUtil.getParentOfType(element, PsiClass.class);
if (currentClass != null) {
final PsiField[] allFields = currentClass.getAllFields();
for (PsiField field : allFields) {
logger.info("Field: " + field.getText());
}
}
}
}
Please sign in to leave a comment.
Hello Michael,
ReferencesSearch.search(psiElement).findAll()
For each of the returned PsiReference objects, you can call getElement()
to get the corresponding element.
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Thanks! That is exactly what I was looking for.
When I was trying to figure it out on my own I came up with this code, which really helped me visualize the document tree and see how it works, maybe it will help someone else:
Hello Michael,
There's a plugin that does that for you with a much nicer UI :)
http://plugins.intellij.net/plugin/?id=227
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
Thanks for the heads-up about the PsiViewer plugin. Very nice!