Skip to content

Commit 4d92564

Browse files
authored
refactor: move list_prompts to prompts module completely (#1468)
- Renamed `prompt.lua` to `prompts.lua` for clarity and consistency. - Moved prompt listing logic from `init.lua` to `prompts.lua`. - Updated all references to use the new `prompts` module. - Removed unused model and prompt listing functions from `init.lua`.
1 parent b967f54 commit 4d92564

File tree

2 files changed

+38
-85
lines changed

2 files changed

+38
-85
lines changed

lua/CopilotChat/init.lua

Lines changed: 19 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local async = require('plenary.async')
22
local log = require('plenary.log')
33
local client = require('CopilotChat.client')
44
local constants = require('CopilotChat.constants')
5-
local prompts = require('CopilotChat.prompt')
5+
local prompts = require('CopilotChat.prompts')
66
local select = require('CopilotChat.select')
77
local utils = require('CopilotChat.utils')
88
local curl = require('CopilotChat.utils.curl')
@@ -145,45 +145,6 @@ local function store_sticky(prompt)
145145
state.sticky = sticky
146146
end
147147

148-
--- List available models.
149-
--- @return CopilotChat.client.Model[]
150-
local function list_models()
151-
local models = client:models()
152-
local result = vim.tbl_keys(models)
153-
154-
table.sort(result, function(a, b)
155-
a = models[a]
156-
b = models[b]
157-
if a.provider ~= b.provider then
158-
return a.provider < b.provider
159-
end
160-
return a.id < b.id
161-
end)
162-
163-
return vim.tbl_map(function(id)
164-
return models[id]
165-
end, result)
166-
end
167-
168-
--- List available prompts.
169-
---@return table<string, CopilotChat.config.prompts.Prompt>
170-
local function list_prompts()
171-
local prompts_to_use = {}
172-
173-
for name, prompt in pairs(M.config.prompts) do
174-
local val = prompt
175-
if type(prompt) == 'string' then
176-
val = {
177-
prompt = prompt,
178-
}
179-
end
180-
181-
prompts_to_use[name] = val
182-
end
183-
184-
return prompts_to_use
185-
end
186-
187148
--- Finish writing to chat buffer.
188149
---@param start_of_chat boolean?
189150
local function finish(start_of_chat)
@@ -413,7 +374,22 @@ end
413374
--- Select default Copilot GPT model.
414375
function M.select_model()
415376
async.run(function()
416-
local models = list_models()
377+
local models = client:models()
378+
local result = vim.tbl_keys(models)
379+
380+
table.sort(result, function(a, b)
381+
a = models[a]
382+
b = models[b]
383+
if a.provider ~= b.provider then
384+
return a.provider < b.provider
385+
end
386+
return a.id < b.id
387+
end)
388+
389+
models = vim.tbl_map(function(id)
390+
return models[id]
391+
end, result)
392+
417393
local choices = vim.tbl_map(function(model)
418394
return {
419395
id = model.id,
@@ -467,7 +443,7 @@ end
467443
--- Select a prompt template to use.
468444
---@param config CopilotChat.config.Shared?
469445
function M.select_prompt(config)
470-
local prompts = list_prompts()
446+
local prompts = prompts.list_prompts()
471447
local keys = vim.tbl_keys(prompts)
472448
table.sort(keys)
473449

@@ -859,7 +835,7 @@ function M.setup(config)
859835
end)
860836
end
861837

862-
for name, prompt in pairs(list_prompts()) do
838+
for name, prompt in pairs(prompts.list_prompts()) do
863839
if prompt.prompt then
864840
vim.api.nvim_create_user_command('CopilotChat' .. name, function(args)
865841
local input = prompt.prompt
Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,27 @@ local WORD_NO_INPUT = '([^%s]+)'
1010
local WORD_WITH_INPUT_QUOTED = WORD .. ':`([^`]+)`'
1111
local WORD_WITH_INPUT_UNQUOTED = WORD .. ':?([^%s`]*)'
1212

13-
--- List available models.
14-
--- @return CopilotChat.client.Model[]
15-
local function list_models()
16-
local models = client:models()
17-
local result = vim.tbl_keys(models)
18-
19-
table.sort(result, function(a, b)
20-
a = models[a]
21-
b = models[b]
22-
if a.provider ~= b.provider then
23-
return a.provider < b.provider
24-
end
25-
return a.id < b.id
26-
end)
27-
28-
return vim.tbl_map(function(id)
29-
return models[id]
30-
end, result)
13+
--- Find custom instructions in the current working directory.
14+
---@param cwd string
15+
---@return table
16+
local function find_custom_instructions(cwd)
17+
local out = {}
18+
local copilot_instructions_path = vim.fs.joinpath(cwd, '.github', 'copilot-instructions.md')
19+
local copilot_instructions = files.read_file(copilot_instructions_path)
20+
if copilot_instructions then
21+
table.insert(out, {
22+
filename = copilot_instructions_path,
23+
content = vim.trim(copilot_instructions),
24+
})
25+
end
26+
return out
3127
end
3228

29+
local M = {}
30+
3331
--- List available prompts.
3432
---@return table<string, CopilotChat.config.prompts.Prompt>
35-
local function list_prompts()
33+
function M.list_prompts()
3634
local config = require('CopilotChat.config')
3735
local prompts_to_use = {}
3836

@@ -50,24 +48,6 @@ local function list_prompts()
5048
return prompts_to_use
5149
end
5250

53-
--- Find custom instructions in the current working directory.
54-
---@param cwd string
55-
---@return table
56-
local function find_custom_instructions(cwd)
57-
local out = {}
58-
local copilot_instructions_path = vim.fs.joinpath(cwd, '.github', 'copilot-instructions.md')
59-
local copilot_instructions = files.read_file(copilot_instructions_path)
60-
if copilot_instructions then
61-
table.insert(out, {
62-
filename = copilot_instructions_path,
63-
content = vim.trim(copilot_instructions),
64-
})
65-
end
66-
return out
67-
end
68-
69-
local M = {}
70-
7151
--- Resolve enabled tools from the prompt.
7252
---@param prompt string?
7353
---@param config CopilotChat.config.Shared?
@@ -299,7 +279,7 @@ function M.resolve_prompt(prompt, config)
299279
end
300280
end
301281

302-
local prompts_to_use = list_prompts()
282+
local prompts_to_use = M.list_prompts()
303283
local depth = 0
304284
local MAX_DEPTH = 10
305285

@@ -370,10 +350,7 @@ end
370350
---@async
371351
function M.resolve_model(prompt, config)
372352
config, prompt = M.resolve_prompt(prompt, config)
373-
374-
local models = vim.tbl_map(function(model)
375-
return model.id
376-
end, list_models())
353+
local models = vim.tbl_keys(client:models())
377354

378355
local selected_model = config.model or ''
379356
prompt = prompt:gsub('%$' .. WORD, function(match)

0 commit comments

Comments
 (0)