Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

escape settings (and highlight) - clarification needed #184

Closed
simlei opened this issue Nov 8, 2018 · 3 comments
Closed

escape settings (and highlight) - clarification needed #184

simlei opened this issue Nov 8, 2018 · 3 comments

Comments

@simlei
Copy link

simlei commented Nov 8, 2018

This plugin is awesome! However, I would suggest specifying the role of the "escape" setting for a tool a bit clearer. I am not completely certain what is does and what it doesn't.

I have the ripgrep tool with the default escape chars '\^$.*+?()[]{}|'. This is what I get:

  1. :Grepper <CR> -> (toolprompt) > '(xmap|xnoreremap)' -- this gets me results for both terms. Note the ' around the pattern.
  2. :Grepper <CR> -> (toolprompt) > \(xmap\|xnoreremap\) -- this gets me results for both terms. No single quotes this time
  3. & 4): as above, with queries '\(xmap\|xnoreremap\)' : no results found and (xmap|xnoremap) : syntax error: ( unexpected.

I checked the g:grepper dictionary to be sure the settings were correct, this is the relevant entry:
'rgni': {'escape': '\^$.*+?()[]{}|', 'grepformat': '%f:%l:%c:%m', 'grepprg': 'rg -H --no-heading --vimgrep --no-ignore-vcs '},

So, it seems to me, the escape chars are not actually applied to the query I enter? Both with and without quotes? It would be very helpful to get an elaboration on that.

Additionally, the issue of highlighting the matches in the quickfix window: With 2), the matches are highlightes correctly which is very satisfying. With 1) this does not work. Are the escape characters used to convert the query to a vim regex string, or is the query used as-is to populate the / register? Are (at least) the quotes removed if there are any, like in 1)?

To that end, after having read #60 , I could envision a method to get precise highlighting from using the query together with the escape settings, if

  1. the entered query is let through to the grep program as-is, WITHOUT any characters being escaped (as it seems to behave now, but a bit contrary to the doc which states the query IS being escaped)
  2. the escape chars are informal as to the query (which characters have a special meaning, i.e. do not represent the actual characters), and are used to construct a Vim "very nomagic" \V highlight string. This would work with all the standard regex operators like |, ?, *, +, ... . Grepper would need to strip single quotes from the query (reverse engineering shell behavior as to what the grep program actually receives, but lets keep it at just the quotes), then take that string and escape any special character the escape setting indicates, and prefix it with a '\V'

I'd contribute that, but I need to know the meaning of the escape option...
Thanks for clarifying!

@mhinz
Copy link
Owner

mhinz commented Nov 8, 2018

Think of the prompt as the end of shell input. 1. and 2. work because rg '(xmap|xnoreremap)' and rg \(xmap\|xnoreremap\) in the shell work, but rg '\(xmap\|xnoreremap\)' does not.

When you enter the query manually, the escape characters are not used at all.

The escape characters are important for these cases:

  • The operator. When you visual select this text foo|bar, and escape contains |, then the Grepper will use \| (because that's the proper thing to do for this tool in the shell).
  • Different tools use different escape characters. So, when the prompt gets opened because you used the operator and start cycling through all the available grep tools, the current query needs to be requoted according to the tool's escape characters.
  • For highlighting. But highlighting is fragile and follows a "mostly good enough" approach.

Highlighting is complicated. It's easy to think of a few approaches, but it's hard to get right. That means it should work 1) for all tools the same, 2) should not depend on ANSI colors, and 3) should not break other already working features.

I'm open for discussions and PRs, but since I can't think of a promising approach, I won't work on it myself.

@mhinz mhinz closed this as completed in 4a47e20 Nov 8, 2018
@mhinz
Copy link
Owner

mhinz commented Nov 8, 2018

I hope the above makes it clearer. Thanks for bringing this up! ✨

@simlei
Copy link
Author

simlei commented Nov 8, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants