diff options
author | Jules Aguillon <jules@j3s.fr> | 2022-10-19 17:02:06 +0200 |
---|---|---|
committer | Jules Aguillon <jules@j3s.fr> | 2023-01-19 14:47:19 +0100 |
commit | 5b536613b9851a67f21401fb55d007a5b16b5ebc (patch) | |
tree | b4b748dd47a9c10fb47a8b0153bc3e5ab5be5fcd | |
parent | f611b45ae61b51f21dea3d9f48c3c3ae097edf1b (diff) | |
download | ocaml-5b536613b9851a67f21401fb55d007a5b16b5ebc.tar.gz |
Location: Highlight past the end of the line
Allow to write for example:
File "./test.ml", line 4, characters 46-47:
| f (List.map string_of_int [ 1; 2; 3; 4; 5 ])
^
Maybe you forgot a ';'.
Iterating on the string instead of the range is not useful because range
calculation have already been done.
-rw-r--r-- | parsing/location.ml | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/parsing/location.ml b/parsing/location.ml index 424324d913..c9c0401edb 100644 --- a/parsing/location.ml +++ b/parsing/location.ml @@ -459,20 +459,28 @@ let highlight_quote ppf (* Single-line error *) Format.fprintf ppf "%s | %s@," line_nb line; Format.fprintf ppf "%*s " (String.length line_nb) ""; - String.iteri (fun i c -> + (* Iterate up to [rightmost], which can be larger than the length of + the line because we may point to a location after the end of the + last token on the line, for instance: + {[ + token + ^ + Did you forget ... + ]} *) + for i = 0 to rightmost.pos_cnum - line_start_cnum - 1 do let pos = line_start_cnum + i in if ISet.is_start iset ~pos <> None then Format.fprintf ppf "@{<%s>" highlight_tag; if ISet.mem iset ~pos then Format.pp_print_char ppf '^' - else if pos < rightmost.pos_cnum then begin + else if i < String.length line then begin (* For alignment purposes, align using a tab for each tab in the source code *) - if c = '\t' then Format.pp_print_char ppf '\t' + if line.[i] = '\t' then Format.pp_print_char ppf '\t' else Format.pp_print_char ppf ' ' end; if ISet.is_end iset ~pos <> None then Format.fprintf ppf "@}" - ) line; + done; Format.fprintf ppf "@}@," | _ -> (* Multi-line error *) |