Search in sources :

Example 11 with BallerinaFile

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

the class BallerinaRunConfigurationProducerBase method getFileFromContext.

@Nullable
private static BallerinaFile getFileFromContext(@Nullable ConfigurationContext context) {
    PsiElement contextElement = BallerinaRunUtil.getContextElement(context);
    PsiFile psiFile = contextElement != null ? contextElement.getContainingFile() : null;
    return psiFile instanceof BallerinaFile ? (BallerinaFile) psiFile : null;
}
Also used : BallerinaFile(org.ballerinalang.plugins.idea.psi.BallerinaFile) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with BallerinaFile

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

the class BallerinaRunConfigurationWithMain method checkFileConfiguration.

protected void checkFileConfiguration() throws RuntimeConfigurationError {
    VirtualFile file = findFile(getFilePath());
    if (file == null) {
        throw new RuntimeConfigurationError("Cannot find the specified main file.");
    }
    PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(file);
    if (!(psiFile instanceof BallerinaFile)) {
        throw new RuntimeConfigurationError("Main file is not a valid Ballerina file.");
    }
    if (myRunKind == RunConfigurationKind.MAIN && !BallerinaRunUtil.hasMainFunction(psiFile)) {
        throw new RuntimeConfigurationError("Main run kind is selected, but the file does not contain a main " + "function.");
    }
    if (myRunKind == RunConfigurationKind.SERVICE && !BallerinaRunUtil.hasServices(psiFile)) {
        throw new RuntimeConfigurationError("Service run kind is selected, but the file does not contain any " + "services.");
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) BallerinaFile(org.ballerinalang.plugins.idea.psi.BallerinaFile) PsiFile(com.intellij.psi.PsiFile) RuntimeConfigurationError(com.intellij.execution.configurations.RuntimeConfigurationError)

Example 13 with BallerinaFile

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

the class BallerinaTestConfiguration method checkConfiguration.

@Override
public void checkConfiguration() throws RuntimeConfigurationException {
    super.checkBaseConfiguration();
    VirtualFile file = findFile(getFilePath());
    if (file == null) {
        throw new RuntimeConfigurationError("Cannot find the specified main file.");
    }
    PsiFile psiFile = PsiManager.getInstance(getProject()).findFile(file);
    if (!(psiFile instanceof BallerinaFile)) {
        throw new RuntimeConfigurationError("Selected file is not a valid Ballerina file.");
    }
    if (!file.getName().endsWith(BallerinaConstants.BALLERINA_TEST_FILE_SUFFIX)) {
        throw new RuntimeConfigurationError("Selected file is not a Ballerina test file. File should end with " + "'" + BallerinaConstants.BALLERINA_TEST_FILE_SUFFIX + "' suffix.");
    }
    if (getPackage().isEmpty()) {
        throw new RuntimeConfigurationError("Test files must be in a package.");
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) BallerinaFile(org.ballerinalang.plugins.idea.psi.BallerinaFile) PsiFile(com.intellij.psi.PsiFile) RuntimeConfigurationError(com.intellij.execution.configurations.RuntimeConfigurationError)

Example 14 with BallerinaFile

use of org.ballerinalang.plugins.idea.psi.BallerinaFile 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 15 with BallerinaFile

use of org.ballerinalang.plugins.idea.psi.BallerinaFile 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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Project(com.intellij.openapi.project.Project) BallerinaFile(org.ballerinalang.plugins.idea.psi.BallerinaFile) IdentifierPSINode(org.ballerinalang.plugins.idea.psi.IdentifierPSINode) ArrayList(java.util.ArrayList) PsiFile(com.intellij.psi.PsiFile) QuotedLiteralString(org.ballerinalang.plugins.idea.psi.QuotedLiteralString) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

BallerinaFile (org.ballerinalang.plugins.idea.psi.BallerinaFile)16 PsiElement (com.intellij.psi.PsiElement)12 PsiFile (com.intellij.psi.PsiFile)9 LinkedList (java.util.LinkedList)7 NotNull (org.jetbrains.annotations.NotNull)7 PsiDirectory (com.intellij.psi.PsiDirectory)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 ArrayList (java.util.ArrayList)4 IdentifierPSINode (org.ballerinalang.plugins.idea.psi.IdentifierPSINode)4 PackageDeclarationNode (org.ballerinalang.plugins.idea.psi.PackageDeclarationNode)4 PackageNameNode (org.ballerinalang.plugins.idea.psi.PackageNameNode)4 Nullable (org.jetbrains.annotations.Nullable)4 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)3 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)3 PsiReference (com.intellij.psi.PsiReference)3 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)3 FullyQualifiedPackageNameNode (org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode)3 ImportDeclarationNode (org.ballerinalang.plugins.idea.psi.ImportDeclarationNode)3 RuntimeConfigurationError (com.intellij.execution.configurations.RuntimeConfigurationError)2 Module (com.intellij.openapi.module.Module)2