use of org.ballerinalang.plugins.idea.psi.ResourceDefinitionNode 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.ResourceDefinitionNode in project ballerina by ballerina-lang.
the class DocVariableReference method getVariants.
@NotNull
@Override
public Object[] getVariants() {
List<LookupElement> results = new LinkedList<>();
IdentifierPSINode identifier = getElement();
ResourceDefinitionNode resourceDefinitionNode = PsiTreeUtil.getParentOfType(identifier, ResourceDefinitionNode.class);
if (resourceDefinitionNode != null) {
List<IdentifierPSINode> parameters = BallerinaPsiImplUtil.getParameters(resourceDefinitionNode);
results.addAll(BallerinaCompletionUtils.createParameterLookupElements(parameters));
} else {
DocumentationAttachmentNode documentationAttachmentNode = PsiTreeUtil.getParentOfType(identifier, DocumentationAttachmentNode.class);
if (documentationAttachmentNode == null) {
return results.toArray(new LookupElement[results.size()]);
}
PsiElement nextSibling = documentationAttachmentNode.getNextSibling();
if (nextSibling == null) {
return results.toArray(new LookupElement[results.size()]);
}
}
return results.toArray(new LookupElement[results.size()]);
}
use of org.ballerinalang.plugins.idea.psi.ResourceDefinitionNode in project ballerina by ballerina-lang.
the class BallerinaStructureViewElement method getChildren.
@NotNull
@Override
public TreeElement[] getChildren() {
// The element can be one of BallerinaFile, ConnectorDefinitionNode instance.
if (element instanceof BallerinaFile) {
List<TreeElement> treeElements = new ArrayList<>();
// Add services.
Collection<ServiceDefinitionNode> services = PsiTreeUtil.findChildrenOfType(element, ServiceDefinitionNode.class);
for (PsiElement service : services) {
// In here, instead of using the service, we use service.getParent(). This is done because we
// want to show resources under a service node. This is how the sub nodes can be added.
treeElements.add(new BallerinaStructureViewElement(service));
}
// Add functions.
Collection<FunctionDefinitionNode> functions = PsiTreeUtil.findChildrenOfType(element, FunctionDefinitionNode.class);
for (PsiElement function : functions) {
treeElements.add(new BallerinaStructureViewElement(function));
}
// Add connectors.
Collection<ConnectorDefinitionNode> connectors = PsiTreeUtil.findChildrenOfType(element, ConnectorDefinitionNode.class);
for (PsiElement connector : connectors) {
// In here, instead of using the connector, we use connector.getParent(). This is done because we
// want to show actions under a connector node. This is how the sub nodes can be added.
treeElements.add(new BallerinaStructureViewElement(connector));
}
// Add annotations.
Collection<AnnotationDefinitionNode> annotations = PsiTreeUtil.findChildrenOfType(element, AnnotationDefinitionNode.class);
for (PsiElement annotation : annotations) {
treeElements.add(new BallerinaStructureViewElement(annotation));
}
// Add structs
Collection<StructDefinitionNode> structs = PsiTreeUtil.findChildrenOfType(element, StructDefinitionNode.class);
for (PsiElement struct : structs) {
treeElements.add(new BallerinaStructureViewElement(struct));
}
// Convert the list to an array and return.
return treeElements.toArray(new TreeElement[treeElements.size()]);
} else if (element instanceof ConnectorDefinitionNode) {
// If the element is a ConnectorDefinitionNode instance, we get all actions.
List<TreeElement> treeElements = new ArrayList<>();
// Add actions.
Collection<ActionDefinitionNode> actions = PsiTreeUtil.findChildrenOfType(element, ActionDefinitionNode.class);
for (PsiElement action : actions) {
treeElements.add(new BallerinaStructureViewElement(action));
}
// Convert the list to an array and return.
return treeElements.toArray(new TreeElement[treeElements.size()]);
} else if (element instanceof ServiceDefinitionNode) {
// If the element is a ServiceDefinitionNode instance, we get all resources.
List<TreeElement> treeElements = new ArrayList<>();
// Add resources.
Collection<ResourceDefinitionNode> resources = PsiTreeUtil.findChildrenOfType(element, ResourceDefinitionNode.class);
for (PsiElement resource : resources) {
treeElements.add(new BallerinaStructureViewElement(resource));
}
// Convert the list to an array and return.
return treeElements.toArray(new TreeElement[treeElements.size()]);
} else if (element instanceof FunctionDefinitionNode || element instanceof ResourceDefinitionNode || element instanceof ActionDefinitionNode) {
// If the element is a FunctionDefinitionNode instance, we get all workers.
List<TreeElement> treeElements = new ArrayList<>();
// Add workers.
Collection<WorkerDeclarationNode> workers = PsiTreeUtil.findChildrenOfType(element, WorkerDeclarationNode.class);
for (PsiElement worker : workers) {
treeElements.add(new BallerinaStructureViewElement(worker));
}
// Convert the list to an array and return.
return treeElements.toArray(new TreeElement[treeElements.size()]);
}
// If the element type other than what we check above, return an empty array.
return new TreeElement[0];
}
use of org.ballerinalang.plugins.idea.psi.ResourceDefinitionNode 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.ResourceDefinitionNode in project ballerina by ballerina-lang.
the class BallerinaAnnotator method isMatchingParamAvailable.
private boolean isMatchingParamAvailable(@NotNull AnnotationAttributeNode annotationAttributeNode, @NotNull String value) {
ResourceDefinitionNode resourceDefinitionNode = PsiTreeUtil.getParentOfType(annotationAttributeNode, ResourceDefinitionNode.class);
if (resourceDefinitionNode == null) {
return false;
}
ParameterListNode parameterListNode = PsiTreeUtil.getChildOfType(resourceDefinitionNode, ParameterListNode.class);
if (parameterListNode == null) {
return false;
}
ParameterNode[] parameterNodes = PsiTreeUtil.getChildrenOfType(parameterListNode, ParameterNode.class);
if (parameterNodes == null) {
return false;
}
for (ParameterNode parameterNode : parameterNodes) {
AnnotationAttachmentNode annotationAttachmentNode = PsiTreeUtil.getChildOfType(parameterNode, AnnotationAttachmentNode.class);
if (annotationAttachmentNode == null) {
continue;
}
AnnotationReferenceNode annotationReferenceNode = PsiTreeUtil.getChildOfType(annotationAttachmentNode, AnnotationReferenceNode.class);
if (annotationReferenceNode == null) {
continue;
}
PsiElement paramType = annotationReferenceNode.getNameIdentifier();
if (paramType == null) {
continue;
}
if (!"PathParam".equals(paramType.getText())) {
continue;
}
Collection<AnnotationAttributeValueNode> annotationAttributeValueNodes = PsiTreeUtil.findChildrenOfType(annotationAttachmentNode, AnnotationAttributeValueNode.class);
for (AnnotationAttributeValueNode annotationAttributeValueNode : annotationAttributeValueNodes) {
SimpleLiteralNode simpleLiteralNode = PsiTreeUtil.getChildOfType(annotationAttributeValueNode, SimpleLiteralNode.class);
if (simpleLiteralNode == null || simpleLiteralNode.getFirstChild() == null) {
continue;
}
PsiElement firstChild = simpleLiteralNode.getFirstChild();
if (!(firstChild instanceof LeafPsiElement)) {
continue;
}
if (((LeafPsiElement) firstChild).getElementType() != BallerinaTypes.QUOTED_STRING) {
continue;
}
String text = firstChild.getText();
text = text.substring(1, text.length() - 1);
if (value.equals(text)) {
return true;
}
}
}
// does not have an annotation attachments.
for (ParameterNode parameterNode : parameterNodes) {
AnnotationAttachmentNode annotationAttachmentNode = PsiTreeUtil.getChildOfType(parameterNode, AnnotationAttachmentNode.class);
if (annotationAttachmentNode != null) {
continue;
}
PsiElement nameIdentifier = parameterNode.getNameIdentifier();
if (nameIdentifier == null) {
continue;
}
if (value.equals(nameIdentifier.getText())) {
return true;
}
}
return false;
}
Aggregations