Skip to content

Commit 1dfc47e

Browse files
committed
refactor(config): reorganize shared/static config fields
- Moved UI-related config fields to shared section - Updated type definitions and documentation - Fixed selection highlight to respect config - Improved token count display formatting Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent ef5123e commit 1dfc47e

4 files changed

Lines changed: 59 additions & 45 deletions

File tree

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ Also see [here](/lua/CopilotChat/config.lua):
421421
```lua
422422
{
423423

424-
-- Shared config starts here (can be passed to functions at runtime)
424+
-- Shared config starts here (can be passed to functions at runtime and configured via setup function)
425425

426426
system_prompt = prompts.COPILOT_INSTRUCTIONS, -- System prompt to use (can be specified manually in prompt via /).
427427
model = 'gpt-4o', -- Default model to use, see ':CopilotChatModels' for available models (can be specified manually in prompt via $).
@@ -452,30 +452,30 @@ Also see [here](/lua/CopilotChat/config.lua):
452452
zindex = 1, -- determines if window is on top or below other floating windows
453453
},
454454

455+
show_help = true, -- Shows help message as virtual lines when waiting for user input
456+
show_folds = true, -- Shows folds for sections in chat
457+
highlight_selection = true, -- Highlight selection
458+
highlight_headers = true, -- Highlight headers in chat, disable if using markdown renderers (like render-markdown.nvim)
459+
auto_follow_cursor = true, -- Auto-follow cursor in chat
460+
auto_insert_mode = false, -- Automatically enter insert mode when opening window and on new prompt
461+
insert_at_end = false, -- Move cursor to end of buffer when inserting text
462+
clear_chat_on_new_prompt = false, -- Clears chat on every new prompt
463+
455464
-- Static config starts here (can be configured only via setup function)
456465

457466
debug = false, -- Enable debug logging (same as 'log_level = 'debug')
458467
log_level = 'info', -- Log level to use, 'trace', 'debug', 'info', 'warn', 'error', 'fatal'
459468
proxy = nil, -- [protocol://]host[:port] Use this proxy
460469
allow_insecure = false, -- Allow insecure server connections
470+
471+
chat_autocomplete = true, -- Enable chat autocompletion (when disabled, requires manual `mappings.complete` trigger)
461472
history_path = vim.fn.stdpath('data') .. '/copilotchat_history', -- Default path to stored history
462473

463474
question_header = '## User ', -- Header to use for user questions
464475
answer_header = '## Copilot ', -- Header to use for AI answers
465476
error_header = '## Error ', -- Header to use for errors
466477
separator = '───', -- Separator to use in chat
467478

468-
show_folds = true, -- Shows folds for sections in chat
469-
show_help = true, -- Shows help message as virtual lines when waiting for user input
470-
highlight_selection = true, -- Highlight selection
471-
highlight_headers = true, -- Highlight headers in chat, disable if using markdown renderers (like render-markdown.nvim)
472-
473-
chat_autocomplete = true, -- Enable chat autocompletion (when disabled, requires manual `mappings.complete` trigger)
474-
auto_follow_cursor = true, -- Auto-follow cursor in chat
475-
auto_insert_mode = false, -- Automatically enter insert mode when opening window and on new prompt
476-
insert_at_end = false, -- Move cursor to end of buffer when inserting text
477-
clear_chat_on_new_prompt = false, -- Clears chat on every new prompt
478-
479479
-- default contexts
480480
contexts = {
481481
buffer = {

lua/CopilotChat/config.lua

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,33 @@ local utils = require('CopilotChat.utils')
7878
---@field callback fun(response: string, source: CopilotChat.config.source)?
7979
---@field selection nil|fun(source: CopilotChat.config.source):CopilotChat.config.selection?
8080
---@field window CopilotChat.config.window?
81+
---@field show_help boolean?
82+
---@field show_folds boolean?
83+
---@field highlight_selection boolean?
84+
---@field highlight_headers boolean?
85+
---@field auto_follow_cursor boolean?
86+
---@field auto_insert_mode boolean?
87+
---@field insert_at_end boolean?
88+
---@field clear_chat_on_new_prompt boolean?
8189

8290
--- CopilotChat default configuration
8391
---@class CopilotChat.config : CopilotChat.config.shared
8492
---@field debug boolean?
8593
---@field log_level string?
8694
---@field proxy string?
8795
---@field allow_insecure boolean?
96+
---@field chat_autocomplete boolean?
8897
---@field history_path string?
8998
---@field question_header string?
9099
---@field answer_header string?
91100
---@field error_header string?
92101
---@field separator string?
93-
---@field show_folds boolean?
94-
---@field show_help boolean?
95-
---@field highlight_selection boolean?
96-
---@field highlight_headers boolean?
97-
---@field chat_autocomplete boolean?
98-
---@field auto_follow_cursor boolean?
99-
---@field auto_insert_mode boolean?
100-
---@field insert_at_end boolean?
101-
---@field clear_chat_on_new_prompt boolean?
102102
---@field contexts table<string, CopilotChat.config.context>?
103103
---@field prompts table<string, CopilotChat.config.prompt|string>?
104104
---@field mappings CopilotChat.config.mappings?
105105
return {
106106

107-
-- Shared config starts here (can be passed to functions at runtime)
107+
-- Shared config starts here (can be passed to functions at runtime and configured via setup function)
108108

109109
system_prompt = prompts.COPILOT_INSTRUCTIONS, -- System prompt to use (can be specified manually in prompt via /).
110110
model = 'gpt-4o', -- Default model to use, see ':CopilotChatModels' for available models (can be specified manually in prompt via $).
@@ -135,30 +135,30 @@ return {
135135
zindex = 1, -- determines if window is on top or below other floating windows
136136
},
137137

138+
show_help = true, -- Shows help message as virtual lines when waiting for user input
139+
show_folds = true, -- Shows folds for sections in chat
140+
highlight_selection = true, -- Highlight selection
141+
highlight_headers = true, -- Highlight headers in chat, disable if using markdown renderers (like render-markdown.nvim)
142+
auto_follow_cursor = true, -- Auto-follow cursor in chat
143+
auto_insert_mode = false, -- Automatically enter insert mode when opening window and on new prompt
144+
insert_at_end = false, -- Move cursor to end of buffer when inserting text
145+
clear_chat_on_new_prompt = false, -- Clears chat on every new prompt
146+
138147
-- Static config starts here (can be configured only via setup function)
139148

140149
debug = false, -- Enable debug logging (same as 'log_level = 'debug')
141150
log_level = 'info', -- Log level to use, 'trace', 'debug', 'info', 'warn', 'error', 'fatal'
142151
proxy = nil, -- [protocol://]host[:port] Use this proxy
143152
allow_insecure = false, -- Allow insecure server connections
153+
154+
chat_autocomplete = true, -- Enable chat autocompletion (when disabled, requires manual `mappings.complete` trigger)
144155
history_path = vim.fn.stdpath('data') .. '/copilotchat_history', -- Default path to stored history
145156

146157
question_header = '## User ', -- Header to use for user questions
147158
answer_header = '## Copilot ', -- Header to use for AI answers
148159
error_header = '## Error ', -- Header to use for errors
149160
separator = '───', -- Separator to use in chat
150161

151-
show_folds = true, -- Shows folds for sections in chat
152-
show_help = true, -- Shows help message as virtual lines when waiting for user input
153-
highlight_selection = true, -- Highlight selection
154-
highlight_headers = true, -- Highlight headers in chat, disable if using markdown renderers (like render-markdown.nvim)
155-
156-
chat_autocomplete = true, -- Enable chat autocompletion (when disabled, requires manual `mappings.complete` trigger)
157-
auto_follow_cursor = true, -- Auto-follow cursor in chat
158-
auto_insert_mode = false, -- Automatically enter insert mode when opening window and on new prompt
159-
insert_at_end = false, -- Move cursor to end of buffer when inserting text
160-
clear_chat_on_new_prompt = false, -- Clears chat on every new prompt
161-
162162
-- default contexts
163163
contexts = {
164164
buffer = {

lua/CopilotChat/init.lua

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ local function highlight_selection(clear, config)
6969
vim.api.nvim_buf_clear_namespace(buf, selection_ns, 0, -1)
7070
end
7171

72-
if clear then
72+
if clear or not config.highlight_selection then
7373
return
7474
end
7575

@@ -948,7 +948,10 @@ function M.setup(config)
948948
state.chat:delete()
949949
end
950950
state.chat = Chat(
951-
M.config.show_help and key_to_info('show_help', M.config.mappings.show_help),
951+
M.config.question_header,
952+
M.config.answer_header,
953+
M.config.separator,
954+
key_to_info('show_help', M.config.mappings.show_help),
952955
function(bufnr)
953956
map_key(M.config.mappings.show_help, bufnr, function()
954957
local chat_help = '**`Special tokens`**\n'

lua/CopilotChat/ui/chat.lua

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,24 @@ end
5757
---@field content string?
5858

5959
---@class CopilotChat.ui.Chat : CopilotChat.ui.Overlay
60+
---@field question_header string
61+
---@field answer_header string
62+
---@field separator string
6063
---@field header_ns number
6164
---@field winnr number?
6265
---@field spinner CopilotChat.ui.Spinner
6366
---@field sections table<CopilotChat.ui.Chat.Section>
64-
---@field config CopilotChat.config
67+
---@field config CopilotChat.config.shared
6568
---@field token_count number?
6669
---@field token_max_count number?
67-
local Chat = class(function(self, help, on_buf_create)
70+
local Chat = class(function(self, question_header, answer_header, separator, help, on_buf_create)
6871
Overlay.init(self, 'copilot-chat', help, on_buf_create)
6972
vim.treesitter.language.register('markdown', self.name)
7073

74+
self.question_header = question_header
75+
self.answer_header = answer_header
76+
self.separator = separator
77+
7178
self.header_ns = vim.api.nvim_create_namespace('copilot-chat-headers')
7279
self.winnr = nil
7380
self.spinner = nil
@@ -134,7 +141,7 @@ function Chat:render()
134141
for l, line in ipairs(lines) do
135142
local separator_found = false
136143

137-
if line == self.config.answer_header .. self.config.separator then
144+
if line == self.answer_header .. self.separator then
138145
separator_found = true
139146
if current_section then
140147
current_section.end_line = l - 1
@@ -145,7 +152,7 @@ function Chat:render()
145152
start_line = l + 1,
146153
blocks = {},
147154
}
148-
elseif line == self.config.question_header .. self.config.separator then
155+
elseif line == self.question_header .. self.separator then
149156
separator_found = true
150157
if current_section then
151158
current_section.end_line = l - 1
@@ -165,12 +172,12 @@ function Chat:render()
165172

166173
-- Highlight separators
167174
if self.config.highlight_headers and separator_found then
168-
local sep = vim.fn.strwidth(line) - vim.fn.strwidth(self.config.separator)
175+
local sep = vim.fn.strwidth(line) - vim.fn.strwidth(self.separator)
169176
-- separator line
170177
vim.api.nvim_buf_set_extmark(self.bufnr, self.header_ns, l - 1, sep, {
171178
virt_text_win_col = sep,
172179
virt_text = {
173-
{ string.rep(self.config.separator, vim.go.columns), 'CopilotChatSeparator' },
180+
{ string.rep(self.separator, vim.go.columns), 'CopilotChatSeparator' },
174181
},
175182
priority = 100,
176183
strict = false,
@@ -213,10 +220,14 @@ function Chat:render()
213220

214221
local last_section = sections[#sections]
215222
if last_section and not last_section.answer then
216-
local msg = self.help
223+
local msg = self.config.show_help and self.help or ''
217224
if self.token_count and self.token_max_count then
218-
msg = msg .. '\n' .. self.token_count .. '/' .. self.token_max_count .. ' tokens used'
225+
if msg ~= '' then
226+
msg = msg .. '\n'
227+
end
228+
msg = msg .. self.token_count .. '/' .. self.token_max_count .. ' tokens used'
219229
end
230+
220231
self:show_help(msg, last_section.start_line - last_section.end_line - 1)
221232
else
222233
self:clear_help()
@@ -387,7 +398,7 @@ function Chat:clear()
387398
vim.bo[self.bufnr].modifiable = false
388399
end
389400

390-
---@param config CopilotChat.config
401+
---@param config CopilotChat.config.shared
391402
function Chat:open(config)
392403
self:validate()
393404
self.config = config
@@ -454,7 +465,7 @@ function Chat:open(config)
454465
if config.show_folds then
455466
vim.wo[self.winnr].foldcolumn = '1'
456467
vim.wo[self.winnr].foldmethod = 'expr'
457-
vim.wo[self.winnr].foldexpr = "v:lua.CopilotChatFoldExpr(v:lnum, '" .. config.separator .. "')"
468+
vim.wo[self.winnr].foldexpr = "v:lua.CopilotChatFoldExpr(v:lnum, '" .. self.separator .. "')"
458469
else
459470
vim.wo[self.winnr].foldcolumn = '0'
460471
end

0 commit comments

Comments
 (0)