Skip to content

Commit b054b9f

Browse files
committed
refactor: unify info display into show_info and show_context
Consolidates multiple info display commands (show_system_prompt, show_user_selection, show_user_context) into two more comprehensive commands: - show_info (gi): displays model, agent and system prompt - show_context (gc): shows selection and embeddings This change improves UX by providing more organized and complete information when inspecting chat context and configuration. BREAKING CHANGE: Removed show_system_prompt (gp), show_user_selection (gs) and show_user_context mappings in favor of new unified commands. Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent d261d51 commit b054b9f

3 files changed

Lines changed: 122 additions & 62 deletions

File tree

README.md

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,8 @@ See @deathbeam for [configuration](https://github.com/deathbeam/dotfiles/blob/ma
148148
- `gq` - Add all diffs from chat to quickfix list
149149
- `gy` - Yank nearest diff to register (defaults to `"`)
150150
- `gd` - Show diff between source and nearest diff
151-
- `gp` - Show system prompt for current chat
152-
- `gs` - Show current user selection
153-
- `gc` - Show current user context
151+
- `gi` - Show info about current chat (model, agent, system prompt)
152+
- `gc` - Show current chat context
154153
- `gh` - Show help message
155154

156155
The mappings can be customized by setting the `mappings` table in your configuration. Each mapping can have:
@@ -562,13 +561,10 @@ Also see [here](/lua/CopilotChat/config.lua):
562561
show_diff = {
563562
normal = 'gd',
564563
},
565-
show_system_prompt = {
566-
normal = 'gp',
564+
show_info = {
565+
normal = 'gi',
567566
},
568-
show_user_selection = {
569-
normal = 'gs',
570-
},
571-
show_user_context = {
567+
show_context = {
572568
normal = 'gc',
573569
},
574570
show_help = {

lua/CopilotChat/config.lua

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,8 @@ local utils = require('CopilotChat.utils')
4848
---@field quickfix_diffs CopilotChat.config.mapping?
4949
---@field yank_diff CopilotChat.config.mapping.register?
5050
---@field show_diff CopilotChat.config.mapping?
51-
---@field show_system_prompt CopilotChat.config.mapping?
52-
---@field show_user_selection CopilotChat.config.mapping?
53-
---@field show_user_context CopilotChat.config.mapping?
51+
---@field show_info CopilotChat.config.mapping?
52+
---@field show_context CopilotChat.config.mapping?
5453
---@field show_help CopilotChat.config.mapping?
5554

5655
---@class CopilotChat.config.shared
@@ -378,13 +377,10 @@ return {
378377
show_diff = {
379378
normal = 'gd',
380379
},
381-
show_system_prompt = {
382-
normal = 'gp',
380+
show_info = {
381+
normal = 'gi',
383382
},
384-
show_user_selection = {
385-
normal = 'gs',
386-
},
387-
show_user_context = {
383+
show_context = {
388384
normal = 'gc',
389385
},
390386
show_help = {

lua/CopilotChat/init.lua

Lines changed: 112 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,34 @@ local function resolve_embeddings(prompt, config)
281281
return embeddings:values(), prompt
282282
end
283283

284+
local function resolve_agent(prompt, config)
285+
local agents = vim.tbl_keys(state.copilot:list_agents())
286+
local selected_agent = config.agent
287+
prompt = prompt:gsub('@' .. WORD, function(match)
288+
if vim.tbl_contains(agents, match) then
289+
selected_agent = match
290+
return ''
291+
end
292+
return '@' .. match
293+
end)
294+
295+
return selected_agent, prompt
296+
end
297+
298+
local function resolve_model(prompt, config)
299+
local models = vim.tbl_keys(state.copilot:list_models())
300+
local selected_model = config.model
301+
prompt = prompt:gsub('%$' .. WORD, function(match)
302+
if vim.tbl_contains(models, match) then
303+
selected_model = match
304+
return ''
305+
end
306+
return '$' .. match
307+
end)
308+
309+
return selected_model, prompt
310+
end
311+
284312
---@param start_of_chat boolean?
285313
local function finish(start_of_chat)
286314
if not start_of_chat then
@@ -684,28 +712,9 @@ function M.ask(prompt, config)
684712
local selection = get_selection(config)
685713

686714
local ok, err = pcall(async.run, function()
687-
local embeddings, embedded_prompt = resolve_embeddings(prompt, config)
688-
prompt = embedded_prompt
689-
690-
local agents = vim.tbl_keys(state.copilot:list_agents())
691-
local selected_agent = config.agent
692-
prompt = prompt:gsub('@' .. WORD, function(match)
693-
if vim.tbl_contains(agents, match) then
694-
selected_agent = match
695-
return ''
696-
end
697-
return '@' .. match
698-
end)
699-
700-
local models = vim.tbl_keys(state.copilot:list_models())
701-
local selected_model = config.model
702-
prompt = prompt:gsub('%$' .. WORD, function(match)
703-
if vim.tbl_contains(models, match) then
704-
selected_model = match
705-
return ''
706-
end
707-
return '$' .. match
708-
end)
715+
local embeddings, prompt = resolve_embeddings(prompt, config)
716+
local selected_agent, prompt = resolve_agent(prompt, config)
717+
local selected_model, prompt = resolve_model(prompt, config)
709718

710719
local has_output = false
711720
local query_ok, filtered_embeddings =
@@ -875,6 +884,14 @@ function M.setup(config)
875884
normal = key,
876885
}
877886
end
887+
888+
if name == 'show_system_prompt' then
889+
utils.deprecate('config.mappings.' .. name, 'config.mappings.show_info')
890+
end
891+
892+
if name == 'show_user_context' or name == 'show_user_selection' then
893+
utils.deprecate('config.mappings.' .. name, 'config.mappings.show_context')
894+
end
878895
end
879896
end
880897

@@ -1137,52 +1154,103 @@ function M.setup(config)
11371154
state.diff:show(diff, state.chat.winnr)
11381155
end)
11391156

1140-
map_key('show_system_prompt', bufnr, function()
1157+
map_key('show_info', bufnr, function()
11411158
local section = state.chat:get_closest_section()
1142-
local system_prompt = state.chat.config.system_prompt
1143-
if section and not section.answer then
1144-
_, system_prompt = resolve_prompts(section.content, system_prompt)
1145-
end
1146-
if not system_prompt then
1159+
if not section or section.answer then
11471160
return
11481161
end
11491162

1150-
state.overlay:show(vim.trim(system_prompt) .. '\n', state.chat.winnr, 'markdown')
1163+
local lines = {}
1164+
local config = state.chat.config
1165+
local prompt, system_prompt = resolve_prompts(section.content, config.system_prompt)
1166+
1167+
async.run(function()
1168+
local selected_agent = resolve_agent(prompt, config)
1169+
local selected_model = resolve_model(prompt, config)
1170+
1171+
if selected_model then
1172+
table.insert(lines, '**Model**')
1173+
table.insert(lines, '```')
1174+
table.insert(lines, selected_model)
1175+
table.insert(lines, '```')
1176+
table.insert(lines, '')
1177+
end
1178+
1179+
if selected_agent then
1180+
table.insert(lines, '**Agent**')
1181+
table.insert(lines, '```')
1182+
table.insert(lines, selected_agent)
1183+
table.insert(lines, '```')
1184+
table.insert(lines, '')
1185+
end
1186+
1187+
if system_prompt then
1188+
table.insert(lines, '**System Prompt**')
1189+
table.insert(lines, '```')
1190+
for _, line in ipairs(vim.split(vim.trim(system_prompt), '\n')) do
1191+
table.insert(lines, line)
1192+
end
1193+
table.insert(lines, '```')
1194+
table.insert(lines, '')
1195+
end
1196+
1197+
async.util.scheduler()
1198+
state.overlay:show(
1199+
vim.trim(table.concat(lines, '\n')) .. '\n',
1200+
state.chat.winnr,
1201+
'markdown'
1202+
)
1203+
end)
11511204
end)
11521205

1153-
map_key('show_user_selection', bufnr, function()
1154-
local selection = get_selection(state.chat.config)
1155-
if not selection then
1206+
map_key('show_context', bufnr, function()
1207+
local section = state.chat:get_closest_section()
1208+
if not section or section.answer then
11561209
return
11571210
end
11581211

1159-
state.overlay:show(selection.content, state.chat.winnr, selection.filetype)
1160-
end)
1212+
local lines = {}
11611213

1162-
map_key('show_user_context', bufnr, function()
1163-
local section = state.chat:get_closest_section()
1214+
local selection = get_selection(state.chat.config)
1215+
if selection then
1216+
table.insert(lines, '**Selection**')
1217+
table.insert(lines, '```' .. selection.filetype)
1218+
for _, line in ipairs(vim.split(selection.content, '\n')) do
1219+
table.insert(lines, line)
1220+
end
1221+
table.insert(lines, '```')
1222+
table.insert(lines, '')
1223+
end
11641224

11651225
async.run(function()
11661226
local embeddings = {}
11671227
if section and not section.answer then
11681228
embeddings = resolve_embeddings(section.content, state.chat.config)
11691229
end
11701230

1171-
local text = ''
11721231
for _, embedding in ipairs(embeddings) do
1173-
local lines = vim.split(embedding.content, '\n')
1174-
local preview = table.concat(vim.list_slice(lines, 1, math.min(10, #lines)), '\n')
1175-
local header = string.format('**`%s`** (%s lines)', embedding.filename, #lines)
1176-
if #lines > 10 then
1232+
local embed_lines = vim.split(embedding.content, '\n')
1233+
local preview = vim.list_slice(embed_lines, 1, math.min(10, #embed_lines))
1234+
local header = string.format('**%s** (%s lines)', embedding.filename, #embed_lines)
1235+
if #embed_lines > 10 then
11771236
header = header .. ' (truncated)'
11781237
end
11791238

1180-
text = text
1181-
.. string.format('%s\n```%s\n%s\n```\n\n', header, embedding.filetype, preview)
1239+
table.insert(lines, header)
1240+
table.insert(lines, '```' .. embedding.filetype)
1241+
for _, line in ipairs(preview) do
1242+
table.insert(lines, line)
1243+
end
1244+
table.insert(lines, '```')
1245+
table.insert(lines, '')
11821246
end
11831247

11841248
async.util.scheduler()
1185-
state.overlay:show(vim.trim(text) .. '\n', state.chat.winnr, 'markdown')
1249+
state.overlay:show(
1250+
vim.trim(table.concat(lines, '\n')) .. '\n',
1251+
state.chat.winnr,
1252+
'markdown'
1253+
)
11861254
end)
11871255
end)
11881256

0 commit comments

Comments
 (0)