use of org.ballerinalang.plugins.idea.psi.ExpressionListNode 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.ExpressionListNode in project ballerina by ballerina-lang.
the class LanguageInjectorUtils method isValid.
/**
* Checks whether the provided host is suitable for language injecting.
*
* @param host host string to be checked
* @param packageNames valid package names
* @param functionNames valid function names
* @return {@code true} if suitable for injecting language, {@code false} otherwise.
*/
public static boolean isValid(@NotNull PsiLanguageInjectionHost host, @NotNull Set<String> packageNames, @NotNull Set<String> functionNames) {
if (!(host instanceof QuotedLiteralString)) {
return false;
}
ExpressionListNode expressionListNode = PsiTreeUtil.getTopmostParentOfType(host, ExpressionListNode.class);
if (expressionListNode == null) {
return false;
}
PsiElement firstChild = expressionListNode.getFirstChild();
if (firstChild.getChildren().length > 1) {
return false;
}
FunctionReferenceNode functionReferenceNode = PsiTreeUtil.getPrevSiblingOfType(expressionListNode, FunctionReferenceNode.class);
if (functionReferenceNode == null) {
return false;
}
PackageNameNode packageNameNode = PsiTreeUtil.getChildOfType(functionReferenceNode, PackageNameNode.class);
if (packageNameNode == null) {
return false;
}
IdentifierPSINode functionName = PsiTreeUtil.getChildOfType(functionReferenceNode, IdentifierPSINode.class);
if (functionName == null) {
return false;
}
ExpressionNode expressionNode = PsiTreeUtil.getParentOfType(host, ExpressionNode.class);
if (expressionNode == null) {
return false;
}
Collection<QuotedLiteralString> quotedLiteralStrings = PsiTreeUtil.findChildrenOfType(expressionNode, QuotedLiteralString.class);
// We ignore this case since the string might not be identified correctly and will cause issues.
if (quotedLiteralStrings.size() > 1) {
return false;
}
return packageNames.contains(packageNameNode.getText()) && functionNames.contains(functionName.getText());
}
use of org.ballerinalang.plugins.idea.psi.ExpressionListNode in project ballerina by ballerina-lang.
the class BallerinaParameterInfoHandler method showParameterInfo.
@Override
public void showParameterInfo(@NotNull Object element, @NotNull CreateParameterInfoContext context) {
// This method will be called with the return object of the findElementForParameterInfo(). If it is null,
// this method will not be called.
// Since we know the type, we check and cast the object.
PsiElement currentElement = null;
PsiElement parentElement = null;
List<PsiElement> list = getParameters(element);
if (element instanceof ExpressionListNode) {
ExpressionListNode expressionListNode = (ExpressionListNode) element;
// We need to get the ExpressionListNode parent of current ExpressionListNode.
// Current ExpressionListNode - "WSO2"
// Parent ExpressionListNode - setName("WSO2")
// By doing this, we get the function name because setName("WSO2") is also a ExpressionNode.
PsiElement parent = PsiTreeUtil.getParentOfType(expressionListNode, FunctionInvocationNode.class);
// node is a FunctionInvocationNode.
if (parent == null) {
// So if the parent is null, we consider the FunctionInvocationNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ExpressionNode.class);
}
if (parent == null) {
parent = PsiTreeUtil.getParentOfType(expressionListNode, InvocationNode.class);
}
if (parent == null) {
// So if the parent is null, we consider the ActionInvocationNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ActionInvocationNode.class);
}
if (parent == null) {
// So if the parent is null, we consider the ActionInvocationNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ConnectorInitNode.class);
}
if (parent == null) {
// So if the parent is null, we consider the ExpressionListNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ExpressionListNode.class);
}
if (parent == null) {
parent = expressionListNode;
}
currentElement = expressionListNode;
parentElement = parent;
} else if (element instanceof FunctionInvocationNode) {
FunctionInvocationNode functionInvocationNode = (FunctionInvocationNode) element;
currentElement = functionInvocationNode;
parentElement = functionInvocationNode;
} else if (element instanceof ActionInvocationNode) {
ActionInvocationNode actionInvocationNode = (ActionInvocationNode) element;
currentElement = actionInvocationNode;
parentElement = actionInvocationNode;
} else if (element instanceof ConnectorInitNode) {
ConnectorInitNode connectorInitNode = (ConnectorInitNode) element;
currentElement = connectorInitNode;
parentElement = connectorInitNode;
} else if (element instanceof ExpressionNode) {
ExpressionNode expressionNode = (ExpressionNode) element;
currentElement = expressionNode;
parentElement = expressionNode;
} else if (element instanceof NameReferenceNode) {
NameReferenceNode nameReferenceNode = (NameReferenceNode) element;
currentElement = nameReferenceNode;
parentElement = nameReferenceNode;
} else if (element instanceof FunctionReferenceNode) {
FunctionReferenceNode functionReferenceNode = (FunctionReferenceNode) element;
currentElement = functionReferenceNode;
parentElement = functionReferenceNode;
} else if (element instanceof ConnectorReferenceNode) {
ConnectorReferenceNode connectorReferenceNode = (ConnectorReferenceNode) element;
currentElement = connectorReferenceNode;
parentElement = connectorReferenceNode;
} else if (element instanceof IdentifierPSINode) {
IdentifierPSINode identifier = (IdentifierPSINode) element;
currentElement = identifier;
parentElement = identifier;
} else if (element instanceof InvocationNode) {
InvocationNode invocationNode = (InvocationNode) element;
currentElement = invocationNode;
parentElement = invocationNode;
}
PsiElement namedIdentifierDefNode = getNameIdentifierDefinitionNode(parentElement);
PsiElement nameIdentifier = getNameIdentifier(currentElement, namedIdentifierDefNode);
if (currentElement == null || nameIdentifier == null || !(nameIdentifier instanceof IdentifierPSINode)) {
return;
}
if (list == null) {
list = new LinkedList<>();
}
// If there are no items to show, set a custom object. Otherwise set the list as an array.
if (list.isEmpty() && canResolve(nameIdentifier)) {
// Todo - change how to identify no parameter situation
context.setItemsToShow(new Object[] { "Empty" });
} else {
context.setItemsToShow(list.toArray(new PsiElement[list.size()]));
}
context.showHint(currentElement, currentElement.getTextRange().getStartOffset(), this);
}
use of org.ballerinalang.plugins.idea.psi.ExpressionListNode in project ballerina by ballerina-lang.
the class BallerinaParameterInfoHandler method getParameters.
/**
* Returns the parameter list for the given element.
*
* @param element
* @return
*/
@NotNull
public static List<PsiElement> getParameters(@NotNull Object element) {
List<PsiElement> list = new LinkedList<>();
if (element instanceof ExpressionListNode) {
ExpressionListNode expressionListNode = (ExpressionListNode) element;
// We need to get the ExpressionListNode parent of current ExpressionListNode.
// Current ExpressionListNode - "WSO2"
// Parent ExpressionListNode - setName("WSO2")
PsiElement parent = PsiTreeUtil.getParentOfType(expressionListNode, FunctionInvocationNode.class);
if (parent == null) {
// So if the parent is null, we consider the ActionInvocationNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ActionInvocationNode.class);
}
if (parent == null) {
parent = PsiTreeUtil.getParentOfType(expressionListNode, InvocationNode.class);
}
if (parent == null) {
// So if the parent is null, we consider the ActionInvocationNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ConnectorInitNode.class);
}
if (parent == null) {
// So if the parent is null, we consider the FunctionInvocationNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ExpressionNode.class);
}
if (parent == null) {
// So if the parent is null, we consider the ExpressionListNode as the parent node.
parent = PsiTreeUtil.getParentOfType(expressionListNode, ExpressionListNode.class);
}
if (parent == null) {
parent = expressionListNode;
}
list = getItemsToShow(expressionListNode, parent);
} else if (element instanceof FunctionInvocationNode) {
FunctionInvocationNode functionInvocationNode = (FunctionInvocationNode) element;
list = getItemsToShow(functionInvocationNode, functionInvocationNode);
} else if (element instanceof ActionInvocationNode) {
ActionInvocationNode actionInvocationNode = (ActionInvocationNode) element;
list = getItemsToShow(actionInvocationNode, actionInvocationNode);
} else if (element instanceof ConnectorInitNode) {
ConnectorInitNode connectorInitNode = (ConnectorInitNode) element;
list = getItemsToShow(connectorInitNode, connectorInitNode);
} else if (element instanceof ExpressionNode) {
ExpressionNode expressionNode = (ExpressionNode) element;
list = getItemsToShow(expressionNode, expressionNode);
} else if (element instanceof NameReferenceNode) {
NameReferenceNode nameReferenceNode = (NameReferenceNode) element;
list = getItemsToShow(nameReferenceNode, nameReferenceNode);
} else if (element instanceof FunctionReferenceNode) {
FunctionReferenceNode functionReferenceNode = (FunctionReferenceNode) element;
list = getItemsToShow(functionReferenceNode, functionReferenceNode);
} else if (element instanceof ConnectorReferenceNode) {
ConnectorReferenceNode connectorReferenceNode = (ConnectorReferenceNode) element;
list = getItemsToShow(connectorReferenceNode, connectorReferenceNode);
} else if (element instanceof IdentifierPSINode) {
IdentifierPSINode identifier = (IdentifierPSINode) element;
list = getItemsToShow(identifier, identifier);
} else if (element instanceof InvocationNode) {
InvocationNode invocationNode = (InvocationNode) element;
list = getItemsToShow(invocationNode, invocationNode);
}
return list;
}
use of org.ballerinalang.plugins.idea.psi.ExpressionListNode in project ballerina by ballerina-lang.
the class BallerinaParameterInfoHandler method getCurrentParameterIndex.
public static int getCurrentParameterIndex(@NotNull Object o, int offset) {
// This method updates parameter index node. This will be used to highlight the current parameter in the
// parameter popup.
PsiElement element;
// If the object is a type of ExpressionListNode, cast the object.
if (o instanceof ExpressionListNode) {
element = ((ExpressionListNode) o);
} else if (o instanceof FunctionReferenceNode) {
PsiElement parent = ((FunctionReferenceNode) o).getParent();
ExpressionListNode expressionListNode = PsiTreeUtil.getChildOfType(parent, ExpressionListNode.class);
if (expressionListNode == null) {
return 0;
}
PsiElement[] children = expressionListNode.getChildren();
return children.length / 2;
} else if (o instanceof ConnectorReferenceNode) {
PsiElement parent = ((ConnectorReferenceNode) o).getParent();
ExpressionListNode expressionListNode = PsiTreeUtil.getChildOfType(parent, ExpressionListNode.class);
if (expressionListNode == null) {
return 0;
}
PsiElement[] children = expressionListNode.getChildren();
return children.length / 2;
} else if (o instanceof IdentifierPSINode) {
StatementNode statementNode = PsiTreeUtil.getParentOfType((IdentifierPSINode) o, StatementNode.class);
if (statementNode == null) {
return 0;
}
PsiFile containingFile = statementNode.getContainingFile();
PsiElement elementAtOffset = containingFile.findElementAt(offset);
if (elementAtOffset == null) {
return 0;
}
int count = 0;
int commas = 0;
PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(elementAtOffset);
do {
if (prevVisibleLeaf != null) {
if (prevVisibleLeaf.getText().matches("[,]")) {
count++;
commas++;
} else if (prevVisibleLeaf.getText().matches("[}]")) {
count++;
} else if (prevVisibleLeaf.getText().matches("[{]")) {
count = count - commas;
} else if ("(".equals(prevVisibleLeaf.getText())) {
break;
}
prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
}
} while (prevVisibleLeaf != null);
return count;
} else {
return 0;
}
if (!(o instanceof PsiElement)) {
return -1;
}
// Get the element at offset.
PsiElement psiElement = ((PsiElement) o).getContainingFile().findElementAt(offset);
if (psiElement == null) {
return -1;
}
// Get the child nodes of element.
PsiElement[] children = element.getChildren();
// If there are no child nodes, set current parameter to 0 and return.
if (children.length == 0) {
return 0;
}
// If the number of children are not 0, we need to calculate the correct index.
int index = 0;
// Iterate through all children.
for (PsiElement child : children) {
// Get the offset of the child.
int childTextOffset = child.getTextOffset();
// following condition is true. So we set the current parameter and return.
if (psiElement.getTextOffset() <= childTextOffset) {
return index;
}
// If the child is a LeafPsiElement, increment the index.
if (child instanceof LeafPsiElement) {
index++;
}
}
// the calculated current parameter.
return index;
}
Aggregations