use of org.ballerinalang.plugins.idea.psi.AliasNode in project ballerina by ballerina-lang.
the class BallerinaAnnotator method annotateImportDeclarations.
private void annotateImportDeclarations(@NotNull PsiElement element, @NotNull AnnotationHolder holder) {
AliasNode aliasNode = PsiTreeUtil.findChildOfType(element, AliasNode.class);
if (aliasNode != null) {
// Create the annotation.
Annotation annotation = holder.createInfoAnnotation(aliasNode.getTextRange(), null);
annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.PACKAGE);
} else {
Collection<PackageNameNode> packageNameNodes = PsiTreeUtil.findChildrenOfType(element, PackageNameNode.class);
if (!packageNameNodes.isEmpty()) {
PackageNameNode lastPackageName = (PackageNameNode) packageNameNodes.toArray()[packageNameNodes.size() - 1];
// Create the annotation.
Annotation annotation = holder.createInfoAnnotation(lastPackageName.getTextRange(), null);
annotation.setTextAttributes(BallerinaSyntaxHighlightingColors.PACKAGE);
}
}
}
use of org.ballerinalang.plugins.idea.psi.AliasNode in project ballerina by ballerina-lang.
the class BallerinaCompletionContributor method handlePackageNameNode.
/**
* Add lookups for package declarations.
*
* @param parameters parameters which contain details of completion invocation
* @param resultSet result list which is used to add lookups
*/
private void handlePackageNameNode(@NotNull CompletionParameters parameters, @NotNull CompletionResultSet resultSet) {
PsiElement element = parameters.getPosition();
PsiElement parent = element.getParent();
PsiElement superParent = parent.getParent();
// Check whether we are in a package declaration node
if (superParent.getParent() instanceof PackageDeclarationNode) {
// If we are in a package declaration node, suggest packages.
addPackageSuggestions(resultSet, element);
} else if (superParent.getParent() instanceof ImportDeclarationNode && !(superParent instanceof AliasNode)) {
// If the parent is not an AliasNode and is inside the ImportDeclarationNode, we need to suggest
// packages.
addImportSuggestions(resultSet, element);
}
}
use of org.ballerinalang.plugins.idea.psi.AliasNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method getPackagesImportedAsAliasInCurrentFile.
@NotNull
public static List<PsiElement> getPackagesImportedAsAliasInCurrentFile(@NotNull PsiFile file) {
Collection<ImportDeclarationNode> allImports = PsiTreeUtil.findChildrenOfType(file, ImportDeclarationNode.class);
ArrayList<PsiElement> filteredPackages = new ArrayList<>();
for (ImportDeclarationNode importDeclaration : allImports) {
Collection<AliasNode> aliasNodes = PsiTreeUtil.findChildrenOfType(importDeclaration, AliasNode.class);
if (aliasNodes.isEmpty()) {
continue;
}
PsiElement aliasNode = aliasNodes.iterator().next();
PsiElement firstChild = aliasNode.getFirstChild();
if (firstChild != null) {
filteredPackages.add(firstChild);
}
}
return filteredPackages;
}
use of org.ballerinalang.plugins.idea.psi.AliasNode 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.AliasNode 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;
}
Aggregations