From 9039a041ce1d85a49e6a9ac87735c4110b1d0459 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Wed, 15 Jun 2022 17:38:05 -0300 Subject: [PATCH 01/11] feat: add diagnostics syntax --- analysis/src/Cli.ml | 2 ++ analysis/src/Commands.ml | 6 ++++++ analysis/src/Diagnostics.ml | 27 +++++++++++++++++++++++++++ analysis/src/Protocol.ml | 23 +++++++++++++++++++++-- analysis/tests/src/Diagnostics.res | 3 +++ 5 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 analysis/src/Diagnostics.ml create mode 100644 analysis/tests/src/Diagnostics.res diff --git a/analysis/src/Cli.ml b/analysis/src/Cli.ml index a49326d92..13e72f9e2 100644 --- a/analysis/src/Cli.ml +++ b/analysis/src/Cli.ml @@ -88,6 +88,8 @@ let main () = Commands.codeAction ~path ~pos:(int_of_string line, int_of_string col) ~currentFile ~debug:false + | [_; "diagnosticSyntax"; path;] -> + Commands.diagnosticSyntax ~path | _ :: "reanalyze" :: _ -> let len = Array.length Sys.argv in for i = 1 to len - 2 do diff --git a/analysis/src/Commands.ml b/analysis/src/Commands.ml index 65066f8a8..2bb1d2d4f 100644 --- a/analysis/src/Commands.ml +++ b/analysis/src/Commands.ml @@ -257,6 +257,12 @@ let format ~path = signature else "" +let diagnosticSyntax ~path = + print_endline + (match Diagnostics.document_syntax ~path with + | [] -> Protocol.null + | d -> "[\n" ^ String.concat ",\n" d ^ "\n]") + let test ~path = Uri.stripPath := true; match Files.readFile path with diff --git a/analysis/src/Diagnostics.ml b/analysis/src/Diagnostics.ml new file mode 100644 index 000000000..0ecc39087 --- /dev/null +++ b/analysis/src/Diagnostics.ml @@ -0,0 +1,27 @@ +let document_syntax ~path = + let parse = + Res_driver.parsingEngine.parseImplementation ~forPrinter:false + ~filename:path + in + match parse.diagnostics with + | [] -> [] + | diagnostics -> + diagnostics + |> List.map (fun diagnostic -> + let _, startline, startcol = + Location.get_pos_info (Res_diagnostics.getStartPos diagnostic) + in + let _, endline, endcol = + Location.get_pos_info (Res_diagnostics.getEndPos diagnostic) + in + Protocol.stringifyDiagnostic + { + range = + { + start = {line = startline; character = startcol}; + end_ = {line = endline; character = endcol}; + }; + message = Res_diagnostics.explain diagnostic; + severity = Error; + }) + |> List.rev \ No newline at end of file diff --git a/analysis/src/Protocol.ml b/analysis/src/Protocol.ml index 652f68f30..ae3e92ca3 100644 --- a/analysis/src/Protocol.ml +++ b/analysis/src/Protocol.ml @@ -10,12 +10,18 @@ type completionItem = { documentation : markupContent option; } -type hover = string type location = {uri : string; range : range} type documentSymbolItem = {name : string; kind : int; location : location} type renameFile = {oldUri : string; newUri : string} type textEdit = {range : range; newText : string} +type diagnosticSeverity = Error | Warning | Information | Hint +type diagnostic = { + range : range; + message : string; + severity : diagnosticSeverity; +} + type optionalVersionedTextDocumentIdentifier = { version : int option; uri : string; @@ -89,7 +95,7 @@ let stringifyRenameFile {oldUri; newUri} = }|} (Json.escape oldUri) (Json.escape newUri) -let stringifyTextEdit te = +let stringifyTextEdit (te : textEdit) = Printf.sprintf {|{ "range": %s, "newText": "%s" @@ -123,3 +129,16 @@ let stringifyCodeAction ca = Printf.sprintf {|{"title": "%s", "kind": "%s", "edit": %s}|} ca.title (codeActionKindToString ca.codeActionKind) (ca.edit |> stringifyCodeActionEdit) + +let stringifyDiagnostic d = + Printf.sprintf {|{ + "range: %s, + "message": "%s", + "severity": %d, +}|} + (stringifyRange d.range) (Json.escape d.message) + (match d.severity with + | Error -> 1 + | Warning -> 2 + | Information -> 3 + | Hint -> 4) \ No newline at end of file diff --git a/analysis/tests/src/Diagnostics.res b/analysis/tests/src/Diagnostics.res new file mode 100644 index 000000000..5f61b20bb --- /dev/null +++ b/analysis/tests/src/Diagnostics.res @@ -0,0 +1,3 @@ +let = 1 + 1.0 +let add = =2 +lett a = 2 \ No newline at end of file From c74af0a073498e97bc9d5769742ca1635effd0a6 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Wed, 15 Jun 2022 21:07:49 -0300 Subject: [PATCH 02/11] add help to cli --- analysis/src/Cli.ml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/analysis/src/Cli.ml b/analysis/src/Cli.ml index 13e72f9e2..7e0f1bb77 100644 --- a/analysis/src/Cli.ml +++ b/analysis/src/Cli.ml @@ -10,6 +10,7 @@ API examples: ./rescript-editor-analysis.exe hover src/MyFile.res 10 2 ./rescript-editor-analysis.exe references src/MyFile.res 10 2 ./rescript-editor-analysis.exe rename src/MyFile.res 10 2 foo + ./rescript-editor-analysis.exe diagnosticSyntax src/MyFile.res Dev-time examples: ./rescript-editor-analysis.exe dump src/MyFile.res src/MyFile2.res @@ -60,6 +61,10 @@ Options: ./rescript-editor-analysis.exe format src/MyFile.res + diagnosticSyntax: print to stdout diagnostic for syntax + + ./rescript-editor-analysis.exe diagnosticSyntax src/MyFile.res + test: run tests specified by special comments in file src/MyFile.res ./rescript-editor-analysis.exe test src/src/MyFile.res From 6b4acd03325c8ae48a80469a6025ab53cbec7bcf Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Wed, 15 Jun 2022 21:09:26 -0300 Subject: [PATCH 03/11] add tests --- analysis/src/Commands.ml | 3 ++- analysis/src/Diagnostics.ml | 5 ++--- analysis/tests/bsconfig.json | 1 + .../tests/src/{ => not_compiled}/Diagnostics.res | 4 +++- .../src/not_compiled/expected/Diagnostics.res.txt | 13 +++++++++++++ analysis/tests/test.sh | 9 +++++++++ 6 files changed, 30 insertions(+), 5 deletions(-) rename analysis/tests/src/{ => not_compiled}/Diagnostics.res (60%) create mode 100644 analysis/tests/src/not_compiled/expected/Diagnostics.res.txt diff --git a/analysis/src/Commands.ml b/analysis/src/Commands.ml index 2bb1d2d4f..3c25794a4 100644 --- a/analysis/src/Commands.ml +++ b/analysis/src/Commands.ml @@ -261,7 +261,7 @@ let diagnosticSyntax ~path = print_endline (match Diagnostics.document_syntax ~path with | [] -> Protocol.null - | d -> "[\n" ^ String.concat ",\n" d ^ "\n]") + | d -> Protocol.array d) let test ~path = Uri.stripPath := true; @@ -384,6 +384,7 @@ let test ~path = Printf.printf "%s\nnewText:\n%s<--here\n%s%s\n" (Protocol.stringifyRange range) indent indent newText))) + | "dia" -> diagnosticSyntax ~path | _ -> ()); print_newline ()) in diff --git a/analysis/src/Diagnostics.ml b/analysis/src/Diagnostics.ml index 0ecc39087..981eacc3b 100644 --- a/analysis/src/Diagnostics.ml +++ b/analysis/src/Diagnostics.ml @@ -7,7 +7,7 @@ let document_syntax ~path = | [] -> [] | diagnostics -> diagnostics - |> List.map (fun diagnostic -> + |> List.rev_map (fun diagnostic -> let _, startline, startcol = Location.get_pos_info (Res_diagnostics.getStartPos diagnostic) in @@ -23,5 +23,4 @@ let document_syntax ~path = }; message = Res_diagnostics.explain diagnostic; severity = Error; - }) - |> List.rev \ No newline at end of file + }) \ No newline at end of file diff --git a/analysis/tests/bsconfig.json b/analysis/tests/bsconfig.json index ab6be18e9..4a80fbe5d 100644 --- a/analysis/tests/bsconfig.json +++ b/analysis/tests/bsconfig.json @@ -9,6 +9,7 @@ "subdirs": true } ], + "ignored-dirs": ["src/not_compiled"], "bsc-flags": ["-w -33-44"], "bs-dependencies": ["@rescript/react"], "reason": { "react-jsx": 3 } diff --git a/analysis/tests/src/Diagnostics.res b/analysis/tests/src/not_compiled/Diagnostics.res similarity index 60% rename from analysis/tests/src/Diagnostics.res rename to analysis/tests/src/not_compiled/Diagnostics.res index 5f61b20bb..c81f1e63e 100644 --- a/analysis/tests/src/Diagnostics.res +++ b/analysis/tests/src/not_compiled/Diagnostics.res @@ -1,3 +1,5 @@ let = 1 + 1.0 let add = =2 -lett a = 2 \ No newline at end of file +lett a = 2 + +//^dia \ No newline at end of file diff --git a/analysis/tests/src/not_compiled/expected/Diagnostics.res.txt b/analysis/tests/src/not_compiled/expected/Diagnostics.res.txt new file mode 100644 index 000000000..77a2155a1 --- /dev/null +++ b/analysis/tests/src/not_compiled/expected/Diagnostics.res.txt @@ -0,0 +1,13 @@ +[{ + "range: {"start": {"line": 1, "character": 4}, "end": {"line": 1, "character": 5}}, + "message": "I was expecting a name for this let-binding. Example: `let message = \"hello\"`", + "severity": 1, +}, { + "range: {"start": {"line": 2, "character": 9}, "end": {"line": 2, "character": 11}}, + "message": "This let-binding misses an expression", + "severity": 1, +}, { + "range: {"start": {"line": 3, "character": 4}, "end": {"line": 3, "character": 6}}, + "message": "consecutive statements on a line must be separated by ';' or a newline", + "severity": 1, +}] \ No newline at end of file diff --git a/analysis/tests/test.sh b/analysis/tests/test.sh index 27a98ffb2..d598c37ed 100755 --- a/analysis/tests/test.sh +++ b/analysis/tests/test.sh @@ -7,6 +7,15 @@ for file in src/*.{res,resi}; do fi done +for file in src/not_compiled/*.res; do + output="$(dirname $file)/expected/$(basename $file).txt" + ../rescript-editor-analysis.exe test $file &> $output + # CI. We use LF, and the CI OCaml fork prints CRLF. Convert. + if [ "$RUNNER_OS" == "Windows" ]; then + perl -pi -e 's/\r\n/\n/g' -- $output + fi +done + warningYellow='\033[0;33m' successGreen='\033[0;32m' reset='\033[0m' From 3d1e5c1cdcf35a42437757d04ffc32637971f796 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Wed, 15 Jun 2022 21:29:44 -0300 Subject: [PATCH 04/11] fix tests --- analysis/tests/bsconfig.json | 1 - analysis/tests/{src => }/not_compiled/Diagnostics.res | 0 .../tests/{src => }/not_compiled/expected/Diagnostics.res.txt | 3 ++- analysis/tests/test.sh | 2 +- 4 files changed, 3 insertions(+), 3 deletions(-) rename analysis/tests/{src => }/not_compiled/Diagnostics.res (100%) rename analysis/tests/{src => }/not_compiled/expected/Diagnostics.res.txt (99%) diff --git a/analysis/tests/bsconfig.json b/analysis/tests/bsconfig.json index 4a80fbe5d..ab6be18e9 100644 --- a/analysis/tests/bsconfig.json +++ b/analysis/tests/bsconfig.json @@ -9,7 +9,6 @@ "subdirs": true } ], - "ignored-dirs": ["src/not_compiled"], "bsc-flags": ["-w -33-44"], "bs-dependencies": ["@rescript/react"], "reason": { "react-jsx": 3 } diff --git a/analysis/tests/src/not_compiled/Diagnostics.res b/analysis/tests/not_compiled/Diagnostics.res similarity index 100% rename from analysis/tests/src/not_compiled/Diagnostics.res rename to analysis/tests/not_compiled/Diagnostics.res diff --git a/analysis/tests/src/not_compiled/expected/Diagnostics.res.txt b/analysis/tests/not_compiled/expected/Diagnostics.res.txt similarity index 99% rename from analysis/tests/src/not_compiled/expected/Diagnostics.res.txt rename to analysis/tests/not_compiled/expected/Diagnostics.res.txt index 77a2155a1..30e2ff8be 100644 --- a/analysis/tests/src/not_compiled/expected/Diagnostics.res.txt +++ b/analysis/tests/not_compiled/expected/Diagnostics.res.txt @@ -10,4 +10,5 @@ "range: {"start": {"line": 3, "character": 4}, "end": {"line": 3, "character": 6}}, "message": "consecutive statements on a line must be separated by ';' or a newline", "severity": 1, -}] \ No newline at end of file +}] + diff --git a/analysis/tests/test.sh b/analysis/tests/test.sh index d598c37ed..c931641e9 100755 --- a/analysis/tests/test.sh +++ b/analysis/tests/test.sh @@ -7,7 +7,7 @@ for file in src/*.{res,resi}; do fi done -for file in src/not_compiled/*.res; do +for file in not_compiled/*.res; do output="$(dirname $file)/expected/$(basename $file).txt" ../rescript-editor-analysis.exe test $file &> $output # CI. We use LF, and the CI OCaml fork prints CRLF. Convert. From 0a681efe665266f97cab6797d72a3467b5ca1314 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sat, 18 Jun 2022 00:53:29 -0300 Subject: [PATCH 05/11] fix stringify --- analysis/src/Commands.ml | 2 +- analysis/src/Protocol.ml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/analysis/src/Commands.ml b/analysis/src/Commands.ml index 3c25794a4..16b023593 100644 --- a/analysis/src/Commands.ml +++ b/analysis/src/Commands.ml @@ -260,7 +260,7 @@ let format ~path = let diagnosticSyntax ~path = print_endline (match Diagnostics.document_syntax ~path with - | [] -> Protocol.null + | [] -> "[]" | d -> Protocol.array d) let test ~path = diff --git a/analysis/src/Protocol.ml b/analysis/src/Protocol.ml index ae3e92ca3..a5218363f 100644 --- a/analysis/src/Protocol.ml +++ b/analysis/src/Protocol.ml @@ -132,7 +132,7 @@ let stringifyCodeAction ca = let stringifyDiagnostic d = Printf.sprintf {|{ - "range: %s, + "range": %s, "message": "%s", "severity": %d, }|} From 3b41ad0c8ab1e488f46f7492d3dd226ca8fd5f97 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sat, 18 Jun 2022 02:09:35 -0300 Subject: [PATCH 06/11] fix position --- analysis/src/Diagnostics.ml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/analysis/src/Diagnostics.ml b/analysis/src/Diagnostics.ml index 981eacc3b..ebd0109e5 100644 --- a/analysis/src/Diagnostics.ml +++ b/analysis/src/Diagnostics.ml @@ -18,8 +18,8 @@ let document_syntax ~path = { range = { - start = {line = startline; character = startcol}; - end_ = {line = endline; character = endcol}; + start = {line = startline - 1; character = startcol}; + end_ = {line = endline - 1; character = endcol}; }; message = Res_diagnostics.explain diagnostic; severity = Error; From d11c1433c1317c364ca94120e8d842bc9744a2f3 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sat, 18 Jun 2022 02:09:54 -0300 Subject: [PATCH 07/11] add source --- analysis/src/Protocol.ml | 1 + 1 file changed, 1 insertion(+) diff --git a/analysis/src/Protocol.ml b/analysis/src/Protocol.ml index a5218363f..b8ceac8b9 100644 --- a/analysis/src/Protocol.ml +++ b/analysis/src/Protocol.ml @@ -135,6 +135,7 @@ let stringifyDiagnostic d = "range": %s, "message": "%s", "severity": %d, + "source": "ReScript" }|} (stringifyRange d.range) (Json.escape d.message) (match d.severity with From f9de5e06f8f1f4a84e86dd400f398110da8b9483 Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sat, 18 Jun 2022 02:14:13 -0300 Subject: [PATCH 08/11] fix test --- analysis/tests/not_compiled/expected/Diagnostics.res.txt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/analysis/tests/not_compiled/expected/Diagnostics.res.txt b/analysis/tests/not_compiled/expected/Diagnostics.res.txt index 30e2ff8be..d0ef667ff 100644 --- a/analysis/tests/not_compiled/expected/Diagnostics.res.txt +++ b/analysis/tests/not_compiled/expected/Diagnostics.res.txt @@ -1,14 +1,17 @@ [{ - "range: {"start": {"line": 1, "character": 4}, "end": {"line": 1, "character": 5}}, + "range": {"start": {"line": 0, "character": 4}, "end": {"line": 0, "character": 5}}, "message": "I was expecting a name for this let-binding. Example: `let message = \"hello\"`", "severity": 1, + "source": "ReScript" }, { - "range: {"start": {"line": 2, "character": 9}, "end": {"line": 2, "character": 11}}, + "range": {"start": {"line": 1, "character": 9}, "end": {"line": 1, "character": 11}}, "message": "This let-binding misses an expression", "severity": 1, + "source": "ReScript" }, { - "range: {"start": {"line": 3, "character": 4}, "end": {"line": 3, "character": 6}}, + "range": {"start": {"line": 2, "character": 4}, "end": {"line": 2, "character": 6}}, "message": "consecutive statements on a line must be separated by ';' or a newline", "severity": 1, + "source": "ReScript" }] From 7519dceb67d28927faaa8f944da1dbb8aaa4963a Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Sat, 18 Jun 2022 02:37:55 -0300 Subject: [PATCH 09/11] feat(server): syntax error as you type --- server/src/server.ts | 31 ++++++++++++++++++++++++++++++- server/src/utils.ts | 8 ++++---- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/server/src/server.ts b/server/src/server.ts index eb6b36afd..a01048248 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -50,7 +50,7 @@ let projectsFiles: Map< let codeActionsFromDiagnostics: codeActions.filesCodeActions = {}; // will be properly defined later depending on the mode (stdio/node-rpc) -let send: (msg: p.Message) => void = (_) => {}; +let send: (msg: p.Message) => void = (_) => { }; interface CreateInterfaceRequestParams { uri: string; @@ -588,6 +588,33 @@ function format(msg: p.RequestMessage): Array { } } +const updateDiagnosticSyntax = (fileUri: string, fileContent: string) => { + const filePath = fileURLToPath(fileUri); + const tmpname = utils.createFileInTempDir(); + fs.writeFileSync(tmpname, fileContent, { encoding: "utf-8" }); + + const items: p.Diagnostic[] | [] = utils.runAnalysisAfterSanityCheck( + filePath, + [ + "diagnosticSyntax", + tmpname + ], + ); + + const notification: p.NotificationMessage = { + jsonrpc: c.jsonrpcVersion, + method: "textDocument/publishDiagnostics", + params: { + uri: fileUri, + diagnostics: items ?? [] + } + } + + fs.unlink(tmpname, () => null); + + send(notification) +} + function createInterface(msg: p.RequestMessage): p.Message { let params = msg.params as CreateInterfaceRequestParams; let extension = path.extname(params.uri); @@ -774,6 +801,7 @@ function onMessage(msg: p.Message) { } else if (msg.method === DidOpenTextDocumentNotification.method) { let params = msg.params as p.DidOpenTextDocumentParams; openedFile(params.textDocument.uri, params.textDocument.text); + updateDiagnosticSyntax(params.textDocument.uri, params.textDocument.text); } else if (msg.method === DidChangeTextDocumentNotification.method) { let params = msg.params as p.DidChangeTextDocumentParams; let extName = path.extname(params.textDocument.uri); @@ -787,6 +815,7 @@ function onMessage(msg: p.Message) { params.textDocument.uri, changes[changes.length - 1].text ); + updateDiagnosticSyntax(params.textDocument.uri, changes[changes.length - 1].text); } } } else if (msg.method === DidCloseTextDocumentNotification.method) { diff --git a/server/src/utils.ts b/server/src/utils.ts index c3176697c..c67fb5fd7 100644 --- a/server/src/utils.ts +++ b/server/src/utils.ts @@ -603,9 +603,9 @@ export let parseCompilerLogOutput = ( // 10 ┆ } else if (line.startsWith(" ")) { // part of the actual diagnostics message - parsedDiagnostics[parsedDiagnostics.length - 1].content.push( - line.slice(2) - ); + parsedDiagnostics[parsedDiagnostics.length - 1].content.push( + line.slice(2) + ); } else if (line.trim() != "") { // We'll assume that everything else is also part of the diagnostics too. // Most of these should have been indented 2 spaces; sadly, some of them @@ -635,7 +635,7 @@ export let parseCompilerLogOutput = ( range, source: "ReScript", // remove start and end whitespaces/newlines - message: diagnosticMessage.join("\n").trim() + "\n", + message: diagnosticMessage.join("\n").trim(), }; // Check for potential code actions From 39079ad4db9e3f97c03cd13bb607895daf972ead Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Wed, 29 Jun 2022 12:58:38 -0300 Subject: [PATCH 10/11] support .resi --- analysis/src/Diagnostics.ml | 58 ++++++++++++++++++++++--------------- server/src/server.ts | 5 ++-- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/analysis/src/Diagnostics.ml b/analysis/src/Diagnostics.ml index ebd0109e5..23130f82c 100644 --- a/analysis/src/Diagnostics.ml +++ b/analysis/src/Diagnostics.ml @@ -1,26 +1,36 @@ let document_syntax ~path = - let parse = - Res_driver.parsingEngine.parseImplementation ~forPrinter:false - ~filename:path + let get_diagnostics diagnostics = + match diagnostics with + | [] -> [] + | diagnostics -> + diagnostics + |> List.rev_map (fun diagnostic -> + let _, startline, startcol = + Location.get_pos_info (Res_diagnostics.getStartPos diagnostic) + in + let _, endline, endcol = + Location.get_pos_info (Res_diagnostics.getEndPos diagnostic) + in + Protocol.stringifyDiagnostic + { + range = + { + start = {line = startline - 1; character = startcol}; + end_ = {line = endline - 1; character = endcol}; + }; + message = Res_diagnostics.explain diagnostic; + severity = Error; + }) in - match parse.diagnostics with - | [] -> [] - | diagnostics -> - diagnostics - |> List.rev_map (fun diagnostic -> - let _, startline, startcol = - Location.get_pos_info (Res_diagnostics.getStartPos diagnostic) - in - let _, endline, endcol = - Location.get_pos_info (Res_diagnostics.getEndPos diagnostic) - in - Protocol.stringifyDiagnostic - { - range = - { - start = {line = startline - 1; character = startcol}; - end_ = {line = endline - 1; character = endcol}; - }; - message = Res_diagnostics.explain diagnostic; - severity = Error; - }) \ No newline at end of file + if FindFiles.isImplementation path then + let parseImplementation = + Res_driver.parsingEngine.parseImplementation ~forPrinter:false + ~filename:path + in + get_diagnostics parseImplementation.diagnostics + else if FindFiles.isInterface path then + let parseInterface = + Res_driver.parsingEngine.parseInterface ~forPrinter:false ~filename:path + in + get_diagnostics parseInterface.diagnostics + else [] \ No newline at end of file diff --git a/server/src/server.ts b/server/src/server.ts index 2d1429e05..94e3d34a3 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -599,8 +599,9 @@ function format(msg: p.RequestMessage): Array { } const updateDiagnosticSyntax = (fileUri: string, fileContent: string) => { - const filePath = fileURLToPath(fileUri); - const tmpname = utils.createFileInTempDir(); + let filePath = fileURLToPath(fileUri); + let extension = path.extname(filePath); + let tmpname = utils.createFileInTempDir(extension = extension); fs.writeFileSync(tmpname, fileContent, { encoding: "utf-8" }); const items: p.Diagnostic[] | [] = utils.runAnalysisAfterSanityCheck( From d93311a373ca0f72492c92f493baa3f0fd2bea8f Mon Sep 17 00:00:00 2001 From: Pedro Castro Date: Wed, 29 Jun 2022 15:16:03 -0300 Subject: [PATCH 11/11] refactor --- analysis/src/Commands.ml | 4 +- analysis/src/Diagnostics.ml | 39 +++++++++---------- analysis/src/Protocol.ml | 10 ++--- .../not_compiled/expected/Diagnostics.res.txt | 8 ++-- server/src/server.ts | 4 +- 5 files changed, 28 insertions(+), 37 deletions(-) diff --git a/analysis/src/Commands.ml b/analysis/src/Commands.ml index 16b023593..48c4d6338 100644 --- a/analysis/src/Commands.ml +++ b/analysis/src/Commands.ml @@ -259,9 +259,7 @@ let format ~path = let diagnosticSyntax ~path = print_endline - (match Diagnostics.document_syntax ~path with - | [] -> "[]" - | d -> Protocol.array d) + (Diagnostics.document_syntax ~path |> Protocol.array) let test ~path = Uri.stripPath := true; diff --git a/analysis/src/Diagnostics.ml b/analysis/src/Diagnostics.ml index 23130f82c..5ef3673dd 100644 --- a/analysis/src/Diagnostics.ml +++ b/analysis/src/Diagnostics.ml @@ -1,26 +1,23 @@ let document_syntax ~path = let get_diagnostics diagnostics = - match diagnostics with - | [] -> [] - | diagnostics -> - diagnostics - |> List.rev_map (fun diagnostic -> - let _, startline, startcol = - Location.get_pos_info (Res_diagnostics.getStartPos diagnostic) - in - let _, endline, endcol = - Location.get_pos_info (Res_diagnostics.getEndPos diagnostic) - in - Protocol.stringifyDiagnostic - { - range = - { - start = {line = startline - 1; character = startcol}; - end_ = {line = endline - 1; character = endcol}; - }; - message = Res_diagnostics.explain diagnostic; - severity = Error; - }) + diagnostics + |> List.map (fun diagnostic -> + let _, startline, startcol = + Location.get_pos_info (Res_diagnostics.getStartPos diagnostic) + in + let _, endline, endcol = + Location.get_pos_info (Res_diagnostics.getEndPos diagnostic) + in + Protocol.stringifyDiagnostic + { + range = + { + start = {line = startline - 1; character = startcol}; + end_ = {line = endline - 1; character = endcol}; + }; + message = Res_diagnostics.explain diagnostic; + severity = 1; + }) in if FindFiles.isImplementation path then let parseImplementation = diff --git a/analysis/src/Protocol.ml b/analysis/src/Protocol.ml index 71fd33b94..4a03b1508 100644 --- a/analysis/src/Protocol.ml +++ b/analysis/src/Protocol.ml @@ -15,11 +15,10 @@ type documentSymbolItem = {name : string; kind : int; location : location} type renameFile = {oldUri : string; newUri : string} type textEdit = {range : range; newText : string} -type diagnosticSeverity = Error | Warning | Information | Hint type diagnostic = { range : range; message : string; - severity : diagnosticSeverity; + severity : int; } type optionalVersionedTextDocumentIdentifier = { @@ -133,6 +132,7 @@ let stringifyCodeAction ca = (codeActionKindToString ca.codeActionKind) (ca.edit |> stringifyCodeActionEdit) +(* https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#diagnostic *) let stringifyDiagnostic d = Printf.sprintf {|{ "range": %s, @@ -141,8 +141,4 @@ let stringifyDiagnostic d = "source": "ReScript" }|} (stringifyRange d.range) (Json.escape d.message) - (match d.severity with - | Error -> 1 - | Warning -> 2 - | Information -> 3 - | Hint -> 4) \ No newline at end of file + d.severity \ No newline at end of file diff --git a/analysis/tests/not_compiled/expected/Diagnostics.res.txt b/analysis/tests/not_compiled/expected/Diagnostics.res.txt index d0ef667ff..a5df33b71 100644 --- a/analysis/tests/not_compiled/expected/Diagnostics.res.txt +++ b/analysis/tests/not_compiled/expected/Diagnostics.res.txt @@ -1,6 +1,6 @@ [{ - "range": {"start": {"line": 0, "character": 4}, "end": {"line": 0, "character": 5}}, - "message": "I was expecting a name for this let-binding. Example: `let message = \"hello\"`", + "range": {"start": {"line": 2, "character": 4}, "end": {"line": 2, "character": 6}}, + "message": "consecutive statements on a line must be separated by ';' or a newline", "severity": 1, "source": "ReScript" }, { @@ -9,8 +9,8 @@ "severity": 1, "source": "ReScript" }, { - "range": {"start": {"line": 2, "character": 4}, "end": {"line": 2, "character": 6}}, - "message": "consecutive statements on a line must be separated by ';' or a newline", + "range": {"start": {"line": 0, "character": 4}, "end": {"line": 0, "character": 5}}, + "message": "I was expecting a name for this let-binding. Example: `let message = \"hello\"`", "severity": 1, "source": "ReScript" }] diff --git a/server/src/server.ts b/server/src/server.ts index 94e3d34a3..f99ac82d3 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -601,7 +601,7 @@ function format(msg: p.RequestMessage): Array { const updateDiagnosticSyntax = (fileUri: string, fileContent: string) => { let filePath = fileURLToPath(fileUri); let extension = path.extname(filePath); - let tmpname = utils.createFileInTempDir(extension = extension); + let tmpname = utils.createFileInTempDir(extension); fs.writeFileSync(tmpname, fileContent, { encoding: "utf-8" }); const items: p.Diagnostic[] | [] = utils.runAnalysisAfterSanityCheck( @@ -617,7 +617,7 @@ const updateDiagnosticSyntax = (fileUri: string, fileContent: string) => { method: "textDocument/publishDiagnostics", params: { uri: fileUri, - diagnostics: items ?? [] + diagnostics: items } }