use of org.ballerinalang.plugins.idea.psi.EnumFieldNode in project ballerina by ballerina-lang.
the class BallerinaParserDefinition method createElement.
@NotNull
public PsiElement createElement(ASTNode node) {
IElementType elementType = node.getElementType();
if (elementType instanceof TokenIElementType) {
return new ANTLRPsiNode(node);
}
if (!(elementType instanceof RuleIElementType)) {
return new ANTLRPsiNode(node);
}
RuleIElementType ruleElType = (RuleIElementType) elementType;
switch(ruleElType.getRuleIndex()) {
case BallerinaParser.RULE_functionDefinition:
return new FunctionDefinitionNode(node);
case BallerinaParser.RULE_callableUnitBody:
return new CallableUnitBodyNode(node);
case BallerinaParser.RULE_nameReference:
return new NameReferenceNode(node);
case BallerinaParser.RULE_variableReference:
return new VariableReferenceNode(node);
case BallerinaParser.RULE_variableDefinitionStatement:
return new VariableDefinitionNode(node);
case BallerinaParser.RULE_parameter:
return new ParameterNode(node);
case BallerinaParser.RULE_resourceDefinition:
return new ResourceDefinitionNode(node);
case BallerinaParser.RULE_packageName:
return new PackageNameNode(node);
case BallerinaParser.RULE_fullyQualifiedPackageName:
return new FullyQualifiedPackageNameNode(node);
case BallerinaParser.RULE_expressionList:
return new ExpressionListNode(node);
case BallerinaParser.RULE_expression:
return new ExpressionNode(node);
case BallerinaParser.RULE_functionInvocation:
return new FunctionInvocationNode(node);
case BallerinaParser.RULE_compilationUnit:
return new CompilationUnitNode(node);
case BallerinaParser.RULE_packageDeclaration:
return new PackageDeclarationNode(node);
case BallerinaParser.RULE_annotationAttachment:
return new AnnotationAttachmentNode(node);
case BallerinaParser.RULE_serviceBody:
return new ServiceBodyNode(node);
case BallerinaParser.RULE_importDeclaration:
return new ImportDeclarationNode(node);
case BallerinaParser.RULE_statement:
return new StatementNode(node);
case BallerinaParser.RULE_typeName:
return new TypeNameNode(node);
case BallerinaParser.RULE_constantDefinition:
return new ConstantDefinitionNode(node);
case BallerinaParser.RULE_structDefinition:
return new StructDefinitionNode(node);
case BallerinaParser.RULE_simpleLiteral:
return new SimpleLiteralNode(node);
case BallerinaParser.RULE_packageAlias:
return new AliasNode(node);
case BallerinaParser.RULE_parameterList:
return new ParameterListNode(node);
case BallerinaParser.RULE_fieldDefinition:
return new FieldDefinitionNode(node);
case BallerinaParser.RULE_parameterTypeNameList:
return new ParameterTypeNameList(node);
case BallerinaParser.RULE_parameterTypeName:
return new ConnectorInitNode(node);
case BallerinaParser.RULE_serviceDefinition:
return new ServiceDefinitionNode(node);
case BallerinaParser.RULE_valueTypeName:
return new ValueTypeNameNode(node);
case BallerinaParser.RULE_annotationDefinition:
return new AnnotationAttributeValueNode(node);
case BallerinaParser.RULE_structBody:
return new StructBodyNode(node);
case BallerinaParser.RULE_returnParameter:
return new ReturnParameterNode(node);
case BallerinaParser.RULE_attachmentPoint:
return new AttachmentPointNode(node);
case BallerinaParser.RULE_definition:
return new DefinitionNode(node);
case BallerinaParser.RULE_ifElseStatement:
return new IfElseStatementNode(node);
case BallerinaParser.RULE_assignmentStatement:
return new AssignmentStatementNode(node);
case BallerinaParser.RULE_variableReferenceList:
return new VariableReferenceListNode(node);
case BallerinaParser.RULE_recordLiteral:
return new RecordLiteralNode(node);
case BallerinaParser.RULE_globalVariableDefinition:
return new GlobalVariableDefinitionNode(node);
case BallerinaParser.RULE_recordKeyValue:
return new RecordKeyValueNode(node);
case BallerinaParser.RULE_forkJoinStatement:
return new ForkJoinStatementNode(node);
case BallerinaParser.RULE_returnStatement:
return new ReturnStatementNode(node);
case BallerinaParser.RULE_throwStatement:
return new ThrowStatementNode(node);
case BallerinaParser.RULE_transformerDefinition:
return new TransformerDefinitionNode(node);
case BallerinaParser.RULE_workerReply:
return new WorkerReplyNode(node);
case BallerinaParser.RULE_triggerWorker:
return new TriggerWorkerNode(node);
case BallerinaParser.RULE_workerDeclaration:
return new WorkerDeclarationNode(node);
case BallerinaParser.RULE_workerBody:
return new WorkerBodyNode(node);
case BallerinaParser.RULE_joinConditions:
return new JoinConditionNode(node);
case BallerinaParser.RULE_field:
return new FieldNode(node);
case BallerinaParser.RULE_recordKey:
return new RecordKeyNode(node);
case BallerinaParser.RULE_recordValue:
return new RecordValueNode(node);
case BallerinaParser.RULE_functionReference:
return new FunctionReferenceNode(node);
case BallerinaParser.RULE_codeBlockBody:
return new CodeBlockBodyNode(node);
case BallerinaParser.RULE_nonEmptyCodeBlockBody:
return new NonEmptyCodeBlockBodyNode(node);
case BallerinaParser.RULE_tryCatchStatement:
return new TryCatchStatementNode(node);
case BallerinaParser.RULE_catchClause:
return new CatchClauseNode(node);
case BallerinaParser.RULE_codeBlockParameter:
return new CodeBlockParameterNode(node);
case BallerinaParser.RULE_foreachStatement:
return new ForEachStatementNode(node);
case BallerinaParser.RULE_joinClause:
return new JoinClauseNode(node);
case BallerinaParser.RULE_timeoutClause:
return new TimeoutClauseNode(node);
case BallerinaParser.RULE_xmlAttrib:
return new XmlAttribNode(node);
case BallerinaParser.RULE_namespaceDeclaration:
return new NamespaceDeclarationNode(node);
case BallerinaParser.RULE_stringTemplateLiteral:
return new StringTemplateLiteralNode(node);
case BallerinaParser.RULE_stringTemplateContent:
return new StringTemplateContentNode(node);
case BallerinaParser.RULE_typeConversion:
return new TypeConversionNode(node);
case BallerinaParser.RULE_xmlContent:
return new XmlContentNode(node);
case BallerinaParser.RULE_invocation:
return new InvocationNode(node);
case BallerinaParser.RULE_enumDefinition:
return new EnumDefinitionNode(node);
case BallerinaParser.RULE_enumField:
return new EnumFieldNode(node);
case BallerinaParser.RULE_builtInReferenceTypeName:
return new BuiltInReferenceTypeNameNode(node);
case BallerinaParser.RULE_referenceTypeName:
return new ReferenceTypeNameNode(node);
case BallerinaParser.RULE_functionTypeName:
return new FunctionTypeNameNode(node);
case BallerinaParser.RULE_lambdaFunction:
return new LambdaFunctionNode(node);
case BallerinaParser.RULE_endpointDeclaration:
return new EndpointDeclarationNode(node);
case BallerinaParser.RULE_anonStructTypeName:
return new AnonStructTypeNameNode(node);
case BallerinaParser.RULE_userDefineTypeName:
return new UserDefinedTypeName(node);
case BallerinaParser.RULE_anyIdentifierName:
return new AnyIdentifierNameNode(node);
case BallerinaParser.RULE_documentationAttachment:
return new DocumentationAttachmentNode(node);
case BallerinaParser.RULE_documentationTemplateInlineCode:
return new DocumentationTemplateInlineCodeNode(node);
case BallerinaParser.RULE_singleBackTickDocInlineCode:
return new SingleBackTickDocInlineCodeNode(node);
case BallerinaParser.RULE_doubleBackTickDocInlineCode:
return new DoubleBackTickInlineCodeNode(node);
case BallerinaParser.RULE_tripleBackTickDocInlineCode:
return new TripleBackTickInlineCodeNode(node);
case BallerinaParser.RULE_deprecatedAttachment:
return new DeprecatedAttachmentNode(node);
case BallerinaParser.RULE_deprecatedText:
return new DeprecatedTextNode(node);
case BallerinaParser.RULE_singleBackTickDeprecatedInlineCode:
return new SingleBackTickDeprecatedInlineCodeNode(node);
case BallerinaParser.RULE_doubleBackTickDeprecatedInlineCode:
return new DoubleBackTickDeprecatedInlineCodeNode(node);
case BallerinaParser.RULE_tripleBackTickDeprecatedInlineCode:
return new TripleBackTickDeprecatedInlineCodeNode(node);
case BallerinaParser.RULE_documentationTemplateAttributeDescription:
return new DocumentationTemplateAttributeDescriptionNode(node);
case BallerinaParser.RULE_formalParameterList:
return new FormalParameterListNode(node);
case BallerinaParser.RULE_integerLiteral:
return new IntegerLiteralNode(node);
case BallerinaParser.RULE_matchPatternClause:
return new MatchPatternClauseNode(node);
default:
return new ANTLRPsiNode(node);
}
}
use of org.ballerinalang.plugins.idea.psi.EnumFieldNode in project ballerina by ballerina-lang.
the class BallerinaCompletionUtils method createEnumFieldLookupElements.
@NotNull
public static List<LookupElement> createEnumFieldLookupElements(@NotNull Collection<EnumFieldNode> enumFieldNodes, @NotNull IdentifierPSINode definitionName) {
List<LookupElement> lookupElements = new LinkedList<>();
for (EnumFieldNode fieldDefinitionNode : enumFieldNodes) {
IdentifierPSINode fieldName = PsiTreeUtil.getChildOfType(fieldDefinitionNode, IdentifierPSINode.class);
if (fieldName == null) {
continue;
}
LookupElement lookupElement = BallerinaCompletionUtils.createEnumFieldLookupElement(fieldName, definitionName);
lookupElements.add(lookupElement);
}
return lookupElements;
}
use of org.ballerinalang.plugins.idea.psi.EnumFieldNode in project ballerina by ballerina-lang.
the class RecordValueReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
List<LookupElement> results = new LinkedList<>();
IdentifierPSINode identifier = getElement();
PsiElement prevSibling = identifier.getPrevSibling();
if (prevSibling != null && prevSibling.getPrevSibling() != null && prevSibling.getPrevSibling() instanceof PackageNameNode) {
PsiReference reference = prevSibling.getPrevSibling().findReferenceAt(0);
if (reference != null) {
PsiElement resolvedElement = reference.resolve();
if (resolvedElement instanceof PsiDirectory) {
PsiDirectory currentPackage = (PsiDirectory) resolvedElement;
List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(currentPackage, true, true);
results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
List<IdentifierPSINode> enums = BallerinaPsiImplUtil.getAllEnumsFromPackage(currentPackage, true, true);
results.addAll(BallerinaCompletionUtils.createEnumLookupElements(enums, null));
List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesFromPackage(currentPackage, true, true);
results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsFromPackage(currentPackage, true, true);
results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
if (!results.isEmpty()) {
return results.toArray(new LookupElement[results.size()]);
}
}
}
}
PsiFile containingFile = identifier.getContainingFile();
PsiFile originalFile = containingFile.getOriginalFile();
List<LookupElement> packages = BallerinaPsiImplUtil.getPackagesAsLookups(originalFile, true, PackageCompletionInsertHandler.INSTANCE_WITH_AUTO_POPUP, true, AutoImportInsertHandler.INSTANCE_WITH_AUTO_POPUP);
results.addAll(packages);
// Todo - improve suggesting elements from a package in StructValueNode since it will not be identified properly
RecordKeyValueNode recordKeyValueNode = PsiTreeUtil.getParentOfType(identifier, RecordKeyValueNode.class);
if (recordKeyValueNode != null) {
RecordKeyNode mapStructKeyNode = PsiTreeUtil.getChildOfType(recordKeyValueNode, RecordKeyNode.class);
if (mapStructKeyNode != null) {
// Todo - use util?
List<PsiElement> importedPackages = BallerinaPsiImplUtil.getImportedPackages(containingFile);
for (PsiElement importedPackage : importedPackages) {
PsiReference reference = importedPackage.findReferenceAt(0);
if (reference == null) {
continue;
}
PsiElement resolvedElement = reference.resolve();
if (resolvedElement == null) {
continue;
}
PsiDirectory resolvedPackage = (PsiDirectory) resolvedElement;
if (mapStructKeyNode.getText().equals(resolvedPackage.getName())) {
List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(resolvedPackage, false, false);
results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
Collection<EnumFieldNode> enums = PsiTreeUtil.findChildrenOfType(resolvedPackage, EnumFieldNode.class);
results.addAll(BallerinaCompletionUtils.createEnumFieldLookupElements(enums, (IdentifierPSINode) resolvedElement));
List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesFromPackage(resolvedPackage, false, false);
results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsFromPackage(resolvedPackage, false, false);
results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
}
}
}
}
PsiDirectory currentPackage = originalFile.getParent();
if (currentPackage != null) {
List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(currentPackage, true, true);
results.addAll(BallerinaCompletionUtils.createFunctionLookupElements(functions));
List<IdentifierPSINode> enums = BallerinaPsiImplUtil.getAllEnumsFromPackage(currentPackage, true, true);
results.addAll(BallerinaCompletionUtils.createEnumLookupElements(enums, null));
List<IdentifierPSINode> globalVariables = BallerinaPsiImplUtil.getAllGlobalVariablesFromPackage(currentPackage, true, true);
results.addAll(BallerinaCompletionUtils.createGlobalVariableLookupElements(globalVariables));
List<IdentifierPSINode> constants = BallerinaPsiImplUtil.getAllConstantsFromPackage(currentPackage, true, true);
results.addAll(BallerinaCompletionUtils.createConstantLookupElements(constants));
}
// 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));
}
return results.toArray(new LookupElement[results.size()]);
}
use of org.ballerinalang.plugins.idea.psi.EnumFieldNode in project ballerina by ballerina-lang.
the class EnumFieldReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
List<LookupElement> results = new LinkedList<>();
IdentifierPSINode identifier = getElement();
PsiElement dot = PsiTreeUtil.prevVisibleLeaf(identifier);
if (dot == null || !".".equals(dot.getText())) {
return results.toArray(new LookupElement[results.size()]);
}
PsiElement enumReferenceNode = PsiTreeUtil.prevVisibleLeaf(dot);
if (enumReferenceNode == null) {
return results.toArray(new LookupElement[results.size()]);
}
PsiReference reference = enumReferenceNode.findReferenceAt(enumReferenceNode.getTextLength());
if (reference == null) {
return results.toArray(new LookupElement[results.size()]);
}
PsiElement resolvedElement = reference.resolve();
if (resolvedElement == null) {
return results.toArray(new LookupElement[results.size()]);
}
PsiElement resolvedElementParent = resolvedElement.getParent();
if (!(resolvedElementParent instanceof EnumDefinitionNode)) {
return results.toArray(new LookupElement[results.size()]);
}
Collection<EnumFieldNode> fieldDefinitionNodes = PsiTreeUtil.findChildrenOfType(resolvedElementParent, EnumFieldNode.class);
results.addAll(BallerinaCompletionUtils.createEnumFieldLookupElements(fieldDefinitionNodes, (IdentifierPSINode) resolvedElement));
return results.toArray(new LookupElement[results.size()]);
}
use of org.ballerinalang.plugins.idea.psi.EnumFieldNode 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