Skip to content

feat(analysis): add diagnostics syntax #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 29, 2022
Merged
2 changes: 2 additions & 0 deletions analysis/src/Cli.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions analysis/src/Commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions analysis/src/Diagnostics.ml
Original file line number Diff line number Diff line change
@@ -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
23 changes: 21 additions & 2 deletions analysis/src/Protocol.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)
3 changes: 3 additions & 0 deletions analysis/tests/src/Diagnostics.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
let = 1 + 1.0
let add = =2
lett a = 2