Skip to content

Commit

Permalink
Merge pull request #10582 from kit-ty-kate/source-highlighting-tabs
Browse files Browse the repository at this point in the history
Fix single-line source highlighting in the presence of tabs
  • Loading branch information
Octachron committed Sep 23, 2021
2 parents e93f6f8 + 97d57de commit c8c3a95
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ testsuite/tests/generated-parse-errors/errors.* typo.very-long-line
testsuite/tools/*.S typo.missing-header
testsuite/tools/*.asm typo.missing-header
testsuite/typing typo.missing-header
testsuite/tests/messages/highlight_tabs.ml typo.tab

# prune testsuite reference files
testsuite/tests/**/*.reference typo.prune
Expand Down
5 changes: 5 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ Working version
Makefile.config.
(Damien Doligez, review by Mark Shinwell and Gabriel Scherer)

- #9116, #9118, #10582: Fix single-line source highlighting in the
presence of tabs
(Armaël Guéneau, review by Gabriel Scherer,
split off from #9118 by Kate Deplaix, report by Ricardo M. Correia)

### Internal/compiler-libs changes:

- #1599: add unset directive to ocamltest to clear environment variables before
Expand Down
12 changes: 9 additions & 3 deletions parsing/location.ml
Original file line number Diff line number Diff line change
Expand Up @@ -464,14 +464,20 @@ let highlight_quote ppf
(* Single-line error *)
Format.fprintf ppf "%s | %s@," line_nb line;
Format.fprintf ppf "%*s " (String.length line_nb) "";
for pos = line_start_cnum to rightmost.pos_cnum - 1 do
String.iteri (fun i c ->
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 Format.pp_print_char ppf ' ';
else if pos < rightmost.pos_cnum 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'
else Format.pp_print_char ppf ' '
end;
if ISet.is_end iset ~pos <> None then
Format.fprintf ppf "@}"
done;
) line;
Format.fprintf ppf "@}@,"
| _ ->
(* Multi-line error *)
Expand Down
13 changes: 13 additions & 0 deletions testsuite/tests/messages/highlight_tabs.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(* TEST
* expect
*)

let x = abc
;;
[%%expect{|
Line 1, characters 10-13:
1 | let x = abc
^^^
Error: Unbound value abc
Hint: Did you mean abs?
|}];;

0 comments on commit c8c3a95

Please sign in to comment.