feat: Better configurability

This commit is contained in:
FalsePattern 2024-03-10 17:09:48 +01:00
parent 85bd68393c
commit ec5c07c0a1
Signed by: falsepattern
GPG key ID: E930CDEC50C50E23
9 changed files with 135 additions and 37 deletions

View file

@ -28,13 +28,13 @@ import java.nio.file.Path;
public class ToolchainZLSConfigProvider implements ZLSConfigProvider { public class ToolchainZLSConfigProvider implements ZLSConfigProvider {
@Override @Override
public @NotNull ZLSConfig getEnvironment(Project project) { public void getEnvironment(Project project, ZLSConfig.ZLSConfigBuilder builder) {
val projectSettings = ZigProjectSettingsService.getInstance(project); val projectSettings = ZigProjectSettingsService.getInstance(project);
val toolchain = projectSettings.getToolchain(); val toolchain = projectSettings.getToolchain();
if (toolchain == null) if (toolchain == null)
return ZLSConfig.EMPTY; return;
val projectDir = ProjectUtil.guessProjectDir(project); val projectDir = ProjectUtil.guessProjectDir(project);
val env = toolchain.zig().getEnv(projectDir == null ? Path.of(".") : projectDir.toNioPath()); val env = toolchain.zig().getEnv(projectDir == null ? Path.of(".") : projectDir.toNioPath());
return env.map(e -> new ZLSConfig(e.zigExecutable(), e.libDirectory())).orElse(ZLSConfig.EMPTY); env.ifPresent(e -> builder.zig_exe_path(e.zigExecutable()).zig_lib_path(e.libDirectory()));
} }
} }

View file

@ -16,30 +16,17 @@
package com.falsepattern.zigbrains.zig.environment; package com.falsepattern.zigbrains.zig.environment;
import com.google.gson.JsonObject; import lombok.Builder;
import com.google.gson.annotations.SerializedName; import lombok.With;
import lombok.val;
import org.jetbrains.annotations.NotNull;
import java.util.Optional;
public record ZLSConfig(@NotNull Optional<String> zigExePath, @With
@NotNull Optional<String> zigLibPath) { @Builder
public ZLSConfig(String zigExePath, String zigLibPath) { public record ZLSConfig(String zig_exe_path,
this(Optional.ofNullable(zigExePath), Optional.ofNullable(zigLibPath)); String zig_lib_path,
} Boolean enable_build_on_save,
String build_on_save_step,
public ZLSConfig overrideWith(ZLSConfig other) { Boolean dangerous_comptime_experiments_do_not_enable,
return new ZLSConfig(other.zigExePath.or(() -> zigExePath), Boolean highlight_global_var_declarations
other.zigLibPath.or(() -> zigLibPath)); ) {
}
public static final ZLSConfig EMPTY = new ZLSConfig(Optional.empty(), Optional.empty());
public JsonObject toJson() {
val result = new JsonObject();
zigExePath.ifPresent(s -> result.addProperty("zig_exe_path", s));
zigLibPath.ifPresent(s -> result.addProperty("zig_lib_path", s));
return result;
}
} }

View file

@ -18,17 +18,20 @@ package com.falsepattern.zigbrains.zig.environment;
import com.intellij.openapi.extensions.ExtensionPointName; import com.intellij.openapi.extensions.ExtensionPointName;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import lombok.val;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
public interface ZLSConfigProvider { public interface ZLSConfigProvider {
ExtensionPointName<ZLSConfigProvider> EXTENSION_POINT_NAME = ExtensionPointName.create("com.falsepattern.zigbrains.zlsConfigProvider"); ExtensionPointName<ZLSConfigProvider> EXTENSION_POINT_NAME = ExtensionPointName.create("com.falsepattern.zigbrains.zlsConfigProvider");
static @NotNull ZLSConfig findEnvironment(Project project) { static @NotNull ZLSConfig findEnvironment(Project project) {
return EXTENSION_POINT_NAME.getExtensionList() var config = ZLSConfig.builder();
.stream() val extensions = EXTENSION_POINT_NAME.getExtensionList();
.map(it -> it.getEnvironment(project)) for (val extension: extensions) {
.reduce(ZLSConfig.EMPTY, ZLSConfig::overrideWith); extension.getEnvironment(project, config);
}
return config.build();
} }
@NotNull ZLSConfig getEnvironment(Project project); void getEnvironment(Project project, ZLSConfig.ZLSConfigBuilder builder);
} }

View file

@ -74,14 +74,14 @@ public class ZLSStartupActivity implements ProjectActivity {
try { try {
val tmpFile = Files.createTempFile("zigbrains-zls-autoconf", ".json"); val tmpFile = Files.createTempFile("zigbrains-zls-autoconf", ".json");
val config = ZLSConfigProvider.findEnvironment(project); val config = ZLSConfigProvider.findEnvironment(project);
if (config.zigExePath().isEmpty() && config.zigLibPath().isEmpty()) { if (config.zig_exe_path().isEmpty() && config.zig_lib_path().isEmpty()) {
Notifications.Bus.notify(new Notification("ZigBrains.ZLS", "(ZLS) Failed to detect zig path from project toolchain", NotificationType.WARNING)); Notifications.Bus.notify(new Notification("ZigBrains.ZLS", "(ZLS) Failed to detect zig path from project toolchain", NotificationType.WARNING));
configOK = false; configOK = false;
break blk; break blk;
} }
try (val writer = Files.newBufferedWriter(tmpFile)) { try (val writer = Files.newBufferedWriter(tmpFile)) {
val gson = new Gson(); val gson = new Gson();
gson.toJson(config.toJson(), writer); gson.toJson(config, writer);
} }
configPath = tmpFile.toAbsolutePath().toString(); configPath = tmpFile.toAbsolutePath().toString();
} catch (IOException e) { } catch (IOException e) {

View file

@ -21,6 +21,7 @@ import com.intellij.openapi.ui.TextBrowseFolderListener;
import com.intellij.openapi.ui.TextFieldWithBrowseButton; import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.ui.components.JBCheckBox; import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBLabel; import com.intellij.ui.components.JBLabel;
import com.intellij.ui.components.JBTextField;
import com.intellij.util.ui.FormBuilder; import com.intellij.util.ui.FormBuilder;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
@ -32,17 +33,29 @@ public class ZLSSettingsComponent {
private final TextFieldWithBrowseButton zlsPathText = new TextFieldWithBrowseButton(); private final TextFieldWithBrowseButton zlsPathText = new TextFieldWithBrowseButton();
private final TextFieldWithBrowseButton zlsConfigPathText = new TextFieldWithBrowseButton(); private final TextFieldWithBrowseButton zlsConfigPathText = new TextFieldWithBrowseButton();
private final JBCheckBox asyncFoldingCheckBox = new JBCheckBox(); private final JBCheckBox asyncFoldingCheckBox = new JBCheckBox();
private final JBCheckBox debugCheckBox = new JBCheckBox();
private final JBCheckBox messageTraceCheckBox = new JBCheckBox();
private final JBCheckBox increaseTimeouts = new JBCheckBox(); private final JBCheckBox increaseTimeouts = new JBCheckBox();
private final JBCheckBox buildOnSave = new JBCheckBox();
private final JBTextField buildOnSaveStep = new JBTextField();
private final JBCheckBox highlightGlobalVarDeclarations = new JBCheckBox();
private final JBCheckBox dangerousComptimeExperimentsDoNotEnable = new JBCheckBox();
private final JBCheckBox messageTraceCheckBox = new JBCheckBox();
private final JBCheckBox debugCheckBox = new JBCheckBox();
private final JButton autodetectZls = new JButton("Autodetect"); private final JButton autodetectZls = new JButton("Autodetect");
{
buildOnSave.setToolTipText("Whether to enable build-on-save diagnostics");
buildOnSaveStep.setToolTipText("Select which step should be executed on build-on-save");
highlightGlobalVarDeclarations.setToolTipText("Whether to highlight global var declarations");
dangerousComptimeExperimentsDoNotEnable.setToolTipText("Whether to use the comptime interpreter");
}
public ZLSSettingsComponent() { public ZLSSettingsComponent() {
zlsPathText.addBrowseFolderListener( zlsPathText.addBrowseFolderListener(
new TextBrowseFolderListener(new FileChooserDescriptor(true, false, false, false, false, false))); new TextBrowseFolderListener(new FileChooserDescriptor(true, false, false, false, false, false)));
myMainPanel = FormBuilder.createFormBuilder() myMainPanel = FormBuilder.createFormBuilder()
.addComponent(new JBLabel("ZLS settings")) .addComponent(new JBLabel("ZLS launch settings"))
.addVerticalGap(10) .addVerticalGap(10)
.addLabeledComponent(new JBLabel("ZLS path: "), zlsPathText, 1, false) .addLabeledComponent(new JBLabel("ZLS path: "), zlsPathText, 1, false)
.addComponent(autodetectZls) .addComponent(autodetectZls)
@ -52,6 +65,13 @@ public class ZLSSettingsComponent {
.addLabeledComponent(new JBLabel("Asynchronous code folding ranges: "), .addLabeledComponent(new JBLabel("Asynchronous code folding ranges: "),
asyncFoldingCheckBox, 1, false) asyncFoldingCheckBox, 1, false)
.addSeparator() .addSeparator()
.addComponent(new JBLabel("ZLS configuration"))
.addVerticalGap(10)
.addLabeledComponent("Build on save", buildOnSave)
.addLabeledComponent("Build on save step", buildOnSaveStep)
.addLabeledComponent("Highlight global variable declarations", highlightGlobalVarDeclarations)
.addLabeledComponent("Dangerous comptime experiments (do not enable)", dangerousComptimeExperimentsDoNotEnable)
.addSeparator()
.addComponent(new JBLabel( .addComponent(new JBLabel(
"Developer settings (only usable when the IDE was launched with " + "Developer settings (only usable when the IDE was launched with " +
"the runIDE gradle task in ZigBrains!)")) "the runIDE gradle task in ZigBrains!)"))
@ -119,4 +139,36 @@ public class ZLSSettingsComponent {
public void setMessageTrace(boolean state) { public void setMessageTrace(boolean state) {
messageTraceCheckBox.setSelected(state); messageTraceCheckBox.setSelected(state);
} }
public boolean getBuildOnSave() {
return buildOnSave.isSelected();
}
public void setBuildOnSave(boolean state) {
buildOnSave.setSelected(state);
}
public String getBuildOnSaveStep() {
return buildOnSaveStep.getText();
}
public void setBuildOnSaveStep(String value) {
buildOnSaveStep.setText(value);
}
public boolean getHighlightGlobalVarDeclarations() {
return highlightGlobalVarDeclarations.isSelected();
}
public void setHighlightGlobalVarDeclarations(boolean state) {
highlightGlobalVarDeclarations.setSelected(state);
}
public boolean getDangerousComptimeExperimentsDoNotEnable() {
return dangerousComptimeExperimentsDoNotEnable.isSelected();
}
public void setDangerousComptimeExperimentsDoNotEnable(boolean state) {
dangerousComptimeExperimentsDoNotEnable.setSelected(state);
}
} }

View file

@ -0,0 +1,33 @@
/*
* Copyright 2023-2024 FalsePattern
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.falsepattern.zigbrains.zig.settings;
import com.falsepattern.zigbrains.zig.environment.ZLSConfig;
import com.falsepattern.zigbrains.zig.environment.ZLSConfigProvider;
import com.intellij.openapi.project.Project;
import lombok.val;
public class ZLSSettingsConfigProvider implements ZLSConfigProvider {
@Override
public void getEnvironment(Project project, ZLSConfig.ZLSConfigBuilder builder) {
val state = ZLSSettingsState.getInstance(project);
builder.enable_build_on_save(state.buildOnSave);
builder.build_on_save_step(state.buildOnSaveStep);
builder.highlight_global_var_declarations(state.highlightGlobalVarDeclarations);
builder.dangerous_comptime_experiments_do_not_enable(state.dangerousComptimeExperimentsDoNotEnable);
}
}

View file

@ -58,6 +58,10 @@ public class ZLSSettingsConfigurable implements Configurable {
modified |= settings.debug != appSettingsComponent.getDebug(); modified |= settings.debug != appSettingsComponent.getDebug();
modified |= settings.messageTrace != appSettingsComponent.getMessageTrace(); modified |= settings.messageTrace != appSettingsComponent.getMessageTrace();
modified |= settings.increaseTimeouts != appSettingsComponent.getIncreaseTimeouts(); modified |= settings.increaseTimeouts != appSettingsComponent.getIncreaseTimeouts();
modified |= settings.buildOnSave != appSettingsComponent.getBuildOnSave();
modified |= !settings.buildOnSaveStep.equals(appSettingsComponent.getBuildOnSaveStep());
modified |= settings.highlightGlobalVarDeclarations != appSettingsComponent.getHighlightGlobalVarDeclarations();
modified |= settings.dangerousComptimeExperimentsDoNotEnable != appSettingsComponent.getDangerousComptimeExperimentsDoNotEnable();
return modified; return modified;
} }
@ -71,6 +75,11 @@ public class ZLSSettingsConfigurable implements Configurable {
settings.debug = appSettingsComponent.getDebug(); settings.debug = appSettingsComponent.getDebug();
settings.messageTrace = appSettingsComponent.getMessageTrace(); settings.messageTrace = appSettingsComponent.getMessageTrace();
settings.increaseTimeouts = appSettingsComponent.getIncreaseTimeouts(); settings.increaseTimeouts = appSettingsComponent.getIncreaseTimeouts();
settings.buildOnSave = appSettingsComponent.getBuildOnSave();
settings.buildOnSaveStep = appSettingsComponent.getBuildOnSaveStep();
settings.highlightGlobalVarDeclarations = appSettingsComponent.getHighlightGlobalVarDeclarations();
settings.dangerousComptimeExperimentsDoNotEnable = appSettingsComponent.getDangerousComptimeExperimentsDoNotEnable();
if (reloadZLS) { if (reloadZLS) {
ZLSStartupActivity.initZLS(project); ZLSStartupActivity.initZLS(project);
} }
@ -86,6 +95,11 @@ public class ZLSSettingsConfigurable implements Configurable {
appSettingsComponent.setMessageTrace(settings.messageTrace); appSettingsComponent.setMessageTrace(settings.messageTrace);
appSettingsComponent.setIncreaseTimeouts(settings.increaseTimeouts); appSettingsComponent.setIncreaseTimeouts(settings.increaseTimeouts);
appSettingsComponent.setAsyncFolding(settings.asyncFolding); appSettingsComponent.setAsyncFolding(settings.asyncFolding);
appSettingsComponent.setBuildOnSave(settings.buildOnSave);
appSettingsComponent.setBuildOnSaveStep(settings.buildOnSaveStep);
appSettingsComponent.setHighlightGlobalVarDeclarations(settings.highlightGlobalVarDeclarations);
appSettingsComponent.setDangerousComptimeExperimentsDoNotEnable(settings.dangerousComptimeExperimentsDoNotEnable);
} }
@Override @Override

View file

@ -42,6 +42,11 @@ public final class ZLSSettingsState implements PersistentStateComponent<ZLSSetti
public boolean debug = false; public boolean debug = false;
public boolean messageTrace = false; public boolean messageTrace = false;
public boolean buildOnSave = false;
public String buildOnSaveStep = "install";
public boolean dangerousComptimeExperimentsDoNotEnable = false;
public boolean highlightGlobalVarDeclarations = false;
public static Optional<String> executablePathFinder(String exe) { public static Optional<String> executablePathFinder(String exe) {
var exeName = SystemInfo.isWindows ? exe + ".exe" : exe; var exeName = SystemInfo.isWindows ? exe + ".exe" : exe;
var PATH = System.getenv("PATH").split(File.pathSeparator); var PATH = System.getenv("PATH").split(File.pathSeparator);

View file

@ -110,6 +110,10 @@
id="ZigBrains.ZLS"/> id="ZigBrains.ZLS"/>
</extensions> </extensions>
<extensions defaultExtensionNs="com.falsepattern.zigbrains">
<zlsConfigProvider implementation="com.falsepattern.zigbrains.zig.settings.ZLSSettingsConfigProvider"/>
</extensions>
<actions> <actions>