fix: load config just once per call

This commit is contained in:
2025-01-05 01:45:43 +01:00
parent 6950c542ad
commit ca729140c3
4 changed files with 103 additions and 93 deletions

View File

@@ -1,34 +1,41 @@
local M = {}
local config = require('chatgpt_nvim.config')
local conf = config.load()
-- Remove local conf = config.load()
-- We'll have each function accept 'conf' from outside or handle it in init.lua
-- local config = require('chatgpt_nvim.config')
-- local conf = config.load()
local prompts = require('chatgpt_nvim.prompts')
local debug_bufnr = nil
if conf.improved_debug then
-- Check if a debug buffer is already open. Close it first to avoid duplicates.
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
local name = vim.api.nvim_buf_get_name(buf)
if name == "ChatGPT_Debug_Log" then
vim.api.nvim_buf_delete(buf, {force = true})
end
end
debug_bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(debug_bufnr, "ChatGPT_Debug_Log")
vim.api.nvim_buf_set_option(debug_bufnr, "filetype", "log")
-- We'll store a reference to conf at runtime
local runtime_conf = nil
function M.setup_ui(conf)
runtime_conf = conf
if runtime_conf.improved_debug then
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
local name = vim.api.nvim_buf_get_name(buf)
if name == "ChatGPT_Debug_Log" then
vim.api.nvim_buf_delete(buf, {force = true})
end
end
debug_bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(debug_bufnr, "ChatGPT_Debug_Log")
vim.api.nvim_buf_set_option(debug_bufnr, "filetype", "log")
end
end
function M.debug_log(msg)
if conf.improved_debug and debug_bufnr then
if runtime_conf and runtime_conf.improved_debug and debug_bufnr then
vim.api.nvim_buf_set_lines(debug_bufnr, -1, -1, false, { msg })
else
if conf.debug then
if runtime_conf and runtime_conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:debug] " .. msg .. "\n")
end
end
end
function M.pick_directories(dirs)
function M.pick_directories(dirs, conf)
local selected_dirs = {}
local selection_instructions = prompts["file-selection-instructions"]
local lines = { selection_instructions }
@@ -37,7 +44,6 @@ function M.pick_directories(dirs)
table.insert(lines, d)
end
-- If a file selection buffer is already open, close it to avoid confusion
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
local name = vim.api.nvim_buf_get_name(buf)
if name:match("ChatGPT_File_Selection") then
@@ -68,7 +74,6 @@ function M.pick_directories(dirs)
once = true,
callback = function()
on_write()
-- Automatically close the buffer once saved
vim.cmd("bd! " .. bufnr)
end
})
@@ -76,7 +81,6 @@ function M.pick_directories(dirs)
vim.cmd("split")
vim.cmd("buffer " .. bufnr)
-- Wait up to 30s for user to close
vim.wait(30000, function()
local winids = vim.api.nvim_tabpage_list_wins(0)
for _, w in ipairs(winids) do
@@ -91,4 +95,4 @@ function M.pick_directories(dirs)
return selected_dirs
end
return M
return M