From 31d6dc47f2e43991b0513b47d49b36bfaafe6a56 Mon Sep 17 00:00:00 2001 From: akiyuki ishikawa Date: Mon, 18 Nov 2024 19:52:05 +0900 Subject: [PATCH] fix code_action over tramp --- core/handler/code_action.py | 16 +++++----------- core/handler/rename.py | 11 ++--------- core/utils.py | 31 ++++++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/core/handler/code_action.py b/core/handler/code_action.py index f2cc981307..2ca73723f9 100644 --- a/core/handler/code_action.py +++ b/core/handler/code_action.py @@ -12,12 +12,12 @@ class CodeAction(Handler): def process_request(self, lsp_server_name, diagnostics, range_start, range_end, action_kind) -> dict: self.action_kind = action_kind self.lsp_server_name = lsp_server_name - + range = { "start": range_start, "end": range_end } - + if isinstance(action_kind, str): context = { "diagnostics": diagnostics, @@ -27,18 +27,12 @@ def process_request(self, lsp_server_name, diagnostics, range_start, range_end, context = { "diagnostics": diagnostics } - + return dict(range=range, context=context) def process_response(self, response) -> None: remote_connection_info = get_remote_connection_info() - if remote_connection_info != "" : + if remote_connection_info != "": for item in response: - changes = item["edit"]["changes"] - new_changes = {} - for file in changes.keys(): - tramp_file = local_path_to_tramp_path(file, remote_connection_info) - new_changes[tramp_file] = changes[file] - item["edit"]["changes"] = new_changes - + convert_workspace_edit_path_to_tramped_path(item["edit"], remote_connection_info) self.file_action.push_code_actions(response, self.lsp_server_name, self.action_kind) diff --git a/core/handler/rename.py b/core/handler/rename.py index 4de4f02472..b70c4f08b0 100644 --- a/core/handler/rename.py +++ b/core/handler/rename.py @@ -16,14 +16,7 @@ def process_response(self, response: dict) -> None: remote_connection_info = get_remote_connection_info() logger.info(response) - if remote_connection_info != "" : - changes = response["changes"] - new_changes = {} - for file in changes.keys(): - tramp_file = local_path_to_tramp_path(file, remote_connection_info) - new_changes[tramp_file] = changes[file] - response["changes"] = new_changes - + convert_workspace_edit_path_to_tramped_path(response, remote_connection_info) eval_in_emacs("lsp-bridge-workspace-apply-edit", response) - + message_emacs("Rename done.") diff --git a/core/utils.py b/core/utils.py index 2186f7b0c7..19e419dd28 100755 --- a/core/utils.py +++ b/core/utils.py @@ -153,6 +153,35 @@ def get_remote_connection_info(): global remote_connection_info return remote_connection_info +def convert_workspace_edit_path_to_tramped_path(edit, remote_connection_info): + """ Convert documentUris in a WorkspaceEdit instance from local to remote(tramp). + + ex. 'file://...' --> 'file://ssh:... + + About WorkspaceEdit interfeface: + https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#workspaceEdit + """ + if "documentChanges" in edit: + # documentChanges's item can be one of (TextDocumentEdit, CreateFile, DeleteFile, RenameFile) + for change in edit["documentChanges"]: + if change.get("textDocument", {}).get("uri") is not None: + # TextDocumentEdit + change["textDocument"]["uri"] = local_path_to_tramp_path(change["textDocument"]["uri"], remote_connection_info) + elif "uri" in change: + # CreateFile | DeleteFile + change["uri"] = local_path_to_tramp_path(change["uri"], remote_connection_info) + elif "oldUri" in change: + # RenameFile + change["oldUri"] = local_path_to_tramp_path(change["oldUri"], remote_connection_info) + change["newUri"] = local_path_to_tramp_path(change["newUri"], remote_connection_info) + elif "changes" in edit: + changes = edit["changes"] + new_changes = {} + for file in changes.keys(): + tramp_file = local_path_to_tramp_path(file, remote_connection_info) + new_changes[tramp_file] = changes[file] + edit["changes"] = new_changes + def local_path_to_tramp_path(path, tramp_method): """convert path in DocumentUri format to tramp format.""" tramp_path = path.replace("file://", "file://" + tramp_method) @@ -366,7 +395,7 @@ def get_project_path(filepath): if get_os_name() == "windows": path_parts = path_from_git.split("/") # if this is a Unix-style absolute path, which should be a Windows-style one - if path_parts[0] == "/": + if path_parts[0] == "/": windows_path = path_parts[1] + ":/" + "/".join(path_parts[2:]) return windows_path else: