Skip to content

Commit bce4540

Browse files
authored
feat(nui): allow callbacks in nui input with option (#1372)
1 parent dcb63ab commit bce4540

File tree

4 files changed

+58
-13
lines changed

4 files changed

+58
-13
lines changed

doc/neo-tree.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,6 +1431,21 @@ have.
14311431
"neo_tree_popup_buffer_leave"~
14321432
Fired after leaving a neo-tree popup buffer.
14331433

1434+
"neo_tree_popup_input_ready"~
1435+
Fired after NuiInput is ready. Use this event to default to normal mode etc.
1436+
This is fired inside a vim.schedule.
1437+
1438+
>lua
1439+
{
1440+
event = "neo_tree_popup_input_ready",
1441+
---@param input NuiInput
1442+
handler = function(input)
1443+
-- enter input popup with normal mode by default.
1444+
vim.cmd("stopinsert")
1445+
end,
1446+
}
1447+
<
1448+
14341449
"neo_tree_window_before_open"~
14351450
Fired before opening a new Neo-tree window. Called with the following arg:
14361451
*neo-tree-window-event-args*

lua/neo-tree/events/init.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ local M = {
2424
NEO_TREE_LSP_UPDATE = "neo_tree_lsp_update",
2525
NEO_TREE_POPUP_BUFFER_ENTER = "neo_tree_popup_buffer_enter",
2626
NEO_TREE_POPUP_BUFFER_LEAVE = "neo_tree_popup_buffer_leave",
27+
NEO_TREE_POPUP_INPUT_READY = "neo_tree_popup_input_ready",
2728
NEO_TREE_WINDOW_AFTER_CLOSE = "neo_tree_window_after_close",
2829
NEO_TREE_WINDOW_AFTER_OPEN = "neo_tree_window_after_open",
2930
NEO_TREE_WINDOW_BEFORE_CLOSE = "neo_tree_window_before_close",

lua/neo-tree/setup/deprecations.lua

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,24 @@ local migrations = {}
66

77
M.show_migrations = function()
88
if #migrations > 0 then
9-
for i, message in ipairs(migrations) do
10-
migrations[i] = " * " .. message
9+
local content = {}
10+
for _, message in ipairs(migrations) do
11+
vim.list_extend(content, vim.split("\n## " .. message, "\n", { trimempty = false }))
1112
end
12-
table.insert(
13-
migrations,
14-
1,
15-
"# Neo-tree configuration has been updated. Please review the changes below."
16-
)
13+
local header = "# Neo-tree configuration has been updated. Please review the changes below."
14+
table.insert(content, 1, header)
1715
local buf = vim.api.nvim_create_buf(false, true)
18-
vim.api.nvim_buf_set_lines(buf, 0, -1, false, migrations)
16+
vim.api.nvim_buf_set_lines(buf, 0, -1, false, content)
1917
vim.api.nvim_buf_set_option(buf, "buftype", "nofile")
18+
vim.api.nvim_buf_set_option(buf, "wrap", true)
2019
vim.api.nvim_buf_set_option(buf, "bufhidden", "wipe")
2120
vim.api.nvim_buf_set_option(buf, "buflisted", false)
2221
vim.api.nvim_buf_set_option(buf, "swapfile", false)
2322
vim.api.nvim_buf_set_option(buf, "modifiable", false)
2423
vim.api.nvim_buf_set_option(buf, "filetype", "markdown")
2524
vim.api.nvim_buf_set_name(buf, "Neo-tree migrations")
2625
vim.defer_fn(function()
27-
vim.cmd(string.format("%ssplit", #migrations))
26+
vim.cmd(string.format("%ssplit", #content))
2827
vim.api.nvim_win_set_buf(0, buf)
2928
end, 100)
3029
end
@@ -60,11 +59,12 @@ M.migrate = function(config)
6059
end
6160
end
6261

63-
local removed = function(key)
62+
local removed = function(key, desc)
6463
local value = utils.get_value(config, key)
6564
if type(value) ~= "nil" then
6665
utils.set_value(config, key, nil)
67-
migrations[#migrations + 1] = string.format("The `%s` option has been removed.", key)
66+
migrations[#migrations + 1] =
67+
string.format("The `%s` option has been removed.\n%s", key, desc or "")
6868
end
6969
end
7070

@@ -106,6 +106,30 @@ M.migrate = function(config)
106106
-- v3.x
107107
removed("close_floats_on_escape_key")
108108

109+
-- v4.x
110+
removed(
111+
"enable_normal_mode_for_inputs",
112+
[[
113+
Please use `neo_tree_popup_input_ready` event instead and call `stopinsert` inside the handler.
114+
<https://github.com/nvim-neo-tree/neo-tree.nvim/pull/1372>
115+
116+
See instructions in `:h neo-tree-events` for more details.
117+
118+
```lua
119+
event_handlers = {
120+
{
121+
event = "neo_tree_popup_input_ready",
122+
---@param input NuiInput
123+
handler = function(input)
124+
-- enter input popup with normal mode by default.
125+
vim.cmd("stopinsert")
126+
end,
127+
}
128+
}
129+
```
130+
]]
131+
)
132+
109133
return migrations
110134
end
111135

lua/neo-tree/ui/inputs.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local vim = vim
22
local Input = require("nui.input")
33
local popups = require("neo-tree.ui.popups")
44
local utils = require("neo-tree.utils")
5+
local events = require("neo-tree.events")
56

67
local M = {}
78

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

17-
if config.enable_normal_mode_for_inputs and input.prompt_type ~= "confirm" then
18+
if input.prompt_type ~= "confirm" then
1819
vim.schedule(function()
19-
vim.cmd("stopinsert")
20+
-- deprecate this option in next version
21+
if config.enable_normal_mode_for_inputs then
22+
vim.cmd("stopinsert")
23+
end
24+
events.fire_event(events.NEO_TREE_POPUP_INPUT_READY)
2025
end)
2126
end
2227

0 commit comments

Comments
 (0)