Skip to content

Commit 205677d

Browse files
committed
fix: display the initial value of the Prompt component correctly
1 parent b3e740a commit 205677d

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

lua/nui-components/prompt.lua

+31-8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local Text = require("nui.text")
22
local TextInput = require("nui-components.text-input")
33
local fn = require("nui-components.utils.fn")
44

5-
local Prompt = TextInput:extend("Gap")
5+
local Prompt = TextInput:extend("Prompt")
66

77
function Prompt:init(props, popup_options)
88
Prompt.super.init(
@@ -37,30 +37,38 @@ end
3737
function Prompt:_attach_change_listener()
3838
local props = self:get_props()
3939

40+
self._private.once = true
41+
4042
vim.api.nvim_buf_attach(self.bufnr, false, {
4143
on_lines = self:if_not_mounting_phase_wrap(function()
4244
local prefix_length = self._private.prefix:length()
43-
local value_with_prompt = vim.api.nvim_buf_get_lines(self.bufnr, 0, 1, false)[1]
45+
46+
local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)
47+
local value_with_prompt = table.concat(lines, "")
4448
local value = string.sub(value_with_prompt, prefix_length + 1)
4549

46-
self:set_current_value(value)
47-
props.on_change(value, self)
50+
if not self._private.once then
51+
self:set_current_value(value)
52+
props.on_change(value, self)
53+
end
4854

4955
self:_update_placeholder()
5056

51-
if prefix_length > 0 then
57+
if prefix_length > 0 and self._private.once then
5258
vim.schedule(function()
5359
self._private.prefix:highlight(self.bufnr, self.ns_id, 1, 0)
60+
self._private.once = false
5461
end)
5562
end
5663
end),
5764
})
5865
end
5966

60-
function Prompt:on_renderer_initialization(...)
61-
Prompt.super.on_renderer_initialization(self, ...)
62-
67+
function Prompt:on_mount()
6368
local props = self:get_props()
69+
70+
Prompt.super.on_mount(self)
71+
6472
local function is_nui_text(value)
6573
return value.class and value.class.name == "NuiText"
6674
end
@@ -69,6 +77,21 @@ function Prompt:on_renderer_initialization(...)
6977
vim.fn.prompt_setprompt(self.bufnr, self._private.prefix:content())
7078
end
7179

80+
function Prompt:on_update()
81+
local mode = vim.fn.mode()
82+
local current_winid = vim.api.nvim_get_current_win()
83+
84+
if not (current_winid == self.winid and mode == "i") then
85+
vim.schedule(function()
86+
if self:is_first_render() then
87+
local value = table.concat(self:get_lines(), "")
88+
vim.api.nvim_feedkeys(value, "n", true)
89+
vim.api.nvim_win_set_cursor(self.winid, { 1, #value })
90+
end
91+
end)
92+
end
93+
end
94+
7295
function Prompt:mappings()
7396
local props = self:get_props()
7497

0 commit comments

Comments
 (0)