Search in sources :

Example 1 with BallerinaTestConfiguration

use of org.ballerinalang.plugins.idea.runconfig.test.BallerinaTestConfiguration in project ballerina by ballerina-lang.

the class BallerinaRunConfigurationProducerBase method setupConfigurationFromContext.

@Override
protected boolean setupConfigurationFromContext(@NotNull T configuration, @NotNull ConfigurationContext context, Ref<PsiElement> sourceElement) {
    // This method will be called with each configuration type. So we need to return true for correct
    // configuration type after updating the configurations.
    PsiFile file = getFileFromContext(context);
    if (file == null) {
        return false;
    }
    // Get the element. This will be an identifier element.
    PsiElement element = sourceElement.get();
    // Get the FunctionDefinitionNode parent from element (if exists).
    FunctionDefinitionNode functionNode = PsiTreeUtil.getParentOfType(element, FunctionDefinitionNode.class);
    // Get the ServiceDefinitionNode parent from element (if exists).
    ServiceDefinitionNode serviceDefinitionNode = PsiTreeUtil.getParentOfType(element, ServiceDefinitionNode.class);
    // Setup configuration for Ballerina test files.
    if (file.getName().endsWith(BallerinaConstants.BALLERINA_TEST_FILE_SUFFIX) && functionNode != null && BallerinaRunUtil.isTestFunction(functionNode)) {
        if (!(configuration instanceof BallerinaTestConfiguration)) {
            return false;
        }
        PackageDeclarationNode packageDeclarationNode = PsiTreeUtil.findChildOfType(file, PackageDeclarationNode.class);
        // Get the package path node. We need this to get the package path of the file.
        FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.findChildOfType(packageDeclarationNode, FullyQualifiedPackageNameNode.class);
        String packageInFile = "";
        if (fullyQualifiedPackageNameNode != null) {
            // Regardless of the OS, separator character will be "/".
            packageInFile = fullyQualifiedPackageNameNode.getText().replaceAll("\\.", "/");
        }
        RunnerAndConfigurationSettings existingConfigurations = context.findExisting();
        if (existingConfigurations != null) {
            // Get the RunConfiguration.
            RunConfiguration existingConfiguration = existingConfigurations.getConfiguration();
            // Run configuration might be an application configuration. So we need to check the type.
            if (existingConfiguration instanceof BallerinaTestConfiguration) {
                // Set other configurations.
                setTestConfigurations((BallerinaTestConfiguration) existingConfiguration, file, packageInFile);
                return true;
            }
            return false;
        } else {
            // If an existing configuration is not found and the configuration provided is of correct type.
            String configName = getConfigurationName(file);
            // Set the config name. This will be the file name.
            configuration.setName(configName);
            // Set the file path.
            configuration.setFilePath(file.getVirtualFile().getPath());
            // Set the module.
            Module module = context.getModule();
            if (module != null) {
                configuration.setModule(module);
            }
            // Set other configurations.
            setTestConfigurations((BallerinaTestConfiguration) configuration, file, packageInFile);
            return true;
        }
    }
    // Get the declared package in the file if available.
    String packageInFile = "";
    boolean isPackageDeclared = false;
    // Get the PackageDeclarationNode if available.
    PackageDeclarationNode packageDeclarationNode = PsiTreeUtil.findChildOfType(file, PackageDeclarationNode.class);
    if (packageDeclarationNode != null) {
        isPackageDeclared = true;
    }
    // Get the package path node. We need this to get the package path of the file.
    FullyQualifiedPackageNameNode fullyQualifiedPackageNameNode = PsiTreeUtil.findChildOfType(packageDeclarationNode, FullyQualifiedPackageNameNode.class);
    if (fullyQualifiedPackageNameNode != null) {
        // Regardless of the OS, separator character will be "/".
        packageInFile = fullyQualifiedPackageNameNode.getText().replaceAll("\\.", "/");
    }
    // Get existing configuration if available.
    RunnerAndConfigurationSettings existingConfigurations = context.findExisting();
    if (existingConfigurations != null) {
        // Get the RunConfiguration.
        RunConfiguration existingConfiguration = existingConfigurations.getConfiguration();
        // Run configuration might be an application configuration. So we need to check the type.
        if (existingConfiguration instanceof BallerinaApplicationConfiguration) {
            // Set other configurations.
            setConfigurations((BallerinaApplicationConfiguration) existingConfiguration, file, functionNode, serviceDefinitionNode, packageInFile, isPackageDeclared);
            return true;
        }
    } else if (configuration instanceof BallerinaApplicationConfiguration) {
        // If an existing configuration is not found and the configuration provided is of correct type.
        String configName = getConfigurationName(file);
        // Set the config name. This will be the file name.
        configuration.setName(configName);
        // Set the file path.
        configuration.setFilePath(file.getVirtualFile().getPath());
        // Set the module.
        Module module = context.getModule();
        if (module != null) {
            configuration.setModule(module);
        }
        // Set other configurations.
        setConfigurations((BallerinaApplicationConfiguration) configuration, file, functionNode, serviceDefinitionNode, packageInFile, isPackageDeclared);
        return true;
    }
    // Return false if the provided configuration type cannot be applied.
    return false;
}
Also used : BallerinaTestConfiguration(org.ballerinalang.plugins.idea.runconfig.test.BallerinaTestConfiguration) PackageDeclarationNode(org.ballerinalang.plugins.idea.psi.PackageDeclarationNode) BallerinaApplicationConfiguration(org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationConfiguration) ServiceDefinitionNode(org.ballerinalang.plugins.idea.psi.ServiceDefinitionNode) FullyQualifiedPackageNameNode(org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode) RunConfiguration(com.intellij.execution.configurations.RunConfiguration) FunctionDefinitionNode(org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode) RunnerAndConfigurationSettings(com.intellij.execution.RunnerAndConfigurationSettings) PsiFile(com.intellij.psi.PsiFile) Module(com.intellij.openapi.module.Module) PsiElement(com.intellij.psi.PsiElement)

Aggregations

RunnerAndConfigurationSettings (com.intellij.execution.RunnerAndConfigurationSettings)1 RunConfiguration (com.intellij.execution.configurations.RunConfiguration)1 Module (com.intellij.openapi.module.Module)1 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 FullyQualifiedPackageNameNode (org.ballerinalang.plugins.idea.psi.FullyQualifiedPackageNameNode)1 FunctionDefinitionNode (org.ballerinalang.plugins.idea.psi.FunctionDefinitionNode)1 PackageDeclarationNode (org.ballerinalang.plugins.idea.psi.PackageDeclarationNode)1 ServiceDefinitionNode (org.ballerinalang.plugins.idea.psi.ServiceDefinitionNode)1 BallerinaApplicationConfiguration (org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationConfiguration)1 BallerinaTestConfiguration (org.ballerinalang.plugins.idea.runconfig.test.BallerinaTestConfiguration)1