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