Skip to content

Commit 8c79c8d

Browse files
committed
...?
1 parent 3c42890 commit 8c79c8d

File tree

1 file changed

+60
-26
lines changed

1 file changed

+60
-26
lines changed

lua/dotvim/extra/ui/copilot-chat.lua

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ function M.toggle()
77
end
88

99
local n = require("nui-components")
10-
local chat = require("CopilotChat")
1110

1211
local win_width = vim.o.columns
1312
local win_height = vim.o.lines
@@ -17,40 +16,75 @@ function M.toggle()
1716

1817
local renderer = n.create_renderer {
1918
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+
-- },
2625
}
2726

2827
local signal = n.create_signal {
2928
prompt = "",
29+
is_preview_visible = false,
3030
}
3131

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
5042
end,
5143
},
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+
)
5353
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)
5488
end
5589

5690
return M

0 commit comments

Comments
 (0)