Skip to content

Commit ac8d259

Browse files
committed
fix(prompt): add select_prompts to suit UI decorator plugins such as dressing and telescope
1 parent 5cb87c0 commit ac8d259

File tree

7 files changed

+54
-15
lines changed

7 files changed

+54
-15
lines changed

doc/nvim-tree-lua.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ Subsequent calls to setup will replace the previous configuration.
178178
sync_root_with_cwd = false,
179179
reload_on_bufenter = false,
180180
respect_buf_cwd = false,
181-
on_attach = "disable", -- function(bufnr). If nil, will use the deprecated mapping strategy
182-
remove_keymaps = false, -- boolean (disable totally or not) or list of key (lhs)
181+
on_attach = "disable",
182+
remove_keymaps = false,
183+
select_prompts = false,
183184
view = {
184185
adaptive_size = false,
185186
centralize_selection = false,
@@ -621,6 +622,11 @@ This can be used to remove the default mappings in the tree.
621622
- Ignore by passing `false`
622623
Type: `bool` or `{string}`, Default: `false`
623624

625+
*nvim-tree.select_prompts*
626+
Use |vim.ui.select| style prompts. Necessary when using a UI prompt decorator
627+
such as dressing.nvim or telescope-ui-select.nvim
628+
Type: `boolean`, Default: `false`
629+
624630
*nvim-tree.view*
625631
Window / buffer setup.
626632

lua/nvim-tree.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,9 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
438438
sync_root_with_cwd = false,
439439
reload_on_bufenter = false,
440440
respect_buf_cwd = false,
441-
on_attach = "disable", -- function(bufnr). If nil, will use the deprecated mapping strategy
442-
remove_keymaps = false, -- boolean (disable totally or not) or list of key (lhs)
441+
on_attach = "disable",
442+
remove_keymaps = false,
443+
select_prompts = false,
443444
view = {
444445
adaptive_size = false,
445446
centralize_selection = false,

lua/nvim-tree/actions/fs/copy-paste.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,14 @@ local function do_single_paste(source, dest, action_type, action_fn)
9494
end
9595

9696
if dest_stats then
97-
vim.ui.input({ prompt = dest .. " already exists. Overwrite? y/n/r(ename): " }, function(choice)
97+
local prompt_select = "Overwrite " .. dest .. " ?"
98+
local prompt_input = prompt_select .. " y/n/r(ename): "
99+
lib.prompt(prompt_input, prompt_select, { "y", "n", "r" }, { "Yes", "No", "Rename" }, function(item_short)
98100
utils.clear_prompt()
99-
if choice == "y" then
101+
if item_short == "y" then
100102
on_process()
101-
elseif choice == "r" then
102-
vim.ui.input({ prompt = "New name: ", default = dest, completion = "dir" }, function(new_dest)
103+
elseif item_short == "r" then
104+
vim.ui.input({ prompt = "Rename to ", default = dest, completion = "dir" }, function(new_dest)
103105
utils.clear_prompt()
104106
if new_dest then
105107
do_single_paste(source, new_dest, action_type, action_fn)

lua/nvim-tree/actions/fs/create-file.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ end
1919

2020
local function create_file(file)
2121
if utils.file_exists(file) then
22-
vim.ui.input({ prompt = file .. " already exists. Overwrite? y/n: " }, function(choice)
22+
local prompt_select = "Overwrite " .. file .. " ?"
23+
local prompt_input = prompt_select .. " y/n: "
24+
lib.prompt(prompt_input, prompt_select, { "y", "n" }, { "Yes", "No" }, function(item_short)
2325
utils.clear_prompt()
24-
if choice == "y" then
26+
if item_short == "y" then
2527
create_and_notify(file)
2628
end
2729
end)

lua/nvim-tree/actions/fs/remove-file.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local luv = vim.loop
44
local utils = require "nvim-tree.utils"
55
local events = require "nvim-tree.events"
66
local view = require "nvim-tree.view"
7+
local lib = require "nvim-tree.lib"
78

89
local M = {}
910

@@ -74,10 +75,11 @@ function M.fn(node)
7475
if node.name == ".." then
7576
return
7677
end
77-
78-
vim.ui.input({ prompt = "Remove " .. node.name .. " ? y/n: " }, function(choice)
78+
local prompt_select = "Remove " .. node.name .. " ?"
79+
local prompt_input = prompt_select .. " y/n: "
80+
lib.prompt(prompt_input, prompt_select, { "y", "n" }, { "Yes", "No" }, function(item_short)
7981
utils.clear_prompt()
80-
if choice == "y" then
82+
if item_short == "y" then
8183
if node.nodes ~= nil and not node.link_to then
8284
local success = remove_dir(node.absolute_path)
8385
if not success then

lua/nvim-tree/actions/fs/trash.lua

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local a = vim.api
22

3+
local lib = require "nvim-tree.lib"
4+
35
local M = {
46
config = {
57
is_windows = vim.fn.has "win32" == 1 or vim.fn.has "win32unix" == 1,
@@ -93,9 +95,11 @@ function M.fn(node)
9395
end
9496

9597
if M.config.trash.require_confirm then
96-
vim.ui.input({ prompt = "Trash " .. node.name .. " ? y/n: " }, function(choice)
98+
local prompt_select = "Trash " .. node.name .. " ?"
99+
local prompt_input = prompt_select .. " y/n: "
100+
lib.prompt(prompt_input, prompt_select, { "y", "n" }, { "Yes", "No" }, function(item_short)
97101
utils.clear_prompt()
98-
if choice == "y" then
102+
if item_short == "y" then
99103
do_trash()
100104
end
101105
end)

lua/nvim-tree/lib.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,27 @@ local function should_hijack_current_buf()
9292
return should_hijack_dir or should_hijack_unnamed
9393
end
9494

95+
function M.prompt(prompt_input, prompt_select, items_short, items_long, callback)
96+
local function format_item(short)
97+
for i, s in ipairs(items_short) do
98+
if short == s then
99+
return items_long[i]
100+
end
101+
end
102+
return ""
103+
end
104+
105+
if M.select_prompts then
106+
vim.ui.select(items_short, { prompt = prompt_select, format_item = format_item }, function(item_short)
107+
callback(item_short)
108+
end)
109+
else
110+
vim.ui.input({ prompt = prompt_input }, function(item_short)
111+
callback(item_short)
112+
end)
113+
end
114+
end
115+
95116
function M.open(cwd)
96117
M.set_target_win()
97118
if not core.get_explorer() or cwd then
@@ -124,6 +145,7 @@ function M.setup(opts)
124145
M.hijack_unnamed_buffer_when_opening = opts.hijack_unnamed_buffer_when_opening
125146
M.hijack_directories = opts.hijack_directories
126147
M.respect_buf_cwd = opts.respect_buf_cwd
148+
M.select_prompts = opts.select_prompts
127149
end
128150

129151
return M

0 commit comments

Comments
 (0)