mattlqx / pre-commit-search-and-replace

Plugin for pre-commit for arbitrary search and replace on committed files.
MIT License
12 stars 7 forks source link

Enable passing multiline option to regex #25

Open cbachhuber opened 2 hours ago

cbachhuber commented 2 hours ago

To enable regex, the config requires the search string to start and end with /. However, ruby regexes pass options after the end delimiter, so that's conflicting. I can reproduce this with the following .pre-commit-search-and-replace.yaml:

- search: /a+/
  replacement: ""

matches a file such as

aaaa

while

- search: /a+/m
  replacement: ""

doesn't match, since this is not seen as regex per this line.

How should we best implement this? Provide another command line argument, similar to --insensitive? I tried that, but it doesn't seem to work as search_opts: "--insensitive" in .pre-commit-search-and-replace.yaml or args: ["--insensitive"] in .pre-commit-config.yaml.

cbachhuber commented 1 hour ago

Since there are only 4 options in ruby regex, I suggest extending the matcher in this line to something like

    !%r{^/.*/([imxo]){0,4}$}.match(string).nil?

and correspondingly passing on the provided options to the regex object.