mirror of
https://github.com/Baipyrus/nvim-config.git
synced 2024-12-26 05:11:45 +00:00
stash drop mapping for telescope.builtin.git_stash
This commit is contained in:
parent
88f27dbfe9
commit
21206df7b3
@ -196,6 +196,74 @@ return {
|
||||
vim.keymap.set('n', '<leader>gb', builtin.git_branches, { desc = '[G]it [B]ranches' })
|
||||
vim.keymap.set('n', '<leader>gc', builtin.git_commits, { desc = '[G]it [C]ommits' })
|
||||
vim.keymap.set('n', '<leader>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' }, '<C-d>', 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', '<leader>gS', git_stash_mappings, { desc = '[G]it [S]tash' })
|
||||
end,
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user