use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class RecordKeyReference method resolve.
@Nullable
@Override
public PsiElement resolve() {
IdentifierPSINode identifier = getElement();
VariableDefinitionNode variableDefinitionNode = PsiTreeUtil.getParentOfType(identifier, VariableDefinitionNode.class);
if (variableDefinitionNode != null) {
return resolve(variableDefinitionNode);
}
GlobalVariableDefinitionNode globalVariableDefinitionNode = PsiTreeUtil.getParentOfType(identifier, GlobalVariableDefinitionNode.class);
if (globalVariableDefinitionNode != null) {
return resolve(globalVariableDefinitionNode);
}
AssignmentStatementNode assignmentStatementNode = PsiTreeUtil.getParentOfType(identifier, AssignmentStatementNode.class);
if (assignmentStatementNode != null) {
PsiElement resolvedElement = resolve(assignmentStatementNode);
if (resolvedElement != null) {
return resolvedElement;
}
}
// Try to resolve to fields in anonymous struct.
PsiElement definitionNode = BallerinaPsiImplUtil.resolveAnonymousStruct(identifier);
if (definitionNode == null) {
return null;
}
if (definitionNode instanceof AnonStructTypeNameNode) {
StructBodyNode structBodyNode = PsiTreeUtil.findChildOfType(definitionNode, StructBodyNode.class);
if (structBodyNode == null) {
return null;
}
List<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.getChildrenOfTypeAsList(structBodyNode, FieldDefinitionNode.class);
for (FieldDefinitionNode fieldDefinitionNode : fieldDefinitionNodes) {
if (fieldDefinitionNode == null) {
continue;
}
IdentifierPSINode fieldName = PsiTreeUtil.getChildOfType(fieldDefinitionNode, IdentifierPSINode.class);
if (fieldName == null) {
continue;
}
if (fieldName.getText().equals(identifier.getText())) {
return fieldName;
}
}
}
if (!(definitionNode instanceof StructDefinitionNode)) {
return null;
}
StructDefinitionNode structDefinitionNode = ((StructDefinitionNode) definitionNode);
IdentifierPSINode structNameNode = PsiTreeUtil.getChildOfType(structDefinitionNode, IdentifierPSINode.class);
if (structNameNode == null) {
return null;
}
Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structDefinitionNode, FieldDefinitionNode.class);
for (FieldDefinitionNode fieldDefinitionNode : fieldDefinitionNodes) {
IdentifierPSINode fieldName = PsiTreeUtil.getChildOfType(fieldDefinitionNode, IdentifierPSINode.class);
if (fieldName != null && identifier.getText().equals(fieldName.getText())) {
return fieldName;
}
}
return null;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class RecordKeyReference method getVariantsFromCurrentPackage.
@NotNull
private List<LookupElement> getVariantsFromCurrentPackage() {
List<LookupElement> results = new LinkedList<>();
IdentifierPSINode identifier = getElement();
VariableDefinitionNode variableDefinitionNode = PsiTreeUtil.getParentOfType(identifier, VariableDefinitionNode.class);
InvocationNode invocationNode = PsiTreeUtil.getParentOfType(identifier, InvocationNode.class);
if (variableDefinitionNode == null || invocationNode != null) {
StructDefinitionNode structDefinitionNode = resolveStructDefinition(identifier);
if (structDefinitionNode == null) {
// Todo - Check for enclosing {} since the parse errors might cause issues when identifying
// RecordLiteralNode element
// RecordLiteralNode mapStructLiteralNode = PsiTreeUtil.getParentOfType(identifier,
// RecordLiteralNode.class);
// if (mapStructLiteralNode == null) {
// return results;
// }
// Try to get fields from an anonymous struct.
PsiElement definitionNode = BallerinaPsiImplUtil.resolveAnonymousStruct(identifier);
if (definitionNode == null) {
return results;
}
if (definitionNode instanceof AnonStructTypeNameNode) {
StructBodyNode structBodyNode = PsiTreeUtil.findChildOfType(definitionNode, StructBodyNode.class);
if (structBodyNode == null) {
return results;
}
List<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.getChildrenOfTypeAsList(structBodyNode, FieldDefinitionNode.class);
results = BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP);
return results;
}
structDefinitionNode = ((StructDefinitionNode) definitionNode);
IdentifierPSINode structNameNode = PsiTreeUtil.getChildOfType(structDefinitionNode, IdentifierPSINode.class);
if (structNameNode == null) {
return results;
}
Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structDefinitionNode, FieldDefinitionNode.class);
results = BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, structNameNode, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP);
return results;
}
IdentifierPSINode structNameNode = PsiTreeUtil.getChildOfType(structDefinitionNode, IdentifierPSINode.class);
if (structNameNode == null) {
return results;
}
Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structDefinitionNode, FieldDefinitionNode.class);
results = BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, structNameNode, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP);
} else {
TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(variableDefinitionNode, TypeNameNode.class);
if (typeNameNode == null) {
return results;
} else {
PsiReference reference = typeNameNode.findReferenceAt(typeNameNode.getTextLength());
if (reference == null) {
return results;
}
PsiElement resolvedElement = reference.resolve();
if (resolvedElement == null) {
return results;
}
PsiElement resolvedElementParent = resolvedElement.getParent();
if (!(resolvedElementParent instanceof StructDefinitionNode)) {
return results;
}
StructBodyNode structBodyNode = PsiTreeUtil.getChildOfType(resolvedElementParent, StructBodyNode.class);
if (structBodyNode == null) {
return results;
}
Collection<FieldDefinitionNode> fieldDefinitionNodes;
if (isInSamePackage(identifier, resolvedElement)) {
fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(structBodyNode, FieldDefinitionNode.class);
} else {
fieldDefinitionNodes = PsiTreeUtil.getChildrenOfTypeAsList(structBodyNode, FieldDefinitionNode.class);
}
results = BallerinaCompletionUtils.createFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) resolvedElement, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP);
}
}
return results;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class StatementReference method getVariantsFromPackage.
@NotNull
private List<LookupElement> getVariantsFromPackage(@NotNull PsiElement packageNameNode) {
List<LookupElement> results = new LinkedList<>();
PsiElement resolvedElement = BallerinaPsiImplUtil.resolvePackage(packageNameNode);
if (resolvedElement == null || !(resolvedElement instanceof PsiDirectory)) {
return results;
}
PsiDirectory containingPackage = (PsiDirectory) resolvedElement;
// Todo - add util method
List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(containingPackage, false, false);
functions = functions.stream().filter(function -> !BallerinaPsiImplUtil.isAttachedFunction(function)).collect(Collectors.toList());
results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
List<IdentifierPSINode> connectors = BallerinaPsiImplUtil.getAllConnectorsFromPackage(containingPackage, false, false);
results.addAll(BallerinaCompletionUtils.createConnectorLookupElements(connectors, AddSpaceInsertHandler.INSTANCE));
List<IdentifierPSINode> structs = BallerinaPsiImplUtil.getAllStructsFromPackage(containingPackage, false, false);
results.addAll(BallerinaCompletionUtils.createStructLookupElements(structs));
List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesFromPackage(containingPackage, false, false);
results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsFromPackage(containingPackage, false, false);
results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
List<IdentifierPSINode> enums = BallerinaPsiImplUtil.getAllEnumsFromPackage(containingPackage, false, false);
results.addAll(BallerinaCompletionUtils.createEnumLookupElements(enums, AddSpaceInsertHandler.INSTANCE));
return results;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class StructReference method resolve.
@Nullable
@Override
public PsiElement resolve() {
IdentifierPSINode identifier = getElement();
PsiElement parent = identifier.getParent();
PackageNameNode packageNameNode = PsiTreeUtil.getChildOfType(parent, PackageNameNode.class);
if (packageNameNode == null) {
return resolveInCurrentPackage();
} else {
return resolveInPackage(packageNameNode);
}
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class StructReference 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) {
results.addAll(getVariantsFromCurrentPackage());
} else {
results.addAll(getVariantsFromPackage(packageNameNode));
}
return results.toArray(new LookupElement[results.size()]);
}
Aggregations