Skip to content

Commit 9a32dc4

Browse files
committed
refactor: move commands and highlights to plugin file
Move commands, highlight definitions and treesitter registration from init.lua to a proper plugin/CopilotChat.lua file. This follows better plugin organization practices and ensures proper loading sequence. Includes: - Moving highlight definitions - Moving commands (CopilotChat*) - Moving autocmd for directory tracking - Moving treesitter language registration - Adding proper plugin loading guard
1 parent dde5116 commit 9a32dc4

File tree

3 files changed

+100
-86
lines changed

3 files changed

+100
-86
lines changed

lua/CopilotChat/init.lua

Lines changed: 1 addition & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,7 @@ end
10601060
---@param config CopilotChat.config?
10611061
function M.setup(config)
10621062
M.config = vim.tbl_deep_extend('force', require('CopilotChat.config'), config or {})
1063+
state.highlights_loaded = false
10631064

10641065
-- Save proxy and insecure settings
10651066
utils.curl_store_args({
@@ -1077,24 +1078,6 @@ function M.setup(config)
10771078
M.log_level(M.config.log_level)
10781079
end
10791080

1080-
vim.api.nvim_set_hl(0, 'CopilotChatStatus', { link = 'DiagnosticHint', default = true })
1081-
vim.api.nvim_set_hl(0, 'CopilotChatHelp', { link = 'DiagnosticInfo', default = true })
1082-
vim.api.nvim_set_hl(0, 'CopilotChatKeyword', { link = 'Keyword', default = true })
1083-
vim.api.nvim_set_hl(0, 'CopilotChatInput', { link = 'Special', default = true })
1084-
vim.api.nvim_set_hl(0, 'CopilotChatSelection', { link = 'Visual', default = true })
1085-
vim.api.nvim_set_hl(
1086-
0,
1087-
'CopilotChatHeader',
1088-
{ link = '@markup.heading.2.markdown', default = true }
1089-
)
1090-
vim.api.nvim_set_hl(
1091-
0,
1092-
'CopilotChatSeparator',
1093-
{ link = '@punctuation.special.markdown', default = true }
1094-
)
1095-
1096-
state.highlights_loaded = false
1097-
10981081
if M.chat then
10991082
M.chat:close(state.source and state.source.bufnr or nil)
11001083
M.chat:delete()
@@ -1194,73 +1177,6 @@ function M.setup(config)
11941177
end
11951178
end
11961179
end
1197-
1198-
vim.api.nvim_create_user_command('CopilotChat', function(args)
1199-
local input = args.args
1200-
if input and vim.trim(input) ~= '' then
1201-
M.ask(input)
1202-
else
1203-
M.open()
1204-
end
1205-
end, {
1206-
nargs = '*',
1207-
force = true,
1208-
range = true,
1209-
})
1210-
1211-
vim.api.nvim_create_user_command('CopilotChatPrompts', function()
1212-
M.select_prompt()
1213-
end, { force = true, range = true })
1214-
vim.api.nvim_create_user_command('CopilotChatModels', function()
1215-
M.select_model()
1216-
end, { force = true })
1217-
vim.api.nvim_create_user_command('CopilotChatAgents', function()
1218-
M.select_agent()
1219-
end, { force = true })
1220-
vim.api.nvim_create_user_command('CopilotChatOpen', function()
1221-
M.open()
1222-
end, { force = true })
1223-
vim.api.nvim_create_user_command('CopilotChatClose', function()
1224-
M.close()
1225-
end, { force = true })
1226-
vim.api.nvim_create_user_command('CopilotChatToggle', function()
1227-
M.toggle()
1228-
end, { force = true })
1229-
vim.api.nvim_create_user_command('CopilotChatStop', function()
1230-
M.stop()
1231-
end, { force = true })
1232-
vim.api.nvim_create_user_command('CopilotChatReset', function()
1233-
M.reset()
1234-
end, { force = true })
1235-
1236-
local function complete_load()
1237-
local options = vim.tbl_map(function(file)
1238-
return vim.fn.fnamemodify(file, ':t:r')
1239-
end, vim.fn.glob(M.config.history_path .. '/*', true, true))
1240-
1241-
if not vim.tbl_contains(options, 'default') then
1242-
table.insert(options, 1, 'default')
1243-
end
1244-
1245-
return options
1246-
end
1247-
vim.api.nvim_create_user_command('CopilotChatSave', function(args)
1248-
M.save(args.args)
1249-
end, { nargs = '*', force = true, complete = complete_load })
1250-
vim.api.nvim_create_user_command('CopilotChatLoad', function(args)
1251-
M.load(args.args)
1252-
end, { nargs = '*', force = true, complete = complete_load })
1253-
1254-
-- Store the current directory to window when directory changes
1255-
-- I dont think there is a better way to do this that functions
1256-
-- with "rooter" plugins, LSP and stuff as vim.fn.getcwd() when
1257-
-- i pass window number inside doesnt work
1258-
vim.api.nvim_create_autocmd({ 'VimEnter', 'WinEnter', 'DirChanged' }, {
1259-
group = vim.api.nvim_create_augroup('CopilotChat', {}),
1260-
callback = function()
1261-
vim.w.cchat_cwd = vim.fn.getcwd()
1262-
end,
1263-
})
12641180
end
12651181

12661182
return M

lua/CopilotChat/ui/chat.lua

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ end
6969
---@field private chat_overlay CopilotChat.ui.Overlay
7070
local Chat = class(function(self, question_header, answer_header, separator, help, on_buf_create)
7171
Overlay.init(self, 'copilot-chat', help, on_buf_create)
72-
vim.treesitter.language.register('markdown', self.name)
7372

7473
self.winnr = nil
7574
self.sections = {}

plugin/CopilotChat.lua

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
if vim.g.loaded_copilot_chat then
2+
return
3+
end
4+
5+
local min_version = '0.10.0'
6+
if vim.fn.has('nvim-' .. min_version) ~= 1 then
7+
vim.notify_once(
8+
('CopilotChat.nvim requires Neovim >= %s'):format(min_version),
9+
vim.log.levels.ERROR
10+
)
11+
return
12+
end
13+
14+
local chat = require('CopilotChat')
15+
16+
-- Setup highlights
17+
vim.api.nvim_set_hl(0, 'CopilotChatStatus', { link = 'DiagnosticHint', default = true })
18+
vim.api.nvim_set_hl(0, 'CopilotChatHelp', { link = 'DiagnosticInfo', default = true })
19+
vim.api.nvim_set_hl(0, 'CopilotChatKeyword', { link = 'Keyword', default = true })
20+
vim.api.nvim_set_hl(0, 'CopilotChatInput', { link = 'Special', default = true })
21+
vim.api.nvim_set_hl(0, 'CopilotChatSelection', { link = 'Visual', default = true })
22+
vim.api.nvim_set_hl(0, 'CopilotChatHeader', { link = '@markup.heading.2.markdown', default = true })
23+
vim.api.nvim_set_hl(
24+
0,
25+
'CopilotChatSeparator',
26+
{ link = '@punctuation.special.markdown', default = true }
27+
)
28+
29+
-- Setup commands
30+
vim.api.nvim_create_user_command('CopilotChat', function(args)
31+
local input = args.args
32+
if input and vim.trim(input) ~= '' then
33+
chat.ask(input)
34+
else
35+
chat.open()
36+
end
37+
end, {
38+
nargs = '*',
39+
force = true,
40+
range = true,
41+
})
42+
vim.api.nvim_create_user_command('CopilotChatPrompts', function()
43+
chat.select_prompt()
44+
end, { force = true, range = true })
45+
vim.api.nvim_create_user_command('CopilotChatModels', function()
46+
chat.select_model()
47+
end, { force = true })
48+
vim.api.nvim_create_user_command('CopilotChatAgents', function()
49+
chat.select_agent()
50+
end, { force = true })
51+
vim.api.nvim_create_user_command('CopilotChatOpen', function()
52+
chat.open()
53+
end, { force = true })
54+
vim.api.nvim_create_user_command('CopilotChatClose', function()
55+
chat.close()
56+
end, { force = true })
57+
vim.api.nvim_create_user_command('CopilotChatToggle', function()
58+
chat.toggle()
59+
end, { force = true })
60+
vim.api.nvim_create_user_command('CopilotChatStop', function()
61+
chat.stop()
62+
end, { force = true })
63+
vim.api.nvim_create_user_command('CopilotChatReset', function()
64+
chat.reset()
65+
end, { force = true })
66+
67+
local function complete_load()
68+
local options = vim.tbl_map(function(file)
69+
return vim.fn.fnamemodify(file, ':t:r')
70+
end, vim.fn.glob(chat.config.history_path .. '/*', true, true))
71+
72+
if not vim.tbl_contains(options, 'default') then
73+
table.insert(options, 1, 'default')
74+
end
75+
76+
return options
77+
end
78+
vim.api.nvim_create_user_command('CopilotChatSave', function(args)
79+
chat.save(args.args)
80+
end, { nargs = '*', force = true, complete = complete_load })
81+
vim.api.nvim_create_user_command('CopilotChatLoad', function(args)
82+
chat.load(args.args)
83+
end, { nargs = '*', force = true, complete = complete_load })
84+
85+
-- Store the current directory to window when directory changes
86+
-- I dont think there is a better way to do this that functions
87+
-- with "rooter" plugins, LSP and stuff as vim.fn.getcwd() when
88+
-- i pass window number inside doesnt work
89+
vim.api.nvim_create_autocmd({ 'VimEnter', 'WinEnter', 'DirChanged' }, {
90+
group = vim.api.nvim_create_augroup('CopilotChat', {}),
91+
callback = function()
92+
vim.w.cchat_cwd = vim.fn.getcwd()
93+
end,
94+
})
95+
96+
-- Setup treesitter
97+
vim.treesitter.language.register('markdown', 'copilot-chat')
98+
99+
vim.g.loaded_copilot_chat = true

0 commit comments

Comments
 (0)