diff --git a/src/main/java/org/c3lang/intellij/C3BuildRunConfiguration.java b/src/main/java/org/c3lang/intellij/C3BuildRunConfiguration.java index 985c377..50fe1af 100644 --- a/src/main/java/org/c3lang/intellij/C3BuildRunConfiguration.java +++ b/src/main/java/org/c3lang/intellij/C3BuildRunConfiguration.java @@ -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() { } @@ -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; diff --git a/src/main/java/org/c3lang/intellij/C3BuildRunConfigurationOptions.java b/src/main/java/org/c3lang/intellij/C3BuildRunConfigurationOptions.java index 9a84f23..2c1adc8 100644 --- a/src/main/java/org/c3lang/intellij/C3BuildRunConfigurationOptions.java +++ b/src/main/java/org/c3lang/intellij/C3BuildRunConfigurationOptions.java @@ -4,7 +4,30 @@ import com.intellij.openapi.components.StoredProperty; -public class C3BuildRunConfigurationOptions extends RunConfigurationOptions -{ +public class C3BuildRunConfigurationOptions extends RunConfigurationOptions { + private final StoredProperty myWorkingDirectory = + string("").provideDelegate(this, "workingDirectory"); + private final StoredProperty 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); + } } diff --git a/src/main/java/org/c3lang/intellij/C3BuildRunEditor.java b/src/main/java/org/c3lang/intellij/C3BuildRunEditor.java index 5a743d1..132c3a5 100644 --- a/src/main/java/org/c3lang/intellij/C3BuildRunEditor.java +++ b/src/main/java/org/c3lang/intellij/C3BuildRunEditor.java @@ -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.*; @@ -10,14 +13,45 @@ public class C3BuildRunEditor extends SettingsEditor { 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() @@ -25,8 +59,16 @@ public class C3BuildRunEditor extends SettingsEditor return panel; } - private void createUIComponents() - { + private void createUIComponents() { + workingDirectoryField = new TextFieldWithBrowseButton(); + workingDirectoryField.addBrowseFolderListener( + "Select Working Directory", + null, + null, + FileChooserDescriptorFactory.createSingleFolderDescriptor() + ); + + argsField = new JTextField(); } } diff --git a/src/main/java/org/c3lang/intellij/C3CompileRunConfiguration.java b/src/main/java/org/c3lang/intellij/C3CompileRunConfiguration.java index 7785a49..ae91f3b 100644 --- a/src/main/java/org/c3lang/intellij/C3CompileRunConfiguration.java +++ b/src/main/java/org/c3lang/intellij/C3CompileRunConfiguration.java @@ -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, @@ -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; diff --git a/src/main/java/org/c3lang/intellij/C3CompileRunConfigurationOptions.java b/src/main/java/org/c3lang/intellij/C3CompileRunConfigurationOptions.java index f5ba003..4b636d9 100644 --- a/src/main/java/org/c3lang/intellij/C3CompileRunConfigurationOptions.java +++ b/src/main/java/org/c3lang/intellij/C3CompileRunConfigurationOptions.java @@ -6,15 +6,42 @@ public class C3CompileRunConfigurationOptions extends RunConfigurationOptions { - private final StoredProperty fileName = string("").provideDelegate(this, "filename"); + private final StoredProperty myWorkingDirectory = + string("").provideDelegate(this, "workingDirectory"); - public String getSourceName() + private final StoredProperty myArgs = + string("").provideDelegate(this, "args"); + + private final StoredProperty 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); } } diff --git a/src/main/java/org/c3lang/intellij/C3CompileRunEditor.form b/src/main/java/org/c3lang/intellij/C3CompileRunEditor.form index 873d1fc..8d669b0 100644 --- a/src/main/java/org/c3lang/intellij/C3CompileRunEditor.form +++ b/src/main/java/org/c3lang/intellij/C3CompileRunEditor.form @@ -7,26 +7,6 @@ - - - - - - - - - - - - - - - - - - - - - + diff --git a/src/main/java/org/c3lang/intellij/C3CompileRunEditor.java b/src/main/java/org/c3lang/intellij/C3CompileRunEditor.java index e2b2ecb..b794f3d 100644 --- a/src/main/java/org/c3lang/intellij/C3CompileRunEditor.java +++ b/src/main/java/org/c3lang/intellij/C3CompileRunEditor.java @@ -1,18 +1,17 @@ package org.c3lang.intellij; import com.intellij.openapi.fileChooser.FileChooserDescriptor; +import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory; import com.intellij.openapi.options.ConfigurationException; import com.intellij.openapi.options.SettingsEditor; import com.intellij.openapi.ui.TextBrowseFolderListener; import com.intellij.openapi.ui.TextFieldWithBrowseButton; import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.ui.components.JBTextField; -import com.jetbrains.JBRFileDialog; +import com.intellij.util.ui.FormBuilder; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; -import java.nio.file.Files; import java.util.Objects; /** @@ -23,19 +22,54 @@ public class C3CompileRunEditor extends SettingsEditor { JPanel panel; - TextFieldWithBrowseButton fileSource; + TextFieldWithBrowseButton sourceFileField; + TextFieldWithBrowseButton workingDirectoryField; + JTextField argsField; - @Override protected void resetEditorFrom(@NotNull C3CompileRunConfiguration c3CompileRunConfiguration) + public C3CompileRunEditor() { + createUIComponents(); + + panel = FormBuilder.createFormBuilder() + .addLabeledComponent("C3 Source file", sourceFileField) + .addLabeledComponent("Working directory", workingDirectoryField) + .addTooltip("This is the directory where the output executable will be produced.") + .addLabeledComponent("Additional arguments", argsField) + .getPanel(); + } + + @Override protected void resetEditorFrom(@NotNull C3CompileRunConfiguration configuration) { - fileSource.setText(c3CompileRunConfiguration.getSourceName()); + // 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 and sourceFileField to their stored values + argsField.setText(configuration.getArgs()); + sourceFileField.setText(configuration.getSourceFile()); } - @Override protected void applyEditorTo(@NotNull C3CompileRunConfiguration c3CompileRunConfiguration) throws - ConfigurationException + @Override protected void applyEditorTo(@NotNull C3CompileRunConfiguration configuration) throws + ConfigurationException { - System.out.println("Set source name to " + fileSource.getText()); - c3CompileRunConfiguration.setSourceName(fileSource.getText()); - System.out.println("Name is now " + c3CompileRunConfiguration.getSourceName()); + if (sourceFileField.getText().isEmpty()) { + throw new ConfigurationException("You must provide a source file."); + } + + if (workingDirectoryField.getText().isEmpty()) { + throw new ConfigurationException("You must provide a working directory."); + } + + configuration.setWorkingDirectory(workingDirectoryField.getText()); + configuration.setArgs(argsField.getText()); + configuration.setSourceFile(sourceFileField.getText()); } @Override protected @NotNull JComponent createEditor() @@ -45,7 +79,17 @@ public class C3CompileRunEditor extends SettingsEditor { - public String sdk = ""; + public String sdk = "c3c"; public static C3SettingsState getInstance() {