use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getAllMatchingElementsFromPackage.
/**
* Returns all the elements in the given directory(package) which matches the given xpath.
*
* @param directory which is used to get the functions
* @return all functions in the given directory(package)
*/
@NotNull
private static <T extends PsiElement> List<IdentifierPSINode> getAllMatchingElementsFromPackage(@NotNull PsiDirectory directory, @NotNull Class<T> clazz, boolean includePrivate, boolean includeBuiltIns) {
Project project = directory.getProject();
List<IdentifierPSINode> results = new ArrayList<>();
VirtualFile virtualFile = directory.getVirtualFile();
VirtualFile[] children = virtualFile.getChildren();
for (VirtualFile child : children) {
if (child.isDirectory()) {
continue;
}
PsiFile psiFile = PsiManager.getInstance(project).findFile(child);
if (!(psiFile instanceof BallerinaFile)) {
continue;
}
results.addAll(getMatchingElementsFromAFile(psiFile, clazz, includePrivate));
}
if (includeBuiltIns) {
// Add elements from built-in packages
for (String builtInDirectory : builtInDirectories) {
VirtualFile file = BallerinaPsiImplUtil.findFileInSDK(project, directory, builtInDirectory);
if (file == null) {
return results;
}
VirtualFile[] builtInFiles = file.getChildren();
for (VirtualFile builtInFile : builtInFiles) {
if (builtInFile.isDirectory() || !"bal".equals(builtInFile.getExtension())) {
continue;
}
// Find the file.
PsiFile psiFile = PsiManager.getInstance(project).findFile(builtInFile);
if (psiFile == null) {
return results;
}
results.addAll(getMatchingElementsFromAFile(psiFile, clazz, includePrivate));
}
}
}
return results;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getAttachedFunctions.
@NotNull
public static List<IdentifierPSINode> getAttachedFunctions(@NotNull StructDefinitionNode structDefinitionNode) {
List<IdentifierPSINode> attachedFunctions = new LinkedList<>();
PsiFile containingFile = structDefinitionNode.getContainingFile();
PsiDirectory containingPackage = containingFile.getParent();
if (containingPackage != null) {
ApplicationManager.getApplication().runReadAction(() -> {
List<IdentifierPSINode> functions = BallerinaPsiImplUtil.getAllFunctionsFromPackage(containingPackage, false, false);
for (IdentifierPSINode function : functions) {
ProgressManager.checkCanceled();
if (!isAttachedFunction(function)) {
continue;
}
PsiElement attachedStruct = getAttachedStruct(function);
if (attachedStruct != null && !attachedStruct.equals(structDefinitionNode)) {
continue;
}
attachedFunctions.add(function);
}
});
}
return attachedFunctions;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getAllGlobalVariablesInScope.
@NotNull
public static List<IdentifierPSINode> getAllGlobalVariablesInScope(@NotNull ScopeNode scope, int caretOffset) {
List<IdentifierPSINode> results = new LinkedList<>();
Collection<GlobalVariableDefinitionNode> variableDefinitionNodes = PsiTreeUtil.findChildrenOfType(scope, GlobalVariableDefinitionNode.class);
for (GlobalVariableDefinitionNode variableDefinitionNode : variableDefinitionNodes) {
PsiElement identifier = variableDefinitionNode.getNameIdentifier();
if (caretOffset != -1) {
if (identifier == null || !(identifier instanceof IdentifierPSINode) || identifier.getTextOffset() > caretOffset) {
continue;
}
PsiElement element = scope.findElementAt(caretOffset);
if (element != null) {
GlobalVariableDefinitionNode definition = PsiTreeUtil.getParentOfType(element, GlobalVariableDefinitionNode.class);
if (definition != null && definition.equals(variableDefinitionNode)) {
continue;
}
}
}
results.add(((IdentifierPSINode) identifier));
}
PsiFile originalFile = ((BallerinaFile) scope).getOriginalFile();
PsiDirectory containingPackage = originalFile.getParent();
if (containingPackage == null) {
return results;
}
PsiFile[] files = containingPackage.getFiles();
for (PsiFile file : files) {
// Do't check the current file again.
if (file.equals(originalFile)) {
continue;
}
variableDefinitionNodes = PsiTreeUtil.findChildrenOfType(file, GlobalVariableDefinitionNode.class);
for (GlobalVariableDefinitionNode variableDefinitionNode : variableDefinitionNodes) {
PsiElement identifier = variableDefinitionNode.getNameIdentifier();
if (identifier != null && identifier instanceof IdentifierPSINode) {
results.add(((IdentifierPSINode) identifier));
}
}
}
return results;
}
use of org.ballerinalang.plugins.idea.psi.IdentifierPSINode 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.IdentifierPSINode in project ballerina by ballerina-lang.
the class ActionInvocationReference method resolve.
@Nullable
@Override
public PsiElement resolve() {
IdentifierPSINode identifier = getElement();
PsiElement parent = identifier.getParent();
PsiElement 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();
variableReference = prevSibling.findReferenceAt(prevSibling.getTextLength());
}
if (variableReference == null) {
return null;
}
PsiElement variableDefinition = variableReference.resolve();
if (variableDefinition == null) {
return null;
}
PsiElement variableDefinitionParent = variableDefinition.getParent();
ConnectorDefinitionNode connectorDefinitionNode;
if (variableDefinitionParent instanceof EndpointDeclarationNode) {
connectorDefinitionNode = BallerinaPsiImplUtil.getConnectorDefinition(((EndpointDeclarationNode) variableDefinitionParent));
} else {
connectorDefinitionNode = BallerinaPsiImplUtil.resolveConnectorFromVariableDefinitionNode(variableDefinitionParent);
}
if (connectorDefinitionNode == null) {
return null;
}
Collection<ActionDefinitionNode> actionDefinitionNodes = PsiTreeUtil.findChildrenOfType(connectorDefinitionNode, ActionDefinitionNode.class);
for (ActionDefinitionNode actionDefinitionNode : actionDefinitionNodes) {
IdentifierPSINode actionIdentifier = PsiTreeUtil.getChildOfType(actionDefinitionNode, IdentifierPSINode.class);
if (actionIdentifier == null) {
continue;
}
if (actionIdentifier.getText().equals(identifier.getText())) {
return actionIdentifier;
}
}
return null;
}
Aggregations