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;
}
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));
}
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());
}
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));
}
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());
}
Aggregations