feat: Apply custom standard library path to zls

This commit is contained in:
FalsePattern 2024-06-13 19:14:41 +02:00
parent 35695d0426
commit 4dab46073d
Signed by: falsepattern
GPG key ID: E930CDEC50C50E23
5 changed files with 38 additions and 5 deletions

View file

@ -17,6 +17,11 @@ Changelog structure reference:
## [Unreleased]
### Added
- Project
- Modifying the standard library path now also applies to ZLS
## [15.1.1]
### Fixed

View file

@ -32,11 +32,15 @@ import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Pair;
import com.intellij.ui.JBColor;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBLabel;
import com.intellij.ui.dsl.builder.AlignX;
import lombok.Getter;
import lombok.val;
import javax.swing.JLabel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.nio.file.Path;
import java.util.Optional;
@ -55,10 +59,23 @@ public class ZigProjectSettingsPanel implements MyDisposable {
private final JLabel toolchainVersion = new JLabel();
private final JBCheckBox stdFieldOverride = new JBCheckBox("Override");
private final TextFieldWithBrowseButton pathToStdField = TextFieldUtil.pathToDirectoryTextField(this,
"Path to Standard Library",
() -> {});
{
stdFieldOverride.addChangeListener(e -> {
if (stdFieldOverride.isSelected()) {
pathToStdField.setEnabled(true);
} else {
pathToStdField.setEnabled(false);
updateUI();
}
});
}
private void autodetect(ActionEvent e) {
autodetect();
}
@ -83,8 +100,12 @@ public class ZigProjectSettingsPanel implements MyDisposable {
pathToToolchain.setText(Optional.ofNullable(value.getToolchainHomeDirectory())
.orElse(""));
stdFieldOverride.setSelected(value.overrideStdPath);
pathToStdField.setText(Optional.ofNullable(value.getExplicitPathToStd()).orElse(""));
pathToStdField.setEnabled(value.overrideStdPath);
updateUI();
}
@ -98,7 +119,11 @@ public class ZigProjectSettingsPanel implements MyDisposable {
r.button("Autodetect", $f(this::autodetect));
});
p2.cell("Toolchain version", toolchainVersion);
p2.cell("Standard library location", pathToStdField, AlignX.FILL);
p2.cell("Override standard library path", stdFieldOverride);
p2.row("Standard library location", row -> {
row.cell(pathToStdField).resizableColumn().align(AlignX.FILL);
row.cell(stdFieldOverride);
});
});
}
@ -126,6 +151,7 @@ public class ZigProjectSettingsPanel implements MyDisposable {
toolchainVersion.setText(StringUtil.orEmpty(zigVersion));
toolchainVersion.setForeground(JBColor.foreground());
if (!stdFieldOverride.isSelected())
pathToStdField.setText(StringUtil.orEmpty(stdPath));
});
}

View file

@ -31,11 +31,12 @@ import java.nio.file.Path;
@AllArgsConstructor
@NoArgsConstructor
public class ZigProjectSettings {
public boolean overrideStdPath;
public String explicitPathToStd;
public String toolchainHomeDirectory;
public ZigProjectSettings(String explicitPathToStd, AbstractZigToolchain toolchain) {
this(explicitPathToStd, (String)null);
this(true, explicitPathToStd, (String)null);
setToolchain(toolchain);
}

View file

@ -41,6 +41,7 @@ public final class ZigProjectSettingsService extends AbstractZigProjectSettingsS
public boolean isModified(ZigProjectSettings otherData) {
val myData = getState();
return !Objects.equals(myData.toolchainHomeDirectory, otherData.toolchainHomeDirectory) ||
!Objects.equals(myData.explicitPathToStd, otherData.explicitPathToStd);
!Objects.equals(myData.explicitPathToStd, otherData.explicitPathToStd) ||
!Objects.equals(myData.overrideStdPath, otherData.overrideStdPath);
}
}

View file

@ -35,6 +35,6 @@ public class ToolchainZLSConfigProvider implements ZLSConfigProvider {
return;
val projectDir = ProjectUtil.guessProjectDir(project);
val env = toolchain.zig().getEnv(projectDir == null ? Path.of(".") : projectDir.toNioPath());
env.ifPresent(e -> builder.zig_exe_path(e.zigExecutable()).zig_lib_path(e.libDirectory()));
env.ifPresent(e -> builder.zig_exe_path(e.zigExecutable()).zig_lib_path(state.overrideStdPath ? Path.of(state.explicitPathToStd).getParent().toString() : e.libDirectory()));
}
}