From 9f90464b2a7dfce9b0e7a02ce88465f5167a201b Mon Sep 17 00:00:00 2001 From: dqnk <64268180+dqnk@users.noreply.github.com> Date: Sun, 19 May 2024 09:34:47 +0200 Subject: [PATCH] visual selection in chat --- lua/chatgpt/flows/chat/base.lua | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/lua/chatgpt/flows/chat/base.lua b/lua/chatgpt/flows/chat/base.lua index f37e1386..438bace6 100644 --- a/lua/chatgpt/flows/chat/base.lua +++ b/lua/chatgpt/flows/chat/base.lua @@ -30,6 +30,8 @@ function Chat:init() -- quit indicator self.active = true + self.editor_bufnr = nil + -- UI ELEMENTS self.layout = nil self.chat_panel = nil @@ -736,6 +738,13 @@ function Chat:get_layout_params() end function Chat:open() + local editor_bufnr = vim.api.nvim_get_current_buf() + self.editor_bufnr = editor_bufnr + local visual_lines = {} + -- if in visual, visual line, visual block modes + if vim.fn.mode() == "v" or vim.fn.mode() == "V" or vim.fn.mode() == "\22" then + visual_lines, _, _, _, _ = Utils.get_visual_lines(editor_bufnr) + end self.settings_panel = Settings.get_settings_panel("chat_completions", self.params) self.help_panel = Help.get_help_panel("chat") self.sessions_panel = Sessions.get_panel(function(session) @@ -1017,6 +1026,8 @@ function Chat:open() self.chat_input:on(event.QuitPre, function() self.active = false end) + + vim.api.nvim_buf_set_lines(self.chat_input.bufnr, -1, -1, false, visual_lines) end function Chat:open_system_panel() @@ -1038,6 +1049,15 @@ function Chat:hide() end function Chat:show() + local editor_bufnr = vim.api.nvim_get_current_buf() + self.editor_bufnr = editor_bufnr + local visual_lines = {} + -- if in visual, visual line, visual block modes + if vim.fn.mode() == "v" or vim.fn.mode() == "V" or vim.fn.mode() == "\22" then + visual_lines, _, _, _, _ = Utils.get_visual_lines(editor_bufnr) + end + vim.api.nvim_buf_set_lines(self.chat_input.bufnr, -1, -1, false, visual_lines) + self:redraw(true) self.layout:show() end