Search in sources :

Example 16 with PackageNameNode

use of org.ballerinalang.plugins.idea.psi.PackageNameNode 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);
        }
    }
}
Also used : PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) Annotation(com.intellij.lang.annotation.Annotation) AliasNode(org.ballerinalang.plugins.idea.psi.AliasNode)

Example 17 with PackageNameNode

use of org.ballerinalang.plugins.idea.psi.PackageNameNode in project ballerina by ballerina-lang.

the class WrongPackageStatementInspection method checkFile.

@Override
@Nullable
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
    // does not work in tests since CodeInsightTestCase copies file into temporary location
    if (ApplicationManager.getApplication().isUnitTestMode()) {
        return new ProblemDescriptor[0];
    }
    if (!(file instanceof BallerinaFile)) {
        return new ProblemDescriptor[0];
    }
    Module module = ModuleUtil.findModuleForFile(file.getVirtualFile(), file.getProject());
    boolean isBallerinaModule = BallerinaSdkService.getInstance(file.getProject()).isBallerinaModule(module);
    if (!isBallerinaModule) {
        return new ProblemDescriptor[0];
    }
    BallerinaFile ballerinaFile = (BallerinaFile) file;
    PsiDirectory directory = ballerinaFile.getContainingDirectory();
    if (directory == null) {
        return new ProblemDescriptor[0];
    }
    List<ProblemDescriptor> problemDescriptors = new LinkedList<>();
    String packageName = BallerinaUtil.suggestPackageNameForDirectory(directory);
    PackageDeclarationNode packageDeclarationNode = PsiTreeUtil.findChildOfType(file, PackageDeclarationNode.class);
    Collection<DefinitionNode> definitionNodes = PsiTreeUtil.findChildrenOfType(file, DefinitionNode.class);
    for (DefinitionNode definitionNode : definitionNodes) {
        PsiElement firstChild = definitionNode.getFirstChild();
        if (firstChild == null || !(firstChild instanceof IdentifierDefSubtree)) {
            return new ProblemDescriptor[0];
        }
        PsiElement nameIdentifier = ((IdentifierDefSubtree) firstChild).getNameIdentifier();
        if (nameIdentifier == null) {
            return new ProblemDescriptor[0];
        }
        if (!Comparing.strEqual(packageName, "", true) && packageDeclarationNode == null) {
            String description = "Missing package statement: '" + packageName + "'";
            ProblemDescriptor problemDescriptor = manager.createProblemDescriptor(nameIdentifier, description, new AdjustPackageNameFix(nameIdentifier, packageName), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, isOnTheFly);
            problemDescriptors.add(problemDescriptor);
        }
    }
    if (!problemDescriptors.isEmpty()) {
        return problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]);
    }
    if (packageDeclarationNode == null) {
        return new ProblemDescriptor[0];
    }
    FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.findChildOfType(packageDeclarationNode, FullyQualifiedPackageNameNode.class);
    if (fullyQualifiedPackageNameNode == null || fullyQualifiedPackageNameNode.getText().isEmpty()) {
        return new ProblemDescriptor[0];
    }
    Collection<PackageNameNode> packageNames = PsiTreeUtil.findChildrenOfType(packageDeclarationNode, PackageNameNode.class);
    if (packageNames.isEmpty()) {
        return new ProblemDescriptor[0];
    }
    LinkedList<PackageNameNode> packageNameNodes = new LinkedList<>();
    packageNameNodes.addAll(packageNames);
    PackageNameNode lastElement = packageNameNodes.getLast();
    if (lastElement == null) {
        return new ProblemDescriptor[0];
    }
    List<LocalQuickFix> availableFixes = new ArrayList<>();
    availableFixes.add(new AdjustPackageNameFix(fullyQualifiedPackageNameNode, packageName));
    PsiElement packageNameIdentifier = lastElement.getNameIdentifier();
    if (packageNameIdentifier == null) {
        return getProblemDescriptors(manager, isOnTheFly, packageName, availableFixes, fullyQualifiedPackageNameNode, lastElement);
    }
    PsiReference reference = packageNameIdentifier.getReference();
    if (reference == null) {
        return getProblemDescriptors(manager, isOnTheFly, packageName, availableFixes, fullyQualifiedPackageNameNode, lastElement);
    }
    PsiElement resolvedElement = reference.resolve();
    if (!(resolvedElement instanceof PsiDirectory)) {
        return getProblemDescriptors(manager, isOnTheFly, packageName, availableFixes, fullyQualifiedPackageNameNode, lastElement);
    }
    String containingDirectoryPackageName = BallerinaUtil.suggestPackageNameForDirectory(((PsiDirectory) resolvedElement));
    if (!Comparing.equal(packageName, containingDirectoryPackageName, true)) {
        if (!availableFixes.isEmpty()) {
            return getProblemDescriptors(manager, isOnTheFly, packageName, availableFixes, fullyQualifiedPackageNameNode, lastElement);
        }
    }
    return new ProblemDescriptor[0];
}
Also used : IdentifierDefSubtree(org.antlr.jetbrains.adaptor.psi.IdentifierDefSubtree) BallerinaFile(org.ballerinalang.plugins.idea.psi.BallerinaFile) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) ArrayList(java.util.ArrayList) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PsiReference(com.intellij.psi.PsiReference) PackageDeclarationNode(org.ballerinalang.plugins.idea.psi.PackageDeclarationNode) LinkedList(java.util.LinkedList) DefinitionNode(org.ballerinalang.plugins.idea.psi.DefinitionNode) FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) PsiDirectory(com.intellij.psi.PsiDirectory) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 18 with PackageNameNode

