import os import sys FILE_URI = "file://" def locate_product_directory (jdt_ls_path): return os.path.join(jdt_ls_path, "org.eclipse.jdt.ls.product/target/repository") def locate_launcher_jar (jdt_ls_path): jdt_ls_product_directory = locate_product_directory(jdt_ls_path) plugins_directory = os.path.join(jdt_ls_product_directory, "plugins") return os.path.join(plugins_directory, [jar for jar in os.listdir(plugins_directory) if jar.startswith("org.eclipse.equinox.launcher_")][0]) def locate_launcher_configuration (jdt_ls_path): return os.path.join(locate_product_directory(jdt_ls_path), "config_linux") # TODO def locate_lombok (): return "/home/malacoda/.m2/repository/org/projectlombok/lombok/1.18.16/lombok-1.18.16.jar" class Java(LspServer): def __init__ (self, arguments): self.path = arguments[0] super().__init__([ "java", f"-javaagent:{locate_lombok}", "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=1044", "-Declipse.application=org.eclipse.jdt.ls.core.id1", "-Dosgi.bundles.defaultStartLevel=4", "-Declipse.product=org.eclipse.jdt.ls.core.product", "-noverify", "-Xmx1G", "-jar", locate_launcher_jar(self.path), "-configuration", locate_launcher_configuration(self.path), "-data", os.path.join(self.path, "data"), "--add-modules=ALL-SYSTEM", "--add-opens", "java.base/java.util=ALL-UNNAMED", "--add-opens", "java.base/java.lang=ALL-UNNAMED" ] + arguments[1:]) @command("java.apply.workspaceEdit") def workspaceEdit (self, arguments): print(f"we {arguments}") 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']['character'], change['range']['start']['character']) change_end = get_character_offset(file_text, change['range']['end']['line'], change['range']['end']['character']) return file_text[:change_start] + change['newText'] + file_text[change_end:]