Search in sources :

Example 46 with IdentifierPSINode

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());
}
Also used : AnnotationAttributeNode(org.ballerinalang.plugins.idea.psi.AnnotationAttributeNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) AnnotationReferenceNode(org.ballerinalang.plugins.idea.psi.AnnotationReferenceNode) AnnotationAttachmentNode(org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode)

Example 47 with IdentifierPSINode

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);
        }
    }
}
Also used : ConstantDefinitionNode(org.ballerinalang.plugins.idea.psi.ConstantDefinitionNode) Matcher(java.util.regex.Matcher) AnnotationAttributeNode(org.ballerinalang.plugins.idea.psi.AnnotationAttributeNode) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) Annotation(com.intellij.lang.annotation.Annotation) IElementType(com.intellij.psi.tree.IElementType) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) DocumentationTemplateAttributeDescriptionNode(org.ballerinalang.plugins.idea.psi.DocumentationTemplateAttributeDescriptionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) RecordKeyReference(org.ballerinalang.plugins.idea.psi.references.RecordKeyReference) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) GlobalVariableDefinitionNode(org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode) AnnotationAttachmentNode(org.ballerinalang.plugins.idea.psi.AnnotationAttachmentNode)

Example 48 with IdentifierPSINode

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();
}
Also used : WorkerDeclarationNode(org.ballerinalang.plugins.idea.psi.WorkerDeclarationNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) JoinClauseNode(org.ballerinalang.plugins.idea.psi.JoinClauseNode) ScopeNode(org.antlr.jetbrains.adaptor.psi.ScopeNode) Nullable(org.jetbrains.annotations.Nullable)

Example 49 with IdentifierPSINode

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;
}
Also used : WorkerDeclarationNode(org.ballerinalang.plugins.idea.psi.WorkerDeclarationNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement) LinkedList(java.util.LinkedList) NotNull(org.jetbrains.annotations.NotNull)

Example 50 with IdentifierPSINode

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;
}
Also used : IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PrioritizedLookupElement(com.intellij.codeInsight.completion.PrioritizedLookupElement) LinkedList(java.util.LinkedList) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)109 PsiElement (com.intellij.psi.PsiElement)70 NotNull (org.jetbrains.annotations.NotNull)63 LinkedList (java.util.LinkedList)58 LookupElement (com.intellij.codeInsight.lookup.LookupElement)48 Nullable (org.jetbrains.annotations.Nullable)32 PsiDirectory (com.intellij.psi.PsiDirectory)29 PsiReference (com.intellij.psi.PsiReference)25 PsiFile (com.intellij.psi.PsiFile)24 PackageNameNode (org.ballerinalang.plugins.idea.psi.PackageNameNode)20 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)17 ScopeNode (org.antlr.jetbrains.adaptor.psi.ScopeNode)15 PrioritizedLookupElement (com.intellij.codeInsight.completion.PrioritizedLookupElement)12 FieldDefinitionNode (org.ballerinalang.plugins.idea.psi.FieldDefinitionNode)12 StructDefinitionNode (org.ballerinalang.plugins.idea.psi.StructDefinitionNode)12 VariableDefinitionNode (org.ballerinalang.plugins.idea.psi.VariableDefinitionNode)10 GlobalVariableDefinitionNode (org.ballerinalang.plugins.idea.psi.GlobalVariableDefinitionNode)9 TypeNameNode (org.ballerinalang.plugins.idea.psi.TypeNameNode)9 ArrayList (java.util.ArrayList)8 NameReferenceNode (org.ballerinalang.plugins.idea.psi.NameReferenceNode)8