From aba433c52229268b563ac14f4d07781275d03923 Mon Sep 17 00:00:00 2001 From: Simon Hauser Date: Mon, 2 Nov 2020 23:00:23 +0100 Subject: 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 --- lua/telescope/actions.lua | 43 +++++++++++++++++++++++++++++++++++++++++-- lua/telescope/mappings.lua | 4 ++-- 2 files changed, 43 insertions(+), 4 deletions(-) (limited to 'lua') diff --git a/lua/telescope/actions.lua b/lua/telescope/actions.lua index 878ac0e..a35ffee 100644 --- a/lua/telescope/actions.lua +++ b/lua/telescope/actions.lua @@ -12,6 +12,37 @@ local actions = setmetatable({}, { 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 function actions.get_current_picker(prompt_bufnr) return state.get_status(prompt_bufnr).picker @@ -51,7 +82,6 @@ end -- TODO: It seems sometimes we get bad styling. local function goto_file_selection(prompt_bufnr, command) - local picker = actions.get_current_picker(prompt_bufnr) local entry = actions.get_selected_entry(prompt_bufnr) if not entry then @@ -90,7 +120,6 @@ local function goto_file_selection(prompt_bufnr, command) a.nvim_win_set_config(preview_win, {style = ''}) end - local original_win_id = picker.original_win_id or 0 local entry_bufnr = entry.bufnr actions.close(prompt_bufnr) @@ -117,6 +146,10 @@ local function goto_file_selection(prompt_bufnr, command) end end +function actions.center(_) + vim.cmd(':normal! zz') +end + function actions.goto_file_selection_edit(prompt_bufnr) goto_file_selection(prompt_bufnr, "edit") end @@ -185,4 +218,10 @@ actions.insert_value = function(prompt_bufnr) return entry.value end +for k, v in pairs(actions) do + actions[k] = transform_action(v) +end + +actions._transform_action = transform_action + return actions diff --git a/lua/telescope/mappings.lua b/lua/telescope/mappings.lua index 561bc8b..766f861 100644 --- a/lua/telescope/mappings.lua +++ b/lua/telescope/mappings.lua @@ -16,7 +16,7 @@ mappings.default_mappings = config.values.default_mappings or { [""] = actions.move_selection_next, [""] = actions.move_selection_previous, - [""] = actions.goto_file_selection_edit, + [""] = actions.goto_file_selection_edit + actions.center, [""] = actions.goto_file_selection_split, [""] = actions.goto_file_selection_vsplit, [""] = actions.goto_file_selection_tabedit, @@ -30,7 +30,7 @@ mappings.default_mappings = config.values.default_mappings or { n = { [""] = actions.close, - [""] = actions.goto_file_selection_edit, + [""] = actions.goto_file_selection_edit + actions.center, [""] = actions.goto_file_selection_split, [""] = actions.goto_file_selection_vsplit, [""] = actions.goto_file_selection_tabedit, -- cgit v1.2.3