use of org.antlr.jetbrains.adaptor.psi.ScopeNode 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;
}
use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getAllLocalVariablesInResolvableScope.
/**
* Returns all local variables in provided scope and all parent contexts.
*
* @param scope
* @param caretOffset
* @return
*/
@NotNull
public static List<IdentifierPSINode> getAllLocalVariablesInResolvableScope(@NotNull ScopeNode scope, int caretOffset) {
List<IdentifierPSINode> results = new LinkedList<>();
if (scope instanceof VariableContainer || scope instanceof CodeBlockScope) {
results.addAll(getAllLocalVariablesInScope(scope, caretOffset));
ScopeNode context = scope.getContext();
if (context != null && !(scope instanceof RestrictedScope)) {
results.addAll(getAllLocalVariablesInResolvableScope(context, caretOffset));
}
} else if (scope instanceof ParameterContainer || scope instanceof LowerLevelDefinition) {
ScopeNode context = scope.getContext();
if (context != null) {
results.addAll(getAllLocalVariablesInResolvableScope(context, caretOffset));
}
}
return results;
}
use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method isValidVariable.
private static boolean isValidVariable(@NotNull VariableDefinitionNode variableDefinitionNode, @NotNull ScopeNode scope, int caretOffset) {
PsiElement elementAtCaret = scope.getContainingFile().findElementAt(caretOffset);
if (elementAtCaret == null) {
return false;
}
ScopeNode closestScope = PsiTreeUtil.getParentOfType(variableDefinitionNode, ScopeNode.class);
if (closestScope == null) {
return false;
}
if (!closestScope.equals(scope)) {
return false;
}
PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(elementAtCaret);
// statement, so there might be other elements before as well.
if (prevVisibleLeaf == null || ";".equals(prevVisibleLeaf.getText())) {
// If we are in a new statement,
PsiElement identifier = variableDefinitionNode.getNameIdentifier();
if (identifier != null && identifier.getTextOffset() < caretOffset) {
return true;
}
return false;
}
// Check previous elements.
PsiElement prevVisibleLeafParent = PsiTreeUtil.getParentOfType(prevVisibleLeaf, VariableDefinitionNode.class);
if (prevVisibleLeafParent != null) {
PsiElement identifier = variableDefinitionNode.getNameIdentifier();
if (identifier != null && identifier.getTextOffset() < caretOffset && !variableDefinitionNode.equals(prevVisibleLeafParent)) {
return true;
}
} else {
PsiElement identifier = variableDefinitionNode.getNameIdentifier();
if (identifier != null && identifier.getTextOffset() < caretOffset) {
return true;
}
}
return false;
}
use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getNamespaceDefinition.
@Nullable
public static PsiElement getNamespaceDefinition(@NotNull IdentifierPSINode identifier) {
ScopeNode scope = PsiTreeUtil.getParentOfType(identifier, CodeBlockScope.class, VariableContainer.class, TopLevelDefinition.class, LowerLevelDefinition.class);
if (scope != null) {
int caretOffset = identifier.getStartOffset();
List<PsiElement> namespaces = BallerinaPsiImplUtil.getAllXmlNamespacesInResolvableScope(scope, caretOffset);
for (PsiElement namespace : namespaces) {
if (namespace == null || namespace.getText().isEmpty()) {
continue;
}
if (namespace.getText().equals(identifier.getText())) {
return namespace;
}
}
}
return null;
}
use of org.antlr.jetbrains.adaptor.psi.ScopeNode in project ballerina by ballerina-lang.
the class NameSpaceReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
List<LookupElement> results = new LinkedList<>();
IdentifierPSINode identifier = getElement();
PsiElement parent = identifier.getParent();
PackageNameNode packageNameNode = PsiTreeUtil.getChildOfType(parent, PackageNameNode.class);
if (packageNameNode == null) {
ScopeNode scope = PsiTreeUtil.getParentOfType(identifier, CodeBlockScope.class, VariableContainer.class, TopLevelDefinition.class, LowerLevelDefinition.class);
if (scope != null) {
int caretOffset = identifier.getStartOffset();
List<PsiElement> namespaces = BallerinaPsiImplUtil.getAllXmlNamespacesInResolvableScope(scope, caretOffset);
results.addAll(BallerinaCompletionUtils.createNamespaceLookupElements(namespaces));
List<IdentifierPSINode> variables = BallerinaPsiImplUtil.getAllLocalVariablesInResolvableScope(scope, caretOffset);
results.addAll(BallerinaCompletionUtils.createVariableLookupElements(variables));
List<IdentifierPSINode> parameters = BallerinaPsiImplUtil.getAllParametersInResolvableScope(scope, caretOffset);
results.addAll(BallerinaCompletionUtils.createParameterLookupElements(parameters));
List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesInResolvableScope(scope);
results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsInResolvableScope(scope);
results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
}
}
return results.toArray(new LookupElement[results.size()]);
}
Aggregations