use of org.ballerinalang.plugins.idea.psi.FunctionReferenceNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getStructDefinition.
/**
* Finds a {@link StructDefinitionNode} which corresponds to the provided {@link VariableReferenceNode}.
*
* @param variableReferenceNode an variable reference node in the assignment statement node
* @param assignmentStatementNode an assignment statement node
* @param structReferenceNode an identifier which is a var variable in the assignment node
* @return
*/
private static StructDefinitionNode getStructDefinition(@NotNull VariableReferenceNode variableReferenceNode, @NotNull AssignmentStatementNode assignmentStatementNode, @NotNull IdentifierPSINode structReferenceNode) {
InvocationNode invocationNode = PsiTreeUtil.getChildOfType(variableReferenceNode, InvocationNode.class);
if (invocationNode != null) {
AnyIdentifierNameNode anyIdentifierNameNode = PsiTreeUtil.getChildOfType(invocationNode, AnyIdentifierNameNode.class);
if (anyIdentifierNameNode != null) {
IdentifierPSINode identifier = PsiTreeUtil.getChildOfType(anyIdentifierNameNode, IdentifierPSINode.class);
if (identifier != null) {
PsiReference reference = identifier.findReferenceAt(identifier.getTextLength());
if (reference == null) {
return null;
}
PsiElement resolvedElement = reference.resolve();
if (resolvedElement == null) {
return null;
}
// Check whether the resolved element's parent is a connector definition.
PsiElement definitionNode = resolvedElement.getParent();
if (!(definitionNode instanceof ActionDefinitionNode)) {
return null;
}
return getStructDefinition(assignmentStatementNode, structReferenceNode, definitionNode);
}
}
}
// Get the first child.
PsiElement node = variableReferenceNode.getFirstChild();
// If te first child is a VariableReferenceNode, it can be a function invocation.
if (node instanceof VariableReferenceNode) {
// Check whether the node is a function invocation.
boolean isFunctionInvocation = isFunctionInvocation((VariableReferenceNode) node);
if (isFunctionInvocation) {
// If it is a function invocation, the first child node will contain the function name.
PsiElement functionName = node.getFirstChild();
// We need to resolve the function name to the corresponding function definition.
PsiReference reference = functionName.findReferenceAt(functionName.getTextLength());
if (reference == null) {
return null;
}
PsiElement resolvedFunctionIdentifier = reference.resolve();
if (resolvedFunctionIdentifier == null) {
return null;
}
// Check whether the resolved element's parent is a function definition.
PsiElement definitionNode = resolvedFunctionIdentifier.getParent();
if (!(definitionNode instanceof FunctionDefinitionNode)) {
return null;
}
return getStructDefinition(assignmentStatementNode, structReferenceNode, definitionNode);
}
} else if (node instanceof NameReferenceNode) {
// If the node is a NameReferenceNode, that means RHS contains a variable.
PsiReference reference = node.findReferenceAt(node.getTextLength());
if (reference == null) {
return null;
}
// We resolve that variable to the definition.
PsiElement resolvedDefinition = reference.resolve();
if (resolvedDefinition == null || !(resolvedDefinition instanceof IdentifierPSINode)) {
return null;
}
// Then recursively call the findStructDefinition method to get the correct definition.
return findStructDefinition((IdentifierPSINode) resolvedDefinition);
} else if (node instanceof FunctionInvocationNode) {
FunctionReferenceNode functionReferenceNode = PsiTreeUtil.getChildOfType(node, FunctionReferenceNode.class);
if (functionReferenceNode == null) {
return null;
}
PsiReference reference = functionReferenceNode.findReferenceAt(functionReferenceNode.getTextLength());
if (reference == null) {
return null;
}
PsiElement resolvedDefinition = reference.resolve();
if (resolvedDefinition == null || !(resolvedDefinition instanceof IdentifierPSINode)) {
return null;
}
PsiElement definitionNode = resolvedDefinition.getParent();
if (!(definitionNode instanceof FunctionDefinitionNode)) {
return null;
}
return getStructDefinition(assignmentStatementNode, structReferenceNode, definitionNode);
}
return null;
}
use of org.ballerinalang.plugins.idea.psi.FunctionReferenceNode in project ballerina by ballerina-lang.
the class BallerinaParameterInfoHandler method findElement.
@Nullable
public static Object findElement(@NotNull PsiElement element, @Nullable PsiElement prevElement) {
// This will allow us to identify the correct ExpressionListNode element.
if (")".equals(element.getText())) {
PsiElement parent = element.getParent();
// invocations will not show proper parameters.
if (parent != null && !(parent instanceof ActionInvocationNode)) {
FunctionReferenceNode functionReferenceNode = PsiTreeUtil.getChildOfType(parent, FunctionReferenceNode.class);
if (functionReferenceNode != null) {
return functionReferenceNode;
}
ConnectorReferenceNode connectorReferenceNode = PsiTreeUtil.getChildOfType(parent, ConnectorReferenceNode.class);
if (connectorReferenceNode != null) {
return connectorReferenceNode;
}
if (parent instanceof StatementNode) {
PsiElement firstChild = parent.getFirstChild();
PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(firstChild);
if (prevVisibleLeaf == null || !"(".equals(prevVisibleLeaf.getText())) {
if (firstChild instanceof PsiErrorElement) {
return PsiTreeUtil.findChildOfType(firstChild, IdentifierPSINode.class);
}
return prevVisibleLeaf;
} else if ("(".equals(prevVisibleLeaf.getText())) {
return PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
} else {
return prevVisibleLeaf.getParent().getFirstChild();
}
}
}
// contain "(".
if (prevElement != null) {
element = prevElement;
}
}
PsiElement node = PsiTreeUtil.getParentOfType(element, ExpressionListNode.class);
// ExpressionListNode can be null if there are no arguments provided.
if (node != null) {
return node;
}
// So we check return the FunctionInvocationNode in that case.
node = PsiTreeUtil.getParentOfType(element, FunctionInvocationNode.class);
if (node != null) {
return node;
}
// If FunctionInvocationNode is null, we check return the ActionInvocationNode in that case.
node = PsiTreeUtil.getParentOfType(element, ActionInvocationNode.class);
if (node != null) {
return node;
}
// If ActionInvocationNode is null, we check return the ConnectorInitNode in that case.
node = PsiTreeUtil.getParentOfType(element, InvocationNode.class);
if (node != null) {
return node;
}
// any expression node parent.
if (element != null && "(".equals(element.getText())) {
node = PsiTreeUtil.getParentOfType(element, ExpressionNode.class);
}
if (node != null) {
return node;
}
if (prevElement != null && prevElement.getText().matches("[{,]") || prevElement instanceof LeafPsiElement) {
return resolve(prevElement);
}
return null;
}
use of org.ballerinalang.plugins.idea.psi.FunctionReferenceNode 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.FunctionReferenceNode in project ballerina by ballerina-lang.
the class BallerinaUnresolvedReferenceInspection method checkFile.
@Override
@Nullable
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
// does not work in tests since CodeInsightTestCase copies file into temporary location
if (ApplicationManager.getApplication().isUnitTestMode()) {
return new ProblemDescriptor[0];
}
if (!(file instanceof BallerinaFile)) {
return new ProblemDescriptor[0];
}
List<ProblemDescriptor> problemDescriptors = new LinkedList<>();
Collection<PackageNameNode> packageNameNodes = PsiTreeUtil.findChildrenOfType(file, PackageNameNode.class);
for (PackageNameNode packageNameNode : packageNameNodes) {
ProgressManager.checkCanceled();
if (packageNameNode == null) {
continue;
}
AliasNode aliasNode = PsiTreeUtil.getParentOfType(packageNameNode, AliasNode.class);
if (aliasNode != null) {
continue;
}
PackageDeclarationNode packageDeclarationNode = PsiTreeUtil.getParentOfType(packageNameNode, PackageDeclarationNode.class);
if (packageDeclarationNode != null) {
continue;
}
XmlAttribNode xmlAttribNode = PsiTreeUtil.getParentOfType(packageNameNode, XmlAttribNode.class);
if (xmlAttribNode != null) {
continue;
}
LocalQuickFix[] availableFixes;
ImportDeclarationNode importDeclarationNode = PsiTreeUtil.getParentOfType(packageNameNode, ImportDeclarationNode.class);
if (importDeclarationNode != null) {
availableFixes = new LocalQuickFix[0];
} else {
BallerinaImportPackageQuickFix quickFix = new BallerinaImportPackageQuickFix(packageNameNode);
availableFixes = new LocalQuickFix[] { quickFix };
}
PsiElement nameIdentifier = packageNameNode.getNameIdentifier();
if (nameIdentifier == null) {
continue;
}
PsiReference reference = nameIdentifier.getReference();
if (reference == null || reference.resolve() == null) {
if (reference instanceof PackageNameReference) {
ResolveResult[] resolveResults = ((PackageNameReference) reference).multiResolve(false);
if (resolveResults.length > 0) {
continue;
}
}
problemDescriptors.add(createProblemDescriptor(manager, packageNameNode, isOnTheFly, availableFixes));
}
}
// Todo - Add new quick fixes.
LocalQuickFix[] availableFixes = new LocalQuickFix[0];
Collection<NameReferenceNode> nameReferenceNodes = PsiTreeUtil.findChildrenOfType(file, NameReferenceNode.class);
problemDescriptors.addAll(getUnresolvedNameReferenceDescriptors(manager, isOnTheFly, availableFixes, nameReferenceNodes));
Collection<AnnotationAttributeNode> annotationAttributeNodes = PsiTreeUtil.findChildrenOfType(file, AnnotationAttributeNode.class);
problemDescriptors.addAll(getUnresolvedReferenceDescriptors(manager, isOnTheFly, availableFixes, annotationAttributeNodes));
Collection<ConnectorReferenceNode> connectorReferenceNodes = PsiTreeUtil.findChildrenOfType(file, ConnectorReferenceNode.class);
problemDescriptors.addAll(getUnresolvedReferenceDescriptors(manager, isOnTheFly, availableFixes, connectorReferenceNodes));
Collection<ActionInvocationNode> actionInvocationNodes = PsiTreeUtil.findChildrenOfType(file, ActionInvocationNode.class);
problemDescriptors.addAll(getUnresolvedReferenceDescriptors(manager, isOnTheFly, availableFixes, actionInvocationNodes));
Collection<FunctionReferenceNode> functionReferenceNodes = PsiTreeUtil.findChildrenOfType(file, FunctionReferenceNode.class);
problemDescriptors.addAll(getUnresolvedReferenceDescriptors(manager, isOnTheFly, availableFixes, functionReferenceNodes));
return problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]);
}
use of org.ballerinalang.plugins.idea.psi.FunctionReferenceNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getTypeFromVariableReference.
@Nullable
private static PsiElement getTypeFromVariableReference(@NotNull AssignmentStatementNode assignmentStatementNode, @NotNull IdentifierPSINode identifier, PsiElement expressionNodeFirstChild) {
int index = getVariableIndexFromVarAssignment(assignmentStatementNode, identifier);
if (index < 0) {
return null;
}
FunctionInvocationNode functionInvocationNode = PsiTreeUtil.getChildOfType(expressionNodeFirstChild, FunctionInvocationNode.class);
if (functionInvocationNode == null) {
return null;
}
FunctionReferenceNode functionReferenceNode = PsiTreeUtil.getChildOfType(functionInvocationNode, FunctionReferenceNode.class);
if (functionReferenceNode == null) {
return null;
}
PsiReference functionReference = functionReferenceNode.findReferenceAt(functionReferenceNode.getTextLength());
if (functionReference == null) {
return null;
}
PsiElement resolvedElement = functionReference.resolve();
if (resolvedElement == null || !(resolvedElement.getParent() instanceof FunctionDefinitionNode)) {
return null;
}
List<TypeNameNode> returnTypes = getReturnTypes(((FunctionDefinitionNode) resolvedElement.getParent()));
// index is 0, size should be at least 1, etc.
if (returnTypes.size() <= index) {
return null;
}
TypeNameNode typeNameNode = returnTypes.get(index);
PsiElement typeNode = getType(typeNameNode, true);
if (typeNode == null) {
return null;
}
return typeNode;
}
Aggregations