use of org.ballerinalang.plugins.idea.psi.PackageNameNode in project ballerina by ballerina-lang.

the class BallerinaImportPackageQuickFix method invoke.

@Override
public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) {
    if (!FileModificationService.getInstance().prepareFileForWrite(file)) {
        return;
    }
    if (!(startElement instanceof PackageNameNode)) {
        return;
    }
    PsiReference reference = startElement.getReference();
    if (reference != null && reference.resolve() != null) {
        return;
    }
    List<String> importPathVariantsToImport = getImportPathVariantsToImport(startElement);
    if (importPathVariantsToImport.size() == 1) {
        Runnable addImport = () -> BallerinaPsiImplUtil.addImport(file, importPathVariantsToImport.get(0), null);
        CommandProcessor.getInstance().runUndoTransparentAction(() -> ApplicationManager.getApplication().runWriteAction(addImport));
    } else {
        performImport(importPathVariantsToImport, file, editor);
    }
}
Also used : PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) PsiReference(com.intellij.psi.PsiReference)

Example 19 with PackageNameNode

use of org.ballerinalang.plugins.idea.psi.PackageNameNode in project ballerina by ballerina-lang.

the class BallerinaImportPackageQuickFix method getImportPathVariantsToImport.

private List<String> getImportPathVariantsToImport(@NotNull PsiElement element) {
    List<PsiDirectory> packagesInResolvableScopes = BallerinaPsiImplUtil.getAllPackagesInResolvableScopes(element.getProject());
    List<String> results = new LinkedList<>();
    if (element instanceof PackageNameNode) {
        for (PsiDirectory packagesInResolvableScope : packagesInResolvableScopes) {
            if (packagesInResolvableScope.getName().equals(element.getText())) {
                String importPath = BallerinaUtil.suggestPackageNameForDirectory(packagesInResolvableScope);
                if (StringUtil.isEmpty(importPath)) {
                    continue;
                }
                results.add(importPath);
            }
        }
    }
    return results;
}
Also used : PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) PsiDirectory(com.intellij.psi.PsiDirectory) LinkedList(java.util.LinkedList)

Example 20 with PackageNameNode

use of org.ballerinalang.plugins.idea.psi.PackageNameNode in project ballerina by ballerina-lang.

the class BallerinaPsiImplUtil method suggestImportPackages.

/**
 * Suggests packages for auto completing packages.
 *
 * @param element package name element
 * @return suggestions for auto completion
 */
@NotNull
public static PsiDirectory[] suggestImportPackages(@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));
        }
    }
    // First we check the sources of module sdk.
    Module module = ModuleUtilCore.findModuleForPsiElement(element);
    if (module != null) {
        Sdk moduleSdk = ModuleRootManager.getInstance(module).getSdk();
        if (moduleSdk != null) {
            VirtualFile[] roots = moduleSdk.getSdkModificator().getRoots(OrderRootType.SOURCES);
            for (VirtualFile root : roots) {
                List<VirtualFile> matches = suggestDirectory(root, packages);
                for (VirtualFile file : matches) {
                    results.add(PsiManager.getInstance(project).findDirectory(file));
                }
            }
        }
        if (!results.isEmpty()) {
            return results.toArray(new PsiDirectory[results.size()]);
        }
    }
    // Then we check the sources of project sdk.
    Sdk projectSdk = ProjectRootManager.getInstance(project).getProjectSdk();
    if (projectSdk != null) {
        VirtualFile[] roots = projectSdk.getSdkModificator().getRoots(OrderRootType.SOURCES);
        for (VirtualFile root : roots) {
            List<VirtualFile> matches = suggestDirectory(root, packages);
            for (VirtualFile file : matches) {
                results.add(PsiManager.getInstance(project).findDirectory(file));
            }
        }
    }
    String sdkHomePath = BallerinaSdkService.getInstance(project).getSdkHomePath(null);
    if (sdkHomePath == null) {
        return results.toArray(new PsiDirectory[results.size()]);
    }
    VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByPath(sdkHomePath + "/src");
    if (virtualFile == null) {
        return results.toArray(new PsiDirectory[results.size()]);
    }
    List<VirtualFile> matches = suggestDirectory(virtualFile, packages);
    for (VirtualFile file : matches) {
        results.add(PsiManager.getInstance(project).findDirectory(file));
    }
    return results.toArray(new PsiDirectory[results.size()]);
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) ArrayList(java.util.ArrayList) QuotedLiteralString(org.ballerinalang.plugins.idea.psi.QuotedLiteralString) Project(com.intellij.openapi.project.Project) FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) PackageNameNode(org.ballerinalang.plugins.idea.psi.PackageNameNode) PsiDirectory(com.intellij.psi.PsiDirectory) Sdk(com.intellij.openapi.projectRoots.Sdk) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement) LeafPsiElement(com.intellij.psi.impl.source.tree.LeafPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PackageNameNode (org.ballerinalang.plugins.idea.psi.PackageNameNode)35 PsiElement (com.intellij.psi.PsiElement)30 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)20 LinkedList (java.util.LinkedList)17 NotNull (org.jetbrains.annotations.NotNull)14 LookupElement (com.intellij.codeInsight.lookup.LookupElement)11 Nullable (org.jetbrains.annotations.Nullable)11 PsiReference (com.intellij.psi.PsiReference)9 FullyQualifiedPackageNameNode (org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode)9 PsiDirectory (com.intellij.psi.PsiDirectory)8 ArrayList (java.util.ArrayList)7 ImportDeclarationNode (org.ballerinalang.plugins.idea.psi.ImportDeclarationNode)7 AliasNode (org.ballerinalang.plugins.idea.psi.AliasNode)5 PackageDeclarationNode (org.ballerinalang.plugins.idea.psi.PackageDeclarationNode)5 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)4 Module (com.intellij.openapi.module.Module)4 PsiFile (com.intellij.psi.PsiFile)4 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)4 BallerinaFile (org.ballerinalang.plugins.idea.psi.BallerinaFile)4 XmlAttribNode (org.ballerinalang.plugins.idea.psi.XmlAttribNode)4