@@ -2,7 +2,7 @@ local Text = require("nui.text")
2
2
local TextInput = require (" nui-components.text-input" )
3
3
local fn = require (" nui-components.utils.fn" )
4
4
5
- local Prompt = TextInput :extend (" Gap " )
5
+ local Prompt = TextInput :extend (" Prompt " )
6
6
7
7
function Prompt :init (props , popup_options )
8
8
Prompt .super .init (
37
37
function Prompt :_attach_change_listener ()
38
38
local props = self :get_props ()
39
39
40
+ self ._private .once = true
41
+
40
42
vim .api .nvim_buf_attach (self .bufnr , false , {
41
43
on_lines = self :if_not_mounting_phase_wrap (function ()
42
44
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 , " " )
44
48
local value = string.sub (value_with_prompt , prefix_length + 1 )
45
49
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
48
54
49
55
self :_update_placeholder ()
50
56
51
- if prefix_length > 0 then
57
+ if prefix_length > 0 and self . _private . once then
52
58
vim .schedule (function ()
53
59
self ._private .prefix :highlight (self .bufnr , self .ns_id , 1 , 0 )
60
+ self ._private .once = false
54
61
end )
55
62
end
56
63
end ),
57
64
})
58
65
end
59
66
60
- function Prompt :on_renderer_initialization (...)
61
- Prompt .super .on_renderer_initialization (self , ... )
62
-
67
+ function Prompt :on_mount ()
63
68
local props = self :get_props ()
69
+
70
+ Prompt .super .on_mount (self )
71
+
64
72
local function is_nui_text (value )
65
73
return value .class and value .class .name == " NuiText"
66
74
end
@@ -69,6 +77,21 @@ function Prompt:on_renderer_initialization(...)
69
77
vim .fn .prompt_setprompt (self .bufnr , self ._private .prefix :content ())
70
78
end
71
79
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
+
72
95
function Prompt :mappings ()
73
96
local props = self :get_props ()
74
97
0 commit comments