Skip to content

Commit

Permalink
feat(SourceMarker): display EOL and EOF points as "EOL" and "EOF"
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Oct 27, 2024
1 parent d426524 commit 5e0dc6b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
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

0 comments on commit 5e0dc6b

Please sign in to comment.