use of org.ballerinalang.plugins.idea.psi.ExpressionVariableDefinitionStatementNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getAllLocalVariablesInScope.
/**
* Returns all local variables in the provided scope.
*
* @param scope
* @param caretOffset
* @return
*/
@NotNull
public static List<IdentifierPSINode> getAllLocalVariablesInScope(@NotNull ScopeNode scope, int caretOffset) {
List<IdentifierPSINode> results = new LinkedList<>();
Collection<VariableDefinitionNode> variableDefinitionNodes = PsiTreeUtil.findChildrenOfType(scope, VariableDefinitionNode.class);
for (VariableDefinitionNode variableDefinitionNode : variableDefinitionNodes) {
if (isValidVariable(variableDefinitionNode, scope, caretOffset)) {
PsiElement identifier = variableDefinitionNode.getNameIdentifier();
if (identifier != null && identifier instanceof IdentifierPSINode) {
results.add(((IdentifierPSINode) identifier));
}
}
}
if (scope instanceof TransformerDefinitionNode) {
Collection<ExpressionVariableDefinitionStatementNode> nodes = PsiTreeUtil.findChildrenOfType(scope, ExpressionVariableDefinitionStatementNode.class);
for (ExpressionVariableDefinitionStatementNode node : nodes) {
ScopeNode closestScope = PsiTreeUtil.getParentOfType(node, TransformerDefinitionNode.class);
if (closestScope == null || !closestScope.equals(scope)) {
continue;
}
PsiElement identifier = node.getNameIdentifier();
if (identifier != null && identifier instanceof IdentifierPSINode) {
results.add(((IdentifierPSINode) identifier));
}
}
}
if (scope instanceof ForEachStatementNode) {
VariableReferenceListNode variableReferenceListNode = PsiTreeUtil.getChildOfType(scope, VariableReferenceListNode.class);
if (variableReferenceListNode != null) {
List<VariableReferenceNode> variableReferenceNodes = PsiTreeUtil.getChildrenOfTypeAsList(variableReferenceListNode, VariableReferenceNode.class);
for (VariableReferenceNode variableReferenceNode : variableReferenceNodes) {
if (variableReferenceNode != null) {
IdentifierPSINode identifier = PsiTreeUtil.findChildOfType(variableReferenceNode, IdentifierPSINode.class);
if (identifier != null) {
results.add(identifier);
}
}
}
}
}
Collection<AssignmentStatementNode> assignmentStatementNodes = PsiTreeUtil.findChildrenOfType(scope, AssignmentStatementNode.class);
for (AssignmentStatementNode assignmentStatementNode : assignmentStatementNodes) {
if (isValidVarAssignment(assignmentStatementNode, scope, caretOffset)) {
results.addAll(getVariablesFromVarAssignment(assignmentStatementNode));
}
}
return results;
}
Aggregations