use of org.ballerinalang.plugins.idea.psi.references.RecordKeyReference 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.RecordKeyReference in project ballerina by ballerina-lang.
the class BallerinaAnnotator method annotateLeafPsiElementNodes.
private void annotateLeafPsiElementNodes(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
IElementType elementType = ((LeafPsiElement) element).getElementType();
PsiElement parentElement = element.getParent();
if (elementType == BallerinaTypes.AT && parentElement instanceof AnnotationAttachmentNode) {
Annotation annotation = holder.createInfoAnnotation(element, null);
annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.ANNOTATION);
} else if (elementType == BallerinaTypes.QUOTED_STRING) {
// In here, we annotate valid escape characters.
String text = element.getText();
Matcher matcher = VALID_ESCAPE_CHAR_PATTERN.matcher(text);
// Get the start offset of the element.
int startOffset = ((LeafPsiElement) element).getStartOffset();
// Iterate through each match.
while (matcher.find()) {
// Get the matching group.
String group = matcher.group(0);
// Calculate the start and end offsets and create the range.
TextRange range = new TextRange(startOffset + matcher.start(), startOffset + matcher.start() + group.length());
// Create the annotation.
Annotation annotation = holder.createInfoAnnotation(range, null);
annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.VALID_STRING_ESCAPE);
}
// Annotate invalid escape characters.
matcher = INVALID_ESCAPE_CHAR_PATTERN.matcher(text);
// Get the start offset of the element.
startOffset = ((LeafPsiElement) element).getStartOffset();
// Iterate through each match.
while (matcher.find()) {
// Get the matching group.
String group = matcher.group(3);
if (group != null) {
// Calculate the start and end offsets and create the range.
TextRange range = new TextRange(startOffset + matcher.start(3), startOffset + matcher.start(3) + group.length());
// Create the annotation.
Annotation annotation = holder.createInfoAnnotation(range, "Invalid string escape");
annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.INVALID_STRING_ESCAPE);
}
}
AnnotationAttributeNode annotationAttributeNode = PsiTreeUtil.getParentOfType(element, AnnotationAttributeNode.class);
boolean canHighlightParameters = canHighlightParameters(element);
if (canHighlightParameters && annotationAttributeNode != null) {
// Annotate query parameters in annotation attachments.
matcher = PATH_PARAMETERS_PATTERN.matcher(text);
// Get the start offset of the element.
startOffset = ((LeafPsiElement) element).getStartOffset();
// Iterate through each match.
while (matcher.find()) {
// Get the matching value without the enclosing {}.
String value = matcher.group(2);
if (value == null) {
continue;
}
// Calculate the start and end offsets and create the range. We need to add 2 to include the
// {} ignored.
TextRange range = new TextRange(startOffset + matcher.start(1), startOffset + matcher.start(1) + value.length() + 2);
// Check whether a matching resource parameter is available.
boolean isMatchAvailable = isMatchingParamAvailable(annotationAttributeNode, value);
// Create the annotation.
if (isMatchAvailable) {
Annotation annotation = holder.createInfoAnnotation(range, "Path parameter '" + value + "'");
annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.TEMPLATE_LANGUAGE_COLOR);
} else {
Annotation annotation = holder.createErrorAnnotation(range, "Path parameter '" + value + "' not found in the resource signature");
annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.INVALID_STRING_ESCAPE);
}
}
}
} else if (elementType == BallerinaTypes.STRING_TEMPLATE_LITERAL_START || elementType == BallerinaTypes.XML_START) {
annotateKeyword(element, holder);
} else if (elementType == BallerinaTypes.DOCUMENTATION_TEMPLATE_START || elementType == BallerinaTypes.DEPRECATED_TEMPLATE_START) {
// This uses an overloaded method so that the color can be easily changeable if required.
annotateKeyword(element, holder, BallerinaSyntaxHighlightingColors.KEYWORD);
} else if (elementType == BallerinaTypes.STRING_TEMPLATE_EXPRESSION_START || elementType == BallerinaTypes.XML_EXPRESSION_START) {
annotateExpressionTemplateStart(element, holder);
} else if (elementType == BallerinaTypes.STRING_TEMPLATE_TEXT || elementType == BallerinaTypes.XML_TEXT) {
annotateText(element, holder);
} else if (elementType == BallerinaTypes.EXPRESSION_END) {
annotateStringLiteralTemplateEnd(element, holder);
} else if (elementType == BallerinaTypes.DOCUMENTATION_TEMPLATE_ATTRIBUTE_START) {
// Doc type.
String msg = null;
switch(element.getText().charAt(0)) {
case 'T':
msg = "Receiver";
break;
case 'P':
msg = "Parameter";
break;
case 'R':
msg = "Return Value";
break;
case 'F':
msg = "Field";
break;
case 'V':
msg = "Variable";
break;
}
TextRange textRange = element.getTextRange();
TextRange newTextRange = new TextRange(textRange.getStartOffset(), textRange.getEndOffset() - 2);
Annotation annotation = holder.createInfoAnnotation(newTextRange, msg);
annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.DOCUMENTATION_INLINE_CODE);
} else if (element instanceof IdentifierPSINode) {
if (parentElement.getParent() instanceof AnnotationAttachmentNode) {
Annotation annotation = holder.createInfoAnnotation(element, null);
annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.ANNOTATION);
return;
}
if (parentElement instanceof DocumentationTemplateAttributeDescriptionNode) {
Annotation annotation = holder.createInfoAnnotation(element, null);
annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.DOCUMENTATION_INLINE_CODE);
}
PsiReference reference = element.getReference();
if (reference == null || reference instanceof RecordKeyReference) {
return;
}
PsiElement resolvedElement = reference.resolve();
if (resolvedElement == null) {
return;
}
PsiElement parent = resolvedElement.getParent();
if (parent instanceof ConstantDefinitionNode) {
Annotation annotation = holder.createInfoAnnotation(element, null);
annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.CONSTANT);
} else if (parent instanceof GlobalVariableDefinitionNode) {
Annotation annotation = holder.createInfoAnnotation(element, null);
annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.GLOBAL_VARIABLE);
}
}
}
Aggregations