From da7d225fcc81fafe7d101098b4b47830e17bd73c Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Mon, 10 Jun 2024 02:18:45 +0800 Subject: [PATCH] Make sure csharp-ls find and load solution under project path. --- core/lspserver.py | 19 +++++++++++++------ core/utils.py | 10 ++++++++-- langserver/csharp-ls.json | 10 +++++++--- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/core/lspserver.py b/core/lspserver.py index 7aadb10b1a..2d548653b1 100755 --- a/core/lspserver.py +++ b/core/lspserver.py @@ -266,11 +266,6 @@ def __init__(self, message_queue, project_path, server_info, server_name, enable self.save_include_text = False # Start LSP server. - command_args = self.server_info["command"] - for i, arg in enumerate(command_args): - command_args[i] = replace_template(arg, self.project_path) - self.server_info["command"] = command_args - self.lsp_subprocess = subprocess.Popen(self.server_info["command"], bufsize=DEFAULT_BUFFER_SIZE, stdin=PIPE, @@ -546,10 +541,22 @@ def send_exit_notification(self): self.sender.send_notification("exit", {}) def get_server_workspace_change_configuration(self): - return { + settings = { "settings": self.server_info.get("settings", {}) } + if self.server_info["name"] == "csharp-ls": + # Set settings.csharp.solution if found *.sln file in project, + # to make sure csharp-ls find and load solution with project path, not current path. + settings = { + "settings": { + "csharp": { + "solution": find_csharp_solution_file(self.project_path) + } + }} + + return settings + def handle_workspace_configuration_request(self, name, request_id, params): settings = self.server_info.get("settings", {}) diff --git a/core/utils.py b/core/utils.py index 09393c9093..b3444af8da 100755 --- a/core/utils.py +++ b/core/utils.py @@ -466,11 +466,17 @@ def replace_template(arg, project_path=None): return arg.replace("%FILEHASH%", os.urandom(21).hex()) elif "%USERPROFILE%" in arg: return arg.replace("%USERPROFILE%", windows_get_env_value("USERPROFILE")) - elif "%PROJECTPATH%" in arg and project_path: - return arg.replace("%PROJECTPATH%", project_path) else: return arg +def find_csharp_solution_file(directory): + for root, dirs, files in os.walk(directory): + for file in files: + if file.endswith(".sln"): + return os.path.join(root, file) + + return None + def touch(path): import os diff --git a/langserver/csharp-ls.json b/langserver/csharp-ls.json index 50a866e909..0bea4b629d 100644 --- a/langserver/csharp-ls.json +++ b/langserver/csharp-ls.json @@ -1,9 +1,13 @@ { "name": "csharp-ls", - "languageId": "c#", - "command": ["$HOME/.dotnet/tools/csharp-ls", "-s", "%PROJECTPATH%"], + "languageId": "csharp", + "command": ["$HOME/.dotnet/tools/csharp-ls"], "forceInlayHint": true, - "settings": {}, + "settings": { + "csharp": { + "solution": null + } + }, "support-single-file": false, "capabilities": { "general": {