mirror of
https://github.com/comby-tools/comby.git
synced 2024-09-11 13:25:36 +03:00
fix rewrite for -newline-separated option and nested matches
This commit is contained in:
parent
ef89413fb4
commit
1f6d2cc1b9
@ -35,6 +35,7 @@ module Pipeline : sig
|
|||||||
-> ?metasyntax:Matchers.metasyntax
|
-> ?metasyntax:Matchers.metasyntax
|
||||||
-> ?fresh:(unit -> string)
|
-> ?fresh:(unit -> string)
|
||||||
-> ?configuration:Matchers.configuration
|
-> ?configuration:Matchers.configuration
|
||||||
|
-> ?substitute_in_place:bool
|
||||||
-> single_source
|
-> single_source
|
||||||
-> Matchers.specification
|
-> Matchers.specification
|
||||||
-> output
|
-> output
|
||||||
|
@ -335,14 +335,9 @@ module Printer = struct
|
|||||||
| Json_lines of json_kind
|
| Json_lines of json_kind
|
||||||
| Match_only
|
| Match_only
|
||||||
|
|
||||||
type replacement_output =
|
val convert : output_options -> output_format
|
||||||
{ output_format : output_format
|
|
||||||
; substitution_kind : substitution_kind
|
|
||||||
}
|
|
||||||
|
|
||||||
val convert : output_options -> replacement_output
|
val print : output_format -> string option -> Replacement.t list -> string -> string -> unit
|
||||||
|
|
||||||
val print : replacement_output -> string option -> Replacement.t list -> string -> string -> unit
|
|
||||||
|
|
||||||
end = struct
|
end = struct
|
||||||
|
|
||||||
@ -364,44 +359,26 @@ module Printer = struct
|
|||||||
| Json_lines of json_kind
|
| Json_lines of json_kind
|
||||||
| Match_only
|
| Match_only
|
||||||
|
|
||||||
type replacement_output =
|
type replacement_output = output_format
|
||||||
{ output_format : output_format
|
|
||||||
; substitution_kind : substitution_kind
|
|
||||||
}
|
|
||||||
|
|
||||||
let convert output_options : replacement_output =
|
let convert output_options : output_format =
|
||||||
let output_format =
|
match output_options with
|
||||||
match output_options with
|
| { interactive_review = Some _; _ } -> Interactive_review
|
||||||
| { interactive_review = Some _; _ } -> Interactive_review
|
| { overwrite_file_in_place = true; _ } -> Overwrite_file
|
||||||
| { overwrite_file_in_place = true; _ } -> Overwrite_file
|
| { stdout = true; _ } -> Stdout
|
||||||
| { stdout = true; _ } -> Stdout
|
| { json_lines = true; overwrite_file_in_place = false; json_only_diff; _ } ->
|
||||||
| { json_lines = true; overwrite_file_in_place = false; json_only_diff; _ } ->
|
if json_only_diff then
|
||||||
if json_only_diff then
|
Json_lines Only_diff
|
||||||
Json_lines Only_diff
|
else
|
||||||
else
|
Json_lines Everything
|
||||||
Json_lines Everything
|
| { diff = true; color = false; _ } ->
|
||||||
| { diff = true; color = false; _ } ->
|
Diff Plain
|
||||||
Diff Plain
|
| { color = true; _ }
|
||||||
| { color = true; _ }
|
| _ ->
|
||||||
| _ ->
|
Diff Colored
|
||||||
Diff Colored
|
|
||||||
in
|
|
||||||
if output_options.substitute_in_place then
|
|
||||||
{ output_format; substitution_kind = In_place }
|
|
||||||
else
|
|
||||||
{ output_format; substitution_kind = Newline_separated }
|
|
||||||
|
|
||||||
let print { output_format; substitution_kind } path replacements rewritten_source source_content =
|
let print output_format path replacements rewritten_source source_content =
|
||||||
let open Replacement in
|
|
||||||
let ppf = Format.std_formatter in
|
let ppf = Format.std_formatter in
|
||||||
let rewritten_source =
|
|
||||||
match substitution_kind with
|
|
||||||
| In_place -> rewritten_source
|
|
||||||
| Newline_separated ->
|
|
||||||
List.rev_map replacements ~f:(fun { replacement_content; _ } -> replacement_content)
|
|
||||||
|> String.concat ~sep:"\n"
|
|
||||||
|> Format.sprintf "%s\n"
|
|
||||||
in
|
|
||||||
let print_if_some output = Option.value_map output ~default:() ~f:(Format.fprintf ppf "%s@.") in
|
let print_if_some output = Option.value_map output ~default:() ~f:(Format.fprintf ppf "%s@.") in
|
||||||
match output_format with
|
match output_format with
|
||||||
| Stdout ->
|
| Stdout ->
|
||||||
@ -435,6 +412,7 @@ type t =
|
|||||||
; interactive_review : interactive_review option
|
; interactive_review : interactive_review option
|
||||||
; matcher : (module Matchers.Matcher.S)
|
; matcher : (module Matchers.Matcher.S)
|
||||||
; metasyntax : Matchers.Metasyntax.t option
|
; metasyntax : Matchers.Metasyntax.t option
|
||||||
|
; substitute_in_place : bool
|
||||||
}
|
}
|
||||||
|
|
||||||
let emit_errors { input_options; output_options; _ } =
|
let emit_errors { input_options; output_options; _ } =
|
||||||
@ -728,6 +706,7 @@ let create
|
|||||||
; color
|
; color
|
||||||
; count
|
; count
|
||||||
; interactive_review
|
; interactive_review
|
||||||
|
; substitute_in_place
|
||||||
; _
|
; _
|
||||||
} as output_options)
|
} as output_options)
|
||||||
} as configuration)
|
} as configuration)
|
||||||
@ -820,7 +799,7 @@ let create
|
|||||||
Printer.Rewrite.convert output_options
|
Printer.Rewrite.convert output_options
|
||||||
|> fun replacement_output ->
|
|> fun replacement_output ->
|
||||||
if match_only && color then
|
if match_only && color then
|
||||||
Printer.Rewrite.print { replacement_output with output_format = Match_only } source_path replacements result source_content
|
Printer.Rewrite.print Match_only source_path replacements result source_content
|
||||||
else
|
else
|
||||||
Printer.Rewrite.print replacement_output source_path replacements result source_content
|
Printer.Rewrite.print replacement_output source_path replacements result source_content
|
||||||
in
|
in
|
||||||
@ -834,4 +813,5 @@ let create
|
|||||||
; output_printer
|
; output_printer
|
||||||
; interactive_review
|
; interactive_review
|
||||||
; metasyntax
|
; metasyntax
|
||||||
|
; substitute_in_place
|
||||||
}
|
}
|
||||||
|
@ -92,6 +92,7 @@ type t =
|
|||||||
; interactive_review : interactive_review option
|
; interactive_review : interactive_review option
|
||||||
; matcher : (module Matchers.Matcher.S)
|
; matcher : (module Matchers.Matcher.S)
|
||||||
; metasyntax : Matchers.Metasyntax.t option
|
; metasyntax : Matchers.Metasyntax.t option
|
||||||
|
; substitute_in_place : bool
|
||||||
}
|
}
|
||||||
|
|
||||||
val create : user_input -> t Or_error.t
|
val create : user_input -> t Or_error.t
|
||||||
|
@ -53,6 +53,7 @@ let process_single_source
|
|||||||
?(timeout = 3)
|
?(timeout = 3)
|
||||||
?metasyntax
|
?metasyntax
|
||||||
?fresh
|
?fresh
|
||||||
|
?(substitute_in_place = true)
|
||||||
configuration
|
configuration
|
||||||
source
|
source
|
||||||
(Specification.{ rewrite_template; _ } as specification)
|
(Specification.{ rewrite_template; _ } as specification)
|
||||||
@ -84,7 +85,8 @@ let process_single_source
|
|||||||
(* If there are no matches, return the original source (for editor support). *)
|
(* If there are no matches, return the original source (for editor support). *)
|
||||||
Replacement ([], input_text, 0)
|
Replacement ([], input_text, 0)
|
||||||
| matches ->
|
| matches ->
|
||||||
match Rewrite.all ~source:input_text ?metasyntax ?fresh ?filepath ~rewrite_template matches with
|
let source = if substitute_in_place then Some input_text else None in
|
||||||
|
match Rewrite.all ?source ?metasyntax ?fresh ?filepath ~rewrite_template matches with
|
||||||
| None -> Nothing
|
| None -> Nothing
|
||||||
| Some { rewritten_source; in_place_substitutions } ->
|
| Some { rewritten_source; in_place_substitutions } ->
|
||||||
Replacement (in_place_substitutions, rewritten_source, List.length matches)
|
Replacement (in_place_substitutions, rewritten_source, List.length matches)
|
||||||
@ -197,6 +199,7 @@ let run_interactive
|
|||||||
matcher
|
matcher
|
||||||
fast_offset_conversion
|
fast_offset_conversion
|
||||||
match_configuration
|
match_configuration
|
||||||
|
substitute_in_place
|
||||||
verbose
|
verbose
|
||||||
timeout
|
timeout
|
||||||
sources
|
sources
|
||||||
@ -212,6 +215,7 @@ let run_interactive
|
|||||||
~fast_offset_conversion
|
~fast_offset_conversion
|
||||||
~verbose
|
~verbose
|
||||||
~timeout
|
~timeout
|
||||||
|
~substitute_in_place
|
||||||
match_configuration
|
match_configuration
|
||||||
input
|
input
|
||||||
specification)
|
specification)
|
||||||
@ -236,6 +240,7 @@ let run
|
|||||||
{ matcher
|
{ matcher
|
||||||
; sources
|
; sources
|
||||||
; specifications
|
; specifications
|
||||||
|
; substitute_in_place
|
||||||
; run_options =
|
; run_options =
|
||||||
{ verbose
|
{ verbose
|
||||||
; match_timeout = timeout
|
; match_timeout = timeout
|
||||||
@ -278,6 +283,7 @@ let run
|
|||||||
~timeout
|
~timeout
|
||||||
?metasyntax
|
?metasyntax
|
||||||
?fresh
|
?fresh
|
||||||
|
~substitute_in_place
|
||||||
match_configuration
|
match_configuration
|
||||||
input
|
input
|
||||||
specification)
|
specification)
|
||||||
@ -297,6 +303,7 @@ let run
|
|||||||
matcher
|
matcher
|
||||||
fast_offset_conversion
|
fast_offset_conversion
|
||||||
match_configuration
|
match_configuration
|
||||||
|
substitute_in_place
|
||||||
verbose
|
verbose
|
||||||
timeout
|
timeout
|
||||||
sources
|
sources
|
||||||
@ -311,6 +318,7 @@ let execute
|
|||||||
?metasyntax
|
?metasyntax
|
||||||
?fresh
|
?fresh
|
||||||
?(configuration = Matchers.Configuration.create ())
|
?(configuration = Matchers.Configuration.create ())
|
||||||
|
?substitute_in_place
|
||||||
source
|
source
|
||||||
specification =
|
specification =
|
||||||
process_single_source
|
process_single_source
|
||||||
@ -320,6 +328,7 @@ let execute
|
|||||||
?timeout
|
?timeout
|
||||||
?metasyntax
|
?metasyntax
|
||||||
?fresh
|
?fresh
|
||||||
|
?substitute_in_place
|
||||||
configuration
|
configuration
|
||||||
source
|
source
|
||||||
specification
|
specification
|
||||||
|
@ -15,6 +15,7 @@ val process_single_source
|
|||||||
-> ?timeout:int
|
-> ?timeout:int
|
||||||
-> ?metasyntax:Matchers.Metasyntax.t
|
-> ?metasyntax:Matchers.Metasyntax.t
|
||||||
-> ?fresh:(unit -> string)
|
-> ?fresh:(unit -> string)
|
||||||
|
-> ?substitute_in_place:bool
|
||||||
-> Matchers.Configuration.t
|
-> Matchers.Configuration.t
|
||||||
-> single_source
|
-> single_source
|
||||||
-> Matchers.Specification.t
|
-> Matchers.Specification.t
|
||||||
@ -26,6 +27,7 @@ val execute
|
|||||||
-> ?metasyntax:Matchers.Metasyntax.t
|
-> ?metasyntax:Matchers.Metasyntax.t
|
||||||
-> ?fresh:(unit -> string)
|
-> ?fresh:(unit -> string)
|
||||||
-> ?configuration:Matchers.Configuration.t
|
-> ?configuration:Matchers.Configuration.t
|
||||||
|
-> ?substitute_in_place:bool
|
||||||
-> single_source
|
-> single_source
|
||||||
-> Matchers.Specification.t
|
-> Matchers.Specification.t
|
||||||
-> output
|
-> output
|
||||||
|
@ -1268,3 +1268,19 @@ let%expect_test "test_custom_metasyntax_reserved_identifiers" =
|
|||||||
print_string result;
|
print_string result;
|
||||||
[%expect "
|
[%expect "
|
||||||
(fun x -> f (x x))"]
|
(fun x -> f (x x))"]
|
||||||
|
|
||||||
|
let%expect_test "test_nested_newline_rewrite" =
|
||||||
|
let source = "foo(foo(foo(x)))" in
|
||||||
|
let match_ = "foo(:[x])" in
|
||||||
|
let rule = "where nested" in
|
||||||
|
let rewrite = ":[x]" in
|
||||||
|
let command_args =
|
||||||
|
Format.sprintf "-stdin -sequential -stdout '%s' '%s' -rule '%s' -f .c -newline-separated" match_ rewrite rule
|
||||||
|
in
|
||||||
|
let command = Format.sprintf "%s %s" binary_path command_args in
|
||||||
|
let result = read_expect_stdin_and_stdout command source in
|
||||||
|
print_string result;
|
||||||
|
[%expect{|
|
||||||
|
foo(foo(x))
|
||||||
|
foo(x)
|
||||||
|
x|}]
|
||||||
|
@ -21,6 +21,7 @@ let configuration (module E : Engine.S) =
|
|||||||
; output_printer = (fun _ -> ())
|
; output_printer = (fun _ -> ())
|
||||||
; interactive_review = None
|
; interactive_review = None
|
||||||
; metasyntax = None
|
; metasyntax = None
|
||||||
|
; substitute_in_place = true
|
||||||
}
|
}
|
||||||
|
|
||||||
(* TODO restore this, can't access the Parallel_hack module *)
|
(* TODO restore this, can't access the Parallel_hack module *)
|
||||||
|
Loading…
Reference in New Issue
Block a user