Files
nvim-modular/lua/custom/plugins/neotree-helper.lua
2025-08-29 23:21:21 +02:00

91 lines
2.7 KiB
Lua

-- Neo-tree helper module
--
local M = {}
-- Function to get confirmation input (compatible with different neo-tree versions)
local function get_confirmation(msg, callback)
-- Try the modern approach first
local ok, inputs = pcall(require, 'neo-tree.ui.inputs')
if ok and inputs and inputs.confirm then
return inputs.confirm(msg, callback)
end
-- Fallback to vim.ui.input for compatibility
vim.ui.input({
prompt = msg .. ' (y/N): ',
}, function(input)
local confirmed = input and (input:lower() == 'y' or input:lower() == 'yes')
callback(confirmed)
end)
end
-- Smart open a neo-tree window
function M.smart_open()
local bufname = vim.api.nvim_buf_get_name(0)
local filetype = vim.bo.filetype
local is_real_file = vim.fn.filereadable(bufname) == 1
-- Si c'est le dashboard ministarter, ouvrir dans le home
if filetype == 'ministarter' then
vim.cmd('Neotree toggle reveal=false position=float dir=' .. vim.fn.expand '~')
-- Si c'est un fichier réel, révéler le fichier dans l'arborescence
elseif is_real_file and bufname ~= '' then
vim.cmd('Neotree toggle position=left reveal_force_cwd=true dir=' .. vim.fn.expand '%:p:h')
-- vim.cmd 'Neotree toggle position=left reveal_force_cwd=true'
-- Sinon, ouvrir dans le répertoire de travail courant
else
vim.cmd('Neotree toggle position=left reveal=true dir=' .. vim.fn.getcwd())
-- vim.cmd 'Neotree toggle position=left reveal_force_cwd=true'
end
end
-- Trash the target
function M.trash(state)
local node = state.tree:get_node()
if node.type == 'message' then
return
end
local _, name = require('neo-tree.utils').split_path(node.path)
local msg = string.format("Are you sure you want to trash '%s'?", name)
get_confirmation(msg, function(confirmed)
if not confirmed then
return
end
vim.api.nvim_command('silent !trash-put ' .. vim.fn.shellescape(node.path))
require('neo-tree.sources.manager').refresh(state.name)
end)
end
-- Trash the selections (visual mode)
function M.trash_visual(state, selected_nodes)
local paths_to_trash = {}
for _, node in ipairs(selected_nodes) do
if node.type ~= 'message' then
table.insert(paths_to_trash, node.path)
end
end
if #paths_to_trash == 0 then
return
end
local msg = 'Are you sure you want to trash ' .. #paths_to_trash .. ' items?'
get_confirmation(msg, function(confirmed)
if not confirmed then
return
end
for _, path in ipairs(paths_to_trash) do
vim.api.nvim_command('silent !trash-put ' .. vim.fn.shellescape(path))
end
require('neo-tree.sources.manager').refresh(state.name)
end)
end
return M
-- The line beneath this is called `modeline`. See `:help modeline`
-- vim: ts=2 sts=2 sw=2 et