Update attach_mappings to use new actions api (#249)

This commit is contained in:
Simon Hauser
2020-11-16 21:48:45 +01:00
committed by GitHub
parent 57a7769845
commit 749c86557e
2 changed files with 151 additions and 143 deletions

View File

@@ -201,11 +201,14 @@ end
["<C-i>"] = test_action,
-- 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)
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)
["<C-i>"] = actions.goto_file_selection_split + test_action
local transform_mod = require('telescope.actions.mt').transform_mod
local test_action = transform_mod {
x = function()
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,
}
["<C-i>"] = actions.goto_file_selection_split + test_action.x
```
@@ -215,10 +218,13 @@ A full example:
local actions = require('telescope.actions')
-- 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)
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)
local transform_mod = require('telescope.actions.mt').transform_mod
local test_action = transform_mod {
x = function()
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 {
defaults = {
@@ -259,6 +265,43 @@ function my_custom_picker(results)
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.
## Status (Unstable API)