Update attach_mappings to use new actions api (#249)
This commit is contained in:
61
README.md
61
README.md
@@ -201,11 +201,14 @@ end
|
|||||||
["<C-i>"] = test_action,
|
["<C-i>"] = test_action,
|
||||||
|
|
||||||
-- If you want your function to run after another action you should define it as follows
|
-- If you want your function to run after another action you should define it as follows
|
||||||
local test_action = actions._transform_action(function(prompt_bufnr)
|
local transform_mod = require('telescope.actions.mt').transform_mod
|
||||||
print("This function ran after another action. Prompt_bufnr: " .. prompt_bufnr)
|
local test_action = transform_mod {
|
||||||
-- Enter your function logic here. You can take inspiration from lua/telescope/actions.lua
|
x = function()
|
||||||
end)
|
print("This function ran after another action. Prompt_bufnr: " .. prompt_bufnr)
|
||||||
["<C-i>"] = actions.goto_file_selection_split + test_action
|
-- Enter your function logic here. You can take inspiration from lua/telescope/actions.lua
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
["<C-i>"] = actions.goto_file_selection_split + test_action.x
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -215,10 +218,13 @@ A full example:
|
|||||||
local actions = require('telescope.actions')
|
local actions = require('telescope.actions')
|
||||||
|
|
||||||
-- If you want your function to run after another action you should define it as follows
|
-- If you want your function to run after another action you should define it as follows
|
||||||
local test_action = actions._transform_action(function(prompt_bufnr)
|
local transform_mod = require('telescope.actions.mt').transform_mod
|
||||||
print("This function ran after another action. Prompt_bufnr: " .. prompt_bufnr)
|
local test_action = transform_mod {
|
||||||
-- Enter your function logic here. You can take inspiration from lua/telescope/actions.lua
|
x = function()
|
||||||
end)
|
print("This function ran after another action. Prompt_bufnr: " .. prompt_bufnr)
|
||||||
|
-- Enter your function logic here. You can take inspiration from lua/telescope/actions.lua
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
require('telescope').setup {
|
require('telescope').setup {
|
||||||
defaults = {
|
defaults = {
|
||||||
@@ -259,6 +265,43 @@ function my_custom_picker(results)
|
|||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To override a action, you have to use `attach_mappings` like this (prefered method):
|
||||||
|
|
||||||
|
```lua
|
||||||
|
function my_custom_picker(results)
|
||||||
|
pickers.new(opts, {
|
||||||
|
prompt_title = 'Custom Picker',
|
||||||
|
finder = finders.new_table(results),
|
||||||
|
sorter = sorters.fuzzy_with_index_bias(),
|
||||||
|
attach_mappings = function(prompt_bufnr)
|
||||||
|
-- This will replace goto_file_selection_edit no mather on which key it is mapped by default
|
||||||
|
actions.goto_file_selection_edit:replace(function()
|
||||||
|
-- Code here
|
||||||
|
end)
|
||||||
|
|
||||||
|
-- You can also enhance an action with post and post action which will run before of after an action
|
||||||
|
actions.goto_file_selection_split:enhance {
|
||||||
|
pre = function()
|
||||||
|
-- Will run before actions.goto_file_selection_split
|
||||||
|
end,
|
||||||
|
post = function()
|
||||||
|
-- Will run after actions.goto_file_selection_split
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
|
||||||
|
-- Or replace for all commands: edit, new, vnew and tab
|
||||||
|
actions._goto_file_selection:replace(function(_, cmd)
|
||||||
|
print(cmd) -- Will print edit, new, vnew or tab depending on your keystroke
|
||||||
|
end)
|
||||||
|
|
||||||
|
return true
|
||||||
|
end,
|
||||||
|
}):find()
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
See `lua/telescope/builtin.lua` for examples on how to `attach_mappings` in the prefered way.
|
||||||
|
|
||||||
Additionally, the prompt's filetype will be `TelescopePrompt`. You can customize the filetype as you would normally.
|
Additionally, the prompt's filetype will be `TelescopePrompt`. You can customize the filetype as you would normally.
|
||||||
|
|
||||||
## Status (Unstable API)
|
## Status (Unstable API)
|
||||||
|
|||||||
@@ -71,24 +71,20 @@ builtin.commands = function()
|
|||||||
end
|
end
|
||||||
},
|
},
|
||||||
sorter = conf.generic_sorter(),
|
sorter = conf.generic_sorter(),
|
||||||
attach_mappings = function(prompt_bufnr, map)
|
attach_mappings = function(prompt_bufnr)
|
||||||
local run_command = function()
|
actions.goto_file_selection_edit:replace(function()
|
||||||
local selection = actions.get_selected_entry(prompt_bufnr)
|
local selection = actions.get_selected_entry()
|
||||||
actions.close(prompt_bufnr)
|
actions.close(prompt_bufnr)
|
||||||
local val = selection.value
|
local val = selection.value
|
||||||
local cmd = string.format([[:%s ]], val.name)
|
local cmd = string.format([[:%s ]], val.name)
|
||||||
|
|
||||||
if val.nargs == "0" then
|
if val.nargs == "0" then
|
||||||
vim.cmd(cmd)
|
vim.cmd(cmd)
|
||||||
else
|
else
|
||||||
vim.cmd [[stopinsert]]
|
vim.cmd [[stopinsert]]
|
||||||
vim.fn.feedkeys(cmd)
|
vim.fn.feedkeys(cmd)
|
||||||
end
|
end
|
||||||
|
end)
|
||||||
end
|
|
||||||
|
|
||||||
map('i', '<CR>', run_command)
|
|
||||||
map('n', '<CR>', run_command)
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@@ -225,9 +221,9 @@ builtin.lsp_code_actions = function(opts)
|
|||||||
}
|
}
|
||||||
end
|
end
|
||||||
},
|
},
|
||||||
attach_mappings = function(prompt_bufnr, map)
|
attach_mappings = function(prompt_bufnr)
|
||||||
local execute = function()
|
actions.goto_file_selection_edit:replace(function()
|
||||||
local selection = actions.get_selected_entry(prompt_bufnr)
|
local selection = actions.get_selected_entry()
|
||||||
actions.close(prompt_bufnr)
|
actions.close(prompt_bufnr)
|
||||||
local val = selection.value
|
local val = selection.value
|
||||||
|
|
||||||
@@ -241,10 +237,7 @@ builtin.lsp_code_actions = function(opts)
|
|||||||
else
|
else
|
||||||
vim.lsp.buf.execute_command(val)
|
vim.lsp.buf.execute_command(val)
|
||||||
end
|
end
|
||||||
end
|
end)
|
||||||
|
|
||||||
map('i', '<CR>', execute)
|
|
||||||
map('n', '<CR>', execute)
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end,
|
end,
|
||||||
@@ -398,63 +391,59 @@ builtin.vim_options = function(opts)
|
|||||||
local vim_opts = loadfile(utils.data_directory() .. path.separator .. 'options' .. path.separator .. 'options.lua')().options
|
local vim_opts = loadfile(utils.data_directory() .. path.separator .. 'options' .. path.separator .. 'options.lua')().options
|
||||||
|
|
||||||
pickers.new(opts, {
|
pickers.new(opts, {
|
||||||
prompt = 'options',
|
prompt = 'options',
|
||||||
finder = finders.new_table {
|
finder = finders.new_table {
|
||||||
results = vim_opts,
|
results = vim_opts,
|
||||||
entry_maker = make_entry.gen_from_vimoptions(opts),
|
entry_maker = make_entry.gen_from_vimoptions(opts),
|
||||||
},
|
},
|
||||||
-- TODO: previewer for Vim options
|
-- TODO: previewer for Vim options
|
||||||
-- previewer = previewers.help.new(opts),
|
-- previewer = previewers.help.new(opts),
|
||||||
sorter = sorters.get_fzy_sorter(),
|
sorter = sorters.get_fzy_sorter(),
|
||||||
attach_mappings = function(prompt_bufnr, map)
|
attach_mappings = function(prompt_bufnr)
|
||||||
local edit_option = function()
|
actions.goto_file_selection_edit:replace(function()
|
||||||
local selection = actions.get_selected_entry(prompt_bufnr)
|
local selection = actions.get_selected_entry()
|
||||||
local esc = ""
|
local esc = ""
|
||||||
|
|
||||||
|
if vim.fn.mode() == "i" then
|
||||||
if vim.fn.mode() == "i" then
|
-- TODO: don't make this local
|
||||||
-- TODO: don't make this local
|
esc = vim.api.nvim_replace_termcodes("<esc>", true, false, true)
|
||||||
esc = vim.api.nvim_replace_termcodes("<esc>", true, false, true)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- TODO: Make this actually work.
|
|
||||||
|
|
||||||
-- actions.close(prompt_bufnr)
|
|
||||||
-- vim.api.nvim_win_set_var(vim.fn.nvim_get_current_win(), "telescope", 1)
|
|
||||||
-- print(prompt_bufnr)
|
|
||||||
-- print(vim.fn.bufnr())
|
|
||||||
-- vim.cmd([[ autocmd BufEnter <buffer> ++nested ++once startinsert!]])
|
|
||||||
-- print(vim.fn.winheight(0))
|
|
||||||
|
|
||||||
-- local prompt_winnr = vim.fn.getbufinfo(prompt_bufnr)[1].windows[1]
|
|
||||||
-- print(prompt_winnr)
|
|
||||||
|
|
||||||
-- local float_opts = {}
|
|
||||||
-- float_opts.relative = "editor"
|
|
||||||
-- float_opts.anchor = "sw"
|
|
||||||
-- float_opts.focusable = false
|
|
||||||
-- float_opts.style = "minimal"
|
|
||||||
-- float_opts.row = vim.api.nvim_get_option("lines") - 2 -- TODO: include `cmdheight` and `laststatus` in this calculation
|
|
||||||
-- float_opts.col = 2
|
|
||||||
-- float_opts.height = 10
|
|
||||||
-- float_opts.width = string.len(selection.last_set_from)+15
|
|
||||||
-- local buf = vim.fn.nvim_create_buf(false, true)
|
|
||||||
-- vim.fn.nvim_buf_set_lines(buf, 0, 0, false, {"default value: abcdef", "last set from: " .. selection.last_set_from})
|
|
||||||
-- local status_win = vim.fn.nvim_open_win(buf, false, float_opts)
|
|
||||||
-- -- vim.api.nvim_win_set_option(status_win, "winblend", 100)
|
|
||||||
-- vim.api.nvim_win_set_option(status_win, "winhl", "Normal:PmenuSel")
|
|
||||||
-- -- vim.api.nvim_set_current_win(status_win)
|
|
||||||
-- vim.cmd[[redraw!]]
|
|
||||||
-- vim.cmd("autocmd CmdLineLeave : ++once echom 'beep'")
|
|
||||||
vim.api.nvim_feedkeys(string.format("%s:set %s=%s", esc, selection.name, selection.current_value), "m", true)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
map('i', '<CR>', edit_option)
|
-- TODO: Make this actually work.
|
||||||
map('n', '<CR>', edit_option)
|
|
||||||
|
|
||||||
return true
|
-- actions.close(prompt_bufnr)
|
||||||
end
|
-- vim.api.nvim_win_set_var(vim.fn.nvim_get_current_win(), "telescope", 1)
|
||||||
}):find()
|
-- print(prompt_bufnr)
|
||||||
|
-- print(vim.fn.bufnr())
|
||||||
|
-- vim.cmd([[ autocmd BufEnter <buffer> ++nested ++once startinsert!]])
|
||||||
|
-- print(vim.fn.winheight(0))
|
||||||
|
|
||||||
|
-- local prompt_winnr = vim.fn.getbufinfo(prompt_bufnr)[1].windows[1]
|
||||||
|
-- print(prompt_winnr)
|
||||||
|
|
||||||
|
-- local float_opts = {}
|
||||||
|
-- float_opts.relative = "editor"
|
||||||
|
-- float_opts.anchor = "sw"
|
||||||
|
-- float_opts.focusable = false
|
||||||
|
-- float_opts.style = "minimal"
|
||||||
|
-- float_opts.row = vim.api.nvim_get_option("lines") - 2 -- TODO: include `cmdheight` and `laststatus` in this calculation
|
||||||
|
-- float_opts.col = 2
|
||||||
|
-- float_opts.height = 10
|
||||||
|
-- float_opts.width = string.len(selection.last_set_from)+15
|
||||||
|
-- local buf = vim.fn.nvim_create_buf(false, true)
|
||||||
|
-- vim.fn.nvim_buf_set_lines(buf, 0, 0, false, {"default value: abcdef", "last set from: " .. selection.last_set_from})
|
||||||
|
-- local status_win = vim.fn.nvim_open_win(buf, false, float_opts)
|
||||||
|
-- -- vim.api.nvim_win_set_option(status_win, "winblend", 100)
|
||||||
|
-- vim.api.nvim_win_set_option(status_win, "winhl", "Normal:PmenuSel")
|
||||||
|
-- -- vim.api.nvim_set_current_win(status_win)
|
||||||
|
-- vim.cmd[[redraw!]]
|
||||||
|
-- vim.cmd("autocmd CmdLineLeave : ++once echom 'beep'")
|
||||||
|
vim.api.nvim_feedkeys(string.format("%s:set %s=%s", esc, selection.name, selection.current_value), "m", true)
|
||||||
|
end)
|
||||||
|
|
||||||
|
return true
|
||||||
|
end
|
||||||
|
}):find()
|
||||||
end
|
end
|
||||||
|
|
||||||
builtin.help_tags = function(opts)
|
builtin.help_tags = function(opts)
|
||||||
@@ -478,37 +467,18 @@ builtin.help_tags = function(opts)
|
|||||||
-- TODO: previewer for Vim help
|
-- TODO: previewer for Vim help
|
||||||
previewer = previewers.help.new(opts),
|
previewer = previewers.help.new(opts),
|
||||||
sorter = conf.generic_sorter(opts),
|
sorter = conf.generic_sorter(opts),
|
||||||
attach_mappings = function(prompt_bufnr, map)
|
attach_mappings = function(prompt_bufnr)
|
||||||
local open = function(cmd)
|
actions._goto_file_selection:replace(function(_, cmd)
|
||||||
local selection = actions.get_selected_entry(prompt_bufnr)
|
local selection = actions.get_selected_entry()
|
||||||
|
|
||||||
actions.close(prompt_bufnr)
|
actions.close(prompt_bufnr)
|
||||||
vim.cmd(cmd .. selection.value)
|
if cmd == 'edit' or cmd == 'new' then
|
||||||
end
|
vim.cmd('help ' .. selection.value)
|
||||||
local nhelp = function()
|
elseif cmd == 'vnew' then
|
||||||
return open("help ")
|
vim.cmd('vert bo help ' .. selection.value)
|
||||||
end
|
elseif cmd == 'tabedit' then
|
||||||
local vhelp = function()
|
vim.cmd('tab help ' .. selection.value)
|
||||||
return open("vert bo help ")
|
end
|
||||||
end
|
end)
|
||||||
local hhelp = function()
|
|
||||||
return open("help ")
|
|
||||||
-- Not sure how explictly make horizontal
|
|
||||||
end
|
|
||||||
local thelp = function()
|
|
||||||
return open("tab help ")
|
|
||||||
end
|
|
||||||
-- Perhaps it would be a good idea to have vsplit,tab,hsplit open
|
|
||||||
-- a builtin action that accepts a command to be ran before creating
|
|
||||||
-- the split or tab
|
|
||||||
map('i', '<CR>', nhelp)
|
|
||||||
map('n', '<CR>', nhelp)
|
|
||||||
map('i', '<C-v>', vhelp)
|
|
||||||
map('n', '<C-v>', vhelp)
|
|
||||||
map('i', '<C-x>', hhelp)
|
|
||||||
map('n', '<C-x>', hhelp)
|
|
||||||
map('i', '<C-t>', thelp)
|
|
||||||
map('n', '<C-t>', thelp)
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@@ -538,17 +508,14 @@ builtin.reloader = function(opts)
|
|||||||
-- previewer = previewers.vim_buffer.new(opts),
|
-- previewer = previewers.vim_buffer.new(opts),
|
||||||
sorter = conf.generic_sorter(opts),
|
sorter = conf.generic_sorter(opts),
|
||||||
|
|
||||||
attach_mappings = function(prompt_bufnr, map)
|
attach_mappings = function(prompt_bufnr)
|
||||||
local reload_package = function()
|
actions.goto_file_selection_edit:replace(function()
|
||||||
local selection = actions.get_selected_entry(prompt_bufnr)
|
local selection = actions.get_selected_entry()
|
||||||
|
|
||||||
actions.close(prompt_bufnr)
|
actions.close(prompt_bufnr)
|
||||||
require('plenary.reload').reload_module(selection.value)
|
require('plenary.reload').reload_module(selection.value)
|
||||||
print(string.format("[%s] - module reloaded", selection.value))
|
print(string.format("[%s] - module reloaded", selection.value))
|
||||||
end
|
end)
|
||||||
|
|
||||||
map('i', '<CR>', reload_package)
|
|
||||||
map('n', '<CR>', reload_package)
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@@ -587,8 +554,8 @@ builtin.builtin = function(opts)
|
|||||||
},
|
},
|
||||||
previewer = previewers.qflist.new(opts),
|
previewer = previewers.qflist.new(opts),
|
||||||
sorter = conf.generic_sorter(opts),
|
sorter = conf.generic_sorter(opts),
|
||||||
attach_mappings = function(_, map)
|
attach_mappings = function(_)
|
||||||
map('i', '<CR>', actions.run_builtin)
|
actions.goto_file_selection_edit:replace(actions.run_builtin)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
}):find()
|
}):find()
|
||||||
@@ -758,9 +725,9 @@ builtin.planets = function(opts)
|
|||||||
},
|
},
|
||||||
previewer = previewers.cat.new(opts),
|
previewer = previewers.cat.new(opts),
|
||||||
sorter = conf.generic_sorter(opts),
|
sorter = conf.generic_sorter(opts),
|
||||||
attach_mappings = function(prompt_bufnr, map)
|
attach_mappings = function(prompt_bufnr)
|
||||||
map('i', '<CR>', function()
|
actions.goto_file_selection_edit:replace(function()
|
||||||
local selection = actions.get_selected_entry(prompt_bufnr)
|
local selection = actions.get_selected_entry()
|
||||||
actions.close(prompt_bufnr)
|
actions.close(prompt_bufnr)
|
||||||
|
|
||||||
print("Enjoy astronomy! You viewed:", selection.display)
|
print("Enjoy astronomy! You viewed:", selection.display)
|
||||||
@@ -795,12 +762,12 @@ builtin.current_buffer_fuzzy_find = function(opts)
|
|||||||
end
|
end
|
||||||
},
|
},
|
||||||
sorter = sorters.get_generic_fuzzy_sorter(),
|
sorter = sorters.get_generic_fuzzy_sorter(),
|
||||||
attach_mappings = function(prompt_bufnr)
|
attach_mappings = function()
|
||||||
actions._goto_file_selection:enhance {
|
actions._goto_file_selection:enhance {
|
||||||
post = vim.schedule_wrap(function()
|
post = function()
|
||||||
local selection = actions.get_selected_entry(prompt_bufnr)
|
local selection = actions.get_selected_entry()
|
||||||
vim.api.nvim_win_set_cursor(0, {selection.lnum, 0})
|
vim.api.nvim_win_set_cursor(0, {selection.lnum, 0})
|
||||||
end),
|
end,
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
@@ -828,17 +795,19 @@ builtin.man_pages = function(opts)
|
|||||||
},
|
},
|
||||||
previewer = previewers.man.new(opts),
|
previewer = previewers.man.new(opts),
|
||||||
sorter = sorters.get_generic_fuzzy_sorter(),
|
sorter = sorters.get_generic_fuzzy_sorter(),
|
||||||
attach_mappings = function(prompt_bufnr, map)
|
attach_mappings = function(prompt_bufnr)
|
||||||
local view_manpage = function()
|
actions._goto_file_selection:replace(function(_, cmd)
|
||||||
local selection = actions.get_selected_entry(prompt_bufnr)
|
local selection = actions.get_selected_entry()
|
||||||
|
|
||||||
actions.close(prompt_bufnr)
|
actions.close(prompt_bufnr)
|
||||||
print(vim.inspect(selection.value))
|
if cmd == 'edit' or cmd == 'new' then
|
||||||
vim.cmd("Man " .. selection.value)
|
vim.cmd('Man ' .. selection.value)
|
||||||
end
|
elseif cmd == 'vnew' then
|
||||||
|
vim.cmd('vert bo Man ' .. selection.value)
|
||||||
map('i', '<CR>', view_manpage)
|
elseif cmd == 'tabedit' then
|
||||||
map('n', '<CR>', view_manpage)
|
vim.cmd('tab Man ' .. selection.value)
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
@@ -857,17 +826,13 @@ builtin.colorscheme = function(opts)
|
|||||||
},
|
},
|
||||||
-- TODO: better preview?
|
-- TODO: better preview?
|
||||||
sorter = sorters.get_generic_fuzzy_sorter(),
|
sorter = sorters.get_generic_fuzzy_sorter(),
|
||||||
attach_mappings = function(prompt_bufnr, map)
|
attach_mappings = function(prompt_bufnr)
|
||||||
local change_colorscheme = function()
|
actions.goto_file_selection_edit:replace(function()
|
||||||
local selection = actions.get_selected_entry(prompt_bufnr)
|
local selection = actions.get_selected_entry()
|
||||||
|
|
||||||
actions.close(prompt_bufnr)
|
actions.close(prompt_bufnr)
|
||||||
print(vim.inspect(selection.value))
|
|
||||||
vim.cmd("colorscheme " .. selection.value)
|
vim.cmd("colorscheme " .. selection.value)
|
||||||
end
|
end)
|
||||||
|
|
||||||
map('i', '<CR>', change_colorscheme)
|
|
||||||
map('n', '<CR>', change_colorscheme)
|
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user