Search in sources :

Example 1 with QuotedLiteralString

use of org.ballerinalang.plugins.idea.psi.QuotedLiteralString in project ballerina by ballerina-lang.

the class LanguageInjectorUtils method isValid.

/**
 * Checks whether the provided host is suitable for language injecting.
 *
 * @param host          host string to be checked
 * @param packageNames  valid package names
 * @param functionNames valid function names
 * @return {@code true} if suitable for injecting language, {@code false} otherwise.
 */
public static boolean isValid(@NotNull PsiLanguageInjectionHost host, @NotNull Set<String> packageNames, @NotNull Set<String> functionNames) {
    if (!(host instanceof QuotedLiteralString)) {
        return false;
    }
    ExpressionListNode expressionListNode = PsiTreeUtil.getTopmostParentOfType(host, ExpressionListNode.class);
    if (expressionListNode == null) {
        return false;
    }
    PsiElement firstChild = expressionListNode.getFirstChild();
    if (firstChild.getChildren().length > 1) {
        return false;
    }
    FunctionReferenceNode functionReferenceNode = PsiTreeUtil.getPrevSiblingOfType(expressionListNode, FunctionReferenceNode.class);
    if (functionReferenceNode == null) {
        return false;
    }
    PackageNameNode packageNameNode = PsiTreeUtil.getChildOfType(functionReferenceNode, PackageNameNode.class);
    if (packageNameNode == null) {
        return false;
    }
    IdentifierPSINode functionName = PsiTreeUtil.getChildOfType(functionReferenceNode, IdentifierPSINode.class);
    if (functionName == null) {
        return false;
    }
    ExpressionNode expressionNode = PsiTreeUtil.getParentOfType(host, ExpressionNode.class);
    if (expressionNode == null) {
        return false;
    }
    Collection<QuotedLiteralString> quotedLiteralStrings = PsiTreeUtil.findChildrenOfType(expressionNode, QuotedLiteralString.class);
    // We ignore this case since the string might not be identified correctly and will cause issues.
    if (quotedLiteralStrings.size() > 1) {
        return false;
    }
    return packageNames.contains(packageNameNode.getText()) && functionNames.contains(functionName.getText());
}
Also used : ExpressionListNode(org.ballerinalang.plugins.idea.psi.ExpressionListNode) QuotedLiteralString(org.ballerinalang.plugins.idea.psi.QuotedLiteralString) PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) ExpressionNode(org.ballerinalang.plugins.idea.psi.ExpressionNode) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) FunctionReferenceNode(org.ballerinalang.plugins.idea.psi.FunctionReferenceNode) PsiElement(com.intellij.psi.PsiElement)

Example 2 with QuotedLiteralString

use of org.ballerinalang.plugins.idea.psi.QuotedLiteralString in project ballerina by ballerina-lang.

the class BallerinaStringManipulator method getRangeInElement.

@NotNull
@Override
public TextRange getRangeInElement(@NotNull QuotedLiteralString element) {
    if (element.getTextLength() == 0) {
        return TextRange.EMPTY_RANGE;
    }
    String s = element.getText();
    char quote = s.charAt(0);
    int startOffset = isQuote(quote) ? 1 : 0;
    int endOffset = s.length();
    if (s.length() > 1) {
        char lastChar = s.charAt(s.length() - 1);
        if (isQuote(quote) && lastChar == quote) {
            endOffset = s.length() - 1;
        }
        if (!isQuote(quote) && isQuote(lastChar)) {
            endOffset = s.length() - 1;
        }
    }
    return TextRange.create(startOffset, endOffset);
}
Also used : QuotedLiteralString(org.ballerinalang.plugins.idea.psi.QuotedLiteralString) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

QuotedLiteralString (org.ballerinalang.plugins.idea.psi.QuotedLiteralString)2 PsiElement (com.intellij.psi.PsiElement)1 ExpressionListNode (org.ballerinalang.plugins.idea.psi.ExpressionListNode)1 ExpressionNode (org.ballerinalang.plugins.idea.psi.ExpressionNode)1 FunctionReferenceNode (org.ballerinalang.plugins.idea.psi.FunctionReferenceNode)1 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)1 PackageNameNode (org.ballerinalang.plugins.idea.psi.PackageNameNode)1 NotNull (org.jetbrains.annotations.NotNull)1