Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SourceMarker): display EOL and EOF points as "EOL" and "EOF" #171

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/MarkedSource.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ let dump_marker dump_tag fmt =
| RangeEnd tag -> Format.fprintf fmt {|@[<2>RangeEnd@ @[%a@]@]|} dump_tag tag
| Point tag -> Format.fprintf fmt {|@[<2>Point@ @[%a@]@]|} dump_tag tag

let dump_special_position fmt =
function
| End_of_line -> Format.fprintf fmt {|End_of_line|}
| End_of_file -> Format.fprintf fmt {|End_of_file|}

let dump_token dump_tag fmt =
function
| String str -> Format.fprintf fmt {|@[<2>String@ "%s"@]|} (String.escaped str)
| Marker m -> Format.fprintf fmt {|@[<2>Marker@ @[<1>(%a)@]@]|} (dump_marker dump_tag) m
| Marker (p, m) ->
Format.fprintf fmt {|@[<2>Marker@ @[<1>(@[%a@],@ @[%a@])@]@]|}
(Utils.dump_option dump_special_position) p (dump_marker dump_tag) m

let dump_line dump_tag fmt {markers; tokens} =
Format.fprintf fmt {|@[<1>{@[<2>markers=@,@[%a@]@];@ @[<2>tokens=@ @[%a@]@]}@]|}
Expand Down
7 changes: 6 additions & 1 deletion src/MarkedSourceData.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
(** Special positions. *)
type special_position =
| End_of_line
| End_of_file

(** A marker is a delimiter of a range or a specific point. *)
type 'tag marker =
| RangeBegin of 'tag
Expand All @@ -7,7 +12,7 @@ type 'tag marker =
(** A token is either a string or a marker. *)
type 'tag token =
| String of string
| Marker of 'tag marker
| Marker of special_position option * 'tag marker

(** A line is a list of {!type:segment}s along with tags. *)
type 'tag line =
Expand Down
13 changes: 11 additions & 2 deletions src/SourceMarker.ml
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,20 @@ module Make (Tag : Tag) = struct
| (loc, marker) :: markers when state.cursor.line_num = loc.line_num (* on the same line *) ->
if loc.offset > eof then invalid_arg "Asai.SourceMarker.mark: position beyond EOF; use the debug mode";
if loc.offset > state.eol then invalid_arg "Asai.SourceMarker.mark: unexpected newline; use the debug mode";
let special_position =
if loc.offset = state.eol then
if loc.offset = eof then
Some End_of_file
else
Some End_of_line
else
None
in
let tokens =
if loc.offset = state.cursor.offset then
state.tokens <: Marker marker
state.tokens <: Marker (special_position, marker)
else
state.tokens <: String (read_between ~source state.cursor.offset loc.offset) <: Marker marker
state.tokens <: String (read_between ~source state.cursor.offset loc.offset) <: Marker (special_position, marker)
in
go { state with tokens; cursor = loc } markers
| markers ->
Expand Down
10 changes: 7 additions & 3 deletions src/tty/Tty.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,15 @@ struct
function
| MarkedSource.String s ->
render_styled_segment ~param fmt (TtyTagSet.prioritized set) s; set
| MarkedSource.Marker RangeEnd t ->
| MarkedSource.Marker (_, RangeEnd t) ->
TtyTagSet.remove t set
| MarkedSource.Marker Point t ->
| MarkedSource.Marker (Some End_of_file, Point t) ->
render_styled_segment ~param fmt (Some t) "‹EOF›"; set
| MarkedSource.Marker (Some End_of_line, Point t) ->
render_styled_segment ~param fmt (Some t) "‹EOL›"; set
| MarkedSource.Marker (None, Point t) ->
render_styled_segment ~param fmt (Some t) "‹POS›"; set
| MarkedSource.Marker RangeBegin t ->
| MarkedSource.Marker (_, RangeBegin t) ->
TtyTagSet.add t set
in
Format.fprintf fmt (" " ^^ highlight "%*d |" ^^ " ")
Expand Down
32 changes: 16 additions & 16 deletions test/TestSourceMarker.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ let single_line mode eol () =
[{markers = [(1, "1"); (2, "2")];
tokens =
[String "aaa";
Marker (RangeBegin (1, "1"));
Marker (None, RangeBegin (1, "1"));
String "bbb";
Marker (RangeBegin (2, "2"));
Marker (None, RangeBegin (2, "2"));
String "ccc";
Marker (RangeEnd (1, "1"));
Marker (None, RangeEnd (1, "1"));
String "ddd";
Marker (RangeEnd (2, "2"));
Marker (None, RangeEnd (2, "2"));
String "eee";
]}]}
]}
Expand All @@ -46,13 +46,13 @@ let multi_lines_with_ls () =
[{markers=[];
tokens=
[String "aa";
Marker (RangeBegin (1, "1"));
Marker (None, RangeBegin (1, "1"));
String "bbbbb";
]};
{markers=[(1, "1")];
tokens=
[String "bbbb";
Marker (RangeEnd (1, "1"));
Marker (None, RangeEnd (1, "1"));
String "ccc";
]}]}
]}
Expand Down Expand Up @@ -105,18 +105,18 @@ ggggghh
[{markers=[];
tokens=
[String "aa";
Marker (RangeBegin (1, "2"));
Marker (None, RangeBegin (1, "2"));
String "bbbbb"]};
{markers=[];
tokens=
[String "bbbbbbb"]};
{markers=[(2, "1"); (1, "2")];
tokens=
[String "b";
Marker (RangeBegin (2, "1"));
Marker (None, RangeBegin (2, "1"));
String "*cc";
Marker (RangeEnd (2, "1"));
Marker (RangeEnd (1, "2"));
Marker (None, RangeEnd (2, "1"));
Marker (None, RangeEnd (1, "2"));
String "ddd"]};
{markers=[];
tokens=
Expand All @@ -133,20 +133,20 @@ ggggghh
{markers=[(8, "4"); (4, "3")];
tokens=
[String "ee";
Marker (RangeBegin (4, "3"));
Marker (None, RangeBegin (4, "3"));
String "++";
Marker (RangeBegin (8, "4"));
Marker (None, RangeBegin (8, "4"));
String "fff";
Marker (RangeEnd (8, "4"));
Marker (RangeEnd (4, "3"))]}]};
Marker (Some End_of_line, RangeEnd (8, "4"));
Marker (Some End_of_line, RangeEnd (4, "3"))]}]};
{begin_line_num=15;
end_line_num=15;
lines=
[{markers=[(16, "5")];
tokens=
[Marker (RangeBegin (16, "5"));
[Marker (None, RangeBegin (16, "5"));
String "ggggg";
Marker (RangeEnd (16, "5"));
Marker (None, RangeEnd (16, "5"));
String "hh"]}]}]}]
in
let actual = SM.mark ~line_breaks:`Traditional ~block_splitting_threshold:5 ranges in
Expand Down
12 changes: 6 additions & 6 deletions test/TestTty.expected
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@
○ message 3

→ warning[hello]
23 | wwwwwwwwww‹POS›
23 | wwwwwwwwww‹EOF›
^ this is the main message
■ /path/to/file.cool
23 | wwwwwwwwww‹POS›
23 | wwwwwwwwww‹EOF›
^ ending of another file

→ warning[hello]
Expand Down Expand Up @@ -222,10 +222,10 @@
○ message 3

→ warning[hello]
23 | wwwwwwwwww‹POS›
23 | wwwwwwwwww‹EOF›
^ this is the main message
■ /path/to/file.cool
23 | wwwwwwwwww‹POS›
23 | wwwwwwwwww‹EOF›
^ ending of another file

→ warning[hello]
Expand Down Expand Up @@ -337,9 +337,9 @@
○ message 3

→ warning[hello]
23 | wwwwwwwwww‹POS
23 | wwwwwwwwww‹EOF
^ this is the main message
■ /path/to/file.cool
23 | wwwwwwwwww‹POS
23 | wwwwwwwwww‹EOF
^ ending of another file