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

feat(picker): allow appending original window <cWORD>, <cfile> and cursor line to prompt #3134

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ Many familiar mapping patterns are set up as defaults.
| `<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) |
| `<C-r><C-a>` | Insert cWORD in original window into prompt (insert mode) |
| `<C-r><C-f>` | Insert cfile in original window into prompt (insert mode) |
| `<C-r><C-l>` | Insert cline 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.
Expand Down
24 changes: 24 additions & 0 deletions doc/telescope.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3393,6 +3393,30 @@ actions.insert_original_cword({prompt_bufnr}) *telescope.actions.insert_original
{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.insert_original_cfile({prompt_bufnr}) *telescope.actions.insert_original_cfile()*
Insert the file under the cursor of the original (pre-Telescope) window


Parameters: ~
{prompt_bufnr} (number) The prompt bufnr


actions.insert_original_cline({prompt_bufnr}) *telescope.actions.insert_original_cline()*
Insert the line under the cursor of the original (pre-Telescope) window


Parameters: ~
{prompt_bufnr} (number) The prompt bufnr



================================================================================
ACTIONS_STATE *telescope.actions.state*
Expand Down
21 changes: 21 additions & 0 deletions lua/telescope/actions/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1496,6 +1496,27 @@ actions.insert_original_cword = function(prompt_bufnr)
current_picker:set_prompt(current_picker.original_cword, false)
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

--- Insert the file under the cursor of the original (pre-Telescope) window
---@param prompt_bufnr number: The prompt bufnr
actions.insert_original_cfile = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker:set_prompt(current_picker.original_cfile, false)
end

--- Insert the line under the cursor of the original (pre-Telescope) window
---@param prompt_bufnr number: The prompt bufnr
actions.insert_original_cline = function(prompt_bufnr)
local current_picker = action_state.get_current_picker(prompt_bufnr)
current_picker:set_prompt(current_picker.original_cline, false)
end

actions.nop = function(_) end

actions.mouse_click = function(prompt_bufnr)
Expand Down
3 changes: 3 additions & 0 deletions lua/telescope/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ mappings.default_mappings = config.values.default_mappings
["<C-_>"] = actions.which_key, -- keys from pressing <C-/>
["<C-w>"] = { "<c-s-w>", type = "command" },
["<C-r><C-w>"] = actions.insert_original_cword,
["<C-r><C-a>"] = actions.insert_original_cWORD,
["<C-r><C-f>"] = actions.insert_original_cfile,
["<C-r><C-l>"] = actions.insert_original_cline,

-- disable c-j because we dont want to allow new lines #2123
["<C-j>"] = actions.nop,
Expand Down
4 changes: 4 additions & 0 deletions lua/telescope/pickers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,10 @@ function Picker:find()

self.original_win_id = a.nvim_get_current_win()
_, self.original_cword = pcall(vim.fn.expand, "<cword>")
_, self.original_cWORD = pcall(vim.fn.expand, "<cWORD>")
_, self.original_cfile = pcall(vim.fn.expand, "<cfile>")
_, self.original_cline = pcall(vim.api.nvim_get_current_line)
_, self.original_cline = pcall(vim.trim, self.original_cline)

-- User autocmd run it before create Telescope window
vim.api.nvim_exec_autocmds("User", { pattern = "TelescopeFindPre" })
Expand Down