Populate Run/Debug configuration from an Action, Button or Listener
Hello there
Sorry to bother you guys in here, I know this is not a real bug, but I am really having hard time developing plugins due to missing documentation and lack of examples.
So my goal is to:
1. create a ToolWindow
2. display some data in a table
3. when the user click into a row, populate a Run/Debug configuration **from an existing template** (Ex: nodejs or Java itself).
So for the first two points I had almost no problems; now I have a table that gets some data from an API and a table selection listener which ideally should open a configuration window and populate it with the data from the table.
I have create, following the tutorial, a Run configuration: https://plugins.jetbrains.com/docs/intellij/run-configurations.html
and this is working as expected, it is appearing on the templates list.
However now there is no mention on how to open/populate this configuration from an action, a button or as in my specific case action listener.
Reading through the documentation I came across these:
"`ModuleBasedConfiguration` is a base class for a configuration that is associated with a specific module (for example, Java run configurations use the selected module to determine the run classpath)", it looks like something similar to what I am doing, but then that's it, no other explanation on how to use it or examples.
Also this one sounds similar:
"LocatableConfigurationBase is a common base class that should be used for configurations that can be created from context by a RunConfigurationProducer. It supports automatically generating a name for a configuration from its settings and keeping track of whether the name was changed by the user."
But once again that's it, I have no clue what to do with that, what is the context? DataContext or something else? How can I use it after I have register it?
It would be nice if someone could give me a hint, because honestly I am really lost.
Thanks
### EDIT 1
Ok I have found something more by Googling, those are the progress so far:
RunManager instance = RunManager.getInstance(project);
RunnerAndConfigurationSettings configuration = instance
.createConfiguration("config-test-1", new RunDebuggerConfiguration().getConfigurationFactories()[0]); // This is somewhat improvised
instance.addConfiguration(configuration);
ExecutionEnvironmentBuilder builder = ExecutionEnvironmentBuilder.createOrNull(DefaultRunExecutor.getRunExecutorInstance(), configuration);
if (builder != null) {
ExecutionManager.getInstance(project).restartRunProfile(builder.build());
}
This is still not working properly, however now I am able to add a configuration and I think open the dialog as well, so next I would like to get an existing run configuration from a template and populate it.
I see the `RunManager` exposes `.getConfigurationTemplate`, but is not really clear what `ConfigurationFactory` I need to pass.
### EDIT 2
With this modification I am able to get an existing template/configuration, however I cannot find a way to populate some of its fields (Ex: Environmental Variables).
RunManager instance = RunManager.getInstance(project);
List<RunnerAndConfigurationSettings> allSettings = instance.getAllSettings();
RunnerAndConfigurationSettings runnerAndConfigurationSettings = allSettings.get(0);
runnerAndConfigurationSettings.setEditBeforeRun(true);
ExecutionEnvironmentBuilder builder = ExecutionEnvironmentBuilder
.createOrNull(DefaultRunExecutor.getRunExecutorInstance(), runnerAndConfigurationSettings);
if (builder != null) {
ExecutionManager.getInstance(project).restartRunProfile(builder.build());
}
Please sign in to leave a comment.
You need to cast the RunConfiguration instance to corresponding implementation/interface, e.g. com.intellij.execution.CommonProgramRunConfigurationParameters to access setEnvs/getEnvs()
Ok, that partially helped, thanks.
However I just realized that if, for example, I want the Nodejs run configuration I will have to depend on that plugin, is that right?
Is there a method to retrieve that particular run configuration?
If not then I think is better if I create my custom configuration.
Yes, see https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
com.jetbrains.nodejs.run.NodeJsRunConfiguration
and
com.jetbrains.nodejs.run.NodeJsRunConfigurationType to filter via RunManager
Note there is com.jetbrains.nodejs.run.NodeJSRunConfigurationExtension extension point (NodeJS.runConfigurationExtension) that allows adding custom contributed content in run configuration, maybe that is more suitable?
I think I am going to use this.
Many thanks for the tips, they helped me connecting all the dots.