Skip to content

Commit

Permalink
select which kind of errors one wants to show
Browse files Browse the repository at this point in the history
  • Loading branch information
trefis committed Jul 9, 2019
1 parent 8d49a80 commit da78ee4
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 11 deletions.
15 changes: 11 additions & 4 deletions src/frontend/new/new_commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ increasing size of all entities surrounding the position.
;

command "errors"
~spec:[
arg "-lexing" "<bool> Whether to report lexing errors or not"
(Marg.bool (fun l (_,p,t) -> (l,p,t)));
arg "-parsing" "<bool> Whether to report parsing errors or not"
(Marg.bool (fun p (l,_,t) -> (l,p,t)));
arg "-typing" "<bool> Whether to report typing errors or not"
(Marg.bool (fun t (l,p,_) -> (l,p,t)));
]
~doc:"Returns a list of errors in current buffer.
The value is a list where each item as the shape:
Expand All @@ -194,10 +202,9 @@ mark this range.
It reflects whether Merlin was expecting such an error to be possible or not, \
and is useful for debugging purposes.
`message` is the error description to be shown to the user."
~spec:[]
~default:()
begin fun buffer () ->
run buffer (Query_protocol.Errors)
~default:(true, true, true)
begin fun buffer (lexing, parsing, typing) ->
run buffer (Query_protocol.Errors { lexing; parsing; typing })
end
;

Expand Down
2 changes: 1 addition & 1 deletion src/frontend/old/old_IO.ml
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ let request_of_json context =
| [`String "refresh"] ->
request (Sync Refresh)
| [`String "errors"] ->
request (Query Errors)
request (Query (Errors { lexing = true; parsing = true; typing = true }))
| (`String "dump" :: args) ->
request (Query (Dump args))
| [`String "which"; `String "path"; `String name] ->
Expand Down
10 changes: 7 additions & 3 deletions src/frontend/query_commands.ml
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ let dispatch pipeline (type a) : a Query_protocol.t -> a =
let pos = Mpipeline.get_lexing_pos pipeline pos in
Outline.shape pos [Browse_tree.of_browse browse]

| Errors ->
| Errors { lexing; parsing; typing }->
with_typer pipeline @@ fun typer ->
let verbosity = verbosity pipeline in
Printtyp.wrap_printing_env (Mtyper.get_env typer) ~verbosity @@ fun () ->
Expand Down Expand Up @@ -650,8 +650,12 @@ let dispatch pipeline (type a) : a Query_protocol.t -> a =
if n <> 0 then n else
Lexing.compare_pos (error_end e1) (error_end e2)
in
let errors = List.sort_uniq ~cmp
(lexer_errors @ parser_errors @ typer_errors) in
let errors =
List.sort_uniq ~cmp
((if lexing then lexer_errors else []) @
(if parsing then parser_errors else []) @
(if typing then typer_errors else []))
in
(* Filter anything after first parse error *)
let limit = !first_syntax_error in
if limit = Lexing.dummy_pos then errors else (
Expand Down
14 changes: 12 additions & 2 deletions src/frontend/query_json.ml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,17 @@ let dump (type a) : a t -> json =
"end", mk_position pos_end;
]
| Outline -> mk "outline" []
| Errors -> mk "errors" []
| Errors { lexing; parsing; typing } ->
let args =
if lexing && parsing && typing
then []
else [
"lexing", `Bool lexing;
"parsing", `Bool parsing;
"typing", `Bool typing;
]
in
mk "errors" args
| Shape pos ->
mk "shape" [
"position", mk_position pos;
Expand Down Expand Up @@ -358,7 +368,7 @@ let json_of_response (type a) (query : a t) (response : a) : json =
`List (json_of_outline outlines)
| Shape _, shapes ->
`List (List.map ~f:json_of_shape shapes)
| Errors, errors ->
| Errors _, errors ->
`List (List.map ~f:json_of_error errors)
| Dump _, json -> json
| Path_of_source _, str -> `String str
Expand Down
9 changes: 8 additions & 1 deletion src/frontend/query_protocol.ml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ type shape = {
shape_sub : shape list;
}

type error_filter = {
lexing : bool;
parsing : bool;
typing : bool;
}

type is_tail_position = [`No | `Tail_position | `Tail_call]

type _ _bool = bool
Expand Down Expand Up @@ -150,7 +156,8 @@ type _ t =
: Msource.position
-> shape list t
| Errors(* *)
: Location.error list t
: error_filter
-> Location.error list t
| Dump
: Std.json list
-> Std.json t
Expand Down
8 changes: 8 additions & 0 deletions tests/errors/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(alias
(name runtest)
(deps (:t test.t) test.ml)
(action
(progn
(setenv MERLIN %{dep:../merlin-wrapper}
(run %{bin:mdx} test --syntax=cram %{t}))
(diff? %{t} %{t}.corrected))))
11 changes: 11 additions & 0 deletions tests/errors/test.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(* First a typing error *)

let () = 3

(* Then a parsing error *)

let () = | 3

(* Then a typing error again *)

let () = 3
88 changes: 88 additions & 0 deletions tests/errors/test.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
First ask for all the errors:

$ $MERLIN single errors -filename test.ml < test.ml
{
"class": "return",
"value": [
{
"start": {
"line": 3,
"col": 9
},
"end": {
"line": 3,
"col": 10
},
"type": "typer",
"sub": [],
"valid": true,
"message": "This expression has type int but an expression was expected of type unit"
},
{
"start": {
"line": 7,
"col": 9
},
"end": {
"line": 7,
"col": 10
},
"type": "parser",
"sub": [],
"valid": true,
"message": "Syntax error, expecting expr"
}
],
"notifications": []
}

Notice that the second type error is not returned, as it happens after the first
syntax error.

Now let's just ask for typing errors:

$ $MERLIN single errors -lexing false -parsing false -filename test.ml < test.ml
{
"class": "return",
"value": [
{
"start": {
"line": 3,
"col": 9
},
"end": {
"line": 3,
"col": 10
},
"type": "typer",
"sub": [],
"valid": true,
"message": "This expression has type int but an expression was expected of type unit"
}
],
"notifications": []
}

And let's also try filtering out type errors:

$ $MERLIN single errors -typing false -filename test.ml < test.ml
{
"class": "return",
"value": [
{
"start": {
"line": 7,
"col": 9
},
"end": {
"line": 7,
"col": 10
},
"type": "parser",
"sub": [],
"valid": true,
"message": "Syntax error, expecting expr"
}
],
"notifications": []
}

0 comments on commit da78ee4

Please sign in to comment.