use of org.ballerinalang.plugins.idea.psi.ImportDeclarationNode 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.ImportDeclarationNode 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.ImportDeclarationNode 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.ImportDeclarationNode in project ballerina by ballerina-lang.
the class BallerinaPsiImplUtil method addImport.
/**
* Adds an import declaration node to the file.
*
* @param file file which is to be used to insert the import declaration node
* @param importPath import path to be used in the import declaration node
* @param alias alias if needed. If this is {@code null}, it will be ignored
* @return import declaration node which is added
*/
@NotNull
public static ImportDeclarationNode addImport(@NotNull PsiFile file, @NotNull String importPath, @Nullable String alias) {
PsiElement addedNode;
Collection<ImportDeclarationNode> importDeclarationNodes = PsiTreeUtil.findChildrenOfType(file, ImportDeclarationNode.class);
Project project = file.getProject();
ImportDeclarationNode importDeclaration = BallerinaElementFactory.createImportDeclaration(project, importPath, alias);
if (importDeclarationNodes.isEmpty()) {
Collection<PackageDeclarationNode> packageDeclarationNodes = PsiTreeUtil.findChildrenOfType(file, PackageDeclarationNode.class);
if (packageDeclarationNodes.isEmpty()) {
PsiElement[] children = file.getChildren();
// Children cannot be empty since the IDEA adds placeholder string
PsiElement nonEmptyElement = PsiTreeUtil.skipSiblingsForward(children[0], PsiWhiteSpace.class, PsiComment.class);
if (nonEmptyElement == null) {
nonEmptyElement = children[0];
}
addedNode = file.addBefore(importDeclaration, children[0]);
file.addBefore(BallerinaElementFactory.createDoubleNewLine(project), nonEmptyElement);
} else {
PackageDeclarationNode packageDeclarationNode = packageDeclarationNodes.iterator().next();
PsiElement parent = packageDeclarationNode.getParent();
addedNode = parent.addAfter(importDeclaration, packageDeclarationNode);
parent.addAfter(BallerinaElementFactory.createDoubleNewLine(project), packageDeclarationNode);
}
} else {
LinkedList<ImportDeclarationNode> importDeclarations = new LinkedList<>(importDeclarationNodes);
ImportDeclarationNode lastImport = importDeclarations.getLast();
addedNode = lastImport.getParent().addAfter(importDeclaration, lastImport);
lastImport.getParent().addAfter(BallerinaElementFactory.createNewLine(project), lastImport);
}
return ((ImportDeclarationNode) addedNode);
}
use of org.ballerinalang.plugins.idea.psi.ImportDeclarationNode in project ballerina by ballerina-lang.
the class PackageNameReference method multiResolve.
@NotNull
@Override
public ResolveResult[] multiResolve(boolean incompleteCode) {
IdentifierPSINode identifier = getElement();
if (identifier == null) {
return new ResolveResult[0];
}
AliasNode aliasNode = PsiTreeUtil.getParentOfType(identifier, AliasNode.class);
if (aliasNode != null) {
return new ResolveResult[0];
}
List<ResolveResult> results = new ArrayList<>();
ImportDeclarationNode importDeclarationNode = PsiTreeUtil.getParentOfType(identifier, ImportDeclarationNode.class);
if (importDeclarationNode != null) {
List<PsiDirectory> directories = BallerinaPsiImplUtil.resolveDirectory(identifier);
for (PsiDirectory directory : directories) {
results.add(new PsiElementResolveResult(directory));
}
// Return the results.
return results.toArray(new ResolveResult[results.size()]);
}
PackageDeclarationNode packageDeclarationNode = PsiTreeUtil.getParentOfType(identifier, PackageDeclarationNode.class);
if (packageDeclarationNode != null) {
// If this is a package declaration, resolve the directory.
List<PsiDirectory> directories = BallerinaPsiImplUtil.resolveDirectory(identifier);
for (PsiDirectory directory : directories) {
results.add(new PsiElementResolveResult(directory));
}
// Return the results.
return results.toArray(new ResolveResult[results.size()]);
}
PsiFile containingFile = identifier.getContainingFile();
if (containingFile == null) {
return new ResolveResult[0];
}
List<PsiElement> importedPackages = BallerinaPsiImplUtil.getImportedPackagesInCurrentFile(containingFile);
for (PsiElement importedPackage : importedPackages) {
String packageName = importedPackage.getText();
if (packageName == null || packageName.isEmpty()) {
continue;
}
if (packageName.equals(identifier.getText())) {
PsiReference reference = importedPackage.findReferenceAt(0);
if (reference != null) {
PsiElement resolvedElement = reference.resolve();
if (resolvedElement != null) {
results.add(new PsiElementResolveResult(resolvedElement));
}
}
}
}
importedPackages = BallerinaPsiImplUtil.getPackagesImportedAsAliasInCurrentFile(containingFile);
for (PsiElement importedPackage : importedPackages) {
String packageName = importedPackage.getText();
if (packageName == null || packageName.isEmpty()) {
continue;
}
if (packageName.equals(identifier.getText())) {
IdentifierPSINode nameNode = PsiTreeUtil.findChildOfType(importedPackage, IdentifierPSINode.class);
if (nameNode != null) {
results.add(new PsiElementResolveResult(nameNode));
}
}
}
return results.toArray(new ResolveResult[results.size()]);
}
Aggregations