feat: Actions can now be summed up and center action (#204)

Closes: #182 

* Actions can now be summed up and center action

* fix: Make some complicated changes for metatable

* Update documentation

Co-authored-by: TJ DeVries <devries.timothyj@gmail.com>
This commit is contained in:
Simon Hauser
2020-11-02 23:00:23 +01:00
committed by GitHub
parent 54ef9d90d5
commit aba433c522
3 changed files with 76 additions and 20 deletions

View File

@@ -184,23 +184,28 @@ To see the full list of mappings, check out `lua/telescope/mappings.lua` and the
To override ALL of the default mappings, you can use the `default_mappings` key in the `setup` table. To override ALL of the default mappings, you can use the `default_mappings` key in the `setup` table.
``` ```lua
To disable a keymap, put [map] = false -- To disable a keymap, put [map] = false
-- So, to not map "<C-n>", just put
So, to not map "<C-n>", just put
...,
["<C-n>"] = false, ["<C-n>"] = false,
..., -- Into your config.
Into your config. -- Otherwise, just set the mapping to the function that you want it to be.
["<C-i>"] = actions.goto_file_selection_split,
Otherwise, just set the mapping to the function that you want it to be. -- You can also define your own functions, which then can be mapped to a key
local function test_action(prompt_bufnr)
..., print("Action was attached with prompt_bufnr: ", prompt_bufnr)
["<C-i>"] = actions.goto_file_selection_split -- Enter your function logic here. You can take inspiration from lua/telescope/actions.lua
..., 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
``` ```
@@ -209,6 +214,12 @@ A full example:
```lua ```lua
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
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)
require('telescope').setup { require('telescope').setup {
defaults = { defaults = {
mappings = { mappings = {
@@ -218,6 +229,12 @@ require('telescope').setup {
-- Create a new <c-s> mapping -- Create a new <c-s> mapping
["<c-s>"] = actions.goto_file_selection_split, ["<c-s>"] = actions.goto_file_selection_split,
-- Add up multiple actions
["<CR>"] = actions.goto_file_selection_edit + actions.center,
-- You can perform as many actions in a row as you like
["<CR>"] = actions.goto_file_selection_edit + actions.center + test_action,
}, },
}, },
} }

View File

@@ -12,6 +12,37 @@ local actions = setmetatable({}, {
end end
}) })
local action_mt = {
__call = function(t, ...)
local values = {}
for _, v in ipairs(t) do
local result = {v(...)}
for _, res in ipairs(result) do
table.insert(values, res)
end
end
return unpack(values)
end,
__add = function(lhs, rhs)
local new_actions = {}
for _, v in ipairs(lhs) do
table.insert(new_actions, v)
end
for _, v in ipairs(rhs) do
table.insert(new_actions, v)
end
return setmetatable(new_actions, getmetatable(lhs))
end
}
local transform_action = function(a)
return setmetatable({a}, action_mt)
end
--- Get the current picker object for the prompt --- Get the current picker object for the prompt
function actions.get_current_picker(prompt_bufnr) function actions.get_current_picker(prompt_bufnr)
return state.get_status(prompt_bufnr).picker return state.get_status(prompt_bufnr).picker
@@ -51,7 +82,6 @@ end
-- TODO: It seems sometimes we get bad styling. -- TODO: It seems sometimes we get bad styling.
local function goto_file_selection(prompt_bufnr, command) local function goto_file_selection(prompt_bufnr, command)
local picker = actions.get_current_picker(prompt_bufnr)
local entry = actions.get_selected_entry(prompt_bufnr) local entry = actions.get_selected_entry(prompt_bufnr)
if not entry then if not entry then
@@ -90,7 +120,6 @@ local function goto_file_selection(prompt_bufnr, command)
a.nvim_win_set_config(preview_win, {style = ''}) a.nvim_win_set_config(preview_win, {style = ''})
end end
local original_win_id = picker.original_win_id or 0
local entry_bufnr = entry.bufnr local entry_bufnr = entry.bufnr
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
@@ -117,6 +146,10 @@ local function goto_file_selection(prompt_bufnr, command)
end end
end end
function actions.center(_)
vim.cmd(':normal! zz')
end
function actions.goto_file_selection_edit(prompt_bufnr) function actions.goto_file_selection_edit(prompt_bufnr)
goto_file_selection(prompt_bufnr, "edit") goto_file_selection(prompt_bufnr, "edit")
end end
@@ -185,4 +218,10 @@ actions.insert_value = function(prompt_bufnr)
return entry.value return entry.value
end end
for k, v in pairs(actions) do
actions[k] = transform_action(v)
end
actions._transform_action = transform_action
return actions return actions

View File

@@ -16,7 +16,7 @@ mappings.default_mappings = config.values.default_mappings or {
["<Down>"] = actions.move_selection_next, ["<Down>"] = actions.move_selection_next,
["<Up>"] = actions.move_selection_previous, ["<Up>"] = actions.move_selection_previous,
["<CR>"] = actions.goto_file_selection_edit, ["<CR>"] = actions.goto_file_selection_edit + actions.center,
["<C-x>"] = actions.goto_file_selection_split, ["<C-x>"] = actions.goto_file_selection_split,
["<C-v>"] = actions.goto_file_selection_vsplit, ["<C-v>"] = actions.goto_file_selection_vsplit,
["<C-t>"] = actions.goto_file_selection_tabedit, ["<C-t>"] = actions.goto_file_selection_tabedit,
@@ -30,7 +30,7 @@ mappings.default_mappings = config.values.default_mappings or {
n = { n = {
["<esc>"] = actions.close, ["<esc>"] = actions.close,
["<CR>"] = actions.goto_file_selection_edit, ["<CR>"] = actions.goto_file_selection_edit + actions.center,
["<C-x>"] = actions.goto_file_selection_split, ["<C-x>"] = actions.goto_file_selection_split,
["<C-v>"] = actions.goto_file_selection_vsplit, ["<C-v>"] = actions.goto_file_selection_vsplit,
["<C-t>"] = actions.goto_file_selection_tabedit, ["<C-t>"] = actions.goto_file_selection_tabedit,