diff --git a/README.md b/README.md index 04ad4941..077b326b 100644 --- a/README.md +++ b/README.md @@ -292,7 +292,7 @@ Also see [here](/lua/CopilotChat/config.lua): }, submit_prompt = { normal = '', - insert = '' + insert = '' }, accept_diff = { normal = '', diff --git a/lua/CopilotChat/chat.lua b/lua/CopilotChat/chat.lua index 469fcb04..ccdbcaf8 100644 --- a/lua/CopilotChat/chat.lua +++ b/lua/CopilotChat/chat.lua @@ -10,7 +10,7 @@ ---@field last fun(self: CopilotChat.Chat) ---@field clear fun(self: CopilotChat.Chat) ---@field open fun(self: CopilotChat.Chat, config: CopilotChat.config) ----@field close fun(self: CopilotChat.Chat) +---@field close fun(self: CopilotChat.Chat, bufnr: number?) ---@field focus fun(self: CopilotChat.Chat) ---@field follow fun(self: CopilotChat.Chat) ---@field finish fun(self: CopilotChat.Chat, msg: string?) @@ -40,6 +40,7 @@ local Chat = class(function(self, help, on_buf_create) self.winnr = nil self.spinner = nil self.separator = nil + self.layout = nil self.buf_create = function() local bufnr = vim.api.nvim_create_buf(false, true) @@ -192,6 +193,7 @@ function Chat:open(config) vim.api.nvim_win_set_buf(self.winnr, self.bufnr) end + self.layout = layout self.separator = config.separator vim.wo[self.winnr].wrap = true @@ -209,13 +211,17 @@ function Chat:open(config) self:render() end -function Chat:close() +function Chat:close(bufnr) if self.spinner then self.spinner:finish() end if self:visible() then - vim.api.nvim_win_close(self.winnr, true) + if self.layout == 'replace' then + self:restore(self.winnr, bufnr) + else + vim.api.nvim_win_close(self.winnr, true) + end self.winnr = nil end end diff --git a/lua/CopilotChat/context.lua b/lua/CopilotChat/context.lua index 65dff289..8b8a4016 100644 --- a/lua/CopilotChat/context.lua +++ b/lua/CopilotChat/context.lua @@ -248,6 +248,10 @@ function M.find_for_query(copilot, opts) on_error = on_error, on_done = function(query_out) local query = query_out[1] + if not query then + on_done({}) + return + end log.debug('Prompt:', query.prompt) log.debug('Content:', query.content) local data = data_ranked_by_relatedness(query, out, 20) diff --git a/lua/CopilotChat/copilot.lua b/lua/CopilotChat/copilot.lua index 2a954fee..01031f98 100644 --- a/lua/CopilotChat/copilot.lua +++ b/lua/CopilotChat/copilot.lua @@ -43,6 +43,7 @@ local temp_file = utils.temp_file local prompts = require('CopilotChat.prompts') local tiktoken = require('CopilotChat.tiktoken') local max_tokens = 8192 +local timeout = 30000 local version_headers = { ['editor-version'] = 'Neovim/' .. vim.version().major @@ -309,6 +310,7 @@ function Copilot:with_auth(on_done, on_error) end curl.get(url, { + timeout = timeout, headers = headers, proxy = self.proxy, insecure = self.allow_insecure, @@ -421,6 +423,7 @@ function Copilot:ask(prompt, opts) local headers = generate_headers(self.token.token, self.sessionid, self.machineid) self.current_job = curl .post(url, { + timeout = timeout, headers = headers, body = temp_file(body), proxy = self.proxy, @@ -505,15 +508,15 @@ end ---@param callback fun(table):nil function Copilot:select_model(callback) if self.models_cache ~= nil then - vim.schedule(function() - callback(self.models_cache) - end) + callback(self.models_cache) return end + local url = 'https://api.githubcopilot.com/models' self:with_auth(function() local headers = generate_headers(self.token.token, self.sessionid, self.machineid) curl.get(url, { + timeout = timeout, headers = headers, proxy = self.proxy, insecure = self.allow_insecure, @@ -545,9 +548,7 @@ function Copilot:select_model(callback) return false end, selections) self.models_cache = selections - vim.schedule(function() - callback(self.models_cache) - end) + callback(self.models_cache) end, }) end) @@ -584,6 +585,7 @@ function Copilot:embed(inputs, opts) table.insert(jobs, function(resolve) local headers = generate_headers(self.token.token, self.sessionid, self.machineid) curl.post(url, { + timeout = timeout, headers = headers, body = temp_file(body), proxy = self.proxy, diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index 0370f4bf..b7df7523 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -340,7 +340,7 @@ end --- Close the chat window. function M.close() vim.cmd('stopinsert') - state.chat:close() + state.chat:close(state.source and state.source.bufnr or nil) end --- Toggle the chat window. @@ -359,12 +359,15 @@ function M.response() return state.response end +--- Select a Copilot GPT model. function M.select_model() state.copilot:select_model(function(models) - vim.ui.select(models, { - prompt = 'Select a model', - }, function(choice) - M.config.model = choice + vim.schedule(function() + vim.ui.select(models, { + prompt = 'Select a model', + }, function(choice) + M.config.model = choice + end) end) end) end @@ -712,7 +715,7 @@ function M.setup(config) end if state.chat then - state.chat:close() + state.chat:close(state.source and state.source.bufnr or nil) state.chat:delete() end state.chat = Chat(chat_help, function(bufnr) diff --git a/lua/CopilotChat/overlay.lua b/lua/CopilotChat/overlay.lua index 804e0709..1c12b442 100644 --- a/lua/CopilotChat/overlay.lua +++ b/lua/CopilotChat/overlay.lua @@ -63,7 +63,7 @@ end function Overlay:restore(winnr, bufnr) self.current = nil - vim.api.nvim_win_set_buf(winnr, bufnr) + vim.api.nvim_win_set_buf(winnr, bufnr or 0) vim.api.nvim_win_set_hl_ns(winnr, 0) end