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")
|
@command("java.apply.workspaceEdit")
|
||||||
def workspaceEdit (self, arguments):
|
def workspaceEdit (self, arguments):
|
||||||
print(f"we {arguments}", file=sys.stderr)
|
|
||||||
for argument in arguments:
|
for argument in arguments:
|
||||||
for file_name, changes in argument['changes'].items():
|
self.send_to_client({
|
||||||
file_name = file_name[len(FILE_URI):]
|
"id": -1,
|
||||||
file_contents = ""
|
"method": "workspace/applyEdit",
|
||||||
|
"params": {
|
||||||
if os.path.exists(file_name):
|
"edit": {
|
||||||
with open(file_name) as in_file:
|
"changes": argument['changes']
|
||||||
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:]
|
|
||||||
|
@ -40,6 +40,9 @@ def process_messages (source, sink, handlers, log):
|
|||||||
print("stop message handler", file=log)
|
print("stop message handler", file=log)
|
||||||
|
|
||||||
def transmit_payload (payload, sink):
|
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.write(f"{CONTENT_LENGTH}{len(payload)}\r\n\r\n{payload}".encode())
|
||||||
sink.flush()
|
sink.flush()
|
||||||
|
|
||||||
@ -94,10 +97,10 @@ class LspServer:
|
|||||||
run(self.command)
|
run(self.command)
|
||||||
|
|
||||||
def send_to_client (self, payload):
|
def send_to_client (self, payload):
|
||||||
transmit_payload(sys.stdout, payload)
|
transmit_payload(payload, sys.stdout.buffer)
|
||||||
|
|
||||||
def send_to_server (self, payload):
|
def send_to_server (self, payload):
|
||||||
transmit_payload(self.process.stdout, payload)
|
transmit_payload(payload, self.process.stdout)
|
||||||
|
|
||||||
def close (self):
|
def close (self):
|
||||||
if self.process:
|
if self.process:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user