feat(picker): allow appending original window cword to prompt (#2878)

* feat(picker): allow appending original window cword to prompt

* add default mapping and document

* [docgen] Update doc/telescope.txt
skip-checks: true

* add mapping to README

also format README

* oops

* [docgen] Update doc/telescope.txt
skip-checks: true

---------

Co-authored-by: Github Actions <actions@github>
This commit is contained in:
James Trew
2024-01-19 21:38:27 -05:00
committed by GitHub
parent 9cf58f438f
commit 24778fd72f
5 changed files with 108 additions and 86 deletions

View File

@@ -69,7 +69,7 @@ wiki.
- [sharkdp/fd](https://github.com/sharkdp/fd) (finder)
- [nvim-treesitter/nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter) (finder/preview)
- [neovim LSP]( https://neovim.io/doc/user/lsp.html) (picker)
- [neovim LSP](https://neovim.io/doc/user/lsp.html) (picker)
- [devicons](https://github.com/nvim-tree/nvim-web-devicons) (icons)
### Installation
@@ -96,6 +96,7 @@ call dein#add('nvim-lua/plenary.nvim')
call dein#add('nvim-telescope/telescope.nvim', { 'rev': '0.1.5' })
" or , { 'rev': '0.1.x' })
```
Using [packer.nvim](https://github.com/wbthomason/packer.nvim)
```lua
@@ -135,7 +136,7 @@ to get an understanding of how to use Telescope and how to configure it.
## Usage
Try the command `:Telescope find_files<cr>`
to see if `telescope.nvim` is installed correctly.
to see if `telescope.nvim` is installed correctly.
Using VimL:
@@ -219,10 +220,9 @@ To look at what default configuration options exist please read: `:help
telescope.setup()`. For picker specific `opts` please read: `:help
telescope.builtin`.
To embed the above code snippet in a `.vim` file
(for example in `after/plugin/telescope.nvim.vim`),
wrap it in `lua << EOF code-snippet EOF`:
(for example in `after/plugin/telescope.nvim.vim`),
wrap it in `lua << EOF code-snippet EOF`:
```lua
lua << EOF
@@ -238,7 +238,7 @@ Mappings are fully customizable.
Many familiar mapping patterns are set up as defaults.
| Mappings | Action |
|----------------|------------------------------------------------------|
| -------------- | --------------------------------------------------------- |
| `<C-n>/<Down>` | Next item |
| `<C-p>/<Up>` | Previous item |
| `j/k` | Next/previous (in normal mode) |
@@ -262,7 +262,7 @@ Many familiar mapping patterns are set up as defaults.
| `<S-Tab>` | Toggle selection and move to prev selection |
| `<C-q>` | Send all items not filtered to quickfixlist (qflist) |
| `<M-q>` | Send all selected items to qflist |
| `<C-r><C-w>` | Insert cword in original window into prompt (insert mode) |
To see the full list of mappings, check out `lua/telescope/mappings.lua` and the
`default_mappings` table.
@@ -305,7 +305,7 @@ Built-in functions. Ready to be bound to any key you like.
### File Pickers
| Functions | Description |
|-------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `builtin.find_files` | Lists files in your current working directory, respects .gitignore |
| `builtin.git_files` | Fuzzy search through the output of `git ls-files` command, respects .gitignore |
| `builtin.grep_string` | Searches for the string under your cursor or selection in your current working directory |
@@ -314,7 +314,7 @@ Built-in functions. Ready to be bound to any key you like.
### Vim Pickers
| Functions | Description |
|-------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ----------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `builtin.buffers` | Lists open buffers in current neovim instance |
| `builtin.oldfiles` | Lists previously open files |
| `builtin.commands` | Lists available plugin/user commands and runs them on `<cr>` |
@@ -344,7 +344,7 @@ Built-in functions. Ready to be bound to any key you like.
### Neovim LSP Pickers
| Functions | Description |
|---------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|
| --------------------------------------- | -------------------------------------------------------------------------------------------------------------------------- |
| `builtin.lsp_references` | Lists LSP references for word under the cursor |
| `builtin.lsp_incoming_calls` | Lists LSP incoming calls for word under the cursor |
| `builtin.lsp_outgoing_calls` | Lists LSP outgoing calls for word under the cursor |
@@ -354,13 +354,12 @@ Built-in functions. Ready to be bound to any key you like.
| `builtin.diagnostics` | Lists Diagnostics for all open buffers or a specific buffer. Use option `bufnr=0` for current buffer. |
| `builtin.lsp_implementations` | Goto the implementation of the word under the cursor if there's only one, otherwise show all options in Telescope |
| `builtin.lsp_definitions` | Goto the definition of the word under the cursor, if there's only one, otherwise show all options in Telescope |
| `builtin.lsp_type_definitions` | Goto the definition of the type of the word under the cursor, if there's only one, otherwise show all options in Telescope|
| `builtin.lsp_type_definitions` | Goto the definition of the type of the word under the cursor, if there's only one, otherwise show all options in Telescope |
### Git Pickers
| Functions | Description |
|-------------------------------------|------------------------------------------------------------------------------------------------------------|
| ---------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `builtin.git_commits` | Lists git commits with diff preview, checkout action `<cr>`, reset mixed `<C-r>m`, reset soft `<C-r>s` and reset hard `<C-r>h` |
| `builtin.git_bcommits` | Lists buffer's git commits with diff preview and checks them out on `<cr>` |
| `builtin.git_bcommits_range` | Lists buffer's git commits in a range of lines. Use options `from` and `to` to specify the range. In visual mode, lists commits for the selected lines |
@@ -371,13 +370,13 @@ Built-in functions. Ready to be bound to any key you like.
### Treesitter Picker
| Functions | Description |
|-------------------------------------|---------------------------------------------------|
| -------------------- | ------------------------------------------------- |
| `builtin.treesitter` | Lists Function names, variables, from Treesitter! |
### Lists Picker
| Functions | Description |
|-------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `builtin.planets` | Use the telescope... |
| `builtin.builtin` | Lists Built-in pickers and run them on `<cr>`. |
| `builtin.reloader` | Lists Lua modules and reload them on `<cr>`. |
@@ -386,9 +385,9 @@ Built-in functions. Ready to be bound to any key you like.
## Previewers
| Previewers | Description |
|------------------------------------|-----------------------------------------------------------|
| ----------------------------------- | --------------------------------------------------------- |
| `previewers.vim_buffer_cat.new` | Default previewer for files. Uses vim buffers |
| `previewers.vim_buffer_vimgrep.new`| Default previewer for grep and similar. Uses vim buffers |
| `previewers.vim_buffer_vimgrep.new` | Default previewer for grep and similar. Uses vim buffers |
| `previewers.vim_buffer_qflist.new` | Default previewer for qflist. Uses vim buffers |
| `previewers.cat.new` | Terminal previewer for files. Uses `cat`/`bat` |
| `previewers.vimgrep.new` | Terminal previewer for grep and similar. Uses `cat`/`bat` |
@@ -435,7 +434,7 @@ A data field is passed to the callback, which contains the filetype and the buff
## Sorters
| Sorters | Description |
|------------------------------------|-----------------------------------------------------------------|
| ---------------------------------- | --------------------------------------------------------------- |
| `sorters.get_fuzzy_file` | Telescope's default sorter for files |
| `sorters.get_generic_fuzzy_sorter` | Telescope's default sorter for everything else |
| `sorters.get_levenshtein_sorter` | Using Levenshtein distance algorithm (don't use :D) |
@@ -493,7 +492,7 @@ We have some built in themes but are looking for more cool options.
![dropdown](https://i.imgur.com/SorAcXv.png)
| Themes | Description |
|--------------------------|---------------------------------------------------------------------------------------------|
| --------------------- | ------------------------------------------------------------------------------------------- |
| `themes.get_dropdown` | A list like centered list. [dropdown](https://i.imgur.com/SorAcXv.png) |
| `themes.get_cursor` | [A cursor relative list.](https://github.com/nvim-telescope/telescope.nvim/pull/878) |
| `themes.get_ivy` | Bottom panel overlay. [Ivy #771](https://github.com/nvim-telescope/telescope.nvim/pull/771) |
@@ -564,7 +563,7 @@ for more information and how to realize more complex commands please read
Telescope user autocmds:
| Event | Description |
|---------------------------------|---------------------------------------------------------|
| ------------------------------- | ------------------------------------------------------- |
| `User TelescopeFindPre` | Do it before Telescope creates all the floating windows |
| `User TelescopePreviewerLoaded` | Do it after Telescope previewer window is created |
| `User TelescopeResumePost` | Do it after Telescope resume action is fully completed |

View File

@@ -3307,6 +3307,14 @@ actions.delete_mark({prompt_bufnr}) *telescope.actions.delete_mark()*
{prompt_bufnr} (number) The prompt bufnr
actions.insert_original_cword({prompt_bufnr}) *telescope.actions.insert_original_cword()*
Insert the word under the cursor of the original (pre-Telescope) window
Parameters: ~
{prompt_bufnr} (number) The prompt bufnr
================================================================================
ACTIONS_STATE *telescope.actions.state*

View File

@@ -1484,6 +1484,13 @@ actions.delete_mark = function(prompt_bufnr)
end)
end
--- Insert the word under the cursor of the original (pre-Telescope) window
---@param prompt_bufnr number: The prompt bufnr
actions.insert_original_cword = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker:set_prompt(current_picker.original_cword, false)
end
actions.nop = function(_) end
-- ==================================================

View File

@@ -164,6 +164,7 @@ mappings.default_mappings = config.values.default_mappings
["<C-/>"] = actions.which_key,
["<C-_>"] = actions.which_key, -- keys from pressing <C-/>
["<C-w>"] = { "<c-s-w>", type = "command" },
["<C-r><C-w>"] = actions.insert_original_cword,
-- disable c-j because we dont want to allow new lines #2123
["<C-j>"] = actions.nop,

View File

@@ -529,6 +529,7 @@ function Picker:find()
self:reset_selection()
self.original_win_id = a.nvim_get_current_win()
self.original_cword = vim.fn.expand "<cword>"
-- User autocmd run it before create Telescope window
vim.api.nvim_exec_autocmds("User", { pattern = "TelescopeFindPre" })
@@ -845,7 +846,13 @@ function Picker:delete_selection(delete_cb)
end, 50)
end
function Picker:set_prompt(text)
---@param text string text to set as prompt
---@param reset boolean? whether to replace prompt with text entirely or just append
function Picker:set_prompt(text, reset)
reset = vim.F.if_nil(reset, true)
if not reset then
text = self:_get_prompt() .. text
end
self:reset_prompt(text)
end