Skip to content

Commit

Permalink
refactor: re-organize code
Browse files Browse the repository at this point in the history
  • Loading branch information
favonia committed Oct 28, 2024
1 parent 87c4941 commit 0aabcdf
Show file tree
Hide file tree
Showing 10 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Asai.ml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ module MarkedSource = MarkedSource
module SourceMarker = SourceMarker
module SourceReader = SourceReader

module SourceUtils = SourceUtils
module StringUtils = StringUtils
module RangeFlattener = RangeFlattener
2 changes: 1 addition & 1 deletion src/Asai.mli
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module SourceReader = SourceReader
(**/**)

(** Helper functions for handling user content. This is exposed for internal testing. Absolutely no stability guarantees. *)
module SourceUtils = SourceUtils
module StringUtils = StringUtils

(** The internal flattener that is tightly coupled with {!module:MarkedSource}. This is exposed for internal testing. Absolutely no stability guarantees. *)
module RangeFlattener = RangeFlattener
12 changes: 6 additions & 6 deletions src/SourceMarker.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ include SourceMarkerSigs

(* helper functions used by the register_printer below *)

let print_invalid_offset fmt : SourceUtils.invalid_offset -> unit =
let print_invalid_offset fmt : StringUtils.invalid_offset -> unit =
function
| `Negative i ->
Format.fprintf fmt "its@ offset@ %d@ is@ negative." i
Expand All @@ -15,7 +15,7 @@ let print_invalid_offset fmt : SourceUtils.invalid_offset -> unit =
| `Within_newline (i, (s, e)) ->
Format.fprintf fmt "its@ offset@ %d@ is@ within@ a@ newline@ sequence@ [%d,%d)." i s e

let print_invalid_position fmt : SourceUtils.invalid_position -> unit =
let print_invalid_position fmt : StringUtils.invalid_position -> unit =
function
| `Offset r ->
print_invalid_offset fmt r
Expand All @@ -24,7 +24,7 @@ let print_invalid_position fmt : SourceUtils.invalid_position -> unit =
| `Incorrect_line_num (ln, ln') ->
Format.fprintf fmt "its@ line@ number@ is@ %d@ but@ it@ should@ have@ been@ %d." ln ln'

let print_invalid_range fmt : SourceUtils.invalid_range -> unit =
let print_invalid_range fmt : StringUtils.invalid_range -> unit =
function
| `Begin r ->
Format.fprintf fmt "its@ beginning@ position@ is@ invalid;@ %a" print_invalid_position r
Expand Down Expand Up @@ -77,7 +77,7 @@ module Make (Tag : Tag) = struct
| ((first_loc, _) :: _) as markers ->
let source = SourceReader.load source in
let eof = SourceReader.length source in
let find_eol i = SourceUtils.find_eol ~line_breaks (SourceReader.unsafe_get source) (i, eof) in
let find_eol i = StringUtils.find_eol ~line_breaks (SourceReader.unsafe_get source) (i, eof) in
let rec go state : (Range.position * Tag.t marker) list -> _ =
function
| (loc, marker) :: markers when state.cursor.line_num = loc.line_num (* on the same line *) ->
Expand Down Expand Up @@ -163,8 +163,8 @@ module Make (Tag : Tag) = struct
let source = SourceReader.load @@ Range.source range in
let read = SourceReader.unsafe_get source in
let eof = SourceReader.length source in
try SourceUtils.check_range ~line_breaks ~eof read range
with SourceUtils.Invalid_range reason -> raise @@ Invalid_range (range, reason))
try StringUtils.check_range ~line_breaks ~eof read range
with StringUtils.Invalid_range reason -> raise @@ Invalid_range (range, reason))
ranges

let mark ?(line_breaks=`Traditional) ?(block_splitting_threshold=5) ?(debug=false) ranges =
Expand Down
2 changes: 1 addition & 1 deletion src/SourceMarkerSigs.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
open MarkedSource

exception Invalid_range of Range.t * SourceUtils.invalid_range
exception Invalid_range of Range.t * StringUtils.invalid_range
(** [Invalid_range (range, reason)] means that [range] is an invalid range because of [reason]. This exception will be raised only when the debug mode is enabled. See the [debug] parameter of {!val:SourceMarker.S.mark} for enabling the debug mode. *)

(** The signature of tags *)
Expand Down
2 changes: 1 addition & 1 deletion src/SourceUtils.ml → src/StringUtils.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include SourceUtilsData
include StringUtilsData

exception Invalid_offset of invalid_offset
exception Invalid_position of invalid_position
Expand Down
2 changes: 1 addition & 1 deletion src/SourceUtils.mli → src/StringUtils.mli
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
include module type of SourceUtilsData
include module type of StringUtilsData

(** [find_eol ~line_breaks read (pos, eof)] returns the end position of the first line and the length of the first newline sequence (if any) within the range [\[pos, end)]. If no newlines are found, then [None] is returned as the length.
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/tty/Tty.ml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct
let style = TtyStyle.highlight ~param:param.ansi param.severity tag in
Format.fprintf fmt (highlight "%s")
(Ansi.style_string ~param:param.ansi style)
(SourceUtils.replace_control ~tab_size:param.tab_size segment)
(StringUtils.replace_control ~tab_size:param.tab_size segment)
(Ansi.reset_string ~param:param.ansi style)

(* Current design:
Expand Down
4 changes: 2 additions & 2 deletions test/TestSourceUtils.ml → test/TestStringUtils.ml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
let of_test name ~tab_size input expected =
Alcotest.test_case name `Quick @@ fun () ->
Alcotest.(check string) "Output matched" expected @@
Asai.SourceUtils.replace_control ~tab_size input
Asai.StringUtils.replace_control ~tab_size input

let () =
Alcotest.run "SourceUtils" [
Alcotest.run "StringUtils" [
"replace_control",
[
of_test "tab" ~tab_size:2 "123\t4\t" "123 4 ";
Expand Down
4 changes: 2 additions & 2 deletions test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
(libraries asai alcotest))

(tests
(names TestSourceUtils TestText)
(modules TestSourceUtils TestText)
(names TestStringUtils TestText)
(modules TestStringUtils TestText)
(libraries asai alcotest))

(test
Expand Down

0 comments on commit 0aabcdf

Please sign in to comment.