java: implement java.apply.workspaceEdit by translating it into a workspace/applyEdit and sending back to client
This commit is contained in:
parent
49d673b0b8
commit
cfea3b5f11
36
java
36
java
@ -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']
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -40,6 +40,9 @@ def process_messages (source, sink, handlers, log):
|
||||
print("stop message handler", file=log)
|
||||
|
||||
def transmit_payload (payload, sink):
|
||||
if isinstance(payload, dict):
|
||||
payload = json.dumps(payload)
|
||||
|
||||
sink.write(f"{CONTENT_LENGTH}{len(payload)}\r\n\r\n{payload}".encode())
|
||||
sink.flush()
|
||||
|
||||
@ -94,10 +97,10 @@ class LspServer:
|
||||
run(self.command)
|
||||
|
||||
def send_to_client (self, payload):
|
||||
transmit_payload(sys.stdout, payload)
|
||||
transmit_payload(payload, sys.stdout.buffer)
|
||||
|
||||
def send_to_server (self, payload):
|
||||
transmit_payload(self.process.stdout, payload)
|
||||
transmit_payload(payload, self.process.stdout)
|
||||
|
||||
def close (self):
|
||||
if self.process:
|
||||
|
Loading…
x
Reference in New Issue
Block a user