use of org.ballerinalang.plugins.idea.psi.VariableReferenceNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getVariablesFromVarAssignment.
@NotNull
public static List<IdentifierPSINode> getVariablesFromVarAssignment(@NotNull AssignmentStatementNode assignmentStatementNode) {
List<IdentifierPSINode> results = new LinkedList<>();
VariableReferenceListNode variableReferenceListNode = PsiTreeUtil.getChildOfType(assignmentStatementNode, VariableReferenceListNode.class);
if (variableReferenceListNode == null) {
return results;
}
VariableReferenceNode[] variableReferenceNodes = PsiTreeUtil.getChildrenOfType(variableReferenceListNode, VariableReferenceNode.class);
if (variableReferenceNodes == null) {
return results;
}
for (VariableReferenceNode variableReferenceNode : variableReferenceNodes) {
IdentifierPSINode identifier = PsiTreeUtil.findChildOfType(variableReferenceNode, IdentifierPSINode.class);
if (identifier != null && !"_".equals(identifier.getText())) {
results.add(identifier);
}
}
return results;
}
use of org.ballerinalang.plugins.idea.psi.VariableReferenceNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getStructDefinition.
/**
* Checks the RHS of the provided {@link AssignmentStatementNode} and returns the corresponding
* {@link StructDefinitionNode}.
*
* @param assignmentStatementNode an assignment statement node
* @param structReferenceNode an identifier which is a var variable in the assignment node
* @return {@code null} if cannot suggest a {@link StructDefinitionNode}, otherwise returns the corresponding
* {@link StructDefinitionNode}.
*/
@Nullable
public static StructDefinitionNode getStructDefinition(@NotNull AssignmentStatementNode assignmentStatementNode, @NotNull IdentifierPSINode structReferenceNode) {
if (!BallerinaPsiImplUtil.isVarAssignmentStatement(assignmentStatementNode)) {
return null;
}
ExpressionNode expressionNode = PsiTreeUtil.getChildOfType(assignmentStatementNode, ExpressionNode.class);
if (expressionNode == null) {
return null;
}
PsiElement expressionNodeFirstChild = expressionNode.getFirstChild();
if (expressionNodeFirstChild instanceof VariableReferenceNode) {
return getStructDefinition((VariableReferenceNode) expressionNodeFirstChild, assignmentStatementNode, structReferenceNode);
} else if (expressionNodeFirstChild instanceof TypeCastNode) {
int index = getVariableIndexFromVarAssignment(assignmentStatementNode, structReferenceNode);
if (index == 0) {
return resolveTypeNodeStruct((expressionNodeFirstChild));
} else if (index == 1) {
return getErrorStruct(assignmentStatementNode, structReferenceNode, true, false);
}
} else if (expressionNodeFirstChild instanceof TypeConversionNode) {
int index = getVariableIndexFromVarAssignment(assignmentStatementNode, structReferenceNode);
if (index == 0) {
return resolveTypeNodeStruct((expressionNodeFirstChild));
} else if (index == 1) {
return getErrorStruct(assignmentStatementNode, structReferenceNode, false, true);
}
}
return null;
}
use of org.ballerinalang.plugins.idea.psi.VariableReferenceNode in project ballerina by ballerina-lang.
the class FieldReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
IdentifierPSINode identifier = getElement();
PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(identifier);
// Todo - remove hard coded "."
if (prevVisibleLeaf == null || !".".equals(prevVisibleLeaf.getText())) {
return new LookupElement[0];
}
PsiElement previousField = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
if (previousField == null) {
return new LookupElement[0];
}
PsiReference reference = previousField.findReferenceAt(0);
if (reference == null) {
PsiElement prevSibling = identifier.getParent().getPrevSibling();
if (prevSibling == null) {
return new LookupElement[0];
}
if (prevSibling instanceof VariableReferenceNode) {
PsiElement[] children = prevSibling.getChildren();
if (children.length <= 0) {
return new LookupElement[0];
}
PsiElement firstChild = children[0].getFirstChild();
if (firstChild == null) {
return new LookupElement[0];
}
PsiReference functionReference = firstChild.findReferenceAt(firstChild.getTextLength());
if (functionReference == null) {
return new LookupElement[0];
}
PsiElement resolvedElement = functionReference.resolve();
if (resolvedElement == null) {
return new LookupElement[0];
}
PsiElement parent = resolvedElement.getParent();
if (parent instanceof FunctionDefinitionNode) {
List<TypeNameNode> returnTypes = BallerinaPsiImplUtil.getReturnTypes(((FunctionDefinitionNode) parent));
if (returnTypes.size() == 1) {
TypeNameNode typeNameNode = returnTypes.get(0);
List<LookupElement> functions = Arrays.asList((LookupElement[]) TypeReference.getVariants(typeNameNode));
return functions.toArray(new LookupElement[functions.size()]);
}
}
}
}
if (reference == null) {
return new LookupElement[0];
}
// Todo - use util method
PsiElement resolvedElement = reference.resolve();
if (resolvedElement == null || !(resolvedElement instanceof IdentifierPSINode)) {
return new LookupElement[0];
}
PsiElement resolvedElementParent = resolvedElement.getParent();
StructDefinitionNode structDefinitionNode = null;
List<LookupElement> results = new LinkedList<>();
// Resolve the corresponding resolvedElementParent to get the struct definition.
if (resolvedElementParent instanceof VariableDefinitionNode || resolvedElementParent instanceof CodeBlockParameterNode || resolvedElementParent instanceof ParameterNode) {
structDefinitionNode = BallerinaPsiImplUtil.resolveStructFromDefinitionNode(resolvedElementParent);
} else if (resolvedElementParent instanceof FieldDefinitionNode) {
structDefinitionNode = BallerinaPsiImplUtil.resolveTypeNodeStruct((resolvedElementParent));
} else if (resolvedElementParent instanceof NameReferenceNode) {
AssignmentStatementNode assignmentStatementNode = PsiTreeUtil.getParentOfType(resolvedElement, AssignmentStatementNode.class);
if (assignmentStatementNode != null) {
structDefinitionNode = BallerinaPsiImplUtil.getStructDefinition(assignmentStatementNode, ((IdentifierPSINode) resolvedElement));
} else {
structDefinitionNode = BallerinaPsiImplUtil.findStructDefinition((IdentifierPSINode) resolvedElement);
}
if (structDefinitionNode != null) {
IdentifierPSINode structName = PsiTreeUtil.findChildOfType(structDefinitionNode, IdentifierPSINode.class);
if (structName != null) {
resolvedElement = structName;
}
}
} else if (resolvedElementParent instanceof EnumDefinitionNode) {
Collection<EnumFieldNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(resolvedElementParent, EnumFieldNode.class);
results.addAll(BallerinaCompletionUtils.createEnumFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) resolvedElement));
return results.toArray(new LookupElement[results.size()]);
} else if (resolvedElementParent instanceof StructDefinitionNode) {
structDefinitionNode = ((StructDefinitionNode) resolvedElementParent);
}
if (structDefinitionNode == null) {
return new LookupElement[0];
}
Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structDefinitionNode, FieldDefinitionNode.class);
results.addAll(BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) resolvedElement, null));
List<IdentifierPSINode> attachedFunctions = BallerinaPsiImplUtil.getAttachedFunctions(structDefinitionNode);
results.addAll(BallerinaCompletionUtils.createAttachedFunctionsLookupElements(attachedFunctions));
return results.toArray(new LookupElement[results.size()]);
}
Aggregations