From 8a60d8d57a1bd78714c5b63d87f1e727012ac779 Mon Sep 17 00:00:00 2001 From: Tomas Slusny Date: Tue, 16 Sep 2025 09:17:46 +0200 Subject: [PATCH] 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 --- lua/CopilotChat/init.lua | 104 ++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 56 deletions(-) diff --git a/lua/CopilotChat/init.lua b/lua/CopilotChat/init.lua index d50251f1..d8a3e378 100644 --- a/lua/CopilotChat/init.lua +++ b/lua/CopilotChat/init.lua @@ -28,7 +28,7 @@ local M = setmetatable({}, { local initialized = rawget(t, 'initialized') if not initialized then rawset(t, 'initialized', true) - rawget(t, 'init')() + rawget(t, 'setup')() end return rawget(t, key) @@ -1061,8 +1061,20 @@ function M.log_level(level) end end ---- Initialize the plugin if not already initialized. -function M.init() +--- Set up the plugin +---@param config CopilotChat.config.Config? +function M.setup(config) + for k, v in pairs(vim.tbl_deep_extend('force', M.config, config or {})) do + M.config[k] = v + end + + if not M.config.separator or M.config.separator == '' then + log.warn( + 'Empty separator is not allowed, using default separator instead. Set `separator` in config to change this.' + ) + M.config.separator = '---' + end + -- Set log level if M.config.debug then M.log_level('debug') @@ -1077,70 +1089,50 @@ function M.init() }) -- Load the providers + client:stop() client:set_providers(function() return M.config.providers end) -- Initialize chat - if not M.chat then - M.chat = require('CopilotChat.ui.chat')(M.config, function(bufnr) - for name, _ in pairs(M.config.mappings) do - map_key(name, bufnr) - end - - require('CopilotChat.completion').enable(bufnr, M.config.chat_autocomplete) - - vim.api.nvim_create_autocmd({ 'BufEnter', 'BufLeave' }, { - buffer = bufnr, - callback = function(ev) - if ev.event == 'BufEnter' then - update_source() - end - - vim.schedule(function() - select.highlight(state.source.bufnr, not (M.config.highlight_selection and M.chat:focused())) - end) - end, - }) + if M.chat then + M.chat:close(state.source.bufnr) + M.chat:delete() + end - if M.config.insert_at_end then - vim.api.nvim_create_autocmd({ 'InsertEnter' }, { - buffer = bufnr, - callback = function() - vim.cmd('normal! 0') - vim.cmd('normal! G$') - vim.v.char = 'x' - end, - }) - end + M.chat = require('CopilotChat.ui.chat')(M.config, function(bufnr) + for name, _ in pairs(M.config.mappings) do + map_key(name, bufnr) + end - finish(true) - end) - end -end + require('CopilotChat.completion').enable(bufnr, M.config.chat_autocomplete) ---- Set up the plugin ----@param config CopilotChat.config.Config? -function M.setup(config) - for k, v in pairs(vim.tbl_deep_extend('force', M.config, config or {})) do - M.config[k] = v - end + vim.api.nvim_create_autocmd({ 'BufEnter', 'BufLeave' }, { + buffer = bufnr, + callback = function(ev) + if ev.event == 'BufEnter' then + update_source() + end - if not M.config.separator or M.config.separator == '' then - log.warn( - 'Empty separator is not allowed, using default separator instead. Set `separator` in config to change this.' - ) - M.config.separator = '---' - end + vim.schedule(function() + select.highlight(state.source.bufnr, not (M.config.highlight_selection and M.chat:focused())) + end) + end, + }) - if M.chat then - client:stop() - M.chat:close(state.source.bufnr) - M.chat:delete() - M.chat = nil - end + if M.config.insert_at_end then + vim.api.nvim_create_autocmd({ 'InsertEnter' }, { + buffer = bufnr, + callback = function() + vim.cmd('normal! 0') + vim.cmd('normal! G$') + vim.v.char = 'x' + end, + }) + end - M.init() + finish(true) + end) for name, prompt in pairs(list_prompts()) do if prompt.prompt then