Refactor and improve suggestions' pretty-printing

This commit is contained in:
vbot 2024-06-20 15:49:58 +02:00
parent 441dd54ec3
commit c26d3fdf4a
No known key found for this signature in database
GPG Key ID: A102739F983C6C72
8 changed files with 30 additions and 18 deletions

View File

@ -77,13 +77,25 @@ let sorted_candidates ?(max_elements = 5) suggs given =
in
List.concat candidates |> sub [] max_elements
let format (ppf : Format.formatter) (suggestions_list : string list) =
match suggestions_list with
let format ppf suggs =
let open Format in
let pp_elt elt = fprintf ppf "@{<yellow>\"%s\"@}" elt in
let rec loop = function
| [] -> assert false
| [h] ->
pp_elt h;
pp_print_string ppf "?"
| [h; t] ->
pp_elt h;
fprintf ppf "@ or@ ";
loop [t]
| h :: t ->
pp_elt h;
fprintf ppf ",@ ";
loop t
in
match suggs with
| [] -> ()
| _ :: _ ->
Format.pp_print_string ppf "Maybe you wanted to write : ";
Format.pp_print_list
~pp_sep:(fun ppf () -> Format.fprintf ppf ",@ or ")
(fun ppf string -> Format.fprintf ppf "@{<yellow>\"%s\"@}" string)
ppf suggestions_list;
Format.pp_print_string ppf " ?"
| suggs ->
pp_print_string ppf "Maybe you wanted to write: ";
loop suggs

View File

@ -24,7 +24,7 @@ $ catala test-scope A
│ 8 │ definition wrong_definition = 1
│ │ ‾
│ Maybe you wanted to write : "." ?
│ Maybe you wanted to write: "."?
└─
#return code 123#
```

View File

@ -50,8 +50,8 @@ $ catala test-scope A
│ │ ‾
├─ Article
│ Maybe you wanted to write : "field0", or "field2", or "field10",
or "field1", or "field3" ?
│ Maybe you wanted to write: "field0", "field2", "field10", "field1" or
│ "field3"?
└─
#return code 123#
```

View File

@ -29,7 +29,7 @@ $ catala test-scope A
│ │ ‾‾‾‾‾
├─ Article
│ Maybe you wanted to write : "Case1", or "Case2" ?
│ Maybe you wanted to write: "Case1" or "Case2"?
└─
#return code 123#
```

View File

@ -25,7 +25,7 @@ $ catala Typecheck
│ │ ‾‾‾‾‾
├─ Article
│ Maybe you wanted to write : "Case1" ?
│ Maybe you wanted to write: "Case1"?
└─
#return code 123#
```

View File

@ -30,7 +30,7 @@ $ catala test-scope A
│ 12 │ definitoin i equals 3.
│ │ ‾‾‾‾‾‾‾‾‾‾
│ Maybe you wanted to write : "definition" ?
│ Maybe you wanted to write: "definition"?
└─
┌─[ERROR (2/2)]─
@ -46,7 +46,7 @@ $ catala test-scope A
│ 16 │ definition i equal money of (decimal of x)
│ │ ‾‾‾‾‾
│ Maybe you wanted to write : "equals" ?
│ Maybe you wanted to write: "equals"?
└─
#return code 123#
```

View File

@ -21,7 +21,7 @@ $ catala test-scope A
│ 5 │ output i contents decimal
│ │ ‾‾‾‾‾‾‾‾
│ Maybe you wanted to write : "content" ?
│ Maybe you wanted to write: "content"?
└─
#return code 123#
```

View File

@ -31,7 +31,7 @@ $ catala dcalc -s Titi
│ 2 │ declaration scope Toto:
│ │ ‾‾‾‾
│ Maybe you wanted to write : "bar", or "baz", or "foo" ?
│ Maybe you wanted to write: "bar", "baz" or "foo"?
└─
#return code 123#
```