Skip to content

Commit

Permalink
Merge pull request #386 from Leonidas-from-XIV/semisemi-dedup
Browse files Browse the repository at this point in the history
Deduplicate implementation of `;;` detection
  • Loading branch information
Leonidas-from-XIV authored Jul 27, 2022
2 parents cdcc344 + e6667dc commit 0032bc0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 16 deletions.
4 changes: 1 addition & 3 deletions lib/block.ml
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,7 @@ let guess_ocaml_kind contents =

let ends_by_semi_semi c =
match List.rev c with
| h :: _ ->
let len = String.length h in
len > 2 && h.[len - 1] = ';' && h.[len - 2] = ';'
| h :: _ -> Astring.String.is_suffix ~affix:";;" h
| _ -> false

let pp_line_directive ppf (file, line) = Fmt.pf ppf "#%d %S" line file
Expand Down
4 changes: 4 additions & 0 deletions lib/block.mli
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,7 @@ val executable_contents : syntax:Syntax.t -> t -> string list
(e.g. the phrase result is discarded). *)

val is_active : ?section:string -> t -> bool

(** {2 Helpers} *)

val ends_by_semi_semi : string list -> bool
17 changes: 4 additions & 13 deletions lib/top/mdx_top.ml
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,10 @@ module Phrase = struct
let endpos = lexbuf.Lexing.lex_curr_p in
{ doc = { lexbuf; contents }; startpos; endpos; parsed }

let ends_by_semi_semi c =
match List.rev c with
| h :: _ ->
let len = String.length h in
len > 2 && h.[len - 1] = ';' && h.[len - 2] = ';'
| _ -> false

let parse lines =
let lines = if ends_by_semi_semi lines then lines else lines @ [ ";;" ] in
let lines =
if Mdx.Block.ends_by_semi_semi lines then lines else lines @ [ ";;" ]
in
match parse lines with exception End_of_file -> None | t -> Some t

(** Returns the name of the toplevel directive or [None] if the given phrase
Expand Down Expand Up @@ -394,13 +389,9 @@ let rtrim l = List.rev (ltrim (List.rev l))
let trim l = ltrim (rtrim (List.map trim_line l))

let cut_into_sentences l =
let ends_by_semi_semi h =
let len = String.length h in
len > 2 && h.[len - 1] = ';' && h.[len - 2] = ';'
in
let rec aux acc sentence = function
| [] -> List.rev (List.rev sentence :: acc)
| h :: t when ends_by_semi_semi h ->
| h :: t when Mdx.Block.ends_by_semi_semi [ h ] ->
aux (List.rev (h :: sentence) :: acc) [] t
| h :: t -> aux acc (h :: sentence) t
in
Expand Down

0 comments on commit 0032bc0

Please sign in to comment.