From a64958e5b21d8d49327852b16ab4bd0504455f76 Mon Sep 17 00:00:00 2001 From: Daniil Samylovskikh Date: Sat, 26 Nov 2022 17:24:41 -0800 Subject: [PATCH 1/3] lsp cpp extension working demo --- org.lflang.diagram/build.gradle | 1 + .../lflang/diagram/lsp/LFLanguageServer.java | 272 +++++++++++++++++- .../lsp/LFLanguageServerExtension.java | 15 + 3 files changed, 287 insertions(+), 1 deletion(-) diff --git a/org.lflang.diagram/build.gradle b/org.lflang.diagram/build.gradle index bd14ca4a2c..820847b05b 100644 --- a/org.lflang.diagram/build.gradle +++ b/org.lflang.diagram/build.gradle @@ -19,6 +19,7 @@ dependencies { exclude group: 'org.eclipse.platform', module: 'org.eclipse.swt.*' exclude group: 'org.eclipse.platform', module: 'org.eclipse.swt' } + implementation 'com.fasterxml.jackson.core:jackson-databind:2.8.9' } apply plugin: 'application' diff --git a/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServer.java b/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServer.java index 085247028c..b0336e3883 100644 --- a/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServer.java +++ b/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServer.java @@ -8,6 +8,30 @@ import org.eclipse.xtext.ide.server.hover.IHoverService; import org.eclipse.xtext.util.CancelIndicator; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.Path; +import org.lflang.generator.CodeMap; +import java.nio.charset.StandardCharsets; +import java.io.OutputStream; +import java.io.InputStream; +import com.fasterxml.jackson.databind.ObjectMapper; +// import com.fasterxml.jackson.databind.JsonFactory; +import com.fasterxml.jackson.databind.JsonNode; +import java.util.List; +import java.util.Collections; +import org.eclipse.lsp4j.jsonrpc.messages.Either; +import org.eclipse.lsp4j.MarkedString; +import java.util.Map; +import org.lflang.generator.Position; + + +import org.lflang.generator.GeneratorResult; + /** * The Lingua Franca language and diagram server. * @@ -26,9 +50,255 @@ protected Hover hover(HoverParams params, CancelIndicator cancelIndicator) { // simply because it is easy. This would be done differently were it not for the fact that we plan to rebuild // this infrastructure from scratch anyway. try { - return super.hover(params, cancelIndicator); + System.out.println("got hover r"); + System.out.println(stdin); + System.out.println(stdout); + System.out.println(reader); + System.out.println(writer); + System.out.println(errorReader); + System.out.println(initialized); + System.out.println(pr); + Map result = LFLanguageServerExtension.getGeneratorResult().getCodeMaps(); + int currentBuildToken = LFLanguageServerExtension.getBuildToken(); + if (currentBuildToken != buildToken) { + buildToken = currentBuildToken; + initialized = false; + } + Path ccPath = Paths.get("/Users/daniil_11/lf-workspace/First/src-gen/HelloWorld/HelloWorld/HelloWorld.cc"); + CodeMap codeMap = result.get(ccPath); + Path lfPath = Paths.get("/Users/daniil_11/lf-workspace/First/src/HelloWorld.lf"); + Position codePos = findPosition(params.getPosition().getLine(), params.getPosition().getCharacter(), lfPath, codeMap); + System.out.println("request line is " + codePos.getOneBasedLine() + " char is " + codePos.getOneBasedColumn()); + return clangdHover(codePos); + // return super.hover(params, cancelIndicator); } catch (IndexOutOfBoundsException e) { return IHoverService.EMPTY_HOVER; // Fail silently } } + OutputStream stdin = null; + static Process pr; + InputStream stdout = null; + BufferedReader reader = null; + BufferedWriter writer = null; + BufferedReader errorReader = null; + boolean initialized = false; + int buildToken = -1; + + private Position findPosition(int line, int col, Path lfFile, CodeMap codeMap) { + for (int brutLine = 1; brutLine <= 500; brutLine++) { + for (int brutChar = 1; brutChar <= 100; brutChar++) { + Position generatedPos = Position.fromOneBased(brutLine, brutChar); + Position p = codeMap.adjusted(lfFile, generatedPos); + // System.out.println(brutLine + " line and char " + brutChar + " from generated file maps to " + p.getOneBasedLine() + " and " + p.getOneBasedColumn()); + if (p.getOneBasedLine() == line && p.getOneBasedColumn() == col) return generatedPos; + } + } + return Position.fromOneBased(1, 1); + } + + private Hover clangdHover(Position params) { + if (!initialized){ + process_init(); + // readErrorOutput(); + initialized = true; + clangd_init(); + readOutput(); + did_open_clangd(); + readOutput(); + } + + send_hover_request(params.getOneBasedLine(), params.getOneBasedColumn()); + Hover resultHover = new Hover(); + try { + String jsonResponse = readOutput(); + System.out.println("got response:"); + System.out.println(jsonResponse); + jsonResponse = jsonResponse.substring(jsonResponse.indexOf(System.getProperty("line.separator"))+1);// remove first line since contains the header + jsonResponse = jsonResponse.substring(jsonResponse.indexOf(System.getProperty("line.separator"))+1); + System.out.println("truncated is :"); + System.out.println(jsonResponse); + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(jsonResponse); + System.out.println("whole:"); + System.out.println(jsonNode); + String jsonValue = jsonNode.get("result").get("contents").get("value").textValue(); + List> content = Collections.singletonList(Either.forLeft(jsonValue)); + resultHover.setContents(content); + int lvalue_c = jsonNode.get("result").get("range").get("start").get("character").intValue(); + int lvalue_l = jsonNode.get("result").get("range").get("start").get("line").intValue(); + int rvalue_c = jsonNode.get("result").get("range").get("end").get("character").intValue(); + int rvalue_l = jsonNode.get("result").get("range").get("end").get("line").intValue(); + System.out.println("parsed value: "); + System.out.println(" " + lvalue_c + " " + lvalue_l + " " + rvalue_c + " " + rvalue_l); + return resultHover; + } catch (Exception e) { + System.out.println("main failed"); + } + return null; + } + + // private String readOutput() { + // System.out.println("reading output"); + // try { + // int k = 0; + // char[] buf = new char[5000]; + // int i = 0; + // while (k != -1) { + // buf[i++] = (char) reader.read(); + // System.out.println("read: " + Character.toString(buf[i--])); + // if (!reader.ready()) break; + // } + // // System.out.println(buf); + // String returnStatus = ""; + // // System.out.println("trying to return"); + // for (int j= 0; j < i; j++) returnStatus += buf[j]; + // System.out.println("read output: "); + // System.out.println(returnStatus); + // return returnStatus; + // } catch (Exception e) { + // System.out.println("failed reading clangd response"); + // } + // return ""; + // } + + // private String readOutput() { + // String ret = ""; + // try { + // String str = null; + // System.out.println("reading output"); + // while ((str = reader.readLine()) != null) { + // System.out.println("temp is: " + str); + // ret += str; + // Syste + // } + // } catch (Exception e) { + // System.out.println("failed reading output"); + // } + // System.out.println("read: " + ret); + // return ret; + // } + + private String readOutput() { + char[] chars = new char[8192]; + System.out.println("reading output"); + try { + for(int len; (len = reader.read(chars)) > 0 && reader.ready();) { + + } + System.out.println(chars); + } catch (Exception e) { + System.out.println("failed reading output"); + } + return String.valueOf(chars); + } + + // private String readOutput() { + // System.out.println("reading output"); + // byte[] buffer = new byte[64]; + // int len; + // String collected = ""; + // do { + // try { + // // This depends on in.available() being + // // greater than zero if data is available + // // (so that all data is collected) + // // and upper-bounded by maximum number of + // // bytes that can be read without blocking. + // // Only the latter of these two conditions + // // is guaranteed by the spec. + // System.out.println(stdout.available()); + // len = stdout.read(buffer, 0, Math.min(stdout.available(), buffer.length)); + // if (len > 0) { + // String temp = new String(buffer, 0, len, StandardCharsets.UTF_8); + // collected += temp; + // System.out.println("temp is " + temp); + // } + // } catch (Exception e) { + // break; + // } + // } while (len > 0); + // System.out.println("output is " + collected); + // return collected; + // } + + private void process_init() { + System.out.println("initializing process"); + try { + ProcessBuilder pb = new ProcessBuilder("clangd"); + pr = pb.start(); + + stdin = pr.getOutputStream(); + stdout = pr.getInputStream(); + reader = new BufferedReader (new InputStreamReader(stdout)); + writer = new BufferedWriter(new OutputStreamWriter(stdin)); + + errorReader = new BufferedReader(new InputStreamReader(pr.getErrorStream())); + } catch (Exception e) { + System.out.println("failed process init"); + } + } + + private void readErrorOutput() { + try { + String str = null; + System.out.println("reading error output"); + while ((str = errorReader.readLine()) != null && errorReader.ready()) { + System.out.println(str); + } + } catch (Exception e) { + System.out.println("failed reading error"); + } + } + + private void send_hover_request(int line, int character) { + System.out.println("sending hover request"); + try { + String body = "{\"method\": \"textDocument/hover\",\"jsonrpc\": \"2.0\",\"id\": 1,\"params\": {\"textDocument\": {\"uri\": \"file:///Users/daniil_11/lf-workspace/First/src-gen/HelloWorld/HelloWorld/HelloWorld.cc\"} ,\"position\": {\"line\": "+line+",\"character\": "+character+"}}}\r\n"; + String header = "Content-Length: " + body.length() +"\r\n\r\n"; + String cmd = header + body; + System.out.println(cmd); + writer.write(cmd); + writer.flush(); + } catch (Exception e) { + System.out.println("failed hover request"); + } + + } + + private void clangd_init() { + System.out.println("initializing clangd"); + try { + String body = "{\"jsonrpc\": \"2.0\", \"method\": \"initialize\", \"id\": 1, \"params\": {\"rootUri\": \"file:///Users/daniil_11/lf-workspace/First/src-gen\", \"capabilities\": {\"hover\": {\"dynamicRegistration\": false, \"contentFormat\": []}}}}\r\n"; + String header = "Content-Length: " + body.length() +"\r\n\r\n"; + String cmd = header + body; + System.out.println(cmd); + + writer.write(cmd); + writer.flush(); + + + } catch (Exception e) { + System.out.println("failed clangd init"); + } + } + + private void did_open_clangd() { + System.out.println("sending did open request"); + + try { + String fileContent = Files.readString(Paths.get("/Users/daniil_11/lf-workspace/First/src-gen/HelloWorld/HelloWorld/HelloWorld.cc"), StandardCharsets.UTF_8); + fileContent = fileContent.replaceAll("\"", "\\\\\""); + fileContent = fileContent.replaceAll("\n", "\\\\n"); + String body = "{\"method\": \"textDocument/didOpen\", \"jsonrpc\": \"2.0\", \"params\": {\"textDocument\": {\"uri\": \"file:///Users/daniil_11/lf-workspace/First/src-gen/HelloWorld/HelloWorld/HelloWorld.cc\", \"languageId\": \"cpp\", \"version\": 1, \"text\": \""+fileContent+"\"}}}\r\n"; + String header = "Content-Length: " + (body.length()) +"\r\n\r\n"; + String cmd = header + body; + System.out.println(cmd); + + writer.write(cmd); + writer.flush(); + } catch (Exception e) { + System.out.println("failed clangd did open"); + } + + } } diff --git a/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServerExtension.java index 7436a157a4..6cdd868c77 100644 --- a/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -35,6 +35,8 @@ class LFLanguageServerExtension implements ILanguageServerExtension { /** The access point for reading documents, communicating with the language client, etc. */ private LanguageClient client; + private static GeneratorResult buildResult = null; + private static int buildToken = 0; @Override public void initialize(ILanguageServerAccess access) { @@ -99,6 +101,14 @@ public CompletableFuture buildAndRun(String uri) { }); } + public static GeneratorResult getGeneratorResult() { + return buildResult; + } + + public static int getBuildToken() { + return buildToken; + } + /** * Describes a build process that has a progress. */ @@ -123,6 +133,11 @@ private GeneratorResult buildWithProgress(LanguageClient client, String uri, boo } finally { progress.end(result == null ? "An internal error occurred." : result.getUserMessage()); } + // System.out.println("heheheheheeh"); + // System.out.println(result.getCodeMaps()); + + buildResult = result; + buildToken += 1; return result; } } From d897916523538e73f8600e79c4d9db75dfad9ce2 Mon Sep 17 00:00:00 2001 From: Daniil Samylovskikh Date: Mon, 28 Nov 2022 04:35:52 -0800 Subject: [PATCH 2/3] removed hardcoded values, need to run cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 in src-gen to work --- .../lflang/diagram/lsp/LFLanguageServer.java | 222 ++++++++---------- .../lsp/LFLanguageServerExtension.java | 7 + .../src/org/lflang/generator/CodeMap.java | 4 + 3 files changed, 104 insertions(+), 129 deletions(-) diff --git a/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServer.java b/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServer.java index b0336e3883..be1253f5d4 100644 --- a/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServer.java +++ b/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServer.java @@ -20,7 +20,6 @@ import java.io.OutputStream; import java.io.InputStream; import com.fasterxml.jackson.databind.ObjectMapper; -// import com.fasterxml.jackson.databind.JsonFactory; import com.fasterxml.jackson.databind.JsonNode; import java.util.List; import java.util.Collections; @@ -28,6 +27,10 @@ import org.eclipse.lsp4j.MarkedString; import java.util.Map; import org.lflang.generator.Position; +import org.lflang.generator.Range; +import java.util.NavigableMap; +import java.util.HashMap; +// import javafx.util.Pair; import org.lflang.generator.GeneratorResult; @@ -50,31 +53,68 @@ protected Hover hover(HoverParams params, CancelIndicator cancelIndicator) { // simply because it is easy. This would be done differently were it not for the fact that we plan to rebuild // this infrastructure from scratch anyway. try { - System.out.println("got hover r"); - System.out.println(stdin); - System.out.println(stdout); - System.out.println(reader); - System.out.println(writer); - System.out.println(errorReader); - System.out.println(initialized); - System.out.println(pr); - Map result = LFLanguageServerExtension.getGeneratorResult().getCodeMaps(); int currentBuildToken = LFLanguageServerExtension.getBuildToken(); + Map result = LFLanguageServerExtension.getGeneratorResult().getCodeMaps(); + generatedDirectory = LFLanguageServerExtension.getGeneratorResult().getCommand().directory().toString() + "/src-gen"; if (currentBuildToken != buildToken) { buildToken = currentBuildToken; initialized = false; + // getCodeMaps(); + } - Path ccPath = Paths.get("/Users/daniil_11/lf-workspace/First/src-gen/HelloWorld/HelloWorld/HelloWorld.cc"); - CodeMap codeMap = result.get(ccPath); - Path lfPath = Paths.get("/Users/daniil_11/lf-workspace/First/src/HelloWorld.lf"); - Position codePos = findPosition(params.getPosition().getLine(), params.getPosition().getCharacter(), lfPath, codeMap); - System.out.println("request line is " + codePos.getOneBasedLine() + " char is " + codePos.getOneBasedColumn()); + Path lfPath = LFLanguageServerExtension.getBuildPath(); + System.out.println("lfpath is " + lfPath); + Position codePos = findPosition(params.getPosition().getLine(), params.getPosition().getCharacter(), lfPath, result); + // Position codePos = findPositionInvertedMap(lfPath); return clangdHover(codePos); // return super.hover(params, cancelIndicator); } catch (IndexOutOfBoundsException e) { return IHoverService.EMPTY_HOVER; // Fail silently } } + + // void getCodeMaps() { + // Map codeMaps = LFLanguageServerExtension.getGeneratorResult().getCodeMaps(); + // for (Map.Entry entry : codeMaps.entrySet()) { + // Path generatedPath = entry.getKey(); + // Map> map = entry.getValue().getCodeMap(); + // // Map> constructedPathMap; + // for (Map.Entry> pathEntry : map.entrySet()) { + // NavigableMap pathMap = pathEntry.getValue(); + // Path lfPath = pathEntry.getKey(); + // // NavigableMap constructedRangeMap; + // for (Map.Entry rangeEntry : pathMap.entrySet()) { + // // constructedRangeMap.put(rangeEntry.getValue(), rangeEntry.getKey()); + // Range generatedRange = rangeEntry.getKey(); + // Range lfRange = rangeEntry.getValue(); + // insertToRangeMap(lfPath, generatedPath, lfRange, generatedRange); + // } + // } + // } + // } + + // void insertToRangeMap(Path lfPath, Path generatedPath, Range lfRange, Range generatedRange) { + // if (codeMapping == null) codeMapping = new HashMap>>(); + // HashMap> lfMap = codeMapping.get(lfPath); + // if (lfMap == null) { + // lfMap = new HashMap>(); + // codeMapping.put(lfPath, lfMap); + // } + // HashMap pathMap = lfMap.get(generatedPath); + // if (pathMap == null) { + // pathMap = new HashMap(); + // lfMap.put(generatedPath, pathMap); + // } + // pathMap.put(lfRange, generatedRange); + // System.out.println("putting " + lfPath + " " + generatedPath + " " + lfRange + " " + generatedRange); + // } + + // void insertToRangeMap(Path lfPath, Path generatedPath, Range lfRange, Range generatedRange) { + // if (codeMapping == null) codeMapping = new HashMap, Pair>(); + // codeMapping.put(new Pair(lfPath, lfRange), new Pair(generatedPath, generatedRange)); + // System.out.println("putting " + lfPath + " " + generatedPath + " " + lfRange + " " + generatedRange); + // } + OutputStream stdin = null; static Process pr; InputStream stdout = null; @@ -83,23 +123,35 @@ protected Hover hover(HoverParams params, CancelIndicator cancelIndicator) { BufferedReader errorReader = null; boolean initialized = false; int buildToken = -1; + Path generatedPath; + String generatedDirectory; + // HashMap>> codeMapping = null; + // HashMap, + // Pair> codeMapping = null; - private Position findPosition(int line, int col, Path lfFile, CodeMap codeMap) { - for (int brutLine = 1; brutLine <= 500; brutLine++) { - for (int brutChar = 1; brutChar <= 100; brutChar++) { - Position generatedPos = Position.fromOneBased(brutLine, brutChar); - Position p = codeMap.adjusted(lfFile, generatedPos); - // System.out.println(brutLine + " line and char " + brutChar + " from generated file maps to " + p.getOneBasedLine() + " and " + p.getOneBasedColumn()); - if (p.getOneBasedLine() == line && p.getOneBasedColumn() == col) return generatedPos; + private Position findPosition(int line, int col, Path lfFile, Map generatedResult) { + for (Path sourcePath : generatedResult.keySet()) { + CodeMap codeMap = generatedResult.get(sourcePath); + for (int brutLine = 1; brutLine <= 500; brutLine++) { + for (int brutChar = 1; brutChar <= 100; brutChar++) { + Position generatedPos = Position.fromOneBased(brutLine, brutChar); + Position p = codeMap.adjusted(lfFile, generatedPos); + if (p.getOneBasedLine() == line && p.getOneBasedColumn() == col) { + generatedPath = sourcePath; + return generatedPos; + } + } } } + return Position.fromOneBased(1, 1); } private Hover clangdHover(Position params) { if (!initialized){ process_init(); - // readErrorOutput(); initialized = true; clangd_init(); readOutput(); @@ -110,119 +162,38 @@ private Hover clangdHover(Position params) { send_hover_request(params.getOneBasedLine(), params.getOneBasedColumn()); Hover resultHover = new Hover(); try { - String jsonResponse = readOutput(); - System.out.println("got response:"); - System.out.println(jsonResponse); - jsonResponse = jsonResponse.substring(jsonResponse.indexOf(System.getProperty("line.separator"))+1);// remove first line since contains the header - jsonResponse = jsonResponse.substring(jsonResponse.indexOf(System.getProperty("line.separator"))+1); - System.out.println("truncated is :"); - System.out.println(jsonResponse); - ObjectMapper objectMapper = new ObjectMapper(); - JsonNode jsonNode = objectMapper.readTree(jsonResponse); - System.out.println("whole:"); - System.out.println(jsonNode); - String jsonValue = jsonNode.get("result").get("contents").get("value").textValue(); - List> content = Collections.singletonList(Either.forLeft(jsonValue)); - resultHover.setContents(content); - int lvalue_c = jsonNode.get("result").get("range").get("start").get("character").intValue(); - int lvalue_l = jsonNode.get("result").get("range").get("start").get("line").intValue(); - int rvalue_c = jsonNode.get("result").get("range").get("end").get("character").intValue(); - int rvalue_l = jsonNode.get("result").get("range").get("end").get("line").intValue(); - System.out.println("parsed value: "); - System.out.println(" " + lvalue_c + " " + lvalue_l + " " + rvalue_c + " " + rvalue_l); - return resultHover; + String jsonResponse = readOutput(); + jsonResponse = jsonResponse.substring(jsonResponse.indexOf(System.getProperty("line.separator"))+1);// remove first line since contains the header + jsonResponse = jsonResponse.substring(jsonResponse.indexOf(System.getProperty("line.separator"))+1); + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(jsonResponse); + String jsonValue = jsonNode.get("result").get("contents").get("value").textValue(); + List> content = Collections.singletonList(Either.forLeft(jsonValue)); + resultHover.setContents(content); + int lvalue_c = jsonNode.get("result").get("range").get("start").get("character").intValue(); + int lvalue_l = jsonNode.get("result").get("range").get("start").get("line").intValue(); + int rvalue_c = jsonNode.get("result").get("range").get("end").get("character").intValue(); + int rvalue_l = jsonNode.get("result").get("range").get("end").get("line").intValue(); + return resultHover; } catch (Exception e) { System.out.println("main failed"); } return null; } - // private String readOutput() { - // System.out.println("reading output"); - // try { - // int k = 0; - // char[] buf = new char[5000]; - // int i = 0; - // while (k != -1) { - // buf[i++] = (char) reader.read(); - // System.out.println("read: " + Character.toString(buf[i--])); - // if (!reader.ready()) break; - // } - // // System.out.println(buf); - // String returnStatus = ""; - // // System.out.println("trying to return"); - // for (int j= 0; j < i; j++) returnStatus += buf[j]; - // System.out.println("read output: "); - // System.out.println(returnStatus); - // return returnStatus; - // } catch (Exception e) { - // System.out.println("failed reading clangd response"); - // } - // return ""; - // } - - // private String readOutput() { - // String ret = ""; - // try { - // String str = null; - // System.out.println("reading output"); - // while ((str = reader.readLine()) != null) { - // System.out.println("temp is: " + str); - // ret += str; - // Syste - // } - // } catch (Exception e) { - // System.out.println("failed reading output"); - // } - // System.out.println("read: " + ret); - // return ret; - // } - private String readOutput() { char[] chars = new char[8192]; - System.out.println("reading output"); try { for(int len; (len = reader.read(chars)) > 0 && reader.ready();) { } - System.out.println(chars); } catch (Exception e) { System.out.println("failed reading output"); } return String.valueOf(chars); } - // private String readOutput() { - // System.out.println("reading output"); - // byte[] buffer = new byte[64]; - // int len; - // String collected = ""; - // do { - // try { - // // This depends on in.available() being - // // greater than zero if data is available - // // (so that all data is collected) - // // and upper-bounded by maximum number of - // // bytes that can be read without blocking. - // // Only the latter of these two conditions - // // is guaranteed by the spec. - // System.out.println(stdout.available()); - // len = stdout.read(buffer, 0, Math.min(stdout.available(), buffer.length)); - // if (len > 0) { - // String temp = new String(buffer, 0, len, StandardCharsets.UTF_8); - // collected += temp; - // System.out.println("temp is " + temp); - // } - // } catch (Exception e) { - // break; - // } - // } while (len > 0); - // System.out.println("output is " + collected); - // return collected; - // } - private void process_init() { - System.out.println("initializing process"); try { ProcessBuilder pb = new ProcessBuilder("clangd"); pr = pb.start(); @@ -251,12 +222,10 @@ private void readErrorOutput() { } private void send_hover_request(int line, int character) { - System.out.println("sending hover request"); try { - String body = "{\"method\": \"textDocument/hover\",\"jsonrpc\": \"2.0\",\"id\": 1,\"params\": {\"textDocument\": {\"uri\": \"file:///Users/daniil_11/lf-workspace/First/src-gen/HelloWorld/HelloWorld/HelloWorld.cc\"} ,\"position\": {\"line\": "+line+",\"character\": "+character+"}}}\r\n"; + String body = "{\"method\": \"textDocument/hover\",\"jsonrpc\": \"2.0\",\"id\": 1,\"params\": {\"textDocument\": {\"uri\": \"file://" + generatedPath.toString() + "\"} ,\"position\": {\"line\": "+line+",\"character\": "+character+"}}}\r\n"; String header = "Content-Length: " + body.length() +"\r\n\r\n"; String cmd = header + body; - System.out.println(cmd); writer.write(cmd); writer.flush(); } catch (Exception e) { @@ -266,12 +235,10 @@ private void send_hover_request(int line, int character) { } private void clangd_init() { - System.out.println("initializing clangd"); try { - String body = "{\"jsonrpc\": \"2.0\", \"method\": \"initialize\", \"id\": 1, \"params\": {\"rootUri\": \"file:///Users/daniil_11/lf-workspace/First/src-gen\", \"capabilities\": {\"hover\": {\"dynamicRegistration\": false, \"contentFormat\": []}}}}\r\n"; + String body = "{\"jsonrpc\": \"2.0\", \"method\": \"initialize\", \"id\": 1, \"params\": {\"rootUri\": \"file://" + generatedDirectory + "\", \"capabilities\": {\"hover\": {\"dynamicRegistration\": false, \"contentFormat\": []}}}}\r\n"; String header = "Content-Length: " + body.length() +"\r\n\r\n"; String cmd = header + body; - System.out.println(cmd); writer.write(cmd); writer.flush(); @@ -283,17 +250,14 @@ private void clangd_init() { } private void did_open_clangd() { - System.out.println("sending did open request"); try { - String fileContent = Files.readString(Paths.get("/Users/daniil_11/lf-workspace/First/src-gen/HelloWorld/HelloWorld/HelloWorld.cc"), StandardCharsets.UTF_8); + String fileContent = Files.readString(generatedPath, StandardCharsets.UTF_8); fileContent = fileContent.replaceAll("\"", "\\\\\""); fileContent = fileContent.replaceAll("\n", "\\\\n"); - String body = "{\"method\": \"textDocument/didOpen\", \"jsonrpc\": \"2.0\", \"params\": {\"textDocument\": {\"uri\": \"file:///Users/daniil_11/lf-workspace/First/src-gen/HelloWorld/HelloWorld/HelloWorld.cc\", \"languageId\": \"cpp\", \"version\": 1, \"text\": \""+fileContent+"\"}}}\r\n"; + String body = "{\"method\": \"textDocument/didOpen\", \"jsonrpc\": \"2.0\", \"params\": {\"textDocument\": {\"uri\": \"file://" + generatedPath + "\", \"languageId\": \"cpp\", \"version\": 1, \"text\": \""+fileContent+"\"}}}\r\n"; String header = "Content-Length: " + (body.length()) +"\r\n\r\n"; - String cmd = header + body; - System.out.println(cmd); - + String cmd = header + body; writer.write(cmd); writer.flush(); } catch (Exception e) { diff --git a/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServerExtension.java index 6cdd868c77..c717a4e06e 100644 --- a/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -1,6 +1,7 @@ package org.lflang.diagram.lsp; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.concurrent.CompletableFuture; import java.util.function.Function; import java.util.ArrayList; @@ -37,6 +38,7 @@ class LFLanguageServerExtension implements ILanguageServerExtension { private LanguageClient client; private static GeneratorResult buildResult = null; private static int buildToken = 0; + private static Path buildPath = null; @Override public void initialize(ILanguageServerAccess access) { @@ -109,6 +111,10 @@ public static int getBuildToken() { return buildToken; } + public static Path getBuildPath() { + return buildPath; + } + /** * Describes a build process that has a progress. */ @@ -138,6 +144,7 @@ private GeneratorResult buildWithProgress(LanguageClient client, String uri, boo buildResult = result; buildToken += 1; + buildPath = Paths.get(uri.substring(5)); // remove "file:" return result; } } diff --git a/org.lflang/src/org/lflang/generator/CodeMap.java b/org.lflang/src/org/lflang/generator/CodeMap.java index 09a0533eb0..73b1b127b5 100644 --- a/org.lflang/src/org/lflang/generator/CodeMap.java +++ b/org.lflang/src/org/lflang/generator/CodeMap.java @@ -241,6 +241,10 @@ public String getGeneratedCode() { return generatedCode; } + public Map> getCodeMap() { + return map; + } + /** * Returns the set of all paths to Lingua Franca files * that are known to contain code that corresponds to From b162b640b2f0ff740cea6e88953211785edfd40d Mon Sep 17 00:00:00 2001 From: Daniil Samylovskikh Date: Sat, 14 Jan 2023 20:57:26 -0800 Subject: [PATCH 3/3] cpp lsp extension --- .../lflang/diagram/lsp/CppLanguageServer.java | 274 ++++++++++++++++++ .../lflang/diagram/lsp/LFLanguageServer.java | 242 +--------------- .../lsp/LFLanguageServerExtension.java | 10 +- 3 files changed, 285 insertions(+), 241 deletions(-) create mode 100644 org.lflang.diagram/src/org/lflang/diagram/lsp/CppLanguageServer.java diff --git a/org.lflang.diagram/src/org/lflang/diagram/lsp/CppLanguageServer.java b/org.lflang.diagram/src/org/lflang/diagram/lsp/CppLanguageServer.java new file mode 100644 index 0000000000..778e859cf8 --- /dev/null +++ b/org.lflang.diagram/src/org/lflang/diagram/lsp/CppLanguageServer.java @@ -0,0 +1,274 @@ +package org.lflang.diagram.lsp; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.Path; +import org.lflang.generator.CodeMap; +import java.nio.charset.StandardCharsets; +import java.io.OutputStream; +import java.io.InputStream; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.JsonNode; +import java.util.List; +import java.util.Collections; +import org.eclipse.lsp4j.jsonrpc.messages.Either; +import org.eclipse.lsp4j.MarkedString; +import java.util.Map; +import org.lflang.generator.Position; +import org.lflang.generator.Range; +import java.util.NavigableMap; +import java.util.HashMap; +import java.util.HashSet; +import java.io.File; + +import org.eclipse.lsp4j.Hover; +import org.eclipse.lsp4j.HoverParams; +import org.eclipse.xtext.ide.server.hover.IHoverService; +import org.eclipse.xtext.util.CancelIndicator; + +import org.lflang.generator.GeneratorResult; + +public class CppLanguageServer{ + static OutputStream stdin = null; + static Process pr; + static InputStream stdout = null; + static BufferedReader reader = null; + static BufferedWriter writer = null; + static BufferedReader errorReader = null; + static Path generatedPath; + static String generatedDirectory; + static Map storedGeneratedResults; + static Map storedBuildResults; + + + public static void init() { + process_init(); + clangd_init(); + readOutput(); + storedBuildResults = new HashMap(); + System.out.println("Initialized language server"); + } + + public static Hover hoverRequest(HoverParams params) { + Path lfPath = Paths.get(params.getTextDocument().getUri().substring(5)); + if (!storedBuildResults.containsKey(lfPath)) { + Hover preHover = new Hover(); + List> content = Collections.singletonList(Either.forLeft("You first need to build a project")); + preHover.setContents(content); + return preHover; + } + try { + Map result; + GeneratorResult genResult; + ProjectBuildResult buildResult = storedBuildResults.get(lfPath); + result = buildResult.getGeneratorResult().getCodeMaps(); + generatedDirectory = buildResult.getGeneratorResult().getCommand().directory().toString() + "/src-gen"; + check_and_add_cmake_file(); + Position codePos = findPosition(params.getPosition().getLine(), params.getPosition().getCharacter(), lfPath, result); + if (codePos.getOneBasedLine() == 1 && codePos.getOneBasedColumn() == 1) { + return IHoverService.EMPTY_HOVER; // hover to LF code + } + if (!buildResult.isOpen(generatedPath)) { + buildResult.open(generatedPath); + } + return clangdHover(codePos); + } catch (Exception e) { + return IHoverService.EMPTY_HOVER; // Fail silently + } + } + + private static Position findPosition(int line, int col, Path lfFile, Map generatedResult) { + for (Path sourcePath : generatedResult.keySet()) { + CodeMap codeMap = generatedResult.get(sourcePath); + for (int brutLine = 1; brutLine <= 500; brutLine++) { // TODO change brute force positions + for (int brutChar = 1; brutChar <= 100; brutChar++) { + Position generatedPos = Position.fromOneBased(brutLine, brutChar); + Position p = codeMap.adjusted(lfFile, generatedPos); + if (p.getOneBasedLine() == line && p.getOneBasedColumn() == col) { + generatedPath = sourcePath; + return generatedPos; + } + } + } + } + return Position.fromOneBased(1, 1); + } + + private static Hover clangdHover(Position params) { + send_hover_request(params.getOneBasedLine(), params.getOneBasedColumn()); + Hover resultHover = new Hover(); + try { + String jsonResponse = readOutput(); + jsonResponse = jsonResponse.substring(jsonResponse.indexOf(System.getProperty("line.separator"))+1);// remove first line since contains the header + jsonResponse = jsonResponse.substring(jsonResponse.indexOf(System.getProperty("line.separator"))+1); + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode jsonNode = objectMapper.readTree(jsonResponse); + String jsonValue = jsonNode.get("result").get("contents").get("value").textValue(); + List> content = Collections.singletonList(Either.forLeft(jsonValue)); + resultHover.setContents(content); + // int lvalue_c = jsonNode.get("result").get("range").get("start").get("character").intValue(); + // int lvalue_l = jsonNode.get("result").get("range").get("start").get("line").intValue(); + // int rvalue_c = jsonNode.get("result").get("range").get("end").get("character").intValue(); + // int rvalue_l = jsonNode.get("result").get("range").get("end").get("line").intValue(); + return resultHover; + } catch (Exception e) { + // most often gets here because there is no fields that jsonNode.get() is looking for (i.e. result is null) + return IHoverService.EMPTY_HOVER; + } + } + + private static void check_and_add_cmake_file() { + try { + File cmake_file = new File(generatedDirectory + "compile_commands.json"); + if (!cmake_file.exists()) { + ProcessBuilder cmake_command = new ProcessBuilder("cmake", "-DCMAKE_EXPORT_COMPILE_COMMANDS=1"); + cmake_command.directory(new File(generatedDirectory)); + cmake_command.start(); + } + } catch (Exception e) { + System.out.println("failed creating cmake file"); + } + } + + private static String readOutput() { + char[] chars = new char[8192]; + try { + for(int len; (len = reader.read(chars)) > 0 && reader.ready();) { + + } + } catch (Exception e) { + System.out.println("failed reading output"); + } + return String.valueOf(chars); + } + + private static void process_init() { + try { + ProcessBuilder clangd = new ProcessBuilder("clangd"); + pr = clangd.start(); + + stdin = pr.getOutputStream(); + stdout = pr.getInputStream(); + reader = new BufferedReader (new InputStreamReader(stdout)); + writer = new BufferedWriter(new OutputStreamWriter(stdin)); + + errorReader = new BufferedReader(new InputStreamReader(pr.getErrorStream())); + } catch (Exception e) { + System.out.println("failed process init"); + } + } + + private static void readErrorOutput() { + try { + String str = null; + while ((str = errorReader.readLine()) != null && errorReader.ready()) { + System.out.println(str); + } + } catch (Exception e) { + System.out.println("failed reading error output"); + } + } + + private static void send_hover_request(int line, int character) { + try { + String body = "{\"method\": \"textDocument/hover\",\"jsonrpc\": \"2.0\",\"id\": 1,\"params\": {\"textDocument\": {\"uri\": \"file://" + generatedPath.toString() + "\"} ,\"position\": {\"line\": "+line+",\"character\": "+character+"}}}\r\n"; + String header = "Content-Length: " + body.length() +"\r\n\r\n"; + String cmd = header + body; + writer.write(cmd); + writer.flush(); + } catch (Exception e) { + System.out.println("failed hover request"); + } + + } + + private static void clangd_init() { + try { + String body = "{\"jsonrpc\": \"2.0\", \"method\": \"initialize\", \"id\": 1, \"params\": {\"rootUri\": \"file://" + generatedDirectory + "\", \"capabilities\": {\"hover\": {\"dynamicRegistration\": false, \"contentFormat\": []}}}}\r\n"; + String header = "Content-Length: " + body.length() +"\r\n\r\n"; + String cmd = header + body; + writer.write(cmd); + writer.flush(); + } catch (Exception e) { + System.out.println("failed clangd init"); + } + } + + private static void did_open_clangd(Path path) { + + try { + String fileContent = Files.readString(path, StandardCharsets.UTF_8); + fileContent = fileContent.replaceAll("\"", "\\\\\""); + fileContent = fileContent.replaceAll("\n", "\\\\n"); + String body = "{\"method\": \"textDocument/didOpen\", \"jsonrpc\": \"2.0\", \"params\": {\"textDocument\": {\"uri\": \"file://" + path + "\", \"languageId\": \"cpp\", \"version\": 1, \"text\": \""+fileContent+"\"}}}\r\n"; + String header = "Content-Length: " + (body.length()) +"\r\n\r\n"; + String cmd = header + body; + writer.write(cmd); + writer.flush(); + } catch (Exception e) { + System.out.println("failed clangd did open"); + } + + } + + private static void did_close_clangd(String filePath) { + try { + String body = "{\"method\": \"textDocument/didClose\", \"jsonrpc\": \"2.0\", \"params\": {\"textDocument\": {\"uri\": \"file://" + filePath + "\"}}}\r\n"; + String header = "Content-Length: " + (body.length()) +"\r\n\r\n"; + String cmd = header + body; + writer.write(cmd); + writer.flush(); + } catch (Exception e) { + System.out.println("failed clangd did close"); + } + } + + static class ProjectBuildResult { + Path uri; + GeneratorResult generatorResult; + HashSet clangOpenFiles; + + public ProjectBuildResult(Path u, GeneratorResult gr) { + uri = u; + generatorResult = gr; + clangOpenFiles = new HashSet(); + } + public GeneratorResult getGeneratorResult() { + return generatorResult; + } + public String path() { + return uri.toString(); + } + + public boolean isOpen(Path p) { + return clangOpenFiles.contains(p); + } + public void open(Path p) { + did_open_clangd(p); + readOutput(); + clangOpenFiles.add(p); + } + public void closeOpenFiles() { + for (Path gen_path : clangOpenFiles) { + did_close_clangd(gen_path.toString()); + readOutput(); + } + } + + } + + public static void addBuild(Path uri, GeneratorResult generatorResult) { + ProjectBuildResult buildResult = new ProjectBuildResult(uri, generatorResult); + if (storedBuildResults.containsKey(uri)) { + // update build if was re-built + ProjectBuildResult result = storedBuildResults.get(uri); + result.closeOpenFiles(); + storedBuildResults.remove(uri); + } + storedBuildResults.put(uri, buildResult); + } +} \ No newline at end of file diff --git a/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServer.java b/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServer.java index be1253f5d4..32da33c1a7 100644 --- a/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServer.java +++ b/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServer.java @@ -8,32 +8,7 @@ import org.eclipse.xtext.ide.server.hover.IHoverService; import org.eclipse.xtext.util.CancelIndicator; -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.nio.file.Path; -import org.lflang.generator.CodeMap; -import java.nio.charset.StandardCharsets; -import java.io.OutputStream; -import java.io.InputStream; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.JsonNode; -import java.util.List; -import java.util.Collections; -import org.eclipse.lsp4j.jsonrpc.messages.Either; -import org.eclipse.lsp4j.MarkedString; -import java.util.Map; -import org.lflang.generator.Position; -import org.lflang.generator.Range; -import java.util.NavigableMap; -import java.util.HashMap; -// import javafx.util.Pair; - - -import org.lflang.generator.GeneratorResult; +import org.lflang.diagram.lsp.CppLanguageServer; /** * The Lingua Franca language and diagram server. @@ -45,6 +20,7 @@ public class LFLanguageServer extends KGraphLanguageServerExtension { public void cancelProgress(WorkDoneProgressCancelParams params) { Progress.cancel(params.getToken().getRight().intValue()); } + boolean initialized = false; @Override protected Hover hover(HoverParams params, CancelIndicator cancelIndicator) { @@ -52,217 +28,11 @@ protected Hover hover(HoverParams params, CancelIndicator cancelIndicator) { // upstream of the ungraceful handling (IndexOutOfBoundsException) of said mistake. This patch is applied here // simply because it is easy. This would be done differently were it not for the fact that we plan to rebuild // this infrastructure from scratch anyway. - try { - int currentBuildToken = LFLanguageServerExtension.getBuildToken(); - Map result = LFLanguageServerExtension.getGeneratorResult().getCodeMaps(); - generatedDirectory = LFLanguageServerExtension.getGeneratorResult().getCommand().directory().toString() + "/src-gen"; - if (currentBuildToken != buildToken) { - buildToken = currentBuildToken; - initialized = false; - // getCodeMaps(); - - } - Path lfPath = LFLanguageServerExtension.getBuildPath(); - System.out.println("lfpath is " + lfPath); - Position codePos = findPosition(params.getPosition().getLine(), params.getPosition().getCharacter(), lfPath, result); - // Position codePos = findPositionInvertedMap(lfPath); - return clangdHover(codePos); - // return super.hover(params, cancelIndicator); - } catch (IndexOutOfBoundsException e) { - return IHoverService.EMPTY_HOVER; // Fail silently - } - } - - // void getCodeMaps() { - // Map codeMaps = LFLanguageServerExtension.getGeneratorResult().getCodeMaps(); - // for (Map.Entry entry : codeMaps.entrySet()) { - // Path generatedPath = entry.getKey(); - // Map> map = entry.getValue().getCodeMap(); - // // Map> constructedPathMap; - // for (Map.Entry> pathEntry : map.entrySet()) { - // NavigableMap pathMap = pathEntry.getValue(); - // Path lfPath = pathEntry.getKey(); - // // NavigableMap constructedRangeMap; - // for (Map.Entry rangeEntry : pathMap.entrySet()) { - // // constructedRangeMap.put(rangeEntry.getValue(), rangeEntry.getKey()); - // Range generatedRange = rangeEntry.getKey(); - // Range lfRange = rangeEntry.getValue(); - // insertToRangeMap(lfPath, generatedPath, lfRange, generatedRange); - // } - // } - // } - // } - - // void insertToRangeMap(Path lfPath, Path generatedPath, Range lfRange, Range generatedRange) { - // if (codeMapping == null) codeMapping = new HashMap>>(); - // HashMap> lfMap = codeMapping.get(lfPath); - // if (lfMap == null) { - // lfMap = new HashMap>(); - // codeMapping.put(lfPath, lfMap); - // } - // HashMap pathMap = lfMap.get(generatedPath); - // if (pathMap == null) { - // pathMap = new HashMap(); - // lfMap.put(generatedPath, pathMap); - // } - // pathMap.put(lfRange, generatedRange); - // System.out.println("putting " + lfPath + " " + generatedPath + " " + lfRange + " " + generatedRange); - // } - - // void insertToRangeMap(Path lfPath, Path generatedPath, Range lfRange, Range generatedRange) { - // if (codeMapping == null) codeMapping = new HashMap, Pair>(); - // codeMapping.put(new Pair(lfPath, lfRange), new Pair(generatedPath, generatedRange)); - // System.out.println("putting " + lfPath + " " + generatedPath + " " + lfRange + " " + generatedRange); - // } - - OutputStream stdin = null; - static Process pr; - InputStream stdout = null; - BufferedReader reader = null; - BufferedWriter writer = null; - BufferedReader errorReader = null; - boolean initialized = false; - int buildToken = -1; - Path generatedPath; - String generatedDirectory; - // HashMap>> codeMapping = null; - // HashMap, - // Pair> codeMapping = null; - - private Position findPosition(int line, int col, Path lfFile, Map generatedResult) { - for (Path sourcePath : generatedResult.keySet()) { - CodeMap codeMap = generatedResult.get(sourcePath); - for (int brutLine = 1; brutLine <= 500; brutLine++) { - for (int brutChar = 1; brutChar <= 100; brutChar++) { - Position generatedPos = Position.fromOneBased(brutLine, brutChar); - Position p = codeMap.adjusted(lfFile, generatedPos); - if (p.getOneBasedLine() == line && p.getOneBasedColumn() == col) { - generatedPath = sourcePath; - return generatedPos; - } - } - } - } - - return Position.fromOneBased(1, 1); - } - - private Hover clangdHover(Position params) { - if (!initialized){ - process_init(); + if (!initialized) { + CppLanguageServer.init(); initialized = true; - clangd_init(); - readOutput(); - did_open_clangd(); - readOutput(); - } - - send_hover_request(params.getOneBasedLine(), params.getOneBasedColumn()); - Hover resultHover = new Hover(); - try { - String jsonResponse = readOutput(); - jsonResponse = jsonResponse.substring(jsonResponse.indexOf(System.getProperty("line.separator"))+1);// remove first line since contains the header - jsonResponse = jsonResponse.substring(jsonResponse.indexOf(System.getProperty("line.separator"))+1); - ObjectMapper objectMapper = new ObjectMapper(); - JsonNode jsonNode = objectMapper.readTree(jsonResponse); - String jsonValue = jsonNode.get("result").get("contents").get("value").textValue(); - List> content = Collections.singletonList(Either.forLeft(jsonValue)); - resultHover.setContents(content); - int lvalue_c = jsonNode.get("result").get("range").get("start").get("character").intValue(); - int lvalue_l = jsonNode.get("result").get("range").get("start").get("line").intValue(); - int rvalue_c = jsonNode.get("result").get("range").get("end").get("character").intValue(); - int rvalue_l = jsonNode.get("result").get("range").get("end").get("line").intValue(); - return resultHover; - } catch (Exception e) { - System.out.println("main failed"); - } - return null; - } - - private String readOutput() { - char[] chars = new char[8192]; - try { - for(int len; (len = reader.read(chars)) > 0 && reader.ready();) { - - } - } catch (Exception e) { - System.out.println("failed reading output"); } - return String.valueOf(chars); - } - - private void process_init() { - try { - ProcessBuilder pb = new ProcessBuilder("clangd"); - pr = pb.start(); - - stdin = pr.getOutputStream(); - stdout = pr.getInputStream(); - reader = new BufferedReader (new InputStreamReader(stdout)); - writer = new BufferedWriter(new OutputStreamWriter(stdin)); - - errorReader = new BufferedReader(new InputStreamReader(pr.getErrorStream())); - } catch (Exception e) { - System.out.println("failed process init"); - } - } - - private void readErrorOutput() { - try { - String str = null; - System.out.println("reading error output"); - while ((str = errorReader.readLine()) != null && errorReader.ready()) { - System.out.println(str); - } - } catch (Exception e) { - System.out.println("failed reading error"); - } - } - - private void send_hover_request(int line, int character) { - try { - String body = "{\"method\": \"textDocument/hover\",\"jsonrpc\": \"2.0\",\"id\": 1,\"params\": {\"textDocument\": {\"uri\": \"file://" + generatedPath.toString() + "\"} ,\"position\": {\"line\": "+line+",\"character\": "+character+"}}}\r\n"; - String header = "Content-Length: " + body.length() +"\r\n\r\n"; - String cmd = header + body; - writer.write(cmd); - writer.flush(); - } catch (Exception e) { - System.out.println("failed hover request"); - } - - } - - private void clangd_init() { - try { - String body = "{\"jsonrpc\": \"2.0\", \"method\": \"initialize\", \"id\": 1, \"params\": {\"rootUri\": \"file://" + generatedDirectory + "\", \"capabilities\": {\"hover\": {\"dynamicRegistration\": false, \"contentFormat\": []}}}}\r\n"; - String header = "Content-Length: " + body.length() +"\r\n\r\n"; - String cmd = header + body; - - writer.write(cmd); - writer.flush(); - - - } catch (Exception e) { - System.out.println("failed clangd init"); - } - } - - private void did_open_clangd() { - - try { - String fileContent = Files.readString(generatedPath, StandardCharsets.UTF_8); - fileContent = fileContent.replaceAll("\"", "\\\\\""); - fileContent = fileContent.replaceAll("\n", "\\\\n"); - String body = "{\"method\": \"textDocument/didOpen\", \"jsonrpc\": \"2.0\", \"params\": {\"textDocument\": {\"uri\": \"file://" + generatedPath + "\", \"languageId\": \"cpp\", \"version\": 1, \"text\": \""+fileContent+"\"}}}\r\n"; - String header = "Content-Length: " + (body.length()) +"\r\n\r\n"; - String cmd = header + body; - writer.write(cmd); - writer.flush(); - } catch (Exception e) { - System.out.println("failed clangd did open"); - } - + return CppLanguageServer.hoverRequest(params); + // return IHoverService.EMPTY_HOVER; // Fail silently } } diff --git a/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServerExtension.java b/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServerExtension.java index c717a4e06e..a591579f5f 100644 --- a/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServerExtension.java +++ b/org.lflang.diagram/src/org/lflang/diagram/lsp/LFLanguageServerExtension.java @@ -80,6 +80,7 @@ public CompletableFuture build(String uri) { */ @JsonNotification("generator/partialBuild") public void partialBuild(String uri) { + System.out.println("building on " + uri); if (client == null) return; buildWithProgress(client, uri, false); } @@ -139,12 +140,11 @@ private GeneratorResult buildWithProgress(LanguageClient client, String uri, boo } finally { progress.end(result == null ? "An internal error occurred." : result.getUserMessage()); } - // System.out.println("heheheheheeh"); - // System.out.println(result.getCodeMaps()); - buildResult = result; - buildToken += 1; - buildPath = Paths.get(uri.substring(5)); // remove "file:" + // buildResult = result; + // buildToken += 1; + // buildPath = Paths.get(uri.substring(5)); // remove "file:" + CppLanguageServer.addBuild(Paths.get(uri.substring(5)), result); return result; } }