Skip to content

Commit

Permalink
fix(Explicator): handle the cases where the last newline is missing (#63
Browse files Browse the repository at this point in the history
)
  • Loading branch information
favonia authored Sep 24, 2023
1 parent af5a3dd commit 5bdd2ef
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/Explicator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ module Make (R : Reader) (Style : Style) = struct
go pos.offset

(** Skip the ['\n'] character. *)
let eol_to_next_line (pos : position) : position =
{ pos with
(* Need to update our offset to skip the newline char *)
offset = pos.offset + 1;
start_of_line = pos.offset + 1;
line_num = pos.line_num + 1 }
let eol_to_next_line (pos : position) : position option =
if pos.offset = R.length pos.file_path then None
else
Some
{ file_path = pos.file_path;
(* Need to update our offset to skip the newline char *)
offset = pos.offset + 1;
start_of_line = pos.offset + 1;
line_num = pos.line_num + 1 }

let read_between (begin_ : position) (end_ : position) : string =
String.init (end_.offset - begin_.offset) @@ fun i ->
Expand All @@ -50,7 +53,10 @@ module Make (R : Reader) (Style : Style) = struct
(* Continue the process if [ps] is not empty. *)
match ps with
| [] -> assert (Style.is_none cur.style); lines
| _ -> go ~lines ~segments:Emp {style = cur.style; value = eol_to_next_line eol} ps
| _ ->
match eol_to_next_line eol with
| None -> lines
| Some pos -> go ~lines ~segments:Emp {style = cur.style; value = pos} ps
in
{ start_line_num = start_pos.line_num
; lines = Bwd.to_list @@ go ~lines:Emp ~segments:Emp {style = Style.none; value = start_pos} bs
Expand Down

0 comments on commit 5bdd2ef

Please sign in to comment.