Skip to content

Commit 066e605

Browse files
authored
feat: Make question/answer/error headers configurable (#236)
Example of python version format config: ``` { question_header = '## User ', answer_header = '## Copilot ', error_header = '## Error ', } ``` Closes #233 Signed-off-by: Tomas Slusny <[email protected]>
1 parent 83561e9 commit 066e605

File tree

5 files changed

+30
-19
lines changed

5 files changed

+30
-19
lines changed

MIGRATION.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ local select = require('CopilotChat.select')
5555
chat.setup {
5656
-- Restore the behaviour for CopilotChat to use unnamed register by default
5757
selection = select.unnamed,
58+
-- Restore the format with ## headers as prefixes,
59+
question_header = '## User ',
60+
answer_header = '## Copilot ',
61+
error_header = '## Error ',
5862
}
5963

6064
-- Restore CopilotChatVisual

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,11 @@ Also see [here](/lua/CopilotChat/config.lua):
198198
model = 'gpt-4', -- GPT model to use, 'gpt-3.5-turbo' or 'gpt-4'
199199
temperature = 0.1, -- GPT temperature
200200

201-
name = 'CopilotChat', -- Name to use in chat
201+
question_header = '', -- Header to use for user questions
202+
answer_header = '**Copilot** ', -- Header to use for AI answers
203+
error_header = '**Error** ', -- Header to use for errors
202204
separator = '---', -- Separator to use in chat
205+
203206
show_folds = true, -- Shows folds for sections in chat
204207
show_help = true, -- Shows help message as virtual lines when waiting for user input
205208
auto_follow_cursor = true, -- Auto-follow cursor in chat

lua/CopilotChat/chat.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,10 @@ function Chat:finish(msg)
215215
else
216216
msg = self.help
217217
end
218+
msg = vim.trim(msg)
218219

219220
if msg and msg ~= '' then
220-
local line = vim.api.nvim_buf_line_count(self.bufnr) - 1
221+
local line = vim.api.nvim_buf_line_count(self.bufnr) - 2
221222
show_virt_line(msg, math.max(0, line - 1), self.bufnr, self.mark_ns)
222223
end
223224
end

lua/CopilotChat/config.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ local select = require('CopilotChat.select')
5959
---@field system_prompt string?
6060
---@field model string?
6161
---@field temperature number?
62-
---@field name string?
62+
---@field question_header string?
63+
---@field answer_header string?
64+
---@field error_header string?
6365
---@field separator string?
6466
---@field show_folds boolean?
6567
---@field show_help boolean?
@@ -82,8 +84,11 @@ return {
8284
model = 'gpt-4', -- GPT model to use, 'gpt-3.5-turbo' or 'gpt-4'
8385
temperature = 0.1, -- GPT temperature
8486

85-
name = 'CopilotChat', -- Name to use in chat
87+
question_header = '', -- Header to use for user questions
88+
answer_header = '**Copilot** ', -- Header to use for AI answers
89+
error_header = '**Error** ', -- Header to use for errors
8690
separator = '---', -- Separator to use in chat
91+
8792
show_folds = true, -- Shows folds for sections in chat
8893
show_help = true, -- Shows help message as virtual lines when waiting for user input
8994
auto_follow_cursor = true, -- Auto-follow cursor in chat

lua/CopilotChat/init.lua

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -356,11 +356,11 @@ function M.ask(prompt, config, source)
356356
end
357357

358358
if state.copilot:stop() then
359-
append('\n\n' .. config.separator .. '\n\n')
359+
append('\n\n' .. config.question_header .. config.separator .. '\n\n')
360360
end
361361

362362
append(updated_prompt)
363-
append('\n\n**' .. config.name .. '** ' .. config.separator .. '\n\n')
363+
append('\n\n' .. config.answer_header .. config.separator .. '\n\n')
364364
state.chat:follow()
365365

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

374374
local function on_error(err)
375375
vim.schedule(function()
376-
append('\n\n**Error** ' .. config.separator .. '\n\n')
376+
append('\n\n' .. config.error_header .. config.separator .. '\n\n')
377377
append('```\n' .. err .. '\n```')
378-
append('\n\n' .. config.separator .. '\n\n')
378+
append('\n\n' .. config.question_header .. config.separator .. '\n\n')
379379
state.chat:finish()
380380
if M.config.auto_follow_cursor and M.config.auto_insert_mode and state.chat:active() then
381381
vim.cmd('startinsert')
@@ -405,7 +405,7 @@ function M.ask(prompt, config, source)
405405
on_error = on_error,
406406
on_done = function(response, token_count)
407407
vim.schedule(function()
408-
append('\n\n' .. config.separator .. '\n\n')
408+
append('\n\n' .. config.question_header .. config.separator .. '\n\n')
409409
state.response = response
410410
if tiktoken.available() and token_count and token_count > 0 then
411411
state.chat:finish(token_count .. ' tokens used')
@@ -443,7 +443,7 @@ function M.reset(no_insert)
443443

444444
wrap(function()
445445
state.chat:clear()
446-
append('\n')
446+
append(M.config.question_header .. M.config.separator .. '\n\n')
447447
state.chat:finish()
448448
state.chat:follow()
449449

@@ -491,22 +491,20 @@ function M.load(name, history_path)
491491
for i, message in ipairs(history) do
492492
if message.role == 'user' then
493493
if i > 1 then
494-
append('\n\n' .. M.config.separator .. '\n\n')
495-
else
496-
append('\n')
494+
append('\n\n')
497495
end
496+
append(M.config.question_header .. M.config.separator .. '\n\n')
498497
append(message.content)
499498
elseif message.role == 'assistant' then
500-
append('\n\n**' .. M.config.name .. '** ' .. M.config.separator .. '\n\n')
499+
append('\n\n' .. M.config.answer_header .. M.config.separator .. '\n\n')
501500
append(message.content)
502501
end
503502
end
504503

505-
if #history == 0 then
506-
append('\n')
507-
else
508-
append('\n\n' .. M.config.separator .. '\n')
504+
if #history > 0 then
505+
append('\n\n')
509506
end
507+
append(M.config.question_header .. M.config.separator .. '\n\n')
510508

511509
state.chat:finish()
512510
M.open()
@@ -733,7 +731,7 @@ function M.setup(config)
733731
end
734732
end)
735733

736-
append('\n')
734+
append(M.config.question_header .. M.config.separator .. '\n\n')
737735
state.chat:finish()
738736
end)
739737

0 commit comments

Comments
 (0)