Skip to content

Commit b1d8a44

Browse files
committed
try modifying and reusing the existing Code_frame module for snapshot printing
1 parent dd1cb30 commit b1d8a44

9 files changed

+83
-51
lines changed

analysis/src/Commands.ml

+3-8
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ let test ~path ~debug =
381381
| "com" ->
382382
let currentFile = createCurrentFile () in
383383
if !Cfg.useRevampedCompletion then (
384+
Code_frame.setup (Some Misc.Color.Never);
384385
let source = Files.readFile currentFile in
385386
let completions =
386387
completionRevamped ~debug ~path ~pos:(line, col) ~currentFile
@@ -391,16 +392,10 @@ let test ~path ~debug =
391392
| Some (completable, completionsText), Some text -> (
392393
match SharedTypes.CompletableRevamped.try_loc completable with
393394
| Some loc ->
394-
let range =
395-
CodeFence.
396-
{
397-
start = loc.Location.loc_start.pos_cnum;
398-
finish = loc.Warnings.loc_end.pos_cnum;
399-
}
400-
in
401395
Printf.printf "Found Completable: %s\n\n"
402396
(SharedTypes.CompletableRevamped.toString completable);
403-
CodeFence.format_code_snippet_cropped text (Some range) 3
397+
Code_frame.print ~is_warning:true ~draw_underline:true
398+
~src:text ~start_pos:loc.loc_start ~end_pos:loc.loc_end
404399
|> print_endline;
405400
print_endline completionsText
406401
| None -> ())

compiler/ml/code_frame.ml

+45-4
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ end
104104

105105
let setup = Color.setup
106106

107-
type gutter = Number of int | Elided
107+
type gutter = Number of int | Elided | UnderlinedRow
108108
type highlighted_string = {s: string; start: int; end_: int}
109109
type line = {gutter: gutter; content: highlighted_string list}
110110

@@ -116,7 +116,7 @@ type line = {gutter: gutter; content: highlighted_string list}
116116
- center snippet when it's heavily indented
117117
- ellide intermediate lines when the reported range is huge
118118
*)
119-
let print ~is_warning ~src ~(start_pos : Lexing.position)
119+
let print ~is_warning ~draw_underline ~src ~(start_pos : Lexing.position)
120120
~(end_pos : Lexing.position) =
121121
let indent = 2 in
122122
let highlight_line_start_line = start_pos.pos_lnum in
@@ -175,7 +175,8 @@ let print ~is_warning ~src ~(start_pos : Lexing.position)
175175
|> break_long_line line_width
176176
|> List.mapi (fun i line ->
177177
match gutter with
178-
| Elided -> {s = line; start = 0; end_ = 0}
178+
| Elided | UnderlinedRow ->
179+
{s = line; start = 0; end_ = 0}
179180
| Number line_number ->
180181
let highlight_line_start_offset =
181182
start_pos.pos_cnum - start_pos.pos_bol
@@ -207,7 +208,41 @@ let print ~is_warning ~src ~(start_pos : Lexing.position)
207208
in
208209
{s = line; start; end_})
209210
in
210-
{gutter; content = new_content})
211+
if draw_underline then
212+
let has_highlight =
213+
List.exists (fun {start; end_} -> start < end_) new_content
214+
in
215+
if has_highlight then
216+
let underline_content =
217+
List.map
218+
(fun {start; end_} ->
219+
if start < end_ then
220+
let overline_char = "" in
221+
let underline_length = end_ - start in
222+
let underline =
223+
String.concat ""
224+
(List.init underline_length (fun _ -> overline_char))
225+
in
226+
[
227+
{
228+
s = String.make start ' ' ^ underline;
229+
start = 0;
230+
end_ = 0;
231+
};
232+
]
233+
else [{s = ""; start = 0; end_ = 0}])
234+
new_content
235+
in
236+
[
237+
{gutter; content = new_content};
238+
{
239+
gutter = UnderlinedRow;
240+
content = List.flatten underline_content;
241+
};
242+
]
243+
else [{gutter; content = new_content}]
244+
else [{gutter; content = new_content}])
245+
|> List.flatten
211246
in
212247
let buf = Buffer.create 100 in
213248
let open Color in
@@ -275,5 +310,11 @@ let print ~is_warning ~src ~(start_pos : Lexing.position)
275310
else NoColor
276311
in
277312
add_ch c ch);
313+
add_ch NoColor '\n')
314+
| UnderlinedRow ->
315+
content
316+
|> List.iter (fun line ->
317+
draw_gutter NoColor "";
318+
line.s |> String.iter (fun ch -> add_ch NoColor ch);
278319
add_ch NoColor '\n'));
279320
Buffer.contents buf

compiler/ml/location.ml

+3-2
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,9 @@ let print ?(src = None) ~message_kind intro ppf (loc : t) =
140140
branch might not be reached (aka no inline file content display) so
141141
we don't wanna end up with two line breaks in the the consequent *)
142142
fprintf ppf "@,%s"
143-
(Code_frame.print ~is_warning:(message_kind = `warning) ~src
144-
~start_pos:loc.loc_start ~end_pos:loc.loc_end)
143+
(Code_frame.print ~draw_underline:false
144+
~is_warning:(message_kind = `warning) ~src ~start_pos:loc.loc_start
145+
~end_pos:loc.loc_end)
145146
with
146147
(* this might happen if the file is e.g. "", "_none_" or any of the fake file name placeholders.
147148
we've already printed the location above, so nothing more to do here. *)

tests/analysis_new_tests/tests/test_files/__snapshots__/RecordFieldCompletions.res_Record_field_completion_in_nested_record.snap

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Found Completable: Cexpression
22

3-
1 + // Record field completion in nested record
4-
2 + let x = TestTypeDefs.nestedTestRecord.
5-
-----------------------------
6-
3 + // ^com
7-
4 +
3+
1 // Record field completion in nested record
4+
2 let x = TestTypeDefs.nestedTestRecord.
5+
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
6+
3 // ^com
7+
4
88

99
[{
1010
"label": "test",

tests/analysis_new_tests/tests/test_files/__snapshots__/RecordFieldCompletions.res_Record_field_completion_in_nested_record_another_level.snap

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
Found Completable: Cexpression
22

3-
1 + // Record field completion in nested record, another level
4-
2 + let x = TestTypeDefs.nestedTestRecord.nested.
5-
------------------------------------
6-
3 + // ^com
7-
4 +
3+
1 // Record field completion in nested record, another level
4+
2 let x = TestTypeDefs.nestedTestRecord.nested.
5+
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
6+
3 // ^com
7+
4
88

99
[{
1010
"label": "name",

tests/analysis_new_tests/tests/test_files/__snapshots__/SwitchCaseCompletions.res_Empty_case_array.snap

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
Found Completable: Cpattern
22

3-
1 + // Empty case, array
4-
2 + let someStringArr = ["hello"]
5-
3 +
6-
4 + let x = switch someStringArr {
7-
-------------
8-
5 + |
9-
6 + // ^com
10-
7 + }
3+
2let someStringArr = ["hello"]
4+
3
5+
4let x = switch someStringArr {
6+
│ ‾‾‾‾‾‾‾‾‾‾‾‾‾
7+
5 │ |
8+
6 │ // ^com
119

1210
[{
1311
"label": "[]",

tests/analysis_new_tests/tests/test_files/__snapshots__/SwitchCaseCompletions.res_Empty_case_bool.snap

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
Found Completable: Cpattern
22

3-
1 + // Empty case, bool
4-
2 + let x = switch true {
5-
----
6-
3 + |
7-
4 + // ^com
8-
5 + }
3+
1// Empty case, bool
4+
2let x = switch true {
5+
│ ‾‾‾‾
6+
3 │ |
7+
4 │ // ^com
98

109
[{
1110
"label": "true",

tests/analysis_new_tests/tests/test_files/__snapshots__/SwitchCaseCompletions.res_Empty_case_record.snap

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
Found Completable: Cpattern
22

3-
1 + // Empty case, record
4-
2 + let x = switch TestTypeDefs.nestedTestRecord {
5-
-----------------------------
6-
3 + |
7-
4 + // ^com
8-
5 + }
3+
1// Empty case, record
4+
2let x = switch TestTypeDefs.nestedTestRecord {
5+
│ ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
6+
3 │ |
7+
4 │ // ^com
98

109
[{
1110
"label": "{}",

tests/analysis_new_tests/tests/test_files/__snapshots__/SwitchCaseCompletions.res_Empty_case_string.snap

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
Found Completable: Cpattern
22

3-
1 + // Empty case, string
4-
2 + let str = "hello"
5-
3 + let x = switch str {
6-
---
7-
4 + |
8-
5 + // ^com
9-
6 + }
3+
1// Empty case, string
4+
2let str = "hello"
5+
3let x = switch str {
6+
│ ‾‾‾
7+
4 │ |
8+
5 │ // ^com
109

1110
[{
1211
"label": "\"\"",

0 commit comments

Comments
 (0)