feat: add debug logging

This commit is contained in:
2024-12-14 12:48:16 +01:00
parent 314a65a203
commit e97aa81d8f
5 changed files with 104 additions and 52 deletions

View File

@@ -1,11 +1,16 @@
local M = {}
local uv = vim.loop
local config = require('chatgpt_nvim.config')
local function load_gitignore_patterns(root)
local conf = config.load()
local gitignore_path = root .. "/.gitignore"
local fd = uv.fs_open(gitignore_path, "r", 438)
if not fd then
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:context] No .gitignore found.\n")
end
return {}
end
local stat = uv.fs_fstat(fd)
@@ -19,6 +24,9 @@ local function load_gitignore_patterns(root)
patterns[#patterns+1] = line
end
end
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:context] Loaded " .. #patterns .. " gitignore patterns.\n")
end
return patterns
end
@@ -32,23 +40,35 @@ local function should_ignore_file(file, ignore_patterns)
end
local function is_text_file(file)
local conf = config.load()
local fd = uv.fs_open(file, "r", 438)
if not fd then
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:context] Could not open file: " .. file .. " for reading.\n")
end
return false
end
local chunk = uv.fs_read(fd, 1024, 0) or ""
uv.fs_close(fd)
-- Check for null bytes as a heuristic for binary files
if chunk:find("\0") then
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:context] File appears binary: " .. file .. "\n")
end
return false
end
return true
end
local function scandir(dir, ignore_patterns, files)
files = files or {}
local conf = config.load()
local fd = uv.fs_opendir(dir, nil, 50)
if not fd then return files end
if not fd then
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:context] Could not open dir: " .. dir .. "\n")
end
return files
end
while true do
local ents = uv.fs_readdir(fd)
if not ents then break end
@@ -60,6 +80,10 @@ local function scandir(dir, ignore_patterns, files)
elseif ent.type == "directory" and ent.name ~= ".git" then
scandir(fullpath, ignore_patterns, files)
end
else
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:context] Ignoring file/dir: " .. fullpath .. "\n")
end
end
end
end
@@ -68,6 +92,7 @@ local function scandir(dir, ignore_patterns, files)
end
function M.get_project_files(directories)
local conf = config.load()
local root = vim.fn.getcwd()
local ignore_patterns = load_gitignore_patterns(root)
local all_files = {}
@@ -85,6 +110,10 @@ function M.get_project_files(directories)
table.insert(rel_files, rel)
end
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:context] Found " .. #rel_files .. " project files.\n")
end
return rel_files
end
@@ -95,6 +124,7 @@ function M.get_project_structure(directories)
end
function M.get_file_contents(files)
local conf = config.load()
local root = vim.fn.getcwd()
local sections = {}
for _, f in ipairs(files) do
@@ -111,6 +141,10 @@ function M.get_file_contents(files)
else
uv.fs_close(fd)
end
else
if conf.debug then
vim.api.nvim_out_write("[chatgpt_nvim:context] Could not open file for content: " .. f .. "\n")
end
end
end
return table.concat(sections, "\n")