use of org.ballerinalang.plugins.idea.psi.ParameterListNode in project ballerina by ballerina-lang.
the class BallerinaParameterInfoHandler method updatePresentation.
public static String updatePresentation(Object p, @NotNull ParameterInfoUIContext context) {
// context.getItemsToShow() in showParameterInfo method.
if (p instanceof ParameterListNode) {
// Caste the object.
ParameterListNode element = (ParameterListNode) p;
// Get the parameter presentations.
List<String> parameterPresentations = getParameterPresentations(element);
// These will be used to identify which parameter is selected. This will be highlighted in the popup.
int start = 0;
int end = 0;
StringBuilder builder = new StringBuilder();
// If there are no parameter nodes, set "no parameters" message.
if (parameterPresentations.isEmpty()) {
builder.append(CodeInsightBundle.message("parameter.info.no.parameters"));
} else {
// Get the current parameter index.
int selected = context.getCurrentParameterIndex();
// Iterate through each parameter presentation.
for (int i = 0; i < parameterPresentations.size(); i++) {
// If i != 0, we need to add the , between parameters.
if (i != 0) {
builder.append(", ");
}
// If the current parameter is the selected parameter, get the start index.
if (i == selected) {
start = builder.length();
}
// Append the parameter.
builder.append(parameterPresentations.get(i));
// If the current parameter is the selected parameter, get the end index.
if (i == selected) {
end = builder.length();
}
}
}
// Call setupUIComponentPresentation with necessary arguments.
return context.setupUIComponentPresentation(builder.toString(), start, end, false, false, false, context.getDefaultParameterColor());
} else if (p instanceof ParameterTypeNameList) {
// Caste the object.
ParameterTypeNameList element = (ParameterTypeNameList) p;
// Get the parameter presentations.
List<String> parameterPresentations = getParameterPresentations(element);
// These will be used to identify which parameter is selected. This will be highlighted in the popup.
int start = 0;
int end = 0;
StringBuilder builder = new StringBuilder();
// If there are no parameter nodes, set "no parameters" message.
if (parameterPresentations.isEmpty()) {
builder.append(CodeInsightBundle.message("parameter.info.no.parameters"));
} else {
// Get the current parameter index.
int selected = context.getCurrentParameterIndex();
// Iterate through each parameter presentation.
for (int i = 0; i < parameterPresentations.size(); i++) {
// If i != 0, we need to add the , between parameters.
if (i != 0) {
builder.append(", ");
}
// If the current parameter is the selected parameter, get the start index.
if (i == selected) {
start = builder.length();
}
// Append the parameter.
builder.append(parameterPresentations.get(i));
// If the current parameter is the selected parameter, get the end index.
if (i == selected) {
end = builder.length();
}
}
}
// Call setupUIComponentPresentation with necessary arguments.
return context.setupUIComponentPresentation(builder.toString(), start, end, false, false, false, context.getDefaultParameterColor());
} else if (p.equals("Empty")) {
// We set the "no.parameters" message to the popup.
return context.setupUIComponentPresentation(CodeInsightBundle.message("parameter.info.no.parameters"), 0, 0, false, false, false, context.getDefaultParameterColor());
} else {
// Disable the popup for other types.
context.setUIComponentEnabled(false);
return null;
}
}
use of org.ballerinalang.plugins.idea.psi.ParameterListNode in project ballerina by ballerina-lang.
the class BallerinaDocumentationProvider method getReturnTypes.
/**
* Returns the return types of the given definition node. This return types are later used to generate the
* signature.
*
* @param definitionNode definition node which we want to get the return types
* @return list of return type strings.
*/
private static List<String> getReturnTypes(PsiElement definitionNode) {
List<String> results = new LinkedList<>();
// Parameters are in the ReturnParameterNode. So we first get the ReturnParameterNode from the definition
// node.
ReturnParameterNode node = PsiTreeUtil.findChildOfType(definitionNode, ReturnParameterNode.class);
if (node == null) {
return results;
}
// But there can be two possible scenarios. The actual return types can be in either ParameterTypeNameList or
// ParameterListNode. This is because return types can be named parameters. In that case, ParameterListNode is
// available.
// First we check for ParameterTypeNameList.
ParameterTypeNameList parameterTypeNameList = PsiTreeUtil.findChildOfType(node, ParameterTypeNameList.class);
// If it is available, that means the return types are not named parameters.
if (parameterTypeNameList != null) {
// Each parameter will be of type TypeNameNode. So we get all return types.
Collection<ParameterTypeName> parameterTypeNames = PsiTreeUtil.getChildrenOfTypeAsList(parameterTypeNameList, ParameterTypeName.class);
// Add each TypeNameNode to the result list.
parameterTypeNames.forEach(parameterTypeName -> {
TypeNameNode typeNameNode = PsiTreeUtil.getChildOfType(parameterTypeName, TypeNameNode.class);
if (typeNameNode != null) {
results.add(BallerinaParameterInfoHandler.formatParameter(typeNameNode.getText()));
}
});
// Return the results.
return results;
}
// If there is no return type node, we check for ParameterListNode.
ParameterListNode parameterListNode = PsiTreeUtil.findChildOfType(node, ParameterListNode.class);
if (parameterListNode != null) {
// Actual parameters are in ParameterNodes.
Collection<ParameterNode> parameterNodes = PsiTreeUtil.findChildrenOfType(parameterListNode, ParameterNode.class);
// Add each ParameterNode to the result list.
parameterNodes.forEach(parameterNode -> results.add(BallerinaParameterInfoHandler.formatParameter(parameterNode.getText())));
// Return the results.
return results;
}
// Return empty list.
return results;
}
use of org.ballerinalang.plugins.idea.psi.ParameterListNode 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.ParameterListNode 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;
}
use of org.ballerinalang.plugins.idea.psi.ParameterListNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getParameters.
/**
* Returns parameter for provided definition node.
*
* @param definitionNode
* @return
*/
@NotNull
public static List<IdentifierPSINode> getParameters(@NotNull PsiElement definitionNode) {
List<IdentifierPSINode> parameters = new LinkedList<>();
ParameterListNode parameterListNode = PsiTreeUtil.getChildOfType(definitionNode, ParameterListNode.class);
if (parameterListNode == null) {
return parameters;
}
// Get parameter nodes.
Collection<ParameterNode> parameterNodes = PsiTreeUtil.getChildrenOfTypeAsList(parameterListNode, ParameterNode.class);
for (ParameterNode parameterNode : parameterNodes) {
IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(parameterNode, IdentifierPSINode.class);
if (identifier == null) {
continue;
}
parameters.add(identifier);
}
return parameters;
}
Aggregations