use of org.antlr.jetbrains.adaptor.lexer.RuleIElementType in project ballerina by ballerina-lang.
the class IdentifierPSINode method getReference.
/**
* Create and return a PsiReference object associated with this ID
* node. The reference object will be asked to resolve this ref
* by using the text of this node to identify the appropriate definition
* site. The definition site is typically a subtree for a function
* or variable definition whereas this reference is just to this ID
* leaf node.
* <p>
* As the AST factory has no context and cannot create different kinds
* of PsiNamedElement nodes according to context, every ID node
* in the tree will be of this type. So, we distinguish references
* from definitions or other uses by looking at context in this method
* as we have parent (context) information.
*/
@Override
public PsiReference getReference() {
PsiElement parent = getParent();
IElementType elType = parent.getNode().getElementType();
PsiElement prevVisibleLeaf;
// do not return a reference for the ID nodes in a definition
if (elType instanceof RuleIElementType) {
XmlAttribNode xmlAttribNode = PsiTreeUtil.getParentOfType(parent, XmlAttribNode.class);
switch(((RuleIElementType) elType).getRuleIndex()) {
case RULE_packageName:
if (xmlAttribNode != null) {
return new NameSpaceReference(this);
}
AliasNode aliasNode = PsiTreeUtil.getParentOfType(parent, AliasNode.class);
if (aliasNode != null) {
return null;
}
return new PackageNameReference(this);
case RULE_nameReference:
// If we are currently resolving a var variable, we don't need to resolve it since it is the
// definition.
PsiElement element = getPsi();
if (element instanceof IdentifierPSINode) {
AssignmentStatementNode assignmentStatementNode = PsiTreeUtil.getParentOfType(element, AssignmentStatementNode.class);
if (assignmentStatementNode != null && BallerinaPsiImplUtil.isVarAssignmentStatement(assignmentStatementNode)) {
IdentifierPSINode identifier = (IdentifierPSINode) element;
if (BallerinaPsiImplUtil.isRedeclaredVar(identifier)) {
return new NameReference(this);
}
if (BallerinaPsiImplUtil.isValidVarVariable(assignmentStatementNode, identifier)) {
return null;
}
}
}
if (xmlAttribNode != null) {
return new NameSpaceReference(this);
}
RecordKeyNode recordKeyNode = PsiTreeUtil.getParentOfType(parent, RecordKeyNode.class);
if (recordKeyNode != null) {
return new RecordKeyReference(this);
}
RecordValueNode recordValueNode = PsiTreeUtil.getParentOfType(parent, RecordValueNode.class);
if (recordValueNode != null) {
return new RecordValueReference(this);
}
prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(parent);
if (prevVisibleLeaf != null) {
if (".".equals(prevVisibleLeaf.getText())) {
// Todo - update logic
PsiReference reference = checkAndSuggestReferenceAfterDot();
if (reference != null) {
return reference;
}
return new FieldReference(this);
}
// Todo - remove ","
if ((prevVisibleLeaf instanceof IdentifierPSINode) && prevVisibleLeaf.getParent() instanceof AnnotationDefinitionNode) {
return null;
} else if (("attach".equals(prevVisibleLeaf.getText()) || ",".equals(prevVisibleLeaf.getText())) && prevVisibleLeaf.getParent() instanceof AnnotationDefinitionNode) {
return new AttachmentPointReference(this);
}
}
return new NameReference(this);
case RULE_field:
prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(parent);
PsiReference variableReference = null;
if (prevVisibleLeaf != null && ".".equals(prevVisibleLeaf.getText())) {
PsiElement connectorVariable = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
if (connectorVariable != null) {
variableReference = connectorVariable.findReferenceAt(connectorVariable.getTextLength());
}
} else {
PsiElement prevSibling = parent.getPrevSibling();
if (prevSibling != null) {
PsiReference reference = checkAndSuggestReferenceAfterDot();
if (reference != null) {
return reference;
}
return new FieldReference(this);
}
}
if (variableReference == null) {
return null;
}
PsiElement variableDefinition = variableReference.resolve();
if (variableDefinition == null) {
return new FieldReference(this);
}
ConnectorDefinitionNode connectorDefinitionNode = BallerinaPsiImplUtil.resolveConnectorFromVariableDefinitionNode(variableDefinition.getParent());
if (connectorDefinitionNode != null) {
return new ActionInvocationReference(this);
}
return new FieldReference(this);
case RULE_functionReference:
return new FunctionReference(this);
case RULE_workerReference:
return new WorkerReference(this);
case RULE_attachmentPoint:
return new AttachmentPointReference(this);
case RULE_statement:
// Todo - update logic
PsiReference reference = checkAndSuggestReferenceAfterDot();
if (reference != null) {
return reference;
}
prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(getParent());
if (prevVisibleLeaf != null) {
if (".".equals(prevVisibleLeaf.getText())) {
PsiElement psiElement = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
if (psiElement == null) {
return new FieldReference(this);
}
reference = psiElement.findReferenceAt(psiElement.getTextLength());
if (reference == null) {
return new FieldReference(this);
}
PsiElement resolvedElement = reference.resolve();
if (resolvedElement == null) {
return new FieldReference(this);
}
PsiElement resolvedElementParent = resolvedElement.getParent();
if (resolvedElementParent instanceof ConnectorDefinitionNode) {
return new ActionInvocationReference(this);
}
if (resolvedElementParent instanceof VariableDefinitionNode) {
connectorDefinitionNode = BallerinaPsiImplUtil.resolveConnectorFromVariableDefinitionNode((resolvedElementParent));
if (connectorDefinitionNode != null) {
return new ActionInvocationReference(this);
}
}
return new FieldReference(this);
} else if (prevVisibleLeaf.getText().matches("[,]")) {
return new NameReference(this);
} else if (prevVisibleLeaf.getText().matches("[{]")) {
return new RecordKeyReference(this);
}
}
PsiElement nextVisibleLeaf = PsiTreeUtil.nextVisibleLeaf(getPsi());
if (nextVisibleLeaf != null && ":".equals(nextVisibleLeaf.getText())) {
return new PackageNameReference(this);
}
return new StatementReference(this);
case RULE_invocation:
return suggestReferenceTypeForInvocation();
case RULE_structReference:
return new StructReference(this);
case RULE_transformerReference:
return new TransformerReference(this);
case RULE_recordKey:
return new RecordKeyReference(this);
case RULE_anyIdentifierName:
return suggestReferenceTypeForInvocation();
// return new DocVariableReference(this);
default:
return null;
}
}
if (parent instanceof PsiErrorElement) {
return suggestReferenceType(parent);
}
return null;
}
use of org.antlr.jetbrains.adaptor.lexer.RuleIElementType 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.antlr.jetbrains.adaptor.lexer.RuleIElementType in project ballerina by ballerina-lang.
the class BallerinaFindUsageProvider method getType.
@NotNull
@Override
public String getType(@NotNull PsiElement element) {
if (!(element.getParent() instanceof ANTLRPsiNode)) {
return "";
}
ANTLRPsiNode parent = (ANTLRPsiNode) element.getParent();
RuleIElementType elType = (RuleIElementType) parent.getNode().getElementType();
// Todo - Add more types
switch(elType.getRuleIndex()) {
case RULE_functionDefinition:
return "Function";
case RULE_serviceDefinition:
return "Service";
case RULE_variableDefinitionStatement:
return "Variable";
case RULE_parameter:
return "Parameter";
case RULE_packageName:
return "Package";
case RULE_structDefinition:
return "Struct";
case RULE_constantDefinition:
return "Constant";
case RULE_fieldDefinition:
return "Field";
case RULE_annotationDefinition:
return "Annotation";
case RULE_nameReference:
PsiElement parentNode = PsiTreeUtil.getParentOfType(element, FunctionInvocationNode.class);
if (parentNode != null) {
return "Function";
}
parentNode = PsiTreeUtil.getParentOfType(element, ActionInvocationNode.class);
if (parentNode != null) {
return "Action";
}
parentNode = PsiTreeUtil.getParentOfType(element, AssignmentStatementNode.class);
if (parentNode != null) {
return "Variable";
}
break;
case RULE_globalVariableDefinition:
return "Global Variable";
case RULE_namespaceDeclaration:
return "Namespace";
case RULE_workerDeclaration:
return "Worker";
case RULE_enumDefinition:
return "Enum";
case RULE_enumField:
return "Enum Field";
case RULE_endpointDeclaration:
return "Endpoint";
}
return "";
}
Aggregations