From ac8d259badf915cd9aaad406503d116230296c44 Mon Sep 17 00:00:00 2001 From: Alexander Courtis Date: Thu, 22 Sep 2022 13:23:49 +1000 Subject: [PATCH] fix(prompt): add select_prompts to suit UI decorator plugins such as dressing and telescope --- doc/nvim-tree-lua.txt | 10 ++++++++-- lua/nvim-tree.lua | 5 +++-- lua/nvim-tree/actions/fs/copy-paste.lua | 10 ++++++---- lua/nvim-tree/actions/fs/create-file.lua | 6 ++++-- lua/nvim-tree/actions/fs/remove-file.lua | 8 +++++--- lua/nvim-tree/actions/fs/trash.lua | 8 ++++++-- lua/nvim-tree/lib.lua | 22 ++++++++++++++++++++++ 7 files changed, 54 insertions(+), 15 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 9dc36cad..2a4d06eb 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -178,8 +178,9 @@ Subsequent calls to setup will replace the previous configuration. sync_root_with_cwd = false, reload_on_bufenter = false, respect_buf_cwd = false, - on_attach = "disable", -- function(bufnr). If nil, will use the deprecated mapping strategy - remove_keymaps = false, -- boolean (disable totally or not) or list of key (lhs) + on_attach = "disable", + remove_keymaps = false, + select_prompts = false, view = { adaptive_size = false, centralize_selection = false, @@ -621,6 +622,11 @@ This can be used to remove the default mappings in the tree. - Ignore by passing `false` Type: `bool` or `{string}`, Default: `false` +*nvim-tree.select_prompts* +Use |vim.ui.select| style prompts. Necessary when using a UI prompt decorator +such as dressing.nvim or telescope-ui-select.nvim +Type: `boolean`, Default: `false` + *nvim-tree.view* Window / buffer setup. diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index 0af27560..79bf68e1 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -438,8 +438,9 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS sync_root_with_cwd = false, reload_on_bufenter = false, respect_buf_cwd = false, - on_attach = "disable", -- function(bufnr). If nil, will use the deprecated mapping strategy - remove_keymaps = false, -- boolean (disable totally or not) or list of key (lhs) + on_attach = "disable", + remove_keymaps = false, + select_prompts = false, view = { adaptive_size = false, centralize_selection = false, diff --git a/lua/nvim-tree/actions/fs/copy-paste.lua b/lua/nvim-tree/actions/fs/copy-paste.lua index 07f2fc13..0742ad1d 100644 --- a/lua/nvim-tree/actions/fs/copy-paste.lua +++ b/lua/nvim-tree/actions/fs/copy-paste.lua @@ -94,12 +94,14 @@ local function do_single_paste(source, dest, action_type, action_fn) end if dest_stats then - vim.ui.input({ prompt = dest .. " already exists. Overwrite? y/n/r(ename): " }, function(choice) + local prompt_select = "Overwrite " .. dest .. " ?" + local prompt_input = prompt_select .. " y/n/r(ename): " + lib.prompt(prompt_input, prompt_select, { "y", "n", "r" }, { "Yes", "No", "Rename" }, function(item_short) utils.clear_prompt() - if choice == "y" then + if item_short == "y" then on_process() - elseif choice == "r" then - vim.ui.input({ prompt = "New name: ", default = dest, completion = "dir" }, function(new_dest) + elseif item_short == "r" then + vim.ui.input({ prompt = "Rename to ", default = dest, completion = "dir" }, function(new_dest) utils.clear_prompt() if new_dest then do_single_paste(source, new_dest, action_type, action_fn) diff --git a/lua/nvim-tree/actions/fs/create-file.lua b/lua/nvim-tree/actions/fs/create-file.lua index d5544e8b..552d1838 100644 --- a/lua/nvim-tree/actions/fs/create-file.lua +++ b/lua/nvim-tree/actions/fs/create-file.lua @@ -19,9 +19,11 @@ end local function create_file(file) if utils.file_exists(file) then - vim.ui.input({ prompt = file .. " already exists. Overwrite? y/n: " }, function(choice) + local prompt_select = "Overwrite " .. file .. " ?" + local prompt_input = prompt_select .. " y/n: " + lib.prompt(prompt_input, prompt_select, { "y", "n" }, { "Yes", "No" }, function(item_short) utils.clear_prompt() - if choice == "y" then + if item_short == "y" then create_and_notify(file) end end) diff --git a/lua/nvim-tree/actions/fs/remove-file.lua b/lua/nvim-tree/actions/fs/remove-file.lua index 21f1ace6..3a94e31f 100644 --- a/lua/nvim-tree/actions/fs/remove-file.lua +++ b/lua/nvim-tree/actions/fs/remove-file.lua @@ -4,6 +4,7 @@ local luv = vim.loop local utils = require "nvim-tree.utils" local events = require "nvim-tree.events" local view = require "nvim-tree.view" +local lib = require "nvim-tree.lib" local M = {} @@ -74,10 +75,11 @@ function M.fn(node) if node.name == ".." then return end - - vim.ui.input({ prompt = "Remove " .. node.name .. " ? y/n: " }, function(choice) + local prompt_select = "Remove " .. node.name .. " ?" + local prompt_input = prompt_select .. " y/n: " + lib.prompt(prompt_input, prompt_select, { "y", "n" }, { "Yes", "No" }, function(item_short) utils.clear_prompt() - if choice == "y" then + if item_short == "y" then if node.nodes ~= nil and not node.link_to then local success = remove_dir(node.absolute_path) if not success then diff --git a/lua/nvim-tree/actions/fs/trash.lua b/lua/nvim-tree/actions/fs/trash.lua index 559e6ec1..a50133ab 100644 --- a/lua/nvim-tree/actions/fs/trash.lua +++ b/lua/nvim-tree/actions/fs/trash.lua @@ -1,5 +1,7 @@ local a = vim.api +local lib = require "nvim-tree.lib" + local M = { config = { is_windows = vim.fn.has "win32" == 1 or vim.fn.has "win32unix" == 1, @@ -93,9 +95,11 @@ function M.fn(node) end if M.config.trash.require_confirm then - vim.ui.input({ prompt = "Trash " .. node.name .. " ? y/n: " }, function(choice) + local prompt_select = "Trash " .. node.name .. " ?" + local prompt_input = prompt_select .. " y/n: " + lib.prompt(prompt_input, prompt_select, { "y", "n" }, { "Yes", "No" }, function(item_short) utils.clear_prompt() - if choice == "y" then + if item_short == "y" then do_trash() end end) diff --git a/lua/nvim-tree/lib.lua b/lua/nvim-tree/lib.lua index ff40339a..93821fd6 100644 --- a/lua/nvim-tree/lib.lua +++ b/lua/nvim-tree/lib.lua @@ -92,6 +92,27 @@ local function should_hijack_current_buf() return should_hijack_dir or should_hijack_unnamed end +function M.prompt(prompt_input, prompt_select, items_short, items_long, callback) + local function format_item(short) + for i, s in ipairs(items_short) do + if short == s then + return items_long[i] + end + end + return "" + end + + if M.select_prompts then + vim.ui.select(items_short, { prompt = prompt_select, format_item = format_item }, function(item_short) + callback(item_short) + end) + else + vim.ui.input({ prompt = prompt_input }, function(item_short) + callback(item_short) + end) + end +end + function M.open(cwd) M.set_target_win() if not core.get_explorer() or cwd then @@ -124,6 +145,7 @@ function M.setup(opts) M.hijack_unnamed_buffer_when_opening = opts.hijack_unnamed_buffer_when_opening M.hijack_directories = opts.hijack_directories M.respect_buf_cwd = opts.respect_buf_cwd + M.select_prompts = opts.select_prompts end return M