Skip to content

Commit e3458b2

Browse files
authored
[Super errors] Fix file printing (#1924)
2e99e60 fixed one case but broke another. I'm turning this into a variant instead of commenting & forgetting to match a condition Also encaptulating the tag logic in a function so that they open & close well
1 parent e71f90b commit e3458b2

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

jscomp/super_errors/super_misc.ml

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ let leading_space_count str =
2626
in
2727
_leading_space_count str (String.length str) 0
2828

29+
type current_printed_line_status =
30+
| Is_error_start_line
31+
| Is_error_end_line
32+
| Strictly_between_start_and_end
33+
| Only_error_line
34+
| Not_error_line
35+
2936
(* ocaml's reported line/col numbering is horrible and super error-prone when
3037
being handled programmatically (or humanly for that matter. If you're an
3138
ocaml contributor reading this: who the heck reads the character count
@@ -64,27 +71,30 @@ ppf
6471
| None -> 0
6572
| Some n -> n
6673
in
67-
(* btw, these are unicode chars. They're not of length 1. Careful; we need to
68-
explicitly tell Format to treat them as length 1 below *)
69-
let separator = if columns_to_cut = 0 then "" else "" in
7074
(* coloring *)
71-
let (highlighted_line_number, highlighted_content): (string -> string -> unit, Format.formatter, unit) format * (unit, Format.formatter, unit) format =
75+
let (highlighted_line_number, highlighted_open_tag): (string -> string -> unit, Format.formatter, unit) format * (unit, Format.formatter, unit) format =
7276
if is_warning then ("@{<info>%s@}@{<dim> @<1>%s @}", "@{<info>")
7377
else ("@{<error>%s@}@{<dim> @<1>%s @}", "@{<error>")
7478
in
75-
79+
let print_char_maybe_highlight ~begin_highlight_line ~end_highlight_line ch =
80+
if begin_highlight_line then fprintf ppf highlighted_open_tag;
81+
fprintf ppf "%c@," ch;
82+
if end_highlight_line then fprintf ppf "@}"
83+
in
84+
7685
fprintf ppf "@[<v 0>";
7786
(* inclusive *)
7887
for i = first_shown_line to last_shown_line do
7988
let current_line = lines.(i - 1) in
80-
let current_line_cut = current_line |> string_slice ~start:columns_to_cut in
8189
let padded_line_number = pad (string_of_int i) max_line_number_number_of_digits in
8290

8391
fprintf ppf "@[<h 0>";
8492

8593
fprintf ppf "@[<h 0>";
8694

87-
(* this is where you insrt the vertical separator. Mark them as legnth 1 as explained above *)
95+
(* btw, these are unicode chars. They're not of length 1. Careful; we need to
96+
explicitly tell Format to treat them as length 1 right below *)
97+
let separator = if columns_to_cut = 0 then "" else "" in
8898
if i < start_line || i > end_line then begin
8999
(* normal, non-highlighted line *)
90100
fprintf ppf "%s@{<dim> @<1>%s @}" padded_line_number separator
@@ -97,32 +107,48 @@ ppf
97107

98108
fprintf ppf "@[<hov 0>";
99109

100-
let current_line_strictly_between_start_and_end_line = i > start_line && i < end_line in
101-
102-
if current_line_strictly_between_start_and_end_line then fprintf ppf highlighted_content;
103-
104-
let current_line_cut_length = String.length current_line_cut in
110+
let current_line_status =
111+
if i > start_line && i < end_line then Strictly_between_start_and_end
112+
else if i = start_line && i = end_line then Only_error_line
113+
else if i = start_line then Is_error_start_line
114+
else if i = end_line then Is_error_end_line
115+
else Not_error_line
116+
in
117+
let offset_current_line = current_line |> string_slice ~start:columns_to_cut in
118+
let offset_current_line_length = String.length offset_current_line in
119+
let offset_start_line_start_char = start_line_start_char - columns_to_cut in
120+
let offset_end_line_end_char = end_line_end_char - columns_to_cut in
105121
(* inclusive. To be consistent with using 1-indexed indices and count and i, j will be 1-indexed too *)
106-
for j = 1 to current_line_cut_length do begin
107-
let current_char = current_line_cut.[j - 1] in
108-
if current_line_strictly_between_start_and_end_line then
109-
fprintf ppf "%c@," current_char
110-
else if i = start_line then begin
111-
if j == (start_line_start_char - columns_to_cut) then fprintf ppf highlighted_content;
112-
fprintf ppf "%c@," current_char;
113-
if j == current_line_cut_length then fprintf ppf "@}"
114-
end else if i = end_line then begin
115-
if j == 1 then fprintf ppf highlighted_content;
116-
fprintf ppf "%c@," current_char;
117-
if j == (end_line_end_char - columns_to_cut) then fprintf ppf "@}"
118-
end else
119-
(* normal, non-highlighted line *)
120-
fprintf ppf "%c@," current_char
121-
end
122+
for j = 1 to offset_current_line_length do
123+
let current_char = offset_current_line.[j - 1] in
124+
match current_line_status with
125+
| Strictly_between_start_and_end ->
126+
print_char_maybe_highlight
127+
~begin_highlight_line:(j = 1)
128+
~end_highlight_line:(j = offset_current_line_length)
129+
current_char
130+
| Only_error_line ->
131+
print_char_maybe_highlight
132+
~begin_highlight_line:(j = offset_start_line_start_char)
133+
~end_highlight_line:(j = offset_end_line_end_char)
134+
current_char
135+
| Is_error_start_line ->
136+
print_char_maybe_highlight
137+
~begin_highlight_line:(j = offset_start_line_start_char)
138+
~end_highlight_line:(j = offset_current_line_length)
139+
current_char
140+
| Is_error_end_line ->
141+
print_char_maybe_highlight
142+
~begin_highlight_line:(j = 1)
143+
~end_highlight_line:(j = offset_end_line_end_char)
144+
current_char
145+
| Not_error_line ->
146+
print_char_maybe_highlight
147+
~begin_highlight_line:false
148+
~end_highlight_line:false
149+
current_char
122150
done;
123151

124-
if current_line_strictly_between_start_and_end_line then fprintf ppf "@}";
125-
126152
fprintf ppf "@]"; (* hov *)
127153

128154
fprintf ppf "@]@," (* h *)

0 commit comments

Comments
 (0)