use of org.ballerinalang.plugins.idea.psi.FieldDefinitionNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getType.
/**
* Returns the type of the provided identifier.
*
* @param identifier an identifier
* @return the type of the identifier. This can be one of {@link ValueTypeNameNode} (for value types like string),
* {@link BuiltInReferenceTypeNameNode} (for reference types like json) or {@link TypeNameNode} (for arrays).
*/
@Nullable
public static PsiElement getType(@NotNull IdentifierPSINode identifier) {
PsiElement parent = identifier.getParent();
// In case of lambda functions or functions attached to types.
if (parent instanceof FunctionDefinitionNode) {
CodeBlockParameterNode codeBlockParameterNode = PsiTreeUtil.getChildOfType(parent, CodeBlockParameterNode.class);
if (codeBlockParameterNode != null) {
TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(codeBlockParameterNode, TypeNameNode.class);
if (typeNameNode != null) {
return typeNameNode.getFirstChild();
}
}
}
PsiReference reference = identifier.findReferenceAt(0);
if (reference != null) {
// Todo - Do we need to consider this situation?
}
VariableDefinitionNode variableDefinitionNode = PsiTreeUtil.getParentOfType(identifier, VariableDefinitionNode.class);
if (variableDefinitionNode != null) {
PsiElement typeNode = getType(variableDefinitionNode);
if (typeNode != null) {
return typeNode;
}
}
AssignmentStatementNode assignmentStatementNode = PsiTreeUtil.getParentOfType(identifier, AssignmentStatementNode.class);
if (assignmentStatementNode != null) {
PsiElement typeNode = getType(assignmentStatementNode, identifier);
if (typeNode != null) {
return typeNode;
}
}
ParameterNode parameterNode = PsiTreeUtil.getParentOfType(identifier, ParameterNode.class);
if (parameterNode != null) {
PsiElement typeNode = getType(parameterNode);
if (typeNode != null) {
return typeNode;
}
}
FieldDefinitionNode fieldDefinitionNode = PsiTreeUtil.getParentOfType(identifier, FieldDefinitionNode.class);
if (fieldDefinitionNode != null) {
PsiElement typeNode = getType(fieldDefinitionNode);
if (typeNode != null) {
return typeNode;
}
}
return null;
}
use of org.ballerinalang.plugins.idea.psi.FieldDefinitionNode in project ballerina by ballerina-lang.
the class AnnotationAttributeReference method resolve.
@Nullable
@Override
public PsiElement resolve() {
IdentifierPSINode identifier = getElement();
AnnotationAttachmentNode annotationAttachmentNode = PsiTreeUtil.getParentOfType(identifier, AnnotationAttachmentNode.class);
if (annotationAttachmentNode == null) {
return null;
}
AnnotationReferenceNode annotationReferenceNode = PsiTreeUtil.getChildOfType(annotationAttachmentNode, AnnotationReferenceNode.class);
if (annotationReferenceNode == null) {
return null;
}
int textLength = annotationReferenceNode.getTextLength();
PsiReference reference = annotationReferenceNode.findReferenceAt(textLength);
if (reference == null) {
return null;
}
PsiElement annotationName = reference.resolve();
if (annotationName == null || !(annotationName instanceof IdentifierPSINode)) {
return null;
}
PsiElement annotationDefinition = annotationName.getParent();
Collection<FieldDefinitionNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(annotationDefinition, FieldDefinitionNode.class);
return BallerinaPsiImplUtil.resolveReference(fieldDefinitionNodes, identifier);
}
use of org.ballerinalang.plugins.idea.psi.FieldDefinitionNode in project ballerina by ballerina-lang.
the class NameReference method getVariantsFromCurrentPackage.
@NotNull
private List<LookupElement> getVariantsFromCurrentPackage() {
List<LookupElement> results = new LinkedList<>();
IdentifierPSINode identifier = getElement();
PsiFile containingFile = identifier.getContainingFile();
PsiFile originalFile = containingFile.getOriginalFile();
PsiDirectory containingPackage = originalFile.getParent();
AnnotationAttachmentNode attachmentNode = PsiTreeUtil.getParentOfType(identifier, AnnotationAttachmentNode.class);
if (attachmentNode != null && containingFile instanceof BallerinaFile) {
ScopeNode scope = (BallerinaFile) containingFile;
List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsInResolvableScope(scope);
results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
} else if (containingPackage != null) {
List<LookupElement> packages = BallerinaPsiImplUtil.getPackagesAsLookups(originalFile, true, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP, true, AutoImportInsertHandler.INSTANCE_WITH_AUTO_POPUP);
results.addAll(packages);
PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(identifier);
ANTLRPsiNode definitionParent = PsiTreeUtil.getParentOfType(identifier, CallableUnitBodyNode.class, ServiceBodyNode.class, ResourceDefinitionNode.class, ConnectorBodyNode.class);
TypeNameNode typeNameNode = PsiTreeUtil.getParentOfType(identifier, TypeNameNode.class);
if ((definitionParent != null && !(definitionParent instanceof ResourceDefinitionNode)) || prevVisibleLeaf != null && (!";".equals(prevVisibleLeaf.getText()) && typeNameNode == null || prevVisibleLeaf.getText().matches("[{}]"))) {
List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(containingPackage, true, true);
results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
// Todo - use a util method
ScopeNode scope = PsiTreeUtil.getParentOfType(identifier, CodeBlockScope.class, VariableContainer.class, TopLevelDefinition.class, LowerLevelDefinition.class);
if (scope != null) {
int caretOffset = identifier.getStartOffset();
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));
List<PsiElement> namespaces = BallerinaPsiImplUtil.getAllXmlNamespacesInResolvableScope(scope, caretOffset);
results.addAll(BallerinaCompletionUtils.createNamespaceLookupElements(namespaces));
List<IdentifierPSINode> endpoints = BallerinaPsiImplUtil.getAllEndpointsInResolvableScope(scope, caretOffset);
results.addAll(BallerinaCompletionUtils.createEndpointLookupElements(endpoints));
} else {
ConstantDefinitionNode constantDefinitionNode = PsiTreeUtil.getParentOfType(identifier, ConstantDefinitionNode.class);
GlobalVariableDefinitionNode globalVariableDefinitionNode = PsiTreeUtil.getParentOfType(identifier, GlobalVariableDefinitionNode.class);
if (constantDefinitionNode != null || globalVariableDefinitionNode != null) {
scope = PsiTreeUtil.getParentOfType(constantDefinitionNode, BallerinaFile.class);
}
if (globalVariableDefinitionNode != null) {
scope = PsiTreeUtil.getParentOfType(globalVariableDefinitionNode, BallerinaFile.class);
}
if (scope != null) {
int caretOffset = identifier.getStartOffset();
List<IdentifierPSINode> globalVars = BallerinaPsiImplUtil.getAllGlobalVariablesInResolvableScope(scope, caretOffset);
results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVars));
List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsInResolvableScope(scope, caretOffset);
results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
}
}
}
List<IdentifierPSINode> connectors = BallerinaPsiImplUtil.getAllConnectorsFromPackage(containingPackage, true, true);
results.addAll(BallerinaCompletionUtils.createConnectorLookupElements(connectors, AddSpaceInsertHandler.INSTANCE));
List<IdentifierPSINode> structs = BallerinaPsiImplUtil.getAllStructsFromPackage(containingPackage, true, true);
results.addAll(BallerinaCompletionUtils.createStructLookupElements(structs));
List<IdentifierPSINode> enums = BallerinaPsiImplUtil.getAllEnumsFromPackage(containingPackage, true, true);
results.addAll(BallerinaCompletionUtils.createEnumLookupElements(enums, null));
return results;
}
// Try to get fields from an anonymous struct.
PsiElement structDefinitionNode = BallerinaPsiImplUtil.resolveAnonymousStruct(identifier);
if (structDefinitionNode == null || !(structDefinitionNode instanceof StructDefinitionNode)) {
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);
return results;
}
use of org.ballerinalang.plugins.idea.psi.FieldDefinitionNode 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.FieldDefinitionNode 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;
}
Aggregations