fix: LSP error spam when binaries are missing

This commit is contained in:
FalsePattern 2024-10-25 02:02:22 +02:00
parent 802758af30
commit dd2ccf2387
Signed by: falsepattern
GPG key ID: E930CDEC50C50E23
3 changed files with 48 additions and 26 deletions

View file

@ -17,6 +17,11 @@ Changelog structure reference:
## [Unreleased] ## [Unreleased]
### Fixed
- LSP
- No more error spam when zig or zls binary is missing.
## [18.0.0] ## [18.0.0]
### Added ### Added

View file

@ -41,7 +41,7 @@ public class ZLSLanguageServerFactory implements LanguageServerFactory, Language
@Override @Override
public boolean isEnabled(@NotNull Project project) { public boolean isEnabled(@NotNull Project project) {
return enabled; return enabled && ZLSStreamConnectionProvider.doGetCommand(project, false) != null;
} }
@Override @Override

View file

@ -34,40 +34,48 @@ public class ZLSStreamConnectionProvider extends OSProcessStreamConnectionProvid
public ZLSStreamConnectionProvider(Project project) { public ZLSStreamConnectionProvider(Project project) {
val command = getCommand(project); val command = getCommand(project);
val projectDir = ProjectUtil.guessProjectDir(project); val projectDir = ProjectUtil.guessProjectDir(project);
GeneralCommandLine commandLine; GeneralCommandLine commandLine = null;
try { try {
val cmd = command.get();
if (cmd != null) {
commandLine = new GeneralCommandLine(command.get()); commandLine = new GeneralCommandLine(command.get());
}
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
if (projectDir != null) { if (commandLine != null && projectDir != null) {
commandLine.setWorkDirectory(projectDir.getPath()); commandLine.setWorkDirectory(projectDir.getPath());
} }
setCommandLine(commandLine); setCommandLine(commandLine);
} }
private static List<String> doGetCommand(Project project) { public static List<String> doGetCommand(Project project, boolean warn) {
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) {
Notifications.Bus.notify(new Notification("ZigBrains.ZLS", "Could not detect ZLS binary! Please configure it!", if (warn) {
Notifications.Bus.notify(
new Notification("ZigBrains.ZLS", "Could not detect ZLS binary! Please configure it!",
NotificationType.ERROR)); NotificationType.ERROR));
}
return null; return null;
} }
state.setZlsPath(zlsPath); state.setZlsPath(zlsPath);
} }
if (!validatePath("ZLS Binary", zlsPath, false)) { if (!validatePath("ZLS Binary", zlsPath, false, warn)) {
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)) { if (!configPath.isBlank() && !validatePath("ZLS Config", configPath, false, warn)) {
Notifications.Bus.notify(new Notification("ZigBrains.ZLS", "Using default config path.", if (warn) {
NotificationType.INFORMATION)); Notifications.Bus.notify(
new Notification("ZigBrains.ZLS", "Using default config path.", NotificationType.INFORMATION));
}
configPath = null; configPath = null;
} }
if (configPath == null || configPath.isBlank()) { if (configPath == null || configPath.isBlank()) {
@ -87,8 +95,11 @@ public class ZLSStreamConnectionProvider extends OSProcessStreamConnectionProvid
} }
configPath = tmpFile.toAbsolutePath().toString(); configPath = tmpFile.toAbsolutePath().toString();
} catch (IOException e) { } catch (IOException e) {
Notifications.Bus.notify(new Notification("ZigBrains.ZLS", "Failed to create automatic zls config file", if (warn) {
Notifications.Bus.notify(
new Notification("ZigBrains.ZLS", "Failed to create automatic zls config file",
NotificationType.WARNING)); NotificationType.WARNING));
}
LOG.warn(e); LOG.warn(e);
configOK = false; configOK = false;
} }
@ -121,7 +132,7 @@ public class ZLSStreamConnectionProvider extends OSProcessStreamConnectionProvid
val future = new CompletableFuture<List<String>>(); val future = new CompletableFuture<List<String>>();
ApplicationManager.getApplication().executeOnPooledThread(() -> { ApplicationManager.getApplication().executeOnPooledThread(() -> {
try { try {
future.complete(doGetCommand(project)); future.complete(doGetCommand(project, true));
} catch (Throwable t) { } catch (Throwable t) {
future.completeExceptionally(t); future.completeExceptionally(t);
} }
@ -129,7 +140,7 @@ public class ZLSStreamConnectionProvider extends OSProcessStreamConnectionProvid
return future; return future;
} }
private static boolean validatePath(String name, String pathTxt, boolean dir) { private static boolean validatePath(String name, String pathTxt, boolean dir, boolean warn) {
if (pathTxt == null || pathTxt.isBlank()) { if (pathTxt == null || pathTxt.isBlank()) {
return false; return false;
} }
@ -137,23 +148,29 @@ public class ZLSStreamConnectionProvider extends OSProcessStreamConnectionProvid
try { try {
path = Path.of(pathTxt); path = Path.of(pathTxt);
} catch (InvalidPathException e) { } catch (InvalidPathException e) {
Notifications.Bus.notify( if (warn) {
new Notification("ZigBrains.ZLS", "No " + name, "Invalid " + name + " at path \"" + pathTxt + "\"", Notifications.Bus.notify(new Notification("ZigBrains.ZLS", "No " + name,
"Invalid " + name + " at path \"" + pathTxt + "\"",
NotificationType.ERROR)); NotificationType.ERROR));
}
return false; return false;
} }
if (!Files.exists(path)) { if (!Files.exists(path)) {
if (warn) {
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));
}
return false; return false;
} }
if (Files.isDirectory(path) != dir) { if (Files.isDirectory(path) != dir) {
if (warn) {
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") +
", expected a " + (dir ? "directory" : "file"), ", expected a " + (dir ? "directory" : "file"),
NotificationType.ERROR)); NotificationType.ERROR));
}
return false; return false;
} }
return true; return true;