Search in sources :

Example 1 with BallerinaApplicationConfiguration

use of org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationConfiguration 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)

Example 2 with BallerinaApplicationConfiguration

use of org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationConfiguration in project ballerina by ballerina-lang.

the class BallerinaRunConfigurationProducerTest method testMainWithoutPackageRunConfiguration.

public void testMainWithoutPackageRunConfiguration() {
    PsiFile file = myFixture.configureByText("a.bal", "<caret>\nfunction main() {}");
    PsiElement element = file.findElementAt(myFixture.getCaretOffset());
    assertNotNull(element);
    ConfigurationContext configurationContext = new ConfigurationContext(element);
    BallerinaApplicationRunConfigurationProducer producer = new BallerinaApplicationRunConfigurationProducer();
    BallerinaApplicationConfiguration runConfiguration = createRunMainPackageConfiguration(file.getVirtualFile().getPath());
    assertTrue(producer.isConfigurationFromContext(runConfiguration, configurationContext));
    runConfiguration = createRunMainPackageConfiguration(file.getVirtualFile().getPath() + "_vl");
    assertFalse(producer.isConfigurationFromContext(runConfiguration, configurationContext));
}
Also used : ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) BallerinaApplicationRunConfigurationProducer(org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationRunConfigurationProducer) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) BallerinaApplicationConfiguration(org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationConfiguration)

Example 3 with BallerinaApplicationConfiguration

use of org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationConfiguration in project ballerina by ballerina-lang.

the class BallerinaRunConfigurationProducerTest method testMainWithoutPackageRunConfigurationFromContext.

public void testMainWithoutPackageRunConfigurationFromContext() {
    PsiFile file = myFixture.configureByText("a.bal", "function <caret>main() {}");
    PsiElement element = file.findElementAt(myFixture.getCaretOffset());
    assertNotNull(element);
    ConfigurationContext configurationContext = new ConfigurationContext(element);
    BallerinaApplicationRunConfigurationProducer producer = new BallerinaApplicationRunConfigurationProducer();
    ConfigurationFromContext configurationFromContext = producer.createConfigurationFromContext(configurationContext);
    assertNotNull(configurationFromContext);
    RunConfiguration runConfiguration = configurationFromContext.getConfiguration();
    assertTrue(runConfiguration instanceof BallerinaApplicationConfiguration);
    BallerinaApplicationConfiguration ballerinaApplicationConfiguration = (BallerinaApplicationConfiguration) runConfiguration;
    assertEquals(RunConfigurationKind.MAIN, ballerinaApplicationConfiguration.getRunKind());
    assertEquals(file.getVirtualFile().getPath(), ballerinaApplicationConfiguration.getFilePath());
    assertEmpty(ballerinaApplicationConfiguration.getPackage());
}
Also used : ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) RunConfiguration(com.intellij.execution.configurations.RunConfiguration) ConfigurationFromContext(com.intellij.execution.actions.ConfigurationFromContext) BallerinaApplicationRunConfigurationProducer(org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationRunConfigurationProducer) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) BallerinaApplicationConfiguration(org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationConfiguration)

Example 4 with BallerinaApplicationConfiguration

use of org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationConfiguration in project ballerina by ballerina-lang.

the class BallerinaRunConfigurationProducerTest method testServiceWithPackageRunConfiguration.

public void testServiceWithPackageRunConfiguration() {
    PsiFile file = myFixture.addFileToProject("org/test/a.bal", "package org.test; <caret>\nservice<http> main {}");
    myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
    PsiElement element = file.findElementAt(myFixture.getCaretOffset());
    assertNotNull(element);
    ConfigurationContext configurationContext = new ConfigurationContext(element);
    BallerinaApplicationRunConfigurationProducer producer = new BallerinaApplicationRunConfigurationProducer();
    BallerinaApplicationConfiguration runConfiguration = createRunMainPackageConfiguration(file.getVirtualFile().getPath());
    assertTrue(producer.isConfigurationFromContext(runConfiguration, configurationContext));
    runConfiguration = createRunMainPackageConfiguration(file.getVirtualFile().getPath() + "_vl");
    assertFalse(producer.isConfigurationFromContext(runConfiguration, configurationContext));
}
Also used : ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) BallerinaApplicationRunConfigurationProducer(org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationRunConfigurationProducer) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) BallerinaApplicationConfiguration(org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationConfiguration)

Example 5 with BallerinaApplicationConfiguration

use of org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationConfiguration in project ballerina by ballerina-lang.

the class BallerinaRunConfigurationProducerTest method testServiceWithPackageRunConfigurationFromContext.

public void testServiceWithPackageRunConfigurationFromContext() {
    PsiFile file = myFixture.addFileToProject("org/test/a.bal", "package org.test;\nservice<http> <caret>main {}");
    myFixture.configureFromExistingVirtualFile(file.getVirtualFile());
    PsiElement element = file.findElementAt(myFixture.getCaretOffset());
    assertNotNull(element);
    ConfigurationContext configurationContext = new ConfigurationContext(element);
    BallerinaApplicationRunConfigurationProducer producer = new BallerinaApplicationRunConfigurationProducer();
    ConfigurationFromContext configurationFromContext = producer.createConfigurationFromContext(configurationContext);
    assertNotNull(configurationFromContext);
    RunConfiguration runConfiguration = configurationFromContext.getConfiguration();
    assertTrue(runConfiguration instanceof BallerinaApplicationConfiguration);
    BallerinaApplicationConfiguration ballerinaApplicationConfiguration = (BallerinaApplicationConfiguration) runConfiguration;
    assertEquals(RunConfigurationKind.SERVICE, ballerinaApplicationConfiguration.getRunKind());
    assertEquals(file.getVirtualFile().getPath(), ballerinaApplicationConfiguration.getFilePath());
    assertEquals("org/test", ballerinaApplicationConfiguration.getPackage());
}
Also used : ConfigurationContext(com.intellij.execution.actions.ConfigurationContext) RunConfiguration(com.intellij.execution.configurations.RunConfiguration) ConfigurationFromContext(com.intellij.execution.actions.ConfigurationFromContext) BallerinaApplicationRunConfigurationProducer(org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationRunConfigurationProducer) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) BallerinaApplicationConfiguration(org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationConfiguration)

Aggregations

BallerinaApplicationConfiguration (org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationConfiguration)10 PsiElement (com.intellij.psi.PsiElement)9 PsiFile (com.intellij.psi.PsiFile)9 ConfigurationContext (com.intellij.execution.actions.ConfigurationContext)8 BallerinaApplicationRunConfigurationProducer (org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationRunConfigurationProducer)8 RunConfiguration (com.intellij.execution.configurations.RunConfiguration)5 ConfigurationFromContext (com.intellij.execution.actions.ConfigurationFromContext)4 RunnerAndConfigurationSettings (com.intellij.execution.RunnerAndConfigurationSettings)1 Module (com.intellij.openapi.module.Module)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 BallerinaApplicationRunConfigurationType (org.ballerinalang.plugins.idea.runconfig.application.BallerinaApplicationRunConfigurationType)1 BallerinaTestConfiguration (org.ballerinalang.plugins.idea.runconfig.test.BallerinaTestConfiguration)1