Skip to content

Commit

Permalink
feat: Make question/answer/error headers configurable (#236)
Browse files Browse the repository at this point in the history
Example of python version format config:
```
{
    question_header = '## User ',
    answer_header = '## Copilot ',
    error_header = '## Error ',
}
```

Closes #233

Signed-off-by: Tomas Slusny <[email protected]>
  • Loading branch information
deathbeam authored Mar 24, 2024
1 parent 83561e9 commit 066e605
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 19 deletions.
4 changes: 4 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ local select = require('CopilotChat.select')
chat.setup {
-- Restore the behaviour for CopilotChat to use unnamed register by default
selection = select.unnamed,
-- Restore the format with ## headers as prefixes,
question_header = '## User ',
answer_header = '## Copilot ',
error_header = '## Error ',
}

-- Restore CopilotChatVisual
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,11 @@ Also see [here](/lua/CopilotChat/config.lua):
model = 'gpt-4', -- GPT model to use, 'gpt-3.5-turbo' or 'gpt-4'
temperature = 0.1, -- GPT temperature

name = 'CopilotChat', -- Name to use in chat
question_header = '', -- Header to use for user questions
answer_header = '**Copilot** ', -- Header to use for AI answers
error_header = '**Error** ', -- Header to use for errors
separator = '---', -- Separator to use in chat

show_folds = true, -- Shows folds for sections in chat
show_help = true, -- Shows help message as virtual lines when waiting for user input
auto_follow_cursor = true, -- Auto-follow cursor in chat
Expand Down
3 changes: 2 additions & 1 deletion lua/CopilotChat/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,10 @@ function Chat:finish(msg)
else
msg = self.help
end
msg = vim.trim(msg)

if msg and msg ~= '' then
local line = vim.api.nvim_buf_line_count(self.bufnr) - 1
local line = vim.api.nvim_buf_line_count(self.bufnr) - 2
show_virt_line(msg, math.max(0, line - 1), self.bufnr, self.mark_ns)
end
end
Expand Down
9 changes: 7 additions & 2 deletions lua/CopilotChat/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ local select = require('CopilotChat.select')
---@field system_prompt string?
---@field model string?
---@field temperature number?
---@field name string?
---@field question_header string?
---@field answer_header string?
---@field error_header string?
---@field separator string?
---@field show_folds boolean?
---@field show_help boolean?
Expand All @@ -82,8 +84,11 @@ return {
model = 'gpt-4', -- GPT model to use, 'gpt-3.5-turbo' or 'gpt-4'
temperature = 0.1, -- GPT temperature

name = 'CopilotChat', -- Name to use in chat
question_header = '', -- Header to use for user questions
answer_header = '**Copilot** ', -- Header to use for AI answers
error_header = '**Error** ', -- Header to use for errors
separator = '---', -- Separator to use in chat

show_folds = true, -- Shows folds for sections in chat
show_help = true, -- Shows help message as virtual lines when waiting for user input
auto_follow_cursor = true, -- Auto-follow cursor in chat
Expand Down
28 changes: 13 additions & 15 deletions lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,11 @@ function M.ask(prompt, config, source)
end

if state.copilot:stop() then
append('\n\n' .. config.separator .. '\n\n')
append('\n\n' .. config.question_header .. config.separator .. '\n\n')
end

append(updated_prompt)
append('\n\n**' .. config.name .. '** ' .. config.separator .. '\n\n')
append('\n\n' .. config.answer_header .. config.separator .. '\n\n')
state.chat:follow()

local selected_context = config.context
Expand All @@ -373,9 +373,9 @@ function M.ask(prompt, config, source)

local function on_error(err)
vim.schedule(function()
append('\n\n**Error** ' .. config.separator .. '\n\n')
append('\n\n' .. config.error_header .. config.separator .. '\n\n')
append('```\n' .. err .. '\n```')
append('\n\n' .. config.separator .. '\n\n')
append('\n\n' .. config.question_header .. config.separator .. '\n\n')
state.chat:finish()
if M.config.auto_follow_cursor and M.config.auto_insert_mode and state.chat:active() then
vim.cmd('startinsert')
Expand Down Expand Up @@ -405,7 +405,7 @@ function M.ask(prompt, config, source)
on_error = on_error,
on_done = function(response, token_count)
vim.schedule(function()
append('\n\n' .. config.separator .. '\n\n')
append('\n\n' .. config.question_header .. config.separator .. '\n\n')
state.response = response
if tiktoken.available() and token_count and token_count > 0 then
state.chat:finish(token_count .. ' tokens used')
Expand Down Expand Up @@ -443,7 +443,7 @@ function M.reset(no_insert)

wrap(function()
state.chat:clear()
append('\n')
append(M.config.question_header .. M.config.separator .. '\n\n')
state.chat:finish()
state.chat:follow()

Expand Down Expand Up @@ -491,22 +491,20 @@ function M.load(name, history_path)
for i, message in ipairs(history) do
if message.role == 'user' then
if i > 1 then
append('\n\n' .. M.config.separator .. '\n\n')
else
append('\n')
append('\n\n')
end
append(M.config.question_header .. M.config.separator .. '\n\n')
append(message.content)
elseif message.role == 'assistant' then
append('\n\n**' .. M.config.name .. '** ' .. M.config.separator .. '\n\n')
append('\n\n' .. M.config.answer_header .. M.config.separator .. '\n\n')
append(message.content)
end
end

if #history == 0 then
append('\n')
else
append('\n\n' .. M.config.separator .. '\n')
if #history > 0 then
append('\n\n')
end
append(M.config.question_header .. M.config.separator .. '\n\n')

state.chat:finish()
M.open()
Expand Down Expand Up @@ -733,7 +731,7 @@ function M.setup(config)
end
end)

append('\n')
append(M.config.question_header .. M.config.separator .. '\n\n')
state.chat:finish()
end)

Expand Down

0 comments on commit 066e605

Please sign in to comment.