From fb280372a02769448b0ce01c28ba96217fe434bb Mon Sep 17 00:00:00 2001 From: Rijnard van Tonder Date: Wed, 29 May 2019 01:46:25 -0400 Subject: [PATCH] factor out replacement --- lib/comby.ml | 1 + lib/comby.mli | 1 + lib/dune | 1 + lib/matchers/matchers.ml | 5 ++--- lib/matchers/matchers.mli | 5 ++--- lib/replacement/dune | 5 +++++ lib/{rewriter => replacement}/replacement.ml | 0 lib/{rewriter => replacement}/replacement.mli | 0 lib/rewriter/dune | 2 +- lib/rewriter/replacement_context.ml | 14 -------------- lib/rewriter/replacement_context.mli | 14 -------------- lib/rewriter/rewriter.ml | 1 - lib/rewriter/rewriter.mli | 1 - src/command_configuration.ml | 8 ++++---- src/command_configuration.mli | 2 +- src/main.ml | 4 ++-- src/server.ml | 4 ++-- test/test_rewrite_parts.ml | 6 +++--- 18 files changed, 25 insertions(+), 49 deletions(-) create mode 100644 lib/replacement/dune rename lib/{rewriter => replacement}/replacement.ml (100%) rename lib/{rewriter => replacement}/replacement.mli (100%) delete mode 100644 lib/rewriter/replacement_context.ml delete mode 100644 lib/rewriter/replacement_context.mli diff --git a/lib/comby.ml b/lib/comby.ml index b5732e01..fc3338a8 100644 --- a/lib/comby.ml +++ b/lib/comby.ml @@ -1,5 +1,6 @@ module Language = Language module Matchers = Matchers module Match = Match +module Replacement = Replacement module Rewriter = Rewriter module Statistics = Statistics diff --git a/lib/comby.mli b/lib/comby.mli index b5732e01..fc3338a8 100644 --- a/lib/comby.mli +++ b/lib/comby.mli @@ -1,5 +1,6 @@ module Language = Language module Matchers = Matchers module Match = Match +module Replacement = Replacement module Rewriter = Rewriter module Statistics = Statistics diff --git a/lib/dune b/lib/dune index 41aa6e7c..6b8b48e7 100644 --- a/lib/dune +++ b/lib/dune @@ -8,6 +8,7 @@ mparser mparser.pcre comby.matchers + comby.replacement comby.rewriter comby.match comby.language diff --git a/lib/matchers/matchers.ml b/lib/matchers/matchers.ml index 93350fdb..6e73fe31 100644 --- a/lib/matchers/matchers.ml +++ b/lib/matchers/matchers.ml @@ -1,5 +1,4 @@ -include Languages - module Configuration = Configuration - module type Matcher = Types.Matcher.S + +include Languages diff --git a/lib/matchers/matchers.mli b/lib/matchers/matchers.mli index df7f9149..a0239eb4 100644 --- a/lib/matchers/matchers.mli +++ b/lib/matchers/matchers.mli @@ -1,5 +1,4 @@ -include module type of Languages - module Configuration = Configuration - module type Matcher = Types.Matcher.S + +include module type of Languages diff --git a/lib/replacement/dune b/lib/replacement/dune new file mode 100644 index 00000000..fa2d99d1 --- /dev/null +++ b/lib/replacement/dune @@ -0,0 +1,5 @@ +(library + (name replacement) + (public_name comby.replacement) + (preprocess (pps ppx_deriving.show ppx_deriving.eq ppx_sexp_conv ppx_sexp_message ppx_deriving_yojson bisect_ppx -conditional -no-comment-parsing)) + (libraries comby.parsers comby.match ppxlib core mparser mparser.pcre yojson ppx_deriving_yojson ppx_deriving_yojson.runtime)) diff --git a/lib/rewriter/replacement.ml b/lib/replacement/replacement.ml similarity index 100% rename from lib/rewriter/replacement.ml rename to lib/replacement/replacement.ml diff --git a/lib/rewriter/replacement.mli b/lib/replacement/replacement.mli similarity index 100% rename from lib/rewriter/replacement.mli rename to lib/replacement/replacement.mli diff --git a/lib/rewriter/dune b/lib/rewriter/dune index 0678e888..769232b3 100644 --- a/lib/rewriter/dune +++ b/lib/rewriter/dune @@ -2,4 +2,4 @@ (name rewriter) (public_name comby.rewriter) (preprocess (pps ppx_deriving.show ppx_sexp_conv ppx_deriving_yojson bisect_ppx -conditional -no-comment-parsing)) - (libraries comby.matchers ppxlib core core.uuid)) + (libraries comby.matchers comby.replacement ppxlib core core.uuid)) diff --git a/lib/rewriter/replacement_context.ml b/lib/rewriter/replacement_context.ml deleted file mode 100644 index abe602b4..00000000 --- a/lib/rewriter/replacement_context.ml +++ /dev/null @@ -1,14 +0,0 @@ -open Match - -type t = - { range : range - ; replacement_content : string - ; environment : environment - } -[@@deriving yojson] - -type result = - { rewritten_source : string - ; in_place_substitutions : t list - } -[@@deriving yojson] diff --git a/lib/rewriter/replacement_context.mli b/lib/rewriter/replacement_context.mli deleted file mode 100644 index abe602b4..00000000 --- a/lib/rewriter/replacement_context.mli +++ /dev/null @@ -1,14 +0,0 @@ -open Match - -type t = - { range : range - ; replacement_content : string - ; environment : environment - } -[@@deriving yojson] - -type result = - { rewritten_source : string - ; in_place_substitutions : t list - } -[@@deriving yojson] diff --git a/lib/rewriter/rewriter.ml b/lib/rewriter/rewriter.ml index ff6c5599..8d72a9c3 100644 --- a/lib/rewriter/rewriter.ml +++ b/lib/rewriter/rewriter.ml @@ -1,3 +1,2 @@ -module Replacement = Replacement module Rewrite = Rewrite module Rewrite_template = Rewrite_template diff --git a/lib/rewriter/rewriter.mli b/lib/rewriter/rewriter.mli index ff6c5599..8d72a9c3 100644 --- a/lib/rewriter/rewriter.mli +++ b/lib/rewriter/rewriter.mli @@ -1,3 +1,2 @@ -module Replacement = Replacement module Rewrite = Rewrite module Rewrite_template = Rewrite_template diff --git a/src/command_configuration.ml b/src/command_configuration.ml index baa3a42f..8e260fc0 100644 --- a/src/command_configuration.ml +++ b/src/command_configuration.ml @@ -113,7 +113,7 @@ module Printer = struct } | Replacements of { source_path : string option - ; replacements : Rewriter.Replacement.t list + ; replacements : Replacement.t list ; result : string ; source_content : string } @@ -168,7 +168,7 @@ module Printer = struct val convert : output_options -> replacement_output - val print : replacement_output -> string option -> Rewriter.Replacement.t list -> string -> string -> unit + val print : replacement_output -> string option -> Replacement.t list -> string -> string -> unit end = struct @@ -181,13 +181,13 @@ module Printer = struct (* only used in rewrite *) let get_json_rewrites replacements result = - let value = `List (List.map ~f:Rewriter.Replacement.to_yojson replacements) in + let value = `List (List.map ~f:Replacement.to_yojson replacements) in `Assoc [("uri", `Null); ("rewritten_source", `String result); ("in_place_substitutions", value)] (* only used in rewrite *) let json_rewrites replacements (path: string) (diff: string) result = let value = - `List (List.map ~f:Rewriter.Replacement.to_yojson replacements) in + `List (List.map ~f:Replacement.to_yojson replacements) in `Assoc [ ("uri", `String path) ; ("rewritten_source", `String result) diff --git a/src/command_configuration.mli b/src/command_configuration.mli index 59ef3ddd..6b5834da 100644 --- a/src/command_configuration.mli +++ b/src/command_configuration.mli @@ -8,7 +8,7 @@ module Printer : sig } | Replacements of { source_path : string option - ; replacements : Rewriter.Replacement.t list + ; replacements : Replacement.t list ; result : string ; source_content : string } diff --git a/src/main.ml b/src/main.ml index bc14aa5e..02d573b9 100644 --- a/src/main.ml +++ b/src/main.ml @@ -19,7 +19,7 @@ type json_result = type processed_source_result = | Matches of (Match.t list * int) - | Replacement of (Rewriter.Replacement.t list * string * int) + | Replacement of (Replacement.t list * string * int) | Nothing let verbose_out_file = "/tmp/comby.out" @@ -93,7 +93,7 @@ let process_single_source |> fun matches -> if matches = [] then (* If there are no matches, return the original source (for editor support). *) - Some (Some (Rewriter.Replacement.{ rewritten_source = input_text; in_place_substitutions = [] }), []) + Some (Some (Replacement.{ rewritten_source = input_text; in_place_substitutions = [] }), []) else Some (rewrite rewrite_template rewrite_rule input_text matches, matches) in diff --git a/src/server.ml b/src/server.ml index 160bb364..6bd4260c 100644 --- a/src/server.ml +++ b/src/server.ml @@ -47,7 +47,7 @@ type json_match_result = type json_rewrite_result = { rewritten_source : string - ; in_place_substitutions : Rewriter.Replacement.t list + ; in_place_substitutions : Replacement.t list ; id : int } [@@deriving yojson] @@ -108,7 +108,7 @@ let perform_match request = if debug then Format.printf "Result (400) %s@." error; respond ~code:(`Code 400) (`String error) -let rewrite_to_json id ({ rewritten_source; in_place_substitutions } : Rewriter.Replacement.result) = +let rewrite_to_json id ({ rewritten_source; in_place_substitutions } : Replacement.result) = Format.sprintf "%s" (Yojson.Safe.pretty_to_string (json_rewrite_result_to_yojson diff --git a/test/test_rewrite_parts.ml b/test/test_rewrite_parts.ml index 4d0ac111..8badf9be 100644 --- a/test/test_rewrite_parts.ml +++ b/test/test_rewrite_parts.ml @@ -51,7 +51,7 @@ let%expect_test "comments_in_string_literals_should_not_be_treated_as_comments_b all match_template source |> Rewrite.all ~source ~rewrite_template |> (function - | Some rewrite_result -> print_string (Yojson.Safe.pretty_to_string (Rewriter.Replacement.result_to_yojson rewrite_result)) + | Some rewrite_result -> print_string (Yojson.Safe.pretty_to_string (Replacement.result_to_yojson rewrite_result)) | None -> print_string "BROKEN EXPECT"); [%expect_exact {|{ "rewritten_source": "123433312343331122", @@ -99,7 +99,7 @@ let%expect_test "comments_in_string_literals_should_not_be_treated_as_comments_b all match_template source |> Rewrite.all ~source ~rewrite_template |> (function - | Some rewrite_result -> print_string (Yojson.Safe.pretty_to_string (Rewriter.Replacement.result_to_yojson rewrite_result)) + | Some rewrite_result -> print_string (Yojson.Safe.pretty_to_string (Replacement.result_to_yojson rewrite_result)) | None -> print_string "BROKEN EXPECT"); [%expect_exact {|{ "rewritten_source": "123433312343331122;123433312343331122;", @@ -180,7 +180,7 @@ let%expect_test "multiple_contextual_substitutions" = all match_template source |> Rewrite.all ~source ~rewrite_template |> (function - | Some rewrite_result -> print_string (Yojson.Safe.pretty_to_string (Rewriter.Replacement.result_to_yojson rewrite_result)) + | Some rewrite_result -> print_string (Yojson.Safe.pretty_to_string (Replacement.result_to_yojson rewrite_result)) | None -> print_string "BROKEN EXPECT"); [%expect_exact {|{ "rewritten_source": "xxxx bar xxxx",