Skip to content

Commit

Permalink
Merge pull request #1113 from i-aki-y/fix-remote-code-action
Browse files Browse the repository at this point in the history
Fix: lsp_bridge.py raises a `KeyError` when a code action is triggered in tramp.
  • Loading branch information
manateelazycat authored Nov 18, 2024
2 parents d171d7f + 31d6dc4 commit 72a19f4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
16 changes: 5 additions & 11 deletions core/handler/code_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
11 changes: 2 additions & 9 deletions core/handler/rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
31 changes: 30 additions & 1 deletion core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 72a19f4

Please sign in to comment.