use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaAnnotator method canHighlightParameters.
/**
* Checks whether the query,path parameters can be highlighted in the given element.
*
* @param element element which needs to be checked
* @return {@code true} if parameters can be highlighted, {@code false} otherwise.
*/
private boolean canHighlightParameters(@NotNull PsiElement element) {
AnnotationAttributeNode annotationAttributeNode = PsiTreeUtil.getParentOfType(element, AnnotationAttributeNode.class);
if (annotationAttributeNode == null) {
return false;
}
AnnotationAttachmentNode annotationAttachmentNode = PsiTreeUtil.getParentOfType(annotationAttributeNode, AnnotationAttachmentNode.class);
if (annotationAttachmentNode == null) {
return false;
}
AnnotationReferenceNode annotationReferenceNode = PsiTreeUtil.getChildOfType(annotationAttachmentNode, AnnotationReferenceNode.class);
if (annotationReferenceNode == null) {
return false;
}
IdentifierPSINode annotationName = PsiTreeUtil.getChildOfType(annotationReferenceNode, IdentifierPSINode.class);
return annotationName != null && "resourceConfig".equals(annotationName.getText());
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode 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);
}
}
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class WorkerReference method resolve.
@Nullable
@Override
public PsiElement resolve() {
IdentifierPSINode identifier = getElement();
ScopeNode scopeNode = PsiTreeUtil.getParentOfType(identifier, ScopeNode.class);
if (scopeNode == null) {
return null;
}
if (scopeNode instanceof JoinClauseNode) {
scopeNode = (ScopeNode) scopeNode.getParent();
}
List<WorkerDeclarationNode> workerDeclarations = BallerinaPsiImplUtil.getWorkerDeclarationsInScope(scopeNode);
for (WorkerDeclarationNode workerDeclaration : workerDeclarations) {
IdentifierPSINode workerName = PsiTreeUtil.getChildOfType(workerDeclaration, IdentifierPSINode.class);
if (workerName == null) {
continue;
}
if (identifier.getText().equals(workerName.getText())) {
return workerName;
}
}
return super.resolve();
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaCompletionUtils method createWorkerLookupElements.
@NotNull
public static List<LookupElement> createWorkerLookupElements(@NotNull Collection<WorkerDeclarationNode> workerDeclarationNodes) {
List<LookupElement> lookupElements = new LinkedList<>();
for (WorkerDeclarationNode workerDeclarationNode : workerDeclarationNodes) {
IdentifierPSINode workerName = PsiTreeUtil.getChildOfType(workerDeclarationNode, IdentifierPSINode.class);
if (workerName == null) {
continue;
}
LookupElement lookupElement = createWorkerLookupElement(workerName);
lookupElements.add(lookupElement);
}
return lookupElements;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaCompletionUtils method createTransformerLookupElements.
@NotNull
public static List<LookupElement> createTransformerLookupElements(@NotNull List<IdentifierPSINode> transformers) {
List<LookupElement> lookupElements = new LinkedList<>();
for (IdentifierPSINode transformer : transformers) {
if (transformer == null) {
continue;
}
LookupElement lookupElement = BallerinaCompletionUtils.createTransformerLookupElement(transformer);
lookupElements.add(lookupElement);
}
return lookupElements;
}
Aggregations