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());
}
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);
}
Aggregations