Skip to content

Commit

Permalink
fix: Handle layout replace closing, handle empty embed results
Browse files Browse the repository at this point in the history
Also update documentation for default accept binding and general small cleanup.

Closes #359
Closes #350
Closes #357
Closes #309

Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
  • Loading branch information
deathbeam committed Jul 17, 2024
1 parent 27c7be5 commit a6d74d1
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ Also see [here](/lua/CopilotChat/config.lua):
},
submit_prompt = {
normal = '<CR>',
insert = '<C-m>'
insert = '<C-s>'
},
accept_diff = {
normal = '<C-y>',
Expand Down
12 changes: 9 additions & 3 deletions lua/CopilotChat/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lua/CopilotChat/context.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions lua/CopilotChat/copilot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
15 changes: 9 additions & 6 deletions lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lua/CopilotChat/overlay.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a6d74d1

Please sign in to comment.