Skip to content

Commit

Permalink
feat(nui): allow callbacks in nui input with option (#1372)
Browse files Browse the repository at this point in the history
  • Loading branch information
pysan3 committed Mar 6, 2024
1 parent dcb63ab commit bce4540
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
15 changes: 15 additions & 0 deletions doc/neo-tree.txt
Expand Up @@ -1431,6 +1431,21 @@ have.
"neo_tree_popup_buffer_leave"~
Fired after leaving a neo-tree popup buffer.

"neo_tree_popup_input_ready"~
Fired after NuiInput is ready. Use this event to default to normal mode etc.
This is fired inside a vim.schedule.

>lua
{
event = "neo_tree_popup_input_ready",
---@param input NuiInput
handler = function(input)
-- enter input popup with normal mode by default.
vim.cmd("stopinsert")
end,
}
<

"neo_tree_window_before_open"~
Fired before opening a new Neo-tree window. Called with the following arg:
*neo-tree-window-event-args*
Expand Down
1 change: 1 addition & 0 deletions lua/neo-tree/events/init.lua
Expand Up @@ -24,6 +24,7 @@ local M = {
NEO_TREE_LSP_UPDATE = "neo_tree_lsp_update",
NEO_TREE_POPUP_BUFFER_ENTER = "neo_tree_popup_buffer_enter",
NEO_TREE_POPUP_BUFFER_LEAVE = "neo_tree_popup_buffer_leave",
NEO_TREE_POPUP_INPUT_READY = "neo_tree_popup_input_ready",
NEO_TREE_WINDOW_AFTER_CLOSE = "neo_tree_window_after_close",
NEO_TREE_WINDOW_AFTER_OPEN = "neo_tree_window_after_open",
NEO_TREE_WINDOW_BEFORE_CLOSE = "neo_tree_window_before_close",
Expand Down
46 changes: 35 additions & 11 deletions lua/neo-tree/setup/deprecations.lua
Expand Up @@ -6,25 +6,24 @@ local migrations = {}

M.show_migrations = function()
if #migrations > 0 then
for i, message in ipairs(migrations) do
migrations[i] = " * " .. message
local content = {}
for _, message in ipairs(migrations) do
vim.list_extend(content, vim.split("\n## " .. message, "\n", { trimempty = false }))
end
table.insert(
migrations,
1,
"# Neo-tree configuration has been updated. Please review the changes below."
)
local header = "# Neo-tree configuration has been updated. Please review the changes below."
table.insert(content, 1, header)
local buf = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, migrations)
vim.api.nvim_buf_set_lines(buf, 0, -1, false, content)
vim.api.nvim_buf_set_option(buf, "buftype", "nofile")
vim.api.nvim_buf_set_option(buf, "wrap", true)
vim.api.nvim_buf_set_option(buf, "bufhidden", "wipe")
vim.api.nvim_buf_set_option(buf, "buflisted", false)
vim.api.nvim_buf_set_option(buf, "swapfile", false)
vim.api.nvim_buf_set_option(buf, "modifiable", false)
vim.api.nvim_buf_set_option(buf, "filetype", "markdown")
vim.api.nvim_buf_set_name(buf, "Neo-tree migrations")
vim.defer_fn(function()
vim.cmd(string.format("%ssplit", #migrations))
vim.cmd(string.format("%ssplit", #content))
vim.api.nvim_win_set_buf(0, buf)
end, 100)
end
Expand Down Expand Up @@ -60,11 +59,12 @@ M.migrate = function(config)
end
end

local removed = function(key)
local removed = function(key, desc)
local value = utils.get_value(config, key)
if type(value) ~= "nil" then
utils.set_value(config, key, nil)
migrations[#migrations + 1] = string.format("The `%s` option has been removed.", key)
migrations[#migrations + 1] =
string.format("The `%s` option has been removed.\n%s", key, desc or "")
end
end

Expand Down Expand Up @@ -106,6 +106,30 @@ M.migrate = function(config)
-- v3.x
removed("close_floats_on_escape_key")

-- v4.x
removed(
"enable_normal_mode_for_inputs",
[[
Please use `neo_tree_popup_input_ready` event instead and call `stopinsert` inside the handler.
<https://github.com/nvim-neo-tree/neo-tree.nvim/pull/1372>
See instructions in `:h neo-tree-events` for more details.
```lua
event_handlers = {
{
event = "neo_tree_popup_input_ready",
---@param input NuiInput
handler = function(input)
-- enter input popup with normal mode by default.
vim.cmd("stopinsert")
end,
}
}
```
]]
)

return migrations
end

Expand Down
9 changes: 7 additions & 2 deletions lua/neo-tree/ui/inputs.lua
Expand Up @@ -2,6 +2,7 @@ local vim = vim
local Input = require("nui.input")
local popups = require("neo-tree.ui.popups")
local utils = require("neo-tree.utils")
local events = require("neo-tree.events")

local M = {}

Expand All @@ -14,9 +15,13 @@ M.show_input = function(input, callback)
local config = require("neo-tree").config
input:mount()

if config.enable_normal_mode_for_inputs and input.prompt_type ~= "confirm" then
if input.prompt_type ~= "confirm" then
vim.schedule(function()
vim.cmd("stopinsert")
-- deprecate this option in next version
if config.enable_normal_mode_for_inputs then
vim.cmd("stopinsert")
end
events.fire_event(events.NEO_TREE_POPUP_INPUT_READY)
end)
end

Expand Down

0 comments on commit bce4540

Please sign in to comment.