use of org.ballerinalang.plugins.idea.psi.PackageNameNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method resolveDirectory.
/**
* Resolves a package name to matching directories.
*
* @param identifier the package name identifier which we need to resolve to the directory
* @return resolved directory
*/
@NotNull
public static List<PsiDirectory> resolveDirectory(@NotNull IdentifierPSINode identifier) {
List<PsiDirectory> results = new LinkedList<>();
PsiElement parent;
PsiElement tempParent = identifier.getParent();
if (tempParent == null) {
return results;
}
PsiElement superParent = tempParent.getParent();
AliasNode aliasNode = PsiTreeUtil.getParentOfType(superParent, AliasNode.class);
if (aliasNode != null) {
PsiElement temp = superParent;
while (temp != null && !(temp instanceof FullyQualifiedPackageNameNode)) {
temp = temp.getPrevSibling();
}
if (temp != null) {
parent = temp.getLastChild();
} else {
return results;
}
} else {
parent = tempParent;
}
// This is used to store all the packages which need to resolve the current package.
List<PsiElement> packages = new ArrayList<>();
packages.add(parent);
// Find all previous PackageNameNode elements.
PsiElement sibling = parent.getPrevSibling();
while (sibling != null) {
if (sibling instanceof PackageNameNode) {
// Add the sibling to the index 0 because we are traversing backward.
packages.add(0, sibling);
}
sibling = sibling.getPrevSibling();
}
Project project = identifier.getProject();
PsiFile containingFile = identifier.getContainingFile();
PsiFile originalFile = containingFile.getOriginalFile();
VirtualFile virtualFile = originalFile.getVirtualFile();
if (virtualFile == null) {
return results;
}
Module module = ModuleUtilCore.findModuleForFile(virtualFile, project);
if (module == null) {
return results;
}
results.addAll(findAllMatchingPackages(module, packages));
return results;
}
use of org.ballerinalang.plugins.idea.psi.PackageNameNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method resolveAliasNode.
/**
* Resolves the given alias node to the corresponding directory.
*
* @param aliasNode an alias node
* @return {@link PsiDirectory} which is the definition of the alias node.
*/
@Nullable
public static PsiDirectory resolveAliasNode(@NotNull AliasNode aliasNode) {
ImportDeclarationNode importDeclarationNode = PsiTreeUtil.getParentOfType(aliasNode, ImportDeclarationNode.class);
FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.getChildOfType(importDeclarationNode, FullyQualifiedPackageNameNode.class);
if (fullyQualifiedPackageNameNode == null) {
return null;
}
PackageNameNode[] packageNameNodes = PsiTreeUtil.getChildrenOfType(fullyQualifiedPackageNameNode, PackageNameNode.class);
if (packageNameNodes == null) {
return null;
}
PackageNameNode lastElement = ArrayUtil.getLastElement(packageNameNodes);
if (lastElement == null) {
return null;
}
PsiElement packageName = lastElement.getNameIdentifier();
if (!(packageName instanceof IdentifierPSINode)) {
return null;
}
List<PsiDirectory> directories = BallerinaPsiImplUtil.resolveDirectory(((IdentifierPSINode) packageName));
if (directories.isEmpty()) {
return null;
}
return directories.get(0);
}
use of org.ballerinalang.plugins.idea.psi.PackageNameNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getAllImportsInAFile.
/**
* Returns all imports as a map. Key is the last Package Name node or Alias node. Value is the import path.
*
* @param file file to get imports
* @return map of imports
*/
public static Map<String, String> getAllImportsInAFile(@NotNull PsiFile file) {
Map<String, String> results = new HashMap<>();
Collection<ImportDeclarationNode> importDeclarationNodes = PsiTreeUtil.findChildrenOfType(file, ImportDeclarationNode.class);
for (ImportDeclarationNode importDeclarationNode : importDeclarationNodes) {
// There can be two possible values for the imported package name. If there is no alias node in the
// import declaration, the imported package name is the last package name in the import path node.
// Otherwise the package name is the alias node.
FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.findChildOfType(importDeclarationNode, FullyQualifiedPackageNameNode.class);
AliasNode aliasNode = PsiTreeUtil.findChildOfType(importDeclarationNode, AliasNode.class);
if (aliasNode != null) {
if (fullyQualifiedPackageNameNode != null) {
// Key is the alias name node text. Value is the package path node text.
// Eg: import ballerina.utils as builtin;
// Map: builtin -> ballerina.utils
results.put(aliasNode.getText(), fullyQualifiedPackageNameNode.getText());
}
} else {
if (fullyQualifiedPackageNameNode != null) {
// We need to get all package name nodes from the package path node.
List<PackageNameNode> packageNameNodes = new ArrayList<>(PsiTreeUtil.findChildrenOfType(importDeclarationNode, PackageNameNode.class));
// If there is no package path node, return empty map.
if (packageNameNodes.isEmpty()) {
return results;
}
// The package node is the last package name in the package path.
// Eg: import ballerina.utils;
// Map: utils -> ballerina.utils
PackageNameNode lastNode = packageNameNodes.get(packageNameNodes.size() - 1);
results.put(lastNode.getText(), fullyQualifiedPackageNameNode.getText());
}
}
}
return results;
}
use of org.ballerinalang.plugins.idea.psi.PackageNameNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method suggestCurrentPackagePath.
@NotNull
public static PsiDirectory[] suggestCurrentPackagePath(@NotNull PsiElement element) {
List<PsiDirectory> results = new ArrayList<>();
Project project = element.getProject();
PsiElement parent = element.getParent();
// This is used to store all the packages which need to resolve the current package.
List<PsiElement> packages = new ArrayList<>();
packages.add(parent);
// Find all previous PackageNameNode elements.
PsiElement sibling = parent.getPrevSibling();
while (sibling != null) {
if (sibling instanceof PackageNameNode) {
// Add the sibling to the index 0 because we are traversing backward.
packages.add(0, sibling);
}
sibling = sibling.getPrevSibling();
}
// We need to get the content roots from the project and find matching directories in each content root.
VirtualFile[] contentRoots = ProjectRootManager.getInstance(project).getContentRoots();
for (VirtualFile contentRoot : contentRoots) {
// Get any matching directory from the content root.
List<VirtualFile> matches = suggestDirectory(contentRoot, packages);
// If there are matches, add it to the results.
for (VirtualFile file : matches) {
results.add(PsiManager.getInstance(project).findDirectory(file));
}
}
return results.toArray(new PsiDirectory[results.size()]);
}
use of org.ballerinalang.plugins.idea.psi.PackageNameNode 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());
}
Aggregations