use of org.ballerinalang.plugins.idea.psi.references.WorkerReference 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.ballerinalang.plugins.idea.psi.references.WorkerReference in project ballerina by ballerina-lang.
the class BallerinaKeywordsCompletionContributor method fillCompletionVariants.
@Override
public void fillCompletionVariants(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
PsiElement element = parameters.getPosition();
PsiElement parent = element.getParent();
if (element instanceof LeafPsiElement) {
IElementType elementType = ((LeafPsiElement) element).getElementType();
if (elementType == BallerinaTypes.FLOATING_POINT) {
return;
}
}
if (parent instanceof NameReferenceNode) {
PsiElement prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(element);
if (prevVisibleLeaf != null && "public".equals(prevVisibleLeaf.getText())) {
result.addAllElements(getFileLevelKeywordsAsLookups(false, true, true));
}
if (prevVisibleLeaf instanceof IdentifierPSINode) {
result.addElement(getAttachKeyword());
return;
}
ANTLRPsiNode definitionParent = PsiTreeUtil.getParentOfType(parent, CallableUnitBodyNode.class, ServiceBodyNode.class, ConnectorBodyNode.class);
if (definitionParent != null && prevVisibleLeaf != null && "=".equals(prevVisibleLeaf.getText())) {
result.addElement(getCreateKeyword());
result.addElement(getTypeOfKeyword());
result.addElement(getLengthOfKeyword());
result.addAllElements(getValueKeywords());
}
ExpressionNode expressionNode = PsiTreeUtil.getParentOfType(parent, ExpressionNode.class);
if (expressionNode != null && expressionNode.getChildren().length == 1) {
PsiReference referenceAt = parent.findReferenceAt(0);
if (referenceAt == null || referenceAt instanceof NameReference) {
result.addAllElements(getValueKeywords());
}
PsiElement nextVisibleLeaf = PsiTreeUtil.nextVisibleLeaf(element);
if ((prevVisibleLeaf != null && "(".equals(prevVisibleLeaf.getText())) || (nextVisibleLeaf != null && ")".equals(nextVisibleLeaf.getText()) && !":".equals(prevVisibleLeaf.getText()))) {
addOtherTypeAsLookup(result);
addValueTypesAsLookups(result);
addReferenceTypesAsLookups(result);
}
}
AnnotationAttachmentNode attachmentNode = PsiTreeUtil.getParentOfType(parent, AnnotationAttachmentNode.class);
if (attachmentNode != null) {
result.addAllElements(getValueKeywords());
}
TypeNameNode typeNameNode = PsiTreeUtil.getParentOfType(parent, TypeNameNode.class);
if (typeNameNode != null && prevVisibleLeaf != null && !prevVisibleLeaf.getText().matches("[:.=]")) {
AnnotationDefinitionNode annotationDefinitionNode = PsiTreeUtil.getParentOfType(typeNameNode, AnnotationDefinitionNode.class);
if (annotationDefinitionNode == null) {
addOtherTypeAsLookup(result);
addXmlnsAsLookup(result);
addValueTypesAsLookups(result);
addReferenceTypesAsLookups(result);
}
}
}
if (parent instanceof StatementNode) {
PsiElement prevVisibleSibling = PsiTreeUtil.prevVisibleLeaf(element);
if (prevVisibleSibling != null && "=".equals(prevVisibleSibling.getText())) {
result.addElement(getCreateKeyword());
result.addElement(getTypeOfKeyword());
result.addElement(getLengthOfKeyword());
}
}
if (parent instanceof ConstantDefinitionNode || parent instanceof PsiErrorElement) {
PsiElement prevVisibleSibling = PsiTreeUtil.prevVisibleLeaf(element);
if (prevVisibleSibling != null && "const".equals(prevVisibleSibling.getText())) {
addValueTypesAsLookups(result);
return;
}
}
if (parent instanceof PsiErrorElement) {
PsiElement prevVisibleSibling = PsiTreeUtil.prevVisibleLeaf(element);
PsiElement definitionNode = PsiTreeUtil.getParentOfType(element, FunctionDefinitionNode.class, ServiceDefinitionNode.class, ConnectorDefinitionNode.class, ResourceDefinitionNode.class);
if (definitionNode != null) {
if (prevVisibleSibling != null && "=".equals(prevVisibleSibling.getText())) {
result.addElement(getCreateKeyword());
result.addAllElements(getValueKeywords());
result.addElement(getTypeOfKeyword());
result.addElement(getLengthOfKeyword());
}
if (prevVisibleSibling != null && prevVisibleSibling.getText().matches("[;{}]") && !(prevVisibleSibling.getParent() instanceof AnnotationAttachmentNode)) {
// Todo - change method
addOtherTypeAsLookup(result);
addXmlnsAsLookup(result);
addValueTypesAsLookups(result);
addReferenceTypesAsLookups(result);
if (definitionNode instanceof FunctionDefinitionNode) {
result.addAllElements(getFunctionSpecificKeywords());
}
if (definitionNode instanceof ResourceDefinitionNode) {
result.addAllElements(getResourceSpecificKeywords());
}
if (definitionNode instanceof ServiceDefinitionNode) {
result.addAllElements(getServiceSpecificKeywords());
}
if (definitionNode instanceof ConnectorDefinitionNode) {
result.addAllElements(getConnectorSpecificKeywords());
}
if (!(definitionNode instanceof ServiceDefinitionNode || definitionNode instanceof ConnectorDefinitionNode)) {
result.addAllElements(getCommonKeywords());
}
}
if (prevVisibleSibling != null && !prevVisibleSibling.getText().matches("[{}]")) /*|| !(prevVisibleSibling.getParent() instanceof AnnotationAttachmentNode)*/
{
result.addAllElements(getValueKeywords());
}
}
ConnectorBodyNode connectorBodyNode = PsiTreeUtil.getParentOfType(element, ConnectorBodyNode.class);
if (connectorBodyNode != null) {
result.addAllElements(getConnectorSpecificKeywords());
}
ConnectorDefinitionNode connectorDefinitionNode = PsiTreeUtil.getParentOfType(element, ConnectorDefinitionNode.class);
if (connectorDefinitionNode != null) {
result.addAllElements(getConnectorSpecificKeywords());
}
return;
}
if (parent instanceof NameReferenceNode) {
RecordKeyValueNode recordKeyValueNode = PsiTreeUtil.getParentOfType(parent, RecordKeyValueNode.class);
if (recordKeyValueNode == null) {
PsiElement prevVisibleSibling = PsiTreeUtil.prevVisibleLeaf(element);
if (prevVisibleSibling != null && "{".equals(prevVisibleSibling.getText())) {
FunctionDefinitionNode functionDefinitionNode = PsiTreeUtil.getParentOfType(element, FunctionDefinitionNode.class);
if (functionDefinitionNode != null) {
// Todo - change method
addOtherTypeAsLookup(result);
addXmlnsAsLookup(result);
addValueTypesAsLookups(result);
addReferenceTypesAsLookups(result);
result.addAllElements(getFunctionSpecificKeywords());
result.addAllElements(getCommonKeywords());
result.addAllElements(getValueKeywords());
}
ServiceBodyNode serviceBodyNode = PsiTreeUtil.getParentOfType(element, ServiceBodyNode.class);
if (serviceBodyNode != null) {
result.addAllElements(getServiceSpecificKeywords());
}
ConnectorBodyNode connectorBodyNode = PsiTreeUtil.getParentOfType(element, ConnectorBodyNode.class);
if (connectorBodyNode != null) {
result.addAllElements(getConnectorSpecificKeywords());
}
} else if (prevVisibleSibling != null && "}".equals(prevVisibleSibling.getText())) {
result.addAllElements(getFileLevelKeywordsAsLookups(true, false, false));
}
}
}
if (parent instanceof ResourceDefinitionNode) {
result.addAllElements(getServiceSpecificKeywords());
}
if (parent.getPrevSibling() == null) {
GlobalVariableDefinitionNode globalVariableDefinitionNode = PsiTreeUtil.getParentOfType(element, GlobalVariableDefinitionNode.class);
if (globalVariableDefinitionNode != null) {
PsiElement prevVisibleSibling = PsiTreeUtil.prevVisibleLeaf(element);
if (prevVisibleSibling != null && !(";".equals(prevVisibleSibling.getText()))) {
if (!(prevVisibleSibling.getText().matches("[:=]") || prevVisibleSibling instanceof IdentifierPSINode || "create".equals(prevVisibleSibling.getText()))) {
if (prevVisibleSibling instanceof LeafPsiElement) {
IElementType elementType = ((LeafPsiElement) prevVisibleSibling).getElementType();
if (BallerinaParserDefinition.KEYWORDS.contains(elementType)) {
return;
}
}
result.addAllElements(getCommonKeywords());
}
return;
}
PsiElement definitionNode = globalVariableDefinitionNode.getParent();
PackageDeclarationNode prevPackageDeclarationNode = PsiTreeUtil.getPrevSiblingOfType(definitionNode, PackageDeclarationNode.class);
ImportDeclarationNode prevImportDeclarationNode = PsiTreeUtil.getPrevSiblingOfType(definitionNode, ImportDeclarationNode.class);
ConstantDefinitionNode prevConstantDefinitionNode = PsiTreeUtil.getPrevSiblingOfType(definitionNode, ConstantDefinitionNode.class);
DefinitionNode prevDefinitionNode = PsiTreeUtil.getPrevSiblingOfType(definitionNode, DefinitionNode.class);
GlobalVariableDefinitionNode prevGlobalVariableDefinition = PsiTreeUtil.findChildOfType(prevDefinitionNode, GlobalVariableDefinitionNode.class);
if (prevPackageDeclarationNode == null && prevImportDeclarationNode == null && prevConstantDefinitionNode == null && prevGlobalVariableDefinition == null) {
result.addAllElements(getFileLevelKeywordsAsLookups(true, true, true));
} else if ((prevPackageDeclarationNode != null || prevImportDeclarationNode != null) && prevConstantDefinitionNode == null && prevGlobalVariableDefinition == null) {
result.addAllElements(getFileLevelKeywordsAsLookups(true, false, true));
} else {
result.addAllElements(getFileLevelKeywordsAsLookups(true, false, false));
}
addTypeNamesAsLookups(result);
}
}
if (element instanceof IdentifierPSINode) {
PsiReference reference = element.findReferenceAt(element.getTextLength());
if (reference instanceof WorkerReference) {
result.addAllElements(getWorkerInteractionKeywords());
}
}
}
Aggregations