From 0a2917941079c6ece2ea14b6d71a9e1ead380253 Mon Sep 17 00:00:00 2001 From: Rijnard van Tonder Date: Sun, 6 Jun 2021 13:54:17 -0700 Subject: [PATCH] add .file alias for .file.path and fix filepath --- lib/app/pipeline/pipeline.ml | 7 +++-- lib/kernel/matchers/evaluate.ml | 10 +++++-- lib/kernel/matchers/omega.ml | 4 +-- lib/kernel/matchers/preprocess.ml | 48 +++++++++++++++++-------------- lib/kernel/matchers/template.ml | 2 ++ 5 files changed, 43 insertions(+), 28 deletions(-) diff --git a/lib/app/pipeline/pipeline.ml b/lib/app/pipeline/pipeline.ml index 8865dd3..ebfb887 100644 --- a/lib/app/pipeline/pipeline.ml +++ b/lib/app/pipeline/pipeline.ml @@ -82,8 +82,11 @@ let process_single_source | Some rewrite_template -> match matches with | [] -> - (* If there are no matches, return the original source (for editor support). *) - Replacement ([], input_text, 0) + (* If there are no matches, return the original source (for editor support) if substitute_in_place is active. *) + if substitute_in_place then + Replacement ([], input_text, 0) + else + Nothing | matches -> (* FIXME this should be configured where it's done in command_configuration.ml *) let external_handler = External_semantic.lsif_hover in diff --git a/lib/kernel/matchers/evaluate.ml b/lib/kernel/matchers/evaluate.ml index 7404baf..9ea71a0 100644 --- a/lib/kernel/matchers/evaluate.ml +++ b/lib/kernel/matchers/evaluate.ml @@ -111,11 +111,15 @@ let apply let source = rewrite_substitute (Template.to_string t) env in let configuration = Configuration.create ~match_kind:Fuzzy () in let configuration = { configuration with substitute_in_place } in - let matches = match_all ~configuration ~template ~source () in + let matches = match_all ?filepath ~configuration ~template ~source () in let source = if substitute_in_place then Some source else None in - let result = Rewrite.all ~metasyntax ?source ~rewrite_template matches in + let result = Rewrite.all ~metasyntax ?filepath ?source ~rewrite_template matches in if Option.is_empty result then - true, Some env (* rewrites are always sat. *) + (if substitute_in_place then + (* rewrites are always sat for in-place. always unsat for newline-sep. *) + true, Some env + else + false, Some env) else let Replacement.{ rewritten_source; _ } = Option.value_exn result in (* substitute for variables that are in the outside scope *) diff --git a/lib/kernel/matchers/omega.ml b/lib/kernel/matchers/omega.ml index cde5e24..eeec516 100644 --- a/lib/kernel/matchers/omega.ml +++ b/lib/kernel/matchers/omega.ml @@ -154,7 +154,7 @@ module Make (Language : Types.Language.S) (Meta : Metasyntax.S) (Ext : External. | None -> if rewrite then begin - let result, _ = Template.substitute (Template.parse !rewrite_template) !current_environment_ref in + let result, _ = Template.substitute ?filepath:!filepath_ref (Template.parse !rewrite_template) !current_environment_ref in (* Don't just append, but replace the match context including constant strings. I.e., somewhere where we are appending the parth that matched, it shouldn't, and instead just ignore. *) @@ -193,7 +193,7 @@ module Make (Language : Types.Language.S) (Meta : Metasyntax.S) (Ext : External. if debug then Format.printf "Some new env %s@." @@ Match.Environment.to_string env; current_environment_ref := env; begin - let result, _ = Template.substitute (Template.parse !rewrite_template) !current_environment_ref in + let result, _ = Template.substitute ?filepath:!filepath_ref (Template.parse !rewrite_template) !current_environment_ref in (* Don't just append, but replace the match context including constant strings. I.e., somewhere where we are appending the parth that matched, it shouldn't, and instead just ignore. *) diff --git a/lib/kernel/matchers/preprocess.ml b/lib/kernel/matchers/preprocess.ml index 9fcc988..74faab2 100644 --- a/lib/kernel/matchers/preprocess.ml +++ b/lib/kernel/matchers/preprocess.ml @@ -5,6 +5,27 @@ let debug = | exception Not_found -> false | _ -> true +let append_rule (module Parser : Types.Rule.S) rule parent_rule = + let open Option in + let rule = + rule + >>| Parser.create + >>| function + | Ok rule -> rule + | Error e -> failwith @@ "Could not parse rule for alias entry:"^(Error.to_string_hum e) + in + match parent_rule, rule with + | Some parent_rule, Some rule -> Some (parent_rule @ rule) + | None, Some rule -> Some rule + | Some parent_rule, None -> Some parent_rule + | None, None -> None + +let map_template (module Parser : Types.Rule.S) template pattern match_template rule parent_rule = + let template' = String.substr_replace_all template ~pattern ~with_:match_template in + if debug then Format.printf "Substituted: %s@." template'; + let rule' = append_rule (module Parser) rule parent_rule in + template', rule' + let map_aliases (module Metasyntax : Metasyntax.S) (module External : External.S) @@ -14,24 +35,9 @@ let map_aliases List.fold Metasyntax.aliases ~init:(template, parent_rule) ~f:(fun (template, parent_rule) Types.Metasyntax.{ pattern; match_template; rule } -> - let open Option in - match String.substr_index template ~pattern with - | None -> template, parent_rule - | Some _ -> - let template' = String.substr_replace_all template ~pattern ~with_:match_template in - if debug then Format.printf "Substituted: %s@." template'; - let rule' = - let rule = - rule - >>| Parser.create - >>| function - | Ok rule -> rule - | Error e -> failwith @@ "Could not parse rule for alias entry:"^(Error.to_string_hum e) - in - match parent_rule, rule with - | Some parent_rule, Some rule -> Some (parent_rule @ rule) - | None, Some rule -> Some rule - | Some parent_rule, None -> Some parent_rule - | None, None -> None - in - template', rule') + let template', parent_rule' = + match String.substr_index template ~pattern with + | None -> template, parent_rule + | Some _ -> map_template (module Parser) template pattern match_template rule parent_rule + in + template', parent_rule') diff --git a/lib/kernel/matchers/template.ml b/lib/kernel/matchers/template.ml index d6f9fbd..423a07f 100644 --- a/lib/kernel/matchers/template.ml +++ b/lib/kernel/matchers/template.ml @@ -88,6 +88,7 @@ module Make (Metasyntax : Types.Metasyntax.S) (External : Types.External.S) : Ty | "column" | "column.start" -> ColumnStart | "column.end" -> ColumnEnd + | "file" | "file.path" -> FilePath | "file.name" -> FileName | "file.directory" -> FileDirectory @@ -119,6 +120,7 @@ module Make (Metasyntax : Types.Metasyntax.S) (External : Types.External.S) : Ty ; string "file.path" ; string "file.name" ; string "file.directory" + ; string "file" ; string "lowercase" ; string "UPPERCASE" ; string "Capitalize"