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

Set default_text to visual selection on Nightly #2988

Open
fdschmidt93 opened this issue Mar 16, 2024 · 2 comments
Open

Set default_text to visual selection on Nightly #2988

fdschmidt93 opened this issue Mar 16, 2024 · 2 comments
Labels
enhancement Enhancement to performance, inner workings or existent features

Comments

@fdschmidt93
Copy link
Member

fdschmidt93 commented Mar 16, 2024

Thanks to Shougo and zeertzjq we now have a proper function to get visual text in Neovim (nightly, for now).

local get_selection = function()
  return vim.fn.getregion(
    vim.fn.getpos ".", vim.fn.getpos "v", { mode = vim.fn.mode() }
  )
end
vim.keymap.set(
  "v",
  "<space>rg",
  function() require("telescope.builtin").live_grep {
    default_text = table.concat(get_selection())
  }
  end
)

and as used official in Neovim nightly here https://github.com/neovim/neovim/pull/27663/files

Is this something we would want default_text to, well, default to if the user launches a picker from visual mode and default_text is unset?

Pros:

  • very nice ease of use, highly request feature; I use it with my live_grep variant several times a day

Cons:

  • People can just implement them themselves (which is what we sort of always went with)
  • people with unusual use cases would have to opt out by passing "" or something of sorts.
  • mostly universally applicable only to selected pickers (live_grep, grep_string -- which already supports this IIRC)

Putting this up for discussion. Happy to get thoughts and make a PR if desirable at least in some contexts :) (like adding it also to live_grep). Otherwise, we can also close this FR.

@fdschmidt93 fdschmidt93 added the enhancement Enhancement to performance, inner workings or existent features label Mar 16, 2024
@jamestrew
Copy link
Contributor

From the linked PR:

Also make it work better on a multiline selection.

This reminds me, we don't really support multi-line greps well. Pasting multi-line text into the prompt buffer is not really functional due to the nature of buftype='prompt' but even if we were to use default_text to get around this and pass multi-line text to rg, we're constrained by whether --multiline flag is passed. And then the issue of properly escaping regex characters or passing --fixed-strings comes up.

I think these factors, particularly the last one of escaping regex characters, makes inserting anything longer than <cword> kind of user error-prone, or at the very least confusing. I'm not sure what the best mechanism is for making this user friendly and obvious. I think telescope-live-grep-args is probably better suited for something like this anyways.

Eg. If someone visually selects function() and greps it (already possible with grep_string), and they don't see any results not knowing you really need to grep for function\(\) or pass --fixed-string.

@fdschmidt93
Copy link
Member Author

fdschmidt93 commented Mar 16, 2024

Yeah, there are a lot of wacky cases to consider. I guess that's why it is better left to the user. In other words, best approach is adding something like the above (plus considerations) somewhere clearly to the docs.

(Once the user is generally aware of limitations etc. it is however typically a very nice improvement over and friends)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement to performance, inner workings or existent features
Projects
None yet
Development

No branches or pull requests

2 participants