Skip to content

Commit 8a60d8d

Browse files
committed
refactor(init): merge init logic into setup for simplicity
Simplifies plugin initialization by merging the init logic into the setup function. Removes the separate init function and ensures all configuration, provider setup, and chat initialization are handled in setup. This reduces redundancy and makes the initialization flow clearer. Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent b784122 commit 8a60d8d

1 file changed

Lines changed: 48 additions & 56 deletions

File tree

lua/CopilotChat/init.lua

Lines changed: 48 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ local M = setmetatable({}, {
2828
local initialized = rawget(t, 'initialized')
2929
if not initialized then
3030
rawset(t, 'initialized', true)
31-
rawget(t, 'init')()
31+
rawget(t, 'setup')()
3232
end
3333

3434
return rawget(t, key)
@@ -1061,8 +1061,20 @@ function M.log_level(level)
10611061
end
10621062
end
10631063

1064-
--- Initialize the plugin if not already initialized.
1065-
function M.init()
1064+
--- Set up the plugin
1065+
---@param config CopilotChat.config.Config?
1066+
function M.setup(config)
1067+
for k, v in pairs(vim.tbl_deep_extend('force', M.config, config or {})) do
1068+
M.config[k] = v
1069+
end
1070+
1071+
if not M.config.separator or M.config.separator == '' then
1072+
log.warn(
1073+
'Empty separator is not allowed, using default separator instead. Set `separator` in config to change this.'
1074+
)
1075+
M.config.separator = '---'
1076+
end
1077+
10661078
-- Set log level
10671079
if M.config.debug then
10681080
M.log_level('debug')
@@ -1077,70 +1089,50 @@ function M.init()
10771089
})
10781090

10791091
-- Load the providers
1092+
client:stop()
10801093
client:set_providers(function()
10811094
return M.config.providers
10821095
end)
10831096

10841097
-- Initialize chat
1085-
if not M.chat then
1086-
M.chat = require('CopilotChat.ui.chat')(M.config, function(bufnr)
1087-
for name, _ in pairs(M.config.mappings) do
1088-
map_key(name, bufnr)
1089-
end
1090-
1091-
require('CopilotChat.completion').enable(bufnr, M.config.chat_autocomplete)
1092-
1093-
vim.api.nvim_create_autocmd({ 'BufEnter', 'BufLeave' }, {
1094-
buffer = bufnr,
1095-
callback = function(ev)
1096-
if ev.event == 'BufEnter' then
1097-
update_source()
1098-
end
1099-
1100-
vim.schedule(function()
1101-
select.highlight(state.source.bufnr, not (M.config.highlight_selection and M.chat:focused()))
1102-
end)
1103-
end,
1104-
})
1098+
if M.chat then
1099+
M.chat:close(state.source.bufnr)
1100+
M.chat:delete()
1101+
end
11051102

1106-
if M.config.insert_at_end then
1107-
vim.api.nvim_create_autocmd({ 'InsertEnter' }, {
1108-
buffer = bufnr,
1109-
callback = function()
1110-
vim.cmd('normal! 0')
1111-
vim.cmd('normal! G$')
1112-
vim.v.char = 'x'
1113-
end,
1114-
})
1115-
end
1103+
M.chat = require('CopilotChat.ui.chat')(M.config, function(bufnr)
1104+
for name, _ in pairs(M.config.mappings) do
1105+
map_key(name, bufnr)
1106+
end
11161107

1117-
finish(true)
1118-
end)
1119-
end
1120-
end
1108+
require('CopilotChat.completion').enable(bufnr, M.config.chat_autocomplete)
11211109

1122-
--- Set up the plugin
1123-
---@param config CopilotChat.config.Config?
1124-
function M.setup(config)
1125-
for k, v in pairs(vim.tbl_deep_extend('force', M.config, config or {})) do
1126-
M.config[k] = v
1127-
end
1110+
vim.api.nvim_create_autocmd({ 'BufEnter', 'BufLeave' }, {
1111+
buffer = bufnr,
1112+
callback = function(ev)
1113+
if ev.event == 'BufEnter' then
1114+
update_source()
1115+
end
11281116

1129-
if not M.config.separator or M.config.separator == '' then
1130-
log.warn(
1131-
'Empty separator is not allowed, using default separator instead. Set `separator` in config to change this.'
1132-
)
1133-
M.config.separator = '---'
1134-
end
1117+
vim.schedule(function()
1118+
select.highlight(state.source.bufnr, not (M.config.highlight_selection and M.chat:focused()))
1119+
end)
1120+
end,
1121+
})
11351122

1136-
if M.chat then
1137-
client:stop()
1138-
M.chat:close(state.source.bufnr)
1139-
M.chat:delete()
1140-
M.chat = nil
1141-
end
1123+
if M.config.insert_at_end then
1124+
vim.api.nvim_create_autocmd({ 'InsertEnter' }, {
1125+
buffer = bufnr,
1126+
callback = function()
1127+
vim.cmd('normal! 0')
1128+
vim.cmd('normal! G$')
1129+
vim.v.char = 'x'
1130+
end,
1131+
})
1132+
end
11421133

1143-
M.init()
1134+
finish(true)
1135+
end)
11441136

11451137
for name, prompt in pairs(list_prompts()) do
11461138
if prompt.prompt then

0 commit comments

Comments
 (0)