运行配置教程
大约 3 分钟
运行配置教程
该教程的主要内容是如何实现和注册一个简单的运行配置 ,运行配置 可以用来执行跟编程语言有关的内部或外部扩展程序。例如最常用的 Run/Debug Configuration
以下代码来源于示例插件:run_configuration
注册 ConfigurationType
在plugin.xml 里配置如下内容:
<extensions defaultExtensionNs="com.intellij">
<configurationType
implementation="org.jetbrains.sdk.runConfiguration.DemoRunConfigurationType"/>
</extensions>
实现 ConfigurationType
第一步:新建ConfigurationType的实现类:
// Copyright 2000-2022 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)};
}
}
实现 ConfigurationFactory
ConfigurationFactory 负责创建 运行配置
// Copyright 2000-2022 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;
}
}
存储配置:继承 RunConfigurationOptions 来实现对应的配置选项,
// Copyright 2000-2022 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);
}
}
实现运行配置
提示
一般情况下,新建运行配置 继承RunConfigurationBase 就可以。如果你需要实现特殊的外部配置规则或 I/O ,就需要实现 RunConfiguration
// Copyright 2000-2022 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;
}
};
}
}
创建 运行配置 的UI表单
确认安装并启用了 UI Designer 插件
创建一个UI表单
默认的运行配置表单显示如下图:
绑定UI表单
UI表单会绑定到一个 Java类上,用来处理表单组件的逻辑
// Copyright 2000-2022 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());
}
}
编译、运行插件
运行该插件,会出现自己定义的 运行配置
Loading...