nvim-telescope / telescope.nvim

Find, Filter, Preview, Pick. All lua, all the time.
MIT License
15.56k stars 826 forks source link

Set `default_text` to visual selection on Nightly #2988

Open fdschmidt93 opened 6 months ago

fdschmidt93 commented 6 months ago

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:

Cons:

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.

jamestrew commented 6 months ago

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 commented 6 months ago

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)