From 5bf8799ac639f2525d91fbea990a783f89bb2693 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Wed, 27 Nov 2024 10:48:21 +0100 Subject: [PATCH] extract telescope git functions into separate file --- lua/custom/utils/telescope.lua | 104 ++++++++++++++++++++++++++++ lua/kickstart/plugins/telescope.lua | 100 +------------------------- 2 files changed, 106 insertions(+), 98 deletions(-) create mode 100644 lua/custom/utils/telescope.lua diff --git a/lua/custom/utils/telescope.lua b/lua/custom/utils/telescope.lua new file mode 100644 index 0000000..b618521 --- /dev/null +++ b/lua/custom/utils/telescope.lua @@ -0,0 +1,104 @@ +local M = {} + +-- builtin telescope modules +local actions = require 'telescope.actions' +local builtin = require 'telescope.builtin' + +function M.git_stash_mappings() + builtin.git_stash { + attach_mappings = function(_, map) + -- builtin telescope modules + local utils = require 'telescope.utils' + local action_state = require 'telescope.actions.state' + + -- Reference: https://github.com/nvim-telescope/telescope.nvim/blob/master/lua/telescope/actions/init.lua#L587 + local function git_apply_stash(prompt_bufnr) + local selection = action_state.get_selected_entry() + if selection == nil then + utils.__warn_no_selection 'actions.git_apply_stash' + return + end + actions.close(prompt_bufnr) + -- Remove unwanted '--index' flag from command + local _, ret, stderr = utils.get_os_command_output { 'git', 'stash', 'apply', selection.value } + if ret == 0 then + utils.notify('actions.git_apply_stash', { + msg = string.format("applied: '%s' ", selection.value), + level = 'INFO', + }) + else + utils.notify('actions.git_apply_stash', { + ---@diagnostic disable-next-line: param-type-mismatch + msg = string.format("Error when applying: %s. Git returned: '%s'", selection.value, table.concat(stderr, ' ')), + level = 'ERROR', + }) + end + end + actions.select_default:replace(git_apply_stash) + + -- custom function to match stash index inside curly brackets + local function match_bracket(str, arr) + string.gsub(str, '{(.-)}', function(match) + -- do not insert existing entry into index list + if not vim.list_contains(arr, match) then + table.insert(arr, match) + end + end) + end + + local function git_drop_stash(prompt_bufnr) + local picker = action_state.get_current_picker(prompt_bufnr) + local selection = picker:get_multi_selection() + + local stashes = {} + -- scan all multi_selection entries + for _, entry in ipairs(selection) do + local value = entry.value + match_bracket(value, stashes) + end + + -- scan single selected entry + local entry = action_state.get_selected_entry() + if entry == nil and #stashes == 0 then + vim.notify( + ---@diagnostic disable-next-line: param-type-mismatch + string.format('WARN [%s]: Nothing currently selected', 'git_drop_stash'), + vim.log.levels.WARN, + { title = 'telescope.nvim' } + ) + return + end + match_bracket(entry.value, stashes) + + local success = {} + for idx = #stashes, 1, -1 do + -- execute git command in os + -- reference: https://github.com/nvim-telescope/telescope.nvim/blob/master/lua/telescope/actions/init.lua#L594 + local _, ret, stderr = utils.get_os_command_output { 'git', 'stash', 'drop', string.format('stash@{%d}', stashes[idx]) } + if ret ~= 0 then + -- extracted from telescope.utils.notify: + vim.notify( + ---@diagnostic disable-next-line: param-type-mismatch + string.format("Error when dropping: stash@{%s}. Git returned: '%s'", stashes[idx], table.concat(stderr, ' ')), + vim.log.levels.ERROR, + { title = 'telescope.nvim' } + ) + return + end + table.insert(success, stashes[idx]) + end + + -- extracted from telescope.utils.notify: + vim.notify(string.format("dropping: 'stash@{%s}' ", table.concat(success, ', ')), vim.log.levels.INFO, { title = 'telescope.nvim' }) + + -- refresh picker + actions.close(prompt_bufnr) + vim.schedule(git_stash_mappings) + end + map({ 'n', 'i' }, '', git_drop_stash) + return true + end, + } +end + +return M diff --git a/lua/kickstart/plugins/telescope.lua b/lua/kickstart/plugins/telescope.lua index 316c7c5..c24ba35 100644 --- a/lua/kickstart/plugins/telescope.lua +++ b/lua/kickstart/plugins/telescope.lua @@ -193,107 +193,11 @@ return { end, { desc = '[S]earch [N]eovim files' }) -- Telescope Git Pickers + local utils = require 'custom.utils.telescope' vim.keymap.set('n', 'gb', builtin.git_branches, { desc = '[G]it [B]ranches' }) vim.keymap.set('n', 'gc', builtin.git_commits, { desc = '[G]it [C]ommits' }) vim.keymap.set('n', 'gC', builtin.git_bcommits, { desc = '[G]it Buffer [C]ommits' }) - - local function git_stash_mappings() - builtin.git_stash { - attach_mappings = function(_, map) - -- builtin telescope modules - local utils = require 'telescope.utils' - local action_state = require 'telescope.actions.state' - - -- Reference: https://github.com/nvim-telescope/telescope.nvim/blob/master/lua/telescope/actions/init.lua#L587 - local function git_apply_stash(prompt_bufnr) - local selection = action_state.get_selected_entry() - if selection == nil then - utils.__warn_no_selection 'actions.git_apply_stash' - return - end - actions.close(prompt_bufnr) - -- Remove unwanted '--index' flag from command - local _, ret, stderr = utils.get_os_command_output { 'git', 'stash', 'apply', selection.value } - if ret == 0 then - utils.notify('actions.git_apply_stash', { - msg = string.format("applied: '%s' ", selection.value), - level = 'INFO', - }) - else - utils.notify('actions.git_apply_stash', { - ---@diagnostic disable-next-line: param-type-mismatch - msg = string.format("Error when applying: %s. Git returned: '%s'", selection.value, table.concat(stderr, ' ')), - level = 'ERROR', - }) - end - end - actions.select_default:replace(git_apply_stash) - - -- custom function to match stash index inside curly brackets - local function match_bracket(str, arr) - string.gsub(str, '{(.-)}', function(match) - -- do not insert existing entry into index list - if not vim.list_contains(arr, match) then - table.insert(arr, match) - end - end) - end - - local function git_drop_stash(prompt_bufnr) - local picker = action_state.get_current_picker(prompt_bufnr) - local selection = picker:get_multi_selection() - - local stashes = {} - -- scan all multi_selection entries - for _, entry in ipairs(selection) do - local value = entry.value - match_bracket(value, stashes) - end - - -- scan single selected entry - local entry = action_state.get_selected_entry() - if entry == nil and #stashes == 0 then - vim.notify( - ---@diagnostic disable-next-line: param-type-mismatch - string.format('WARN [%s]: Nothing currently selected', 'git_drop_stash'), - vim.log.levels.WARN, - { title = 'telescope.nvim' } - ) - return - end - match_bracket(entry.value, stashes) - - local success = {} - for idx = #stashes, 1, -1 do - -- execute git command in os - -- reference: https://github.com/nvim-telescope/telescope.nvim/blob/master/lua/telescope/actions/init.lua#L594 - local _, ret, stderr = utils.get_os_command_output { 'git', 'stash', 'drop', string.format('stash@{%d}', stashes[idx]) } - if ret ~= 0 then - -- extracted from telescope.utils.notify: - vim.notify( - ---@diagnostic disable-next-line: param-type-mismatch - string.format("Error when dropping: stash@{%s}. Git returned: '%s'", stashes[idx], table.concat(stderr, ' ')), - vim.log.levels.ERROR, - { title = 'telescope.nvim' } - ) - return - end - table.insert(success, stashes[idx]) - end - - -- extracted from telescope.utils.notify: - vim.notify(string.format("dropping: 'stash@{%s}' ", table.concat(success, ', ')), vim.log.levels.INFO, { title = 'telescope.nvim' }) - - -- refresh picker - actions.close(prompt_bufnr) - vim.schedule(git_stash_mappings) - end - map({ 'n', 'i' }, '', git_drop_stash) - return true - end, - } - end - vim.keymap.set('n', 'gS', git_stash_mappings, { desc = '[G]it [S]tash' }) + vim.keymap.set('n', 'gS', utils.git_stash_mappings, { desc = '[G]it [S]tash' }) end, }, }