Skip to content

Commit

Permalink
internal refactoring
Browse files Browse the repository at this point in the history
1. Rename most file path variables from [filename] to [path] and switch
   from type [string] to [Fpath.t]

```diff
- filename:string
+ path:Fpath.t
```

2. Extract some libraries into their own packages to reuse in other
   projects: loc, error-log, parsing-utils.

In the process of doing [2], the interface of the [fmt] command
changed slightly: now the extension is configured in the code and
doesn't need to be supplied in the dune files invocations.

For example:

```diff
  (with-stdout-to
    dune.inc.gen
    (pipe-stdout
-    (bash "%{bin:bopkit} fmt gen-dune .bop -- \%{bin:bopkit} fmt file")
+    (bash "%{bin:bopkit} fmt gen-dune -- \%{bin:bopkit} fmt file")
     (run dune format-dune-file)))))
```
  • Loading branch information
mbarbin committed Nov 12, 2023
1 parent 131eeb9 commit 66e45dd
Show file tree
Hide file tree
Showing 133 changed files with 397 additions and 1,693 deletions.
7 changes: 7 additions & 0 deletions bopkit.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,23 @@ depends: [
"dune" {>= "3.11" & >= "3.11"}
"ANSITerminal" {>= "0.8.3"}
"appendable-list" {>= "v0.16" & < "v0.17"}
"auto-format" {>= "0.0.1"}
"base" {>= "v0.16" & < "v0.17"}
"bogue" {>= "20221112"}
"command-unix-for-opam" {>= "0.0.1"}
"core" {>= "v0.16" & < "v0.17"}
"core_unix" {>= "v0.16" & < "v0.17"}
"dune-site" {>= "3.11"}
"error-log" {>= "0.0.1"}
"fpath" {>= "0.7.3"}
"fpath-extended" {>= "0.0.2"}
"graphics" {>= "5.1.2"}
"loc" {>= "0.0.1"}
"mdx" {>= "2.2"}
"menhir" {>= "20220210"}
"parsing-utils" {>= "0.0.1"}
"pp" {>= "1.2.0"}
"pp-extended" {>= "0.0.1"}
"ppx_jane" {>= "v0.16" & < "v0.17"}
"ppx_js_style" {>= "v0.16" & < "v0.17"}
"stdio" {>= "v0.16" & < "v0.17"}
Expand Down
2 changes: 1 addition & 1 deletion doc/reference/dune
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
(with-stdout-to
dune.inc.gen
(pipe-stdout
(bash "%{bin:bopkit} fmt gen-dune .bop -- \%{bin:bopkit} fmt file")
(bash "%{bin:bopkit} fmt gen-dune -- \%{bin:bopkit} fmt file")
(run dune format-dune-file)))))

(rule
Expand Down
14 changes: 14 additions & 0 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
(and
(>= v0.16)
(< v0.17)))
(auto-format
(>= 0.0.1))
(base
(and
(>= v0.16)
Expand All @@ -60,14 +62,26 @@
(< v0.17)))
(dune-site
(>= 3.11))
(error-log
(>= 0.0.1))
(fpath
(>= 0.7.3))
(fpath-extended
(>= 0.0.2))
(graphics
(>= 5.1.2))
(loc
(>= 0.0.1))
(mdx
(>= 2.2))
(menhir
(>= 20220210))
(parsing-utils
(>= 0.0.1))
(pp
(>= 1.2.0))
(pp-extended
(>= 0.0.1))
(ppx_jane
(and
(>= v0.16)
Expand Down
21 changes: 10 additions & 11 deletions editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ Checkout: bopkit fmt file -help

```sh
$ bopkit fmt file -help
autoformat a bopkit file
autoformat bopkit files

bopkit fmt file FILE

This is a pretty-print command for bopkit files (extension *.bop).
This is a pretty-print command for bopkit files (extensions .bop).

This reads the contents of a bopkit file supplied in the command line, and
This reads the contents of a file supplied in the command line, and
pretty-print it on stdout, leaving the original file unchanged.

If [-read-contents-from-stdin] is supplied, then the contents of the file is
Expand All @@ -44,18 +44,17 @@ to stdout, however the exit code will be non zero (typically [1]). Errors are
printed on stderr.

The hope for this command is for it to be compatible with editors and build
systems so that you can integrate autoformatting of bopkit files into your
workflow.
systems so that you can integrate autoformatting of files into your workflow.

It is used by [dune fmt] throughout the bopkit project, and has been tested
with vscode. Because this command has been tested with a vscode extension that
strips the last newline, a flag has been added to add an extra blank line,
shall you run into this issue.
Because this command has been tested with a vscode extension that strips the
last newline, a flag has been added to add an extra blank line, shall you run
into this issue.

=== flags ===

[-add-extra-blank-line] . add an extra blank line at the end
[-read-contents-from-stdin]
[--add-extra-blank-line], -add-extra-blank-line
. add an extra blank line at the end
[--read-contents-from-stdin], -read-contents-from-stdin
. read contents from stdin rather than from the
file
[-help], -? . print this help text and exit
Expand Down
8 changes: 4 additions & 4 deletions lib/bit_utils/src/bit_array.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ let to_string (t : t) =
String.init (Array.length t) ~f:(fun i -> Bit_string_encoding.Bit.to_char t.(i))
;;

let of_text_file ~filename =
let of_text_file ~path =
let q = Queue.create () in
In_channel.with_file filename ~f:(fun ic ->
In_channel.with_file (path |> Fpath.to_string) ~f:(fun ic ->
with_return (fun { return } ->
while true do
match In_channel.input_line ic with
Expand All @@ -52,8 +52,8 @@ let of_text_file ~filename =

let to_text_channel t oc = Printf.fprintf oc "%s\n" (to_string t)

let to_text_file t ~filename =
Out_channel.with_file filename ~f:(fun oc -> to_text_channel t oc)
let to_text_file t ~path =
Out_channel.with_file (path |> Fpath.to_string) ~f:(fun oc -> to_text_channel t oc)
;;

let to_int bits =
Expand Down
4 changes: 2 additions & 2 deletions lib/bit_utils/src/bit_array.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ val to_string : t -> string

(** Concat all the value read on all the lines from a file, using the function
[of_01_chars_in_string]. *)
val of_text_file : filename:string -> t
val of_text_file : path:Fpath.t -> t

(** Write a bunch of '0' and '1' to a file, ending with a newline character.
This erases the previous contents of the file if it existed. *)
val to_text_file : t -> filename:string -> unit
val to_text_file : t -> path:Fpath.t -> unit

val to_text_channel : t -> Out_channel.t -> unit

Expand Down
8 changes: 4 additions & 4 deletions lib/bit_utils/src/bit_matrix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ let of_bit_array ~dimx ~dimy code =
init_matrix_linear ~dimx ~dimy ~f:(fun i -> i < len && code.(i))
;;

let of_text_file ~dimx ~dimy ~filename =
of_bit_array ~dimx ~dimy (Bit_array.of_text_file ~filename)
let of_text_file ~dimx ~dimy ~path =
of_bit_array ~dimx ~dimy (Bit_array.of_text_file ~path)
;;

let to_text_channel t oc =
Expand All @@ -24,8 +24,8 @@ let to_text_channel t oc =
Out_channel.newline oc)
;;

let to_text_file t ~filename =
Out_channel.with_file filename ~f:(fun oc -> to_text_channel t oc)
let to_text_file t ~path =
Out_channel.with_file (path |> Fpath.to_string) ~f:(fun oc -> to_text_channel t oc)
;;

let dimx t = Array.length t
Expand Down
4 changes: 2 additions & 2 deletions lib/bit_utils/src/bit_matrix.mli
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ val init_matrix_linear : dimx:int -> dimy:int -> f:(int -> 'a) -> 'a array array
val of_bit_array : dimx:int -> dimy:int -> Bit_array.t -> t

(** See {!val:Bit_array.of_text_file}. *)
val of_text_file : dimx:int -> dimy:int -> filename:string -> t
val of_text_file : dimx:int -> dimy:int -> path:Fpath.t -> t

(** Save it to disk. To make things more readable, write things line by line in
the file (along the dimx dimension). *)
val to_text_file : t -> filename:string -> unit
val to_text_file : t -> path:Fpath.t -> unit

val to_text_channel : t -> Out_channel.t -> unit

Expand Down
2 changes: 1 addition & 1 deletion lib/bit_utils/src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
(public_name bopkit.bit-utils)
(wrapped false)
(flags :standard -w +a-4-40-41-42-44-45-66 -warn-error +a -open Core)
(libraries core)
(libraries core fpath)
(preprocess
(pps ppx_jane ppx_js_style -check-doc-comments)))
8 changes: 4 additions & 4 deletions lib/bit_utils/src/partial_bit_array.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ let to_string t =
String.init (Array.length t) ~f:(fun i -> Bit_string_encoding.Bit_option.to_char t.(i))
;;

let of_text_file ~filename =
let of_text_file ~path =
let q = Queue.create () in
In_channel.with_file filename ~f:(fun ic ->
In_channel.with_file (path |> Fpath.to_string) ~f:(fun ic ->
with_return (fun { return } ->
while true do
match In_channel.input_line ic with
Expand All @@ -36,8 +36,8 @@ let of_text_file ~filename =

let to_text_channel t oc = Printf.fprintf oc "%s\n" (to_string t)

let to_text_file t ~filename =
Out_channel.with_file filename ~f:(fun oc -> to_text_channel t oc)
let to_text_file t ~path =
Out_channel.with_file (path |> Fpath.to_string) ~f:(fun oc -> to_text_channel t oc)
;;

let conflicts t ~with_:bits =
Expand Down
4 changes: 2 additions & 2 deletions lib/bit_utils/src/partial_bit_array.mli
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ val of_01star_chars_in_string : string -> t
val to_string : t -> string

(** See {!val:Bit_array.of_text_file}. *)
val of_text_file : filename:string -> t
val of_text_file : path:Fpath.t -> t

(** Save to disk, ends with a newline. *)
val to_text_file : t -> filename:string -> unit
val to_text_file : t -> path:Fpath.t -> unit

val to_text_channel : t -> Out_channel.t -> unit

Expand Down
8 changes: 4 additions & 4 deletions lib/bit_utils/src/partial_bit_matrix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ let of_partial_bit_array ~dimx ~dimy code =
if i < len then code.(i) else None)
;;

let of_text_file ~dimx ~dimy ~filename =
of_partial_bit_array ~dimx ~dimy (Partial_bit_array.of_text_file ~filename)
let of_text_file ~dimx ~dimy ~path =
of_partial_bit_array ~dimx ~dimy (Partial_bit_array.of_text_file ~path)
;;

let to_text_channel t oc =
Expand All @@ -16,6 +16,6 @@ let to_text_channel t oc =
Out_channel.newline oc)
;;

let to_text_file t ~filename =
Out_channel.with_file filename ~f:(fun oc -> to_text_channel t oc)
let to_text_file t ~path =
Out_channel.with_file (path |> Fpath.to_string) ~f:(fun oc -> to_text_channel t oc)
;;
4 changes: 2 additions & 2 deletions lib/bit_utils/src/partial_bit_matrix.mli
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type t = Partial_bit_array.t array [@@deriving compare, equal, sexp_of]
val of_partial_bit_array : dimx:int -> dimy:int -> Partial_bit_array.t -> t

(** See {!val:Partial_bit_array.of_text_file}. *)
val of_text_file : dimx:int -> dimy:int -> filename:string -> t
val of_text_file : dimx:int -> dimy:int -> path:Fpath.t -> t

val to_text_file : t -> filename:string -> unit
val to_text_file : t -> path:Fpath.t -> unit
val to_text_channel : t -> Out_channel.t -> unit
3 changes: 2 additions & 1 deletion lib/bit_utils/test/dune
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
core
core_unix
core_unix.filename_unix
expect_test_helpers_core)
expect_test_helpers_core
fpath)
(inline_tests)
(preprocess
(pps ppx_jane ppx_js_style -check-doc-comments)))
10 changes: 5 additions & 5 deletions lib/bit_utils/test/test__bit_array.ml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ let%expect_test "to_string roundtrip" =

let%expect_test "text files" =
let test t =
let filename = Filename_unix.temp_file "test__bit_array" "text" in
Bit_array.to_text_file t ~filename;
let contents = In_channel.read_all filename in
let t2 = Bit_array.of_text_file ~filename in
let path = Filename_unix.temp_file "test__bit_array" "text" |> Fpath.v in
Bit_array.to_text_file t ~path;
let contents = In_channel.read_all (path |> Fpath.to_string) in
let t2 = Bit_array.of_text_file ~path in
if not ([%equal: Bit_array.t] t t2)
then raise_s [%sexp "Value does not roundtrip", { t : Bit_array.t; t2 : Bit_array.t }];
let contents2 = Bit_array.to_string t ^ "\n" in
Expand All @@ -55,7 +55,7 @@ let%expect_test "text files" =
raise_s
[%sexp "String contents not equal", { contents : string; contents2 : string }];
print_endline contents;
Core_unix.unlink filename
Core_unix.unlink (path |> Fpath.to_string)
in
test [||];
[%expect {||}];
Expand Down
8 changes: 4 additions & 4 deletions lib/bit_utils/test/test__bit_matrix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ let%expect_test "of_bit_array" =
;;

let%expect_test "of_text_file" =
let filename = Filename_unix.temp_file "test__bit_matrix" "text" in
Out_channel.with_file filename ~f:(fun oc ->
let path = Filename_unix.temp_file "test__bit_matrix" "text" |> Fpath.v in
Out_channel.with_file (path |> Fpath.to_string) ~f:(fun oc ->
Printf.fprintf oc "// Hello comment\n";
Printf.fprintf oc "010101010101\n";
Printf.fprintf oc "011111111110\n");
let test ~dimx ~dimy =
let t = Bit_matrix.of_text_file ~dimx ~dimy ~filename in
let t = Bit_matrix.of_text_file ~dimx ~dimy ~path in
Bit_matrix.to_text_channel t stdout
in
test ~dimx:2 ~dimy:12;
Expand All @@ -63,6 +63,6 @@ let%expect_test "of_text_file" =
0101
0101
0111 |}];
Core_unix.unlink filename;
Core_unix.unlink (path |> Fpath.to_string);
()
;;
10 changes: 5 additions & 5 deletions lib/bit_utils/test/test__partial_bit_array.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ let%expect_test "to_string roundtrip" =

let%expect_test "text files" =
let test t =
let filename = Filename_unix.temp_file "test__bit_array" "text" in
Partial_bit_array.to_text_file t ~filename;
let contents = In_channel.read_all filename in
let t2 = Partial_bit_array.of_text_file ~filename in
let path = Filename_unix.temp_file "test__bit_array" "text" |> Fpath.v in
Partial_bit_array.to_text_file t ~path;
let contents = In_channel.read_all (path |> Fpath.to_string) in
let t2 = Partial_bit_array.of_text_file ~path in
if not ([%equal: Partial_bit_array.t] t t2)
then
raise_s
Expand All @@ -60,7 +60,7 @@ let%expect_test "text files" =
raise_s
[%sexp "String contents not equal", { contents : string; contents2 : string }];
print_endline contents;
Core_unix.unlink filename
Core_unix.unlink (path |> Fpath.to_string)
in
test [||];
[%expect {||}];
Expand Down
8 changes: 4 additions & 4 deletions lib/bit_utils/test/test__partial_bit_matrix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ let%expect_test "of_partial_bit_array" =
;;

let%expect_test "of_text_file" =
let filename = Filename_unix.temp_file "test__bit_matrix" "text" in
Out_channel.with_file filename ~f:(fun oc ->
let path = Filename_unix.temp_file "test__bit_matrix" "text" |> Fpath.v in
Out_channel.with_file (path |> Fpath.to_string) ~f:(fun oc ->
Printf.fprintf oc "// Hello comment\n";
Printf.fprintf oc "010*010*01*1\n";
Printf.fprintf oc "0**111*11110\n");
let test ~dimx ~dimy =
let t = Partial_bit_matrix.of_text_file ~dimx ~dimy ~filename in
let t = Partial_bit_matrix.of_text_file ~dimx ~dimy ~path in
Partial_bit_matrix.to_text_channel t stdout
in
test ~dimx:2 ~dimy:12;
Expand All @@ -49,6 +49,6 @@ let%expect_test "of_text_file" =
010*
01*1
0**1 |}];
Core_unix.unlink filename;
Core_unix.unlink (path |> Fpath.to_string);
()
;;
9 changes: 5 additions & 4 deletions lib/bopkit/src/comments.ml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
type t = Comment.t list Comments_state.Comment_node.t [@@deriving equal, sexp_of]
type t = Comment.t list Parsing_utils.Comments_state.Comment_node.t
[@@deriving equal, sexp_of]

let value t = Comments_state.Comment_node.value_exn t
let value t = Parsing_utils.Comments_state.Comment_node.value_exn t
let is_empty t = List.is_empty (value t)

let make ~attached_to =
Comments_state.comment_node ~attached_to ~f:(fun comments ->
Parsing_utils.Comments_state.comment_node ~attached_to ~f:(fun comments ->
List.map comments ~f:Comment.parse_exn)
;;

let none = Comments_state.Comment_node.return []
let none = Parsing_utils.Comments_state.Comment_node.return []
10 changes: 9 additions & 1 deletion lib/bopkit/src/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
(name bopkit)
(public_name bopkit.bopkit)
(flags :standard -w +a-4-40-41-42-44-45-66 -warn-error +a -open Base)
(libraries appendable-list base bit_utils error_log parsing_utils pp)
(libraries
appendable-list
base
bit_utils
error-log
fpath
fpath-extended
parsing-utils
pp)
(preprocess
(pps ppx_jane ppx_js_style -check-doc-comments)))
Loading

0 comments on commit 66e45dd

Please sign in to comment.