add .file alias for .file.path and fix filepath

This commit is contained in:
Rijnard van Tonder 2021-06-06 13:54:17 -07:00
parent fbe48cb30b
commit 0a29179410
5 changed files with 43 additions and 28 deletions

View File

@ -82,8 +82,11 @@ let process_single_source
| Some rewrite_template -> | Some rewrite_template ->
match matches with match matches with
| [] -> | [] ->
(* If there are no matches, return the original source (for editor support). *) (* If there are no matches, return the original source (for editor support) if substitute_in_place is active. *)
Replacement ([], input_text, 0) if substitute_in_place then
Replacement ([], input_text, 0)
else
Nothing
| matches -> | matches ->
(* FIXME this should be configured where it's done in command_configuration.ml *) (* FIXME this should be configured where it's done in command_configuration.ml *)
let external_handler = External_semantic.lsif_hover in let external_handler = External_semantic.lsif_hover in

View File

@ -111,11 +111,15 @@ let apply
let source = rewrite_substitute (Template.to_string t) env in let source = rewrite_substitute (Template.to_string t) env in
let configuration = Configuration.create ~match_kind:Fuzzy () in let configuration = Configuration.create ~match_kind:Fuzzy () in
let configuration = { configuration with substitute_in_place } 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 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 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 else
let Replacement.{ rewritten_source; _ } = Option.value_exn result in let Replacement.{ rewritten_source; _ } = Option.value_exn result in
(* substitute for variables that are in the outside scope *) (* substitute for variables that are in the outside scope *)

View File

@ -154,7 +154,7 @@ module Make (Language : Types.Language.S) (Meta : Metasyntax.S) (Ext : External.
| None -> | None ->
if rewrite then if rewrite then
begin 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 (* Don't just append, but replace the match context including constant
strings. I.e., somewhere where we are appending the parth that matched, it strings. I.e., somewhere where we are appending the parth that matched, it
shouldn't, and instead just ignore. *) 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; if debug then Format.printf "Some new env %s@." @@ Match.Environment.to_string env;
current_environment_ref := env; current_environment_ref := env;
begin 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 (* Don't just append, but replace the match context including constant
strings. I.e., somewhere where we are appending the parth that matched, it strings. I.e., somewhere where we are appending the parth that matched, it
shouldn't, and instead just ignore. *) shouldn't, and instead just ignore. *)

View File

@ -5,6 +5,27 @@ let debug =
| exception Not_found -> false | exception Not_found -> false
| _ -> true | _ -> 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 let map_aliases
(module Metasyntax : Metasyntax.S) (module Metasyntax : Metasyntax.S)
(module External : External.S) (module External : External.S)
@ -14,24 +35,9 @@ let map_aliases
List.fold Metasyntax.aliases List.fold Metasyntax.aliases
~init:(template, parent_rule) ~init:(template, parent_rule)
~f:(fun (template, parent_rule) Types.Metasyntax.{ pattern; match_template; rule } -> ~f:(fun (template, parent_rule) Types.Metasyntax.{ pattern; match_template; rule } ->
let open Option in let template', parent_rule' =
match String.substr_index template ~pattern with match String.substr_index template ~pattern with
| None -> template, parent_rule | None -> template, parent_rule
| Some _ -> | Some _ -> map_template (module Parser) template pattern match_template rule parent_rule
let template' = String.substr_replace_all template ~pattern ~with_:match_template in in
if debug then Format.printf "Substituted: %s@." template'; template', parent_rule')
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')

View File

@ -88,6 +88,7 @@ module Make (Metasyntax : Types.Metasyntax.S) (External : Types.External.S) : Ty
| "column" | "column"
| "column.start" -> ColumnStart | "column.start" -> ColumnStart
| "column.end" -> ColumnEnd | "column.end" -> ColumnEnd
| "file"
| "file.path" -> FilePath | "file.path" -> FilePath
| "file.name" -> FileName | "file.name" -> FileName
| "file.directory" -> FileDirectory | "file.directory" -> FileDirectory
@ -119,6 +120,7 @@ module Make (Metasyntax : Types.Metasyntax.S) (External : Types.External.S) : Ty
; string "file.path" ; string "file.path"
; string "file.name" ; string "file.name"
; string "file.directory" ; string "file.directory"
; string "file"
; string "lowercase" ; string "lowercase"
; string "UPPERCASE" ; string "UPPERCASE"
; string "Capitalize" ; string "Capitalize"