From 88f27dbfe932f6838e6fb19090d8d4b2074f2e68 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Thu, 21 Nov 2024 15:10:49 +0100 Subject: [PATCH 1/9] replace unnecessary git plugins with telescope functions --- lua/custom/plugins/fugitive.lua | 12 ------------ lua/kickstart/plugins/telescope.lua | 5 +++++ 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/lua/custom/plugins/fugitive.lua b/lua/custom/plugins/fugitive.lua index 1732799..7e547f6 100644 --- a/lua/custom/plugins/fugitive.lua +++ b/lua/custom/plugins/fugitive.lua @@ -2,14 +2,6 @@ return { { -- Git related plugins 'tpope/vim-fugitive', - dependencies = { - { - 'rbong/vim-flog', - lazy = true, - cmd = { 'Flogsplit' }, - }, - 'idanarye/vim-merginal', - }, config = function() -- Switch in fugitive.vim status window with the current one vim.keymap.set('n', 'gs', 'Gedit :', { desc = '[G]it [S]tatus' }) @@ -19,10 +11,6 @@ return { vim.keymap.set('n', 'gP', 'Git push', { desc = '[G]it [P]ush' }) -- Pull changes vim.keymap.set('n', 'gp', 'Git pull', { desc = '[G]it [P]ull' }) - -- Open history graph - vim.keymap.set('n', 'gl', 'Flogsplitwincmd kq', { desc = '[G]it [L]og' }) - -- Open branch manager - vim.keymap.set('n', 'gm', 'Merginal', { desc = '[G]it [M]erginal' }) end, }, } diff --git a/lua/kickstart/plugins/telescope.lua b/lua/kickstart/plugins/telescope.lua index 7e0eb68..1619702 100644 --- a/lua/kickstart/plugins/telescope.lua +++ b/lua/kickstart/plugins/telescope.lua @@ -191,6 +191,11 @@ return { cwd = vim.fn.stdpath 'config', } end, { desc = '[S]earch [N]eovim files' }) + + -- Telescope Git Pickers + 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' }) end, }, } From 21206df7b36062186888958cefda0c2ceccfce21 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Thu, 21 Nov 2024 15:21:19 +0100 Subject: [PATCH 2/9] stash drop mapping for telescope.builtin.git_stash --- lua/kickstart/plugins/telescope.lua | 68 +++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/lua/kickstart/plugins/telescope.lua b/lua/kickstart/plugins/telescope.lua index 1619702..73da14e 100644 --- a/lua/kickstart/plugins/telescope.lua +++ b/lua/kickstart/plugins/telescope.lua @@ -196,6 +196,74 @@ return { 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' + + -- 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 + + map({ 'n', 'i' }, '', function(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 + -- reference: https://github.com/nvim-telescope/telescope.nvim/blob/master/lua/telescope/actions/init.lua#L589 + utils.__warn_no_selection 'actions.git_apply_stash' + 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, { desc = 'git_drop_stash' }) + return true + end, + } + end + vim.keymap.set('n', 'gS', git_stash_mappings, { desc = '[G]it [S]tash' }) end, }, } From db9168e2d12e6bf55b37ca054931255eaf4d988c Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 26 Nov 2024 09:53:13 +0100 Subject: [PATCH 3/9] try not to format html code for convenience --- lua/kickstart/plugins/conform.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/kickstart/plugins/conform.lua b/lua/kickstart/plugins/conform.lua index 476b80b..43a42b0 100644 --- a/lua/kickstart/plugins/conform.lua +++ b/lua/kickstart/plugins/conform.lua @@ -19,7 +19,7 @@ return { -- Disable "format_on_save lsp_fallback" for languages that don't -- have a well standardized coding style. You can add additional -- languages here or re-enable it for the disabled ones. - local disable_filetypes = { c = true, cpp = true, php = true } + local disable_filetypes = { c = true, cpp = true, php = true, html = true } local lsp_format_opt if disable_filetypes[vim.bo[bufnr].filetype] then lsp_format_opt = 'never' From 29aabb6546fc811db923c199ef0b296abbdde82a Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 26 Nov 2024 09:55:26 +0100 Subject: [PATCH 4/9] bugfix: use to delete stash items to avoid remapping scroll --- lua/kickstart/plugins/telescope.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/kickstart/plugins/telescope.lua b/lua/kickstart/plugins/telescope.lua index 73da14e..6f84876 100644 --- a/lua/kickstart/plugins/telescope.lua +++ b/lua/kickstart/plugins/telescope.lua @@ -214,7 +214,7 @@ return { end) end - map({ 'n', 'i' }, '', function(prompt_bufnr) + map({ 'n', 'i' }, '', function(prompt_bufnr) local picker = action_state.get_current_picker(prompt_bufnr) local selection = picker:get_multi_selection() From a1ca7a6d778aa93b7f6a2af7b33f2671b74204d0 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 26 Nov 2024 10:36:46 +0100 Subject: [PATCH 5/9] refactor: extract mapping functionality to use function name for help --- lua/kickstart/plugins/telescope.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lua/kickstart/plugins/telescope.lua b/lua/kickstart/plugins/telescope.lua index 6f84876..a8bec96 100644 --- a/lua/kickstart/plugins/telescope.lua +++ b/lua/kickstart/plugins/telescope.lua @@ -214,7 +214,7 @@ return { end) end - map({ 'n', 'i' }, '', function(prompt_bufnr) + local function git_drop_stash(prompt_bufnr) local picker = action_state.get_current_picker(prompt_bufnr) local selection = picker:get_multi_selection() @@ -258,7 +258,8 @@ return { -- refresh picker actions.close(prompt_bufnr) vim.schedule(git_stash_mappings) - end, { desc = 'git_drop_stash' }) + end + map({ 'n', 'i' }, '', git_drop_stash) return true end, } From 707c1757d8e27e34054f3c90d864b6e03634f9ad Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 26 Nov 2024 10:51:57 +0100 Subject: [PATCH 6/9] replace copied warning notification with custom message --- lua/kickstart/plugins/telescope.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lua/kickstart/plugins/telescope.lua b/lua/kickstart/plugins/telescope.lua index a8bec96..a2f8a21 100644 --- a/lua/kickstart/plugins/telescope.lua +++ b/lua/kickstart/plugins/telescope.lua @@ -228,8 +228,12 @@ return { -- scan single selected entry local entry = action_state.get_selected_entry() if entry == nil and #stashes == 0 then - -- reference: https://github.com/nvim-telescope/telescope.nvim/blob/master/lua/telescope/actions/init.lua#L589 - utils.__warn_no_selection 'actions.git_apply_stash' + 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) From 242577f9d047faa349d4bad96e51053ab041c922 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Tue, 26 Nov 2024 10:52:31 +0100 Subject: [PATCH 7/9] overwrite default stash selection without index flag --- lua/kickstart/plugins/telescope.lua | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lua/kickstart/plugins/telescope.lua b/lua/kickstart/plugins/telescope.lua index a2f8a21..316c7c5 100644 --- a/lua/kickstart/plugins/telescope.lua +++ b/lua/kickstart/plugins/telescope.lua @@ -204,6 +204,31 @@ return { 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) From 5bf8799ac639f2525d91fbea990a783f89bb2693 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Wed, 27 Nov 2024 10:48:21 +0100 Subject: [PATCH 8/9] 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, }, } From 68cec1332928482881487c6d45c2693dc44d76b4 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Wed, 27 Nov 2024 10:53:19 +0100 Subject: [PATCH 9/9] cleanup: extract functionality into dedicated module commands --- lua/custom/utils/telescope.lua | 185 ++++++++++++++++------------ lua/kickstart/plugins/telescope.lua | 2 +- 2 files changed, 105 insertions(+), 82 deletions(-) diff --git a/lua/custom/utils/telescope.lua b/lua/custom/utils/telescope.lua index b618521..90f5b7f 100644 --- a/lua/custom/utils/telescope.lua +++ b/lua/custom/utils/telescope.lua @@ -1,99 +1,122 @@ local M = {} -- builtin telescope modules +local utils = require 'telescope.utils' local actions = require 'telescope.actions' local builtin = require 'telescope.builtin' +local action_state = require 'telescope.actions.state' -function M.git_stash_mappings() +--- Reference: [telescope.actions.git_apply_stash](https://github.com/nvim-telescope/telescope.nvim/blob/master/lua/telescope/actions/init.lua#L587) +--- @param prompt_bufnr number: The prompt buffer number from Telescope. +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 + +--- Custom function to match any unique characters inside a pattern. +--- @param str string: The input string to scan. +--- @param pat string: The pattern to search for. +--- @param arr table: The output table to append to. +function M.match_to_list(str, pat, arr) + string.gsub(str, pat, 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 + +--- Get all selected entries in the Telescope picker and extract stash indices. +--- @param prompt_bufnr number: The prompt buffer number from Telescope. +--- @return table|false: A list of stash indices or false if nothing is selected. +function M.get_stash_selections(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 + M.match_to_list(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 false + end + M.match_to_list(entry.value, '{(.-)}', stashes) + + return stashes +end + +--- Drop git stashes by their indices. +--- @param stashes table: A list of stash indices to drop. +function M.git_drop_stash(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' }) +end + +--- Extend the Telescope `git_stash` picker with custom mappings. +function M.git_stash() 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 - + --- @param prompt_bufnr number: The prompt buffer number from Telescope. 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' } - ) + -- Try getting all selections in telescope picker + local stashes = M.get_stash_selections(prompt_bufnr) + if stashes == false then return end - match_bracket(entry.value, stashes) + assert(type(stashes) == 'table', 'Telescope picker selection did not result in list!') - 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 + -- try dropping and close picker when done + M.git_drop_stash(stashes) actions.close(prompt_bufnr) - vim.schedule(git_stash_mappings) end map({ 'n', 'i' }, '', git_drop_stash) return true diff --git a/lua/kickstart/plugins/telescope.lua b/lua/kickstart/plugins/telescope.lua index c24ba35..a22099d 100644 --- a/lua/kickstart/plugins/telescope.lua +++ b/lua/kickstart/plugins/telescope.lua @@ -197,7 +197,7 @@ return { 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' }) - vim.keymap.set('n', 'gS', utils.git_stash_mappings, { desc = '[G]it [S]tash' }) + vim.keymap.set('n', 'gS', utils.git_stash, { desc = '[G]it [S]tash' }) end, }, }