java: implement java.apply.workspaceEdit by translating it into a workspace/applyEdit and sending back to client

This commit is contained in:
2020-11-08 23:16:39 -06:00
parent 49d673b0b8
commit cfea3b5f11
2 changed files with 14 additions and 29 deletions

36
java
View File

@@ -40,31 +40,13 @@ class Java(LspServer):
@command("java.apply.workspaceEdit")
def workspaceEdit (self, arguments):
print(f"we {arguments}", file=sys.stderr)
for argument in arguments:
for file_name, changes in argument['changes'].items():
file_name = file_name[len(FILE_URI):]
file_contents = ""
if os.path.exists(file_name):
with open(file_name) as in_file:
file_contents = in_file.read()
for change in changes:
file_contents = perform_change(file_contents, change)
with open(file_name, "w") as out_file:
out_file.write(file_contents)
def get_character_offset (file_contents, line, character):
line_pos = 0
while line > 0:
line_pos = file_contents.index("\n", line_pos) + 1
line = line - 1
return line_pos + character
def perform_change (file_text, change):
change_start = get_character_offset(file_text, change['range']['start']['line'], change['range']['start']['character'])
change_end = get_character_offset(file_text, change['range']['end']['line'], change['range']['end']['character'])
print(f"change {change_start} {change['newText']} {change_end}", file=sys.stderr)
return file_text[:change_start] + change['newText'] + file_text[change_end:]
self.send_to_client({
"id": -1,
"method": "workspace/applyEdit",
"params": {
"edit": {
"changes": argument['changes']
}
}
})