Author - Dmitrii Abramov.
This plugin shows AST of selected code fragment and following statistics:
- Number of variable declarations inside selection
- Number of read variable accesses in selection
- Number of exceptions which may be thrown during execution of selected fragment
The easiest way to run the plugin is:
./gradlew runIde
To display AST and info for selection:
- Select some code in editor.
- Press
AST Info
button underAST Info
menu-bar entry. - Tool Window with AST and statistics will appear.
- To see info for another code fragment: GOTO 1.
Plugin uses ToolWindow
from Intellij API to present it's UI.
Internal Intellij AST representation - PSI (Program Structure Interface) is used to present tree structure in plugin.
Algorithm:
- Retrieve nodes of AST which correspond to begin and end of the selection.
- Find lowest common
ancestor
forbegin
andend
node. - If all
ancestor
children intersect with selection, return the subtree with root inancestor
. - Otherwise the list of subtrees with roots in each
ancestor
child which intersects with the selection is returned.
Considering variable as local variable or field.
Visiting all nodes in AST corresponding to selection and counting all variable and field declaration nodes.
Visiting all nodes in AST corresponding to selection and counting all Reference Expression nodes which are read access (i.e. this reference is not on the left of assignment operator).
Here I reused functionality of Intellij API (method com.intellij.codeInsight.ExceptionUtil::getThrownExceptions
).
It traverses AST and processes all function calls and gets all exceptions from their function declaration throws
list.
Also it processes all throw
expressions and try
statements (for try
statement it correctly doesn't count exceptions which are catch by catch
close).