@@ -7,7 +7,6 @@ function M.toggle()
7
7
end
8
8
9
9
local n = require (" nui-components" )
10
- local chat = require (" CopilotChat" )
11
10
12
11
local win_width = vim .o .columns
13
12
local win_height = vim .o .lines
@@ -17,40 +16,75 @@ function M.toggle()
17
16
18
17
local renderer = n .create_renderer {
19
18
width = width ,
20
- height = height ,
21
- relative = " editor" ,
22
- position = {
23
- row = math.floor ((win_height - height ) / 2 ),
24
- col = math.floor ((win_width - width ) / 2 ),
25
- },
19
+ height = 3 ,
20
+ -- relative = "editor",
21
+ -- position = {
22
+ -- row = math.floor((win_height - height) / 2),
23
+ -- col = math.floor((win_width - width) / 2),
24
+ -- },
26
25
}
27
26
28
27
local signal = n .create_signal {
29
28
prompt = " " ,
29
+ is_preview_visible = false ,
30
30
}
31
31
32
- local buttons = function ()
33
- return n .columns {
34
- n .button {
35
- text = " Send" ,
36
- on_click = function ()
37
- local prompt = signal :get (" prompt" )
38
- if prompt == " " then
39
- return
40
- end
41
-
42
- vim .api .nvim_command (" CopilotChat send " .. prompt )
43
- signal :set (" prompt" , " " )
44
- end ,
45
- },
46
- n .button {
47
- text = " Close" ,
48
- on_click = function ()
49
- renderer :close ()
32
+ local buf = vim .api .nvim_create_buf (false , true )
33
+
34
+ local body = function ()
35
+ return n .rows (
36
+ n .text_input {
37
+ border_label = " Prompt" ,
38
+ autofocus = true ,
39
+ wrap = true ,
40
+ on_change = function (value )
41
+ signal .prompt = value
50
42
end ,
51
43
},
52
- }
44
+ n .buffer {
45
+ id = " preview" ,
46
+ flex = 1 ,
47
+ buf = buf ,
48
+ autoscroll = true ,
49
+ border_label = " Preview" ,
50
+ hidden = signal .is_preview_visible :negate (),
51
+ }
52
+ )
53
53
end
54
+
55
+ renderer :add_mappings {
56
+ {
57
+ mode = { " n" , " i" },
58
+ key = " <D-CR>" ,
59
+ handler = function ()
60
+ local chat = require (" CopilotChat" )
61
+ local state = signal :get_value ()
62
+
63
+ renderer :set_size { height = 20 }
64
+ signal .is_preview_visible = true
65
+
66
+ renderer :schedule (function ()
67
+ chat .ask (" Show me something interesting" , {
68
+ callback = function (response )
69
+ -- move cursor to the end of the buffer
70
+ local win_id = renderer :get_component_by_id (" preview" ).winid
71
+ local lines = vim .api .nvim_buf_get_lines (buf , 0 , - 1 , false )
72
+ local col = # lines [# lines ]
73
+ vim .api .nvim_win_set_cursor (
74
+ win_id ,
75
+ { vim .api .nvim_buf_line_count (buf ), col }
76
+ )
77
+ vim .api .nvim_put ({ response }, " l" , false , true )
78
+ end ,
79
+ })
80
+ end )
81
+ end ,
82
+ },
83
+ }
84
+
85
+ M .renderer = renderer
86
+
87
+ renderer :render (body )
54
88
end
55
89
56
90
return M
0 commit comments