feat(create-file): use vim.ui.select for confirmation
fixes #1434 fixes #1294
This commit is contained in:
parent
2d629cab78
commit
1ee6a3ea65
@ -7,15 +7,7 @@ local core = require "nvim-tree.core"
|
|||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
local function create_file(file)
|
local function create_and_notify(file)
|
||||||
if utils.file_exists(file) then
|
|
||||||
print(file .. " already exists. Overwrite? y/n")
|
|
||||||
local ans = utils.get_user_input_char()
|
|
||||||
utils.clear_prompt()
|
|
||||||
if ans ~= "y" then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local ok, fd = pcall(uv.fs_open, file, "w", 420)
|
local ok, fd = pcall(uv.fs_open, file, "w", 420)
|
||||||
if not ok then
|
if not ok then
|
||||||
utils.notify.error("Couldn't create file " .. file)
|
utils.notify.error("Couldn't create file " .. file)
|
||||||
@ -25,6 +17,18 @@ local function create_file(file)
|
|||||||
events._dispatch_file_created(file)
|
events._dispatch_file_created(file)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function create_file(file)
|
||||||
|
if utils.file_exists(file) then
|
||||||
|
vim.ui.select({ "y", "n" }, { prompt = file .. " already exists. Overwrite?" }, function(choice)
|
||||||
|
if choice == "y" then
|
||||||
|
create_and_notify()
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
else
|
||||||
|
create_and_notify()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function get_num_nodes(iter)
|
local function get_num_nodes(iter)
|
||||||
local i = 0
|
local i = 0
|
||||||
for _ in iter do
|
for _ in iter do
|
||||||
@ -61,8 +65,6 @@ function M.fn(node)
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
utils.clear_prompt()
|
|
||||||
|
|
||||||
if utils.file_exists(new_file_path) then
|
if utils.file_exists(new_file_path) then
|
||||||
utils.notify.warn "Cannot create: file already exists"
|
utils.notify.warn "Cannot create: file already exists"
|
||||||
return
|
return
|
||||||
|
|||||||
@ -21,6 +21,14 @@ local function get_split_cmd()
|
|||||||
return "top"
|
return "top"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function get_user_input_char()
|
||||||
|
local c = vim.fn.getchar()
|
||||||
|
while type(c) ~= "number" do
|
||||||
|
c = vim.fn.getchar()
|
||||||
|
end
|
||||||
|
return vim.fn.nr2char(c)
|
||||||
|
end
|
||||||
|
|
||||||
---Get user to pick a window. Selectable windows are all windows in the current
|
---Get user to pick a window. Selectable windows are all windows in the current
|
||||||
---tabpage that aren't NvimTree.
|
---tabpage that aren't NvimTree.
|
||||||
---@return integer|nil -- If a valid window was picked, return its id. If an
|
---@return integer|nil -- If a valid window was picked, return its id. If an
|
||||||
@ -102,9 +110,11 @@ local function pick_window()
|
|||||||
if vim.opt.cmdheight._value ~= 0 then
|
if vim.opt.cmdheight._value ~= 0 then
|
||||||
print "Pick window: "
|
print "Pick window: "
|
||||||
end
|
end
|
||||||
local _, resp = pcall(utils.get_user_input_char)
|
local _, resp = pcall(get_user_input_char)
|
||||||
resp = (resp or ""):upper()
|
resp = (resp or ""):upper()
|
||||||
utils.clear_prompt()
|
if vim.op.cmdheight._value ~= 0 then
|
||||||
|
vim.api.nvim_command "normal! :"
|
||||||
|
end
|
||||||
|
|
||||||
-- Restore window options
|
-- Restore window options
|
||||||
for _, id in ipairs(selectable) do
|
for _, id in ipairs(selectable) do
|
||||||
|
|||||||
@ -96,18 +96,6 @@ end
|
|||||||
|
|
||||||
M.path_separator = path_separator
|
M.path_separator = path_separator
|
||||||
|
|
||||||
function M.clear_prompt()
|
|
||||||
vim.api.nvim_command "normal! :"
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.get_user_input_char()
|
|
||||||
local c = vim.fn.getchar()
|
|
||||||
while type(c) ~= "number" do
|
|
||||||
c = vim.fn.getchar()
|
|
||||||
end
|
|
||||||
return vim.fn.nr2char(c)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- get the node and index of the node from the tree that matches the predicate.
|
-- get the node and index of the node from the tree that matches the predicate.
|
||||||
-- The explored nodes are those displayed on the view.
|
-- The explored nodes are those displayed on the view.
|
||||||
-- @param nodes list of node
|
-- @param nodes list of node
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user