Skip to content

Commit

Permalink
...?
Browse files Browse the repository at this point in the history
  • Loading branch information
TwIStOy committed Apr 21, 2024
1 parent 3c42890 commit 8c79c8d
Showing 1 changed file with 60 additions and 26 deletions.
86 changes: 60 additions & 26 deletions lua/dotvim/extra/ui/copilot-chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ function M.toggle()
end

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

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

local renderer = n.create_renderer {
width = width,
height = height,
relative = "editor",
position = {
row = math.floor((win_height - height) / 2),
col = math.floor((win_width - width) / 2),
},
height = 3,
-- relative = "editor",
-- position = {
-- row = math.floor((win_height - height) / 2),
-- col = math.floor((win_width - width) / 2),
-- },
}

local signal = n.create_signal {
prompt = "",
is_preview_visible = false,
}

local buttons = function()
return n.columns {
n.button {
text = "Send",
on_click = function()
local prompt = signal:get("prompt")
if prompt == "" then
return
end

vim.api.nvim_command("CopilotChat send " .. prompt)
signal:set("prompt", "")
end,
},
n.button {
text = "Close",
on_click = function()
renderer:close()
local buf = vim.api.nvim_create_buf(false, true)

local body = function()
return n.rows(
n.text_input {
border_label = "Prompt",
autofocus = true,
wrap = true,
on_change = function(value)
signal.prompt = value
end,
},
}
n.buffer {
id = "preview",
flex = 1,
buf = buf,
autoscroll = true,
border_label = "Preview",
hidden = signal.is_preview_visible:negate(),
}
)
end

renderer:add_mappings {
{
mode = { "n", "i" },
key = "<D-CR>",
handler = function()
local chat = require("CopilotChat")
local state = signal:get_value()

renderer:set_size { height = 20 }
signal.is_preview_visible = true

renderer:schedule(function()
chat.ask("Show me something interesting", {
callback = function(response)
-- move cursor to the end of the buffer
local win_id = renderer:get_component_by_id("preview").winid
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
local col = #lines[#lines]
vim.api.nvim_win_set_cursor(
win_id,
{ vim.api.nvim_buf_line_count(buf), col }
)
vim.api.nvim_put({ response }, "l", false, true)
end,
})
end)
end,
},
}

M.renderer = renderer

renderer:render(body)
end

return M

0 comments on commit 8c79c8d

Please sign in to comment.