Skip to content

Commit

Permalink
Implement BuildRun and CompileRun configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
yui019 authored and lerno committed Jan 3, 2025
1 parent 079b9db commit 7bad984
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 51 deletions.
32 changes: 32 additions & 0 deletions src/main/java/org/c3lang/intellij/C3BuildRunConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ protected C3BuildRunConfiguration(Project project, ConfigurationFactory factory,
return (C3BuildRunConfigurationOptions)super.getOptions();
}

public String getWorkingDirectory()
{
return getOptions().getWorkingDirectory();
}

public void setWorkingDirectory(String workingDirectory)
{
getOptions().setWorkingDirectory(workingDirectory);
}

public String getArgs()
{
return getOptions().getArgs();
}

public void setArgs(String args)
{
getOptions().setArgs(args);
}

@Override public void checkConfiguration()
{
}
Expand All @@ -43,6 +63,18 @@ protected C3BuildRunConfiguration(Project project, ConfigurationFactory factory,
{
String sdk = C3SettingsState.getInstance().sdk;
GeneralCommandLine commandLine = new GeneralCommandLine(sdk, "run");

// I couldn't just add the whole args string here because the GeneralCommandLine class adds quotes
// around parameters with spaces (so it would look like this: c3c run "--param value" which isn't valid
// syntax).
// Instead, I'm splitting the args string by spaces and adding that array.
if (getArgs() != null) {
commandLine.addParameters(getArgs().split(" "));
}

String workingDirectory = getWorkingDirectory();
commandLine.setWorkDirectory(workingDirectory);

OSProcessHandler processHandler = ProcessHandlerFactory.getInstance().createColoredProcessHandler(commandLine);
ProcessTerminatedListener.attach(processHandler);
return processHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,30 @@
import com.intellij.openapi.components.StoredProperty;


public class C3BuildRunConfigurationOptions extends RunConfigurationOptions
{
public class C3BuildRunConfigurationOptions extends RunConfigurationOptions {
private final StoredProperty<String> myWorkingDirectory =
string("").provideDelegate(this, "workingDirectory");

private final StoredProperty<String> myArgs =
string("").provideDelegate(this, "args");

public String getWorkingDirectory()
{
return myWorkingDirectory.getValue(this);
}

public void setWorkingDirectory(String workingDirectory)
{
myWorkingDirectory.setValue(this, workingDirectory);
}

public String getArgs()
{
return myArgs.getValue(this);
}

public void setArgs(String args)
{
myArgs.setValue(this, args);
}
}
48 changes: 45 additions & 3 deletions src/main/java/org/c3lang/intellij/C3BuildRunEditor.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package org.c3lang.intellij;

import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.options.ConfigurationException;
import com.intellij.openapi.options.SettingsEditor;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.util.ui.FormBuilder;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
Expand All @@ -10,23 +13,62 @@
public class C3BuildRunEditor extends SettingsEditor<C3BuildRunConfiguration>
{
JPanel panel;
TextFieldWithBrowseButton workingDirectoryField;
JTextField argsField;

public C3BuildRunEditor() {
createUIComponents();

panel = FormBuilder.createFormBuilder()
.addLabeledComponent("Working directory", workingDirectoryField)
.addLabeledComponent("Additional arguments", argsField)
.getPanel();
}

@Override protected void resetEditorFrom(@NotNull C3BuildRunConfiguration configuration)
{
// This function is called each time the run configuration form is shown,
// i.e. both when its first created and when it's being edited

if (configuration.getWorkingDirectory().isEmpty()) {
// By default, fill the workingDirectory field with the project's base path
String projectDirectory = configuration.getProject().getBasePath();
workingDirectoryField.setText(projectDirectory);
} else {
// Otherwise (when editing the configuration), set its value to the one that was stored
workingDirectoryField.setText(configuration.getWorkingDirectory());
}

// Also set argsField to its stored value
argsField.setText(configuration.getArgs());
}

@Override protected void applyEditorTo(@NotNull C3BuildRunConfiguration configuration) throws
ConfigurationException
ConfigurationException
{
if (workingDirectoryField.getText().isEmpty()) {
throw new ConfigurationException("You must provide a working directory.");
}

configuration.setWorkingDirectory(workingDirectoryField.getText());
configuration.setArgs(argsField.getText());
}

@Override protected @NotNull JComponent createEditor()
{
return panel;
}

private void createUIComponents()
{
private void createUIComponents() {
workingDirectoryField = new TextFieldWithBrowseButton();
workingDirectoryField.addBrowseFolderListener(
"Select Working Directory",
null,
null,
FileChooserDescriptorFactory.createSingleFolderDescriptor()
);

argsField = new JTextField();
}
}

44 changes: 38 additions & 6 deletions src/main/java/org/c3lang/intellij/C3CompileRunConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,38 @@ protected C3CompileRunConfiguration(Project project, ConfigurationFactory factor
return (C3CompileRunConfigurationOptions)super.getOptions();
}

@Override public void checkConfiguration()
public String getWorkingDirectory()
{
return getOptions().getWorkingDirectory();
}

public String getSourceName()
public void setWorkingDirectory(String workingDirectory)
{
return getOptions().getSourceName();
getOptions().setWorkingDirectory(workingDirectory);
}

public void setSourceName(String string)
public String getArgs()
{
return getOptions().getArgs();
}

public void setArgs(String args)
{
getOptions().setArgs(args);
}

public String getSourceFile()
{
return getOptions().getSourceFile();
}

public void setSourceFile(String file)
{
getOptions().setSourceFile(file);
}

@Override public void checkConfiguration()
{
getOptions().setSourceName(string);
}

@Override public @Nullable RunProfileState getState(@NotNull Executor executor,
Expand All @@ -52,7 +72,19 @@ public void setSourceName(String string)
@Override protected @NotNull ProcessHandler startProcess() throws ExecutionException
{
String sdk = C3SettingsState.getInstance().sdk;
GeneralCommandLine commandLine = new GeneralCommandLine(sdk, "compile-run", getSourceName());
GeneralCommandLine commandLine = new GeneralCommandLine(sdk, "compile-run", getSourceFile());

// I couldn't just add the whole args string here because the GeneralCommandLine class adds quotes
// around parameters with spaces (so it would look like this: c3c run "--param value" which isn't valid
// syntax).
// Instead, I'm splitting the args string by spaces and adding that array.
if (getArgs() != null) {
commandLine.addParameters(getArgs().split(" "));
}

String workingDirectory = getWorkingDirectory();
commandLine.setWorkDirectory(workingDirectory);

OSProcessHandler processHandler = ProcessHandlerFactory.getInstance().createColoredProcessHandler(commandLine);
ProcessTerminatedListener.attach(processHandler);
return processHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,42 @@

public class C3CompileRunConfigurationOptions extends RunConfigurationOptions
{
private final StoredProperty<String> fileName = string("").provideDelegate(this, "filename");
private final StoredProperty<String> myWorkingDirectory =
string("").provideDelegate(this, "workingDirectory");

public String getSourceName()
private final StoredProperty<String> myArgs =
string("").provideDelegate(this, "args");

private final StoredProperty<String> mySourceFile =
string("").provideDelegate(this, "sourceFile");

public String getWorkingDirectory()
{
return myWorkingDirectory.getValue(this);
}

public void setWorkingDirectory(String workingDirectory)
{
myWorkingDirectory.setValue(this, workingDirectory);
}

public String getArgs()
{
return myArgs.getValue(this);
}

public void setArgs(String args)
{
myArgs.setValue(this, args);
}

public String getSourceFile()
{
return fileName.getValue(this);
return mySourceFile.getValue(this);
}

public void setSourceName(String file)
public void setSourceFile(String file)
{
fileName.setValue(this, file);
mySourceFile.setValue(this, file);
}
}
22 changes: 1 addition & 21 deletions src/main/java/org/c3lang/intellij/C3CompileRunEditor.form
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,6 @@
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="28662" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="C3 Source File:"/>
</properties>
</component>
<vspacer id="f2dd4">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="5598c" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="fileSource" custom-create="true">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="3" hsize-policy="6" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
</children>
<children/>
</grid>
</form>
Loading

0 comments on commit 7bad984

Please sign in to comment.