use of org.ballerinalang.plugins.idea.psi.BallerinaFile in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getAllGlobalVariablesInScope.
@NotNull
public static List<IdentifierPSINode> getAllGlobalVariablesInScope(@NotNull ScopeNode scope, int caretOffset) {
List<IdentifierPSINode> results = new LinkedList<>();
Collection<GlobalVariableDefinitionNode> variableDefinitionNodes = PsiTreeUtil.findChildrenOfType(scope, GlobalVariableDefinitionNode.class);
for (GlobalVariableDefinitionNode variableDefinitionNode : variableDefinitionNodes) {
PsiElement identifier = variableDefinitionNode.getNameIdentifier();
if (caretOffset != -1) {
if (identifier == null || !(identifier instanceof IdentifierPSINode) || identifier.getTextOffset() > caretOffset) {
continue;
}
PsiElement element = scope.findElementAt(caretOffset);
if (element != null) {
GlobalVariableDefinitionNode definition = PsiTreeUtil.getParentOfType(element, GlobalVariableDefinitionNode.class);
if (definition != null && definition.equals(variableDefinitionNode)) {
continue;
}
}
}
results.add(((IdentifierPSINode) identifier));
}
PsiFile originalFile = ((BallerinaFile) scope).getOriginalFile();
PsiDirectory containingPackage = originalFile.getParent();
if (containingPackage == null) {
return results;
}
PsiFile[] files = containingPackage.getFiles();
for (PsiFile file : files) {
// Do't check the current file again.
if (file.equals(originalFile)) {
continue;
}
variableDefinitionNodes = PsiTreeUtil.findChildrenOfType(file, GlobalVariableDefinitionNode.class);
for (GlobalVariableDefinitionNode variableDefinitionNode : variableDefinitionNodes) {
PsiElement identifier = variableDefinitionNode.getNameIdentifier();
if (identifier != null && identifier instanceof IdentifierPSINode) {
results.add(((IdentifierPSINode) identifier));
}
}
}
return results;
}
Aggregations