How to list all the variables in scope at a particular PSIStatement?
Answered
Hello,
I'd like to use PSI for Java to get a list of all the variables that are currently in scope at any given PsiStatement.
Example:
private static boolean isVariableDeclaredInsideBlock(PsiCodeBlock codeBlock, PsiVariable inputVar) {
Collection<PsiVariable> blockInnerVariables = PsiTreeUtil.collectElementsOfType(codeBlock, PsiVariable.class);
for (PsiVariable var : blockInnerVariables) {
if (var.equals(inputVar))
return true; //find what is in scope at this point? parameters+blockInnerVariables+var
}
int k = 10;
return false;
}
Any guidance would be greatly appreciated :)
Please sign in to leave a comment.
See PsiScopesUtil#treeWalkUp (com.intellij.codeInsight.completion.BasicExpressionCompletionContributor#processDataflowExpressionTypes as sample). Other useful API: com.intellij.psi.PsiResolveHelper#resolveReferencedVariable, com.siyeh.ig.psiutils.VariableAccessUtils