mirror of
https://github.com/Baipyrus/nvim-config.git
synced 2024-12-25 12:51:45 +00:00
cleanup: extract functionality into dedicated module commands
This commit is contained in:
parent
5bf8799ac6
commit
68cec13329
@ -1,17 +1,13 @@
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
-- builtin telescope modules
|
-- builtin telescope modules
|
||||||
|
local utils = require 'telescope.utils'
|
||||||
local actions = require 'telescope.actions'
|
local actions = require 'telescope.actions'
|
||||||
local builtin = require 'telescope.builtin'
|
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'
|
local action_state = require 'telescope.actions.state'
|
||||||
|
|
||||||
-- Reference: https://github.com/nvim-telescope/telescope.nvim/blob/master/lua/telescope/actions/init.lua#L587
|
--- 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 function git_apply_stash(prompt_bufnr)
|
||||||
local selection = action_state.get_selected_entry()
|
local selection = action_state.get_selected_entry()
|
||||||
if selection == nil then
|
if selection == nil then
|
||||||
@ -34,11 +30,13 @@ function M.git_stash_mappings()
|
|||||||
})
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
actions.select_default:replace(git_apply_stash)
|
|
||||||
|
|
||||||
-- custom function to match stash index inside curly brackets
|
--- Custom function to match any unique characters inside a pattern.
|
||||||
local function match_bracket(str, arr)
|
--- @param str string: The input string to scan.
|
||||||
string.gsub(str, '{(.-)}', function(match)
|
--- @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
|
-- do not insert existing entry into index list
|
||||||
if not vim.list_contains(arr, match) then
|
if not vim.list_contains(arr, match) then
|
||||||
table.insert(arr, match)
|
table.insert(arr, match)
|
||||||
@ -46,7 +44,10 @@ function M.git_stash_mappings()
|
|||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function git_drop_stash(prompt_bufnr)
|
--- 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 picker = action_state.get_current_picker(prompt_bufnr)
|
||||||
local selection = picker:get_multi_selection()
|
local selection = picker:get_multi_selection()
|
||||||
|
|
||||||
@ -54,7 +55,7 @@ function M.git_stash_mappings()
|
|||||||
-- scan all multi_selection entries
|
-- scan all multi_selection entries
|
||||||
for _, entry in ipairs(selection) do
|
for _, entry in ipairs(selection) do
|
||||||
local value = entry.value
|
local value = entry.value
|
||||||
match_bracket(value, stashes)
|
M.match_to_list(value, '{(.-)}', stashes)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- scan single selected entry
|
-- scan single selected entry
|
||||||
@ -66,10 +67,16 @@ function M.git_stash_mappings()
|
|||||||
vim.log.levels.WARN,
|
vim.log.levels.WARN,
|
||||||
{ title = 'telescope.nvim' }
|
{ title = 'telescope.nvim' }
|
||||||
)
|
)
|
||||||
return
|
return false
|
||||||
end
|
end
|
||||||
match_bracket(entry.value, stashes)
|
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 = {}
|
local success = {}
|
||||||
for idx = #stashes, 1, -1 do
|
for idx = #stashes, 1, -1 do
|
||||||
-- execute git command in os
|
-- execute git command in os
|
||||||
@ -90,10 +97,26 @@ function M.git_stash_mappings()
|
|||||||
|
|
||||||
-- extracted from telescope.utils.notify:
|
-- extracted from telescope.utils.notify:
|
||||||
vim.notify(string.format("dropping: 'stash@{%s}' ", table.concat(success, ', ')), vim.log.levels.INFO, { title = 'telescope.nvim' })
|
vim.notify(string.format("dropping: 'stash@{%s}' ", table.concat(success, ', ')), vim.log.levels.INFO, { title = 'telescope.nvim' })
|
||||||
|
end
|
||||||
|
|
||||||
-- refresh picker
|
--- Extend the Telescope `git_stash` picker with custom mappings.
|
||||||
|
function M.git_stash()
|
||||||
|
builtin.git_stash {
|
||||||
|
attach_mappings = function(_, map)
|
||||||
|
actions.select_default:replace(git_apply_stash)
|
||||||
|
|
||||||
|
--- @param prompt_bufnr number: The prompt buffer number from Telescope.
|
||||||
|
local function git_drop_stash(prompt_bufnr)
|
||||||
|
-- Try getting all selections in telescope picker
|
||||||
|
local stashes = M.get_stash_selections(prompt_bufnr)
|
||||||
|
if stashes == false then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
assert(type(stashes) == 'table', 'Telescope picker selection did not result in list!')
|
||||||
|
|
||||||
|
-- try dropping and close picker when done
|
||||||
|
M.git_drop_stash(stashes)
|
||||||
actions.close(prompt_bufnr)
|
actions.close(prompt_bufnr)
|
||||||
vim.schedule(git_stash_mappings)
|
|
||||||
end
|
end
|
||||||
map({ 'n', 'i' }, '<C-S-d>', git_drop_stash)
|
map({ 'n', 'i' }, '<C-S-d>', git_drop_stash)
|
||||||
return true
|
return true
|
||||||
|
@ -197,7 +197,7 @@ return {
|
|||||||
vim.keymap.set('n', '<leader>gb', builtin.git_branches, { desc = '[G]it [B]ranches' })
|
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_commits, { desc = '[G]it [C]ommits' })
|
||||||
vim.keymap.set('n', '<leader>gC', builtin.git_bcommits, { desc = '[G]it Buffer [C]ommits' })
|
vim.keymap.set('n', '<leader>gC', builtin.git_bcommits, { desc = '[G]it Buffer [C]ommits' })
|
||||||
vim.keymap.set('n', '<leader>gS', utils.git_stash_mappings, { desc = '[G]it [S]tash' })
|
vim.keymap.set('n', '<leader>gS', utils.git_stash, { desc = '[G]it [S]tash' })
|
||||||
end,
|
end,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user