fix: rare error in ZLSLanguageServerFactory.isEnabled

This commit is contained in:
FalsePattern 2024-10-25 13:30:06 +02:00
parent efbcd55875
commit 2dec61ffe8
Signed by: falsepattern
GPG key ID: E930CDEC50C50E23
2 changed files with 13 additions and 12 deletions

View file

@ -34,7 +34,8 @@ Changelog structure reference:
### Fixed ### Fixed
- LSP - LSP
- No more error spam when zig or zls binary is missing. - No more error spam when zig or zls binary is missing
- Rare error when checking LSP presence
## [18.0.0] ## [18.0.0]

View file

@ -50,14 +50,14 @@ public class ZLSStreamConnectionProvider extends OSProcessStreamConnectionProvid
setCommandLine(commandLine); setCommandLine(commandLine);
} }
public static List<String> doGetCommand(Project project, boolean warn) { public static List<String> doGetCommand(Project project, boolean safe) {
var svc = ZLSProjectSettingsService.getInstance(project); var svc = ZLSProjectSettingsService.getInstance(project);
val state = svc.getState(); val state = svc.getState();
var zlsPath = state.zlsPath; var zlsPath = state.zlsPath;
if (StringUtil.isEmpty(zlsPath)) { if (StringUtil.isEmpty(zlsPath)) {
zlsPath = com.falsepattern.zigbrains.common.util.FileUtil.findExecutableOnPATH("zls").map(Path::toString).orElse(null); zlsPath = com.falsepattern.zigbrains.common.util.FileUtil.findExecutableOnPATH("zls").map(Path::toString).orElse(null);
if (zlsPath == null) { if (zlsPath == null) {
if (warn) { if (safe) {
Notifications.Bus.notify( Notifications.Bus.notify(
new Notification("ZigBrains.ZLS", "Could not detect ZLS binary! Please configure it!", new Notification("ZigBrains.ZLS", "Could not detect ZLS binary! Please configure it!",
NotificationType.ERROR)); NotificationType.ERROR));
@ -66,19 +66,19 @@ public class ZLSStreamConnectionProvider extends OSProcessStreamConnectionProvid
} }
state.setZlsPath(zlsPath); state.setZlsPath(zlsPath);
} }
if (!validatePath("ZLS Binary", zlsPath, false, warn)) { if (!validatePath("ZLS Binary", zlsPath, false, safe)) {
return null; return null;
} }
var configPath = state.zlsConfigPath; var configPath = state.zlsConfigPath;
boolean configOK = true; boolean configOK = true;
if (!configPath.isBlank() && !validatePath("ZLS Config", configPath, false, warn)) { if (!configPath.isBlank() && !validatePath("ZLS Config", configPath, false, safe)) {
if (warn) { if (safe) {
Notifications.Bus.notify( Notifications.Bus.notify(
new Notification("ZigBrains.ZLS", "Using default config path.", NotificationType.INFORMATION)); new Notification("ZigBrains.ZLS", "Using default config path.", NotificationType.INFORMATION));
} }
configPath = null; configPath = null;
} }
if (configPath == null || configPath.isBlank()) { if ((configPath == null || configPath.isBlank()) && safe) {
blk: blk:
try { try {
val tmpFile = FileUtil.createTempFile("zigbrains-zls-autoconf", ".json", true).toPath(); val tmpFile = FileUtil.createTempFile("zigbrains-zls-autoconf", ".json", true).toPath();
@ -95,7 +95,7 @@ public class ZLSStreamConnectionProvider extends OSProcessStreamConnectionProvid
} }
configPath = tmpFile.toAbsolutePath().toString(); configPath = tmpFile.toAbsolutePath().toString();
} catch (IOException e) { } catch (IOException e) {
if (warn) { if (safe) {
Notifications.Bus.notify( Notifications.Bus.notify(
new Notification("ZigBrains.ZLS", "Failed to create automatic zls config file", new Notification("ZigBrains.ZLS", "Failed to create automatic zls config file",
NotificationType.WARNING)); NotificationType.WARNING));
@ -140,7 +140,7 @@ public class ZLSStreamConnectionProvider extends OSProcessStreamConnectionProvid
return future; return future;
} }
private static boolean validatePath(String name, String pathTxt, boolean dir, boolean warn) { private static boolean validatePath(String name, String pathTxt, boolean dir, boolean safe) {
if (pathTxt == null || pathTxt.isBlank()) { if (pathTxt == null || pathTxt.isBlank()) {
return false; return false;
} }
@ -148,7 +148,7 @@ public class ZLSStreamConnectionProvider extends OSProcessStreamConnectionProvid
try { try {
path = Path.of(pathTxt); path = Path.of(pathTxt);
} catch (InvalidPathException e) { } catch (InvalidPathException e) {
if (warn) { if (safe) {
Notifications.Bus.notify(new Notification("ZigBrains.ZLS", "No " + name, Notifications.Bus.notify(new Notification("ZigBrains.ZLS", "No " + name,
"Invalid " + name + " at path \"" + pathTxt + "\"", "Invalid " + name + " at path \"" + pathTxt + "\"",
NotificationType.ERROR)); NotificationType.ERROR));
@ -156,7 +156,7 @@ public class ZLSStreamConnectionProvider extends OSProcessStreamConnectionProvid
return false; return false;
} }
if (!Files.exists(path)) { if (!Files.exists(path)) {
if (warn) { if (safe) {
Notifications.Bus.notify(new Notification("ZigBrains.ZLS", "No " + name, Notifications.Bus.notify(new Notification("ZigBrains.ZLS", "No " + name,
"The " + name + " at \"" + pathTxt + "\" doesn't exist!", "The " + name + " at \"" + pathTxt + "\" doesn't exist!",
NotificationType.ERROR)); NotificationType.ERROR));
@ -164,7 +164,7 @@ public class ZLSStreamConnectionProvider extends OSProcessStreamConnectionProvid
return false; return false;
} }
if (Files.isDirectory(path) != dir) { if (Files.isDirectory(path) != dir) {
if (warn) { if (safe) {
Notifications.Bus.notify(new Notification("ZigBrains.ZLS", "No " + name, Notifications.Bus.notify(new Notification("ZigBrains.ZLS", "No " + name,
"The " + name + " at \"" + pathTxt + "\" is a " + "The " + name + " at \"" + pathTxt + "\" is a " +
(Files.isDirectory(path) ? "directory" : "file") + (Files.isDirectory(path) ? "directory" : "file") +