IntelliJ Platform Plugin SDK Help

Run Configurations Tutorial

These series of steps show how to register and implement a simple Run Configuration. Run Configurations are used to run internal and external processes from within IntelliJ Platform based products. To get familiar with the concept of a Run Configuration refer Run/Debug Configuration section of IntelliJ IDEA Web Help

Consider the runConfiguration sample plugin available in the code samples. See Code Samples on how to set up and run the plugin.

Pre-Requirements

Create an empty plugin project as described in Creating a Plugin Project.

Register a New ConfigurationType

Add new com.intellij.configurationType extension to the plugin.xml

<extensions defaultExtensionNs="com.intellij"> <configurationType implementation="org.jetbrains.sdk.runConfiguration.DemoRunConfigurationType"/> </extensions>

Implement ConfigurationType

Implement ConfigurationType interface registered in the Step 1.

// Copyright 2000-2021 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.jetbrains.sdk.runConfiguration; import com.intellij.execution.configurations.ConfigurationFactory; import com.intellij.execution.configurations.ConfigurationType; import com.intellij.icons.AllIcons; import org.jetbrains.annotations.NotNull; import javax.swing.*; public class DemoRunConfigurationType implements ConfigurationType { static final String ID = "DemoRunConfiguration"; @NotNull @Override public String getDisplayName() { return "Demo"; } @Override public String getConfigurationTypeDescription() { return "Demo run configuration type"; } @Override public Icon getIcon() { return AllIcons.General.Information; } @NotNull @Override public String getId() { return ID; } @Override public ConfigurationFactory[] getConfigurationFactories() { return new ConfigurationFactory[]{new DemoConfigurationFactory(this)}; } }

Implement a ConfigurationFactory

Implement a new ConfigurationFactory through which custom run configurations will be created.

// Copyright 2000-2021 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.jetbrains.sdk.runConfiguration; import com.intellij.execution.configurations.ConfigurationFactory; import com.intellij.execution.configurations.ConfigurationType; import com.intellij.execution.configurations.RunConfiguration; import com.intellij.openapi.components.BaseState; import com.intellij.openapi.project.Project; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; public class DemoConfigurationFactory extends ConfigurationFactory { protected DemoConfigurationFactory(ConfigurationType type) { super(type); } @Override public @NotNull String getId() { return DemoRunConfigurationType.ID; } @NotNull @Override public RunConfiguration createTemplateConfiguration(@NotNull Project project) { return new DemoRunConfiguration(project, this, "Demo"); } @Nullable @Override public Class<? extends BaseState> getOptionsClass() { return DemoRunConfigurationOptions.class; } }

Implement corresponding configuration options class extending RunConfigurationOptions to store settings.

// Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.jetbrains.sdk.runConfiguration; import com.intellij.execution.configurations.RunConfigurationOptions; import com.intellij.openapi.components.StoredProperty; public class DemoRunConfigurationOptions extends RunConfigurationOptions { private final StoredProperty<String> myScriptName = string("").provideDelegate(this, "scriptName"); public String getScriptName() { return myScriptName.getValue(this); } public void setScriptName(String scriptName) { myScriptName.setValue(this, scriptName); } }

Implement a Run Configuration

To make your changes visible from the UI, implement a new Run Configuration.

Note: In most of the cases you can derive a custom Run Configuration class from the RunConfigurationBase. If you need to implement specific settings externalization rules and I/O behaviour, use RunConfiguration interface.

// Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.jetbrains.sdk.runConfiguration; import com.intellij.execution.ExecutionException; import com.intellij.execution.Executor; import com.intellij.execution.configurations.*; import com.intellij.execution.process.OSProcessHandler; import com.intellij.execution.process.ProcessHandler; import com.intellij.execution.process.ProcessHandlerFactory; import com.intellij.execution.process.ProcessTerminatedListener; import com.intellij.execution.runners.ExecutionEnvironment; import com.intellij.openapi.options.SettingsEditor; import com.intellij.openapi.project.Project; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; public class DemoRunConfiguration extends RunConfigurationBase<DemoRunConfigurationOptions> { protected DemoRunConfiguration(Project project, ConfigurationFactory factory, String name) { super(project, factory, name); } @NotNull @Override protected DemoRunConfigurationOptions getOptions() { return (DemoRunConfigurationOptions) super.getOptions(); } public String getScriptName() { return getOptions().getScriptName(); } public void setScriptName(String scriptName) { getOptions().setScriptName(scriptName); } @NotNull @Override public SettingsEditor<? extends RunConfiguration> getConfigurationEditor() { return new DemoSettingsEditor(); } @Override public void checkConfiguration() { } @Nullable @Override public RunProfileState getState(@NotNull Executor executor, @NotNull ExecutionEnvironment executionEnvironment) { return new CommandLineState(executionEnvironment) { @NotNull @Override protected ProcessHandler startProcess() throws ExecutionException { GeneralCommandLine commandLine = new GeneralCommandLine(getOptions().getScriptName()); OSProcessHandler processHandler = ProcessHandlerFactory.getInstance().createColoredProcessHandler(commandLine); ProcessTerminatedListener.attach(processHandler); return processHandler; } }; } }

Create and Implement Run Configuration UI Form

Make sure UI Designer plugin is enabled.

Create a new UI form that defines, how an inner part of the new Run Configuration should look like.

Default Run Configuration will be looking like this:

Default Run Configuration Look

Bind the UI Form

The UI Form should be bound with a Java class responsible for handling UI components logic.

// Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. package org.jetbrains.sdk.runConfiguration; import com.intellij.openapi.options.SettingsEditor; import com.intellij.openapi.ui.LabeledComponent; import com.intellij.openapi.ui.TextFieldWithBrowseButton; import org.jetbrains.annotations.NotNull; import javax.swing.*; public class DemoSettingsEditor extends SettingsEditor<DemoRunConfiguration> { private JPanel myPanel; private LabeledComponent<TextFieldWithBrowseButton> myScriptName; @Override protected void resetEditorFrom(DemoRunConfiguration demoRunConfiguration) { myScriptName.getComponent().setText(demoRunConfiguration.getScriptName()); } @Override protected void applyEditorTo(@NotNull DemoRunConfiguration demoRunConfiguration) { demoRunConfiguration.setScriptName(myScriptName.getComponent().getText()); } @NotNull @Override protected JComponent createEditor() { return myPanel; } private void createUIComponents() { myScriptName = new LabeledComponent<>(); myScriptName.setComponent(new TextFieldWithBrowseButton()); } }

Compile and Run the Plugin

Refer to Running and Debugging a Plugin.

After going through the steps described above you can create a custom Run Configuration from your plugin.

New Run Configuration Type

Last modified: 26 August 2021