diff --git a/lua/gp/dispatcher.lua b/lua/gp/dispatcher.lua index 26fc76a..c616798 100644 --- a/lua/gp/dispatcher.lua +++ b/lua/gp/dispatcher.lua @@ -2,6 +2,8 @@ -- Dispatcher handles the communication between the plugin and LLM providers. -------------------------------------------------------------------------------- +local uv = vim.uv or vim.loop + local logger = require("gp.logger") local tasker = require("gp.tasker") local vault = require("gp.vault") @@ -18,7 +20,7 @@ local D = { ---@param opts table # user config D.setup = function(opts) - logger.debug("dispatcher setup started\n" .. vim.inspect(opts)) + logger.debug("dispatcher: setup started\n" .. vim.inspect(opts)) D.config.curl_params = opts.curl_params or default_config.curl_params @@ -52,9 +54,26 @@ D.setup = function(opts) D.query_dir = helpers.prepare_dir(D.query_dir, "query store") - local files = vim.fn.glob(D.query_dir .. "/*.json", false, true) + local files = {} + local handle = uv.fs_scandir(D.query_dir) + if handle then + local name, type + while true do + name, type = uv.fs_scandir_next(handle) + if not name then + break + end + local path = D.query_dir .. "/" .. name + type = type or uv.fs_stat(path).type + if type == "file" and name:match("%.json$") then + table.insert(files, path) + end + end + end + + logger.debug("dispatcher: query files: " .. #files) if #files > 200 then - logger.debug("too many query files, truncating cache") + logger.debug("dispatcher: too many query files, truncating cache") table.sort(files, function(a, b) return a > b end) @@ -63,7 +82,7 @@ D.setup = function(opts) end end - logger.debug("dispatcher setup finished\n" .. vim.inspect(D)) + logger.debug("dispatcher: setup finished\n" .. vim.inspect(D)) end ---@param messages table diff --git a/lua/gp/logger.lua b/lua/gp/logger.lua index 8250941..bfac06f 100644 --- a/lua/gp/logger.lua +++ b/lua/gp/logger.lua @@ -33,10 +33,13 @@ M.setup = function(path, sensitive) if vim.fn.isdirectory(dir) == 0 then vim.fn.mkdir(dir, "p") end + + local file_stats = uv.fs_stat(path) + M.debug("Log file " .. file .. " has " .. (file_stats and file_stats.size or 0) .. " bytes") + file = path - -- truncate log file if it's too big - if uv.fs_stat(file) then + if file_stats and file_stats.size > 5 * 1024 * 1024 then local content = {} for line in io.lines(file) do table.insert(content, line) diff --git a/lua/gp/macros/target_filetype.lua b/lua/gp/macros/target_filetype.lua index 21eb2f8..91be8bc 100644 --- a/lua/gp/macros/target_filetype.lua +++ b/lua/gp/macros/target_filetype.lua @@ -1,6 +1,6 @@ local macro = require("gp.macro") -local values = vim.fn.getcompletion("", "filetype") +local values = nil local M = {} @@ -16,6 +16,9 @@ M = { end, completion = function(params) + if not values then + values = vim.fn.getcompletion("", "filetype") + end return values end,