diff --git a/lua/chatgpt_nvim/context.lua b/lua/chatgpt_nvim/context.lua index 55b1933..992cc0f 100644 --- a/lua/chatgpt_nvim/context.lua +++ b/lua/chatgpt_nvim/context.lua @@ -2,6 +2,37 @@ local M = {} local uv = vim.loop +-- Converts a .gitignore-style pattern to a Lua pattern +local function gitignore_to_lua_pattern(gip) + -- Trim spaces + gip = gip:gsub("^%s*(.-)%s*$", "%1") + + -- Escape magic chars in Lua patterns + local magic_chars = "().^$+%-*?[]" + gip = gip:gsub("["..magic_chars.."]", "%%%1") + + -- Convert ** to .- (match any path, including dirs) + gip = gip:gsub("%%%%%*%%%%%*", ".*") + + -- Convert * to [^/]* (match anything except /) + gip = gip:gsub("%%%%%*", "[^/]*") + + -- If pattern starts with /, ensure it matches start of string + if gip:sub(1,1) == "/" then + gip = "^" .. gip:sub(2) + else + -- Otherwise allow matching anywhere + gip = gip + end + + -- If pattern ends with /, ensure it matches a directory + if gip:sub(-1) == "/" then + gip = gip .. ".*" + end + + return gip +end + local function load_gitignore_patterns(root, conf) local gitignore_path = root .. "/.gitignore" local fd = uv.fs_open(gitignore_path, "r", 438) @@ -19,7 +50,7 @@ local function load_gitignore_patterns(root, conf) for line in data:gmatch("[^\r\n]+") do line = line:match("^%s*(.-)%s*$") if line ~= "" and not line:match("^#") then - patterns[#patterns+1] = line + table.insert(patterns, gitignore_to_lua_pattern(line)) end end if conf.debug then @@ -30,7 +61,7 @@ end local function should_ignore_file(file, ignore_patterns, conf) for _, pattern in ipairs(ignore_patterns) do - if file:find(pattern, 1, true) then + if file:match(pattern) then if conf.debug then vim.api.nvim_out_write("[chatgpt_nvim:context] Ignoring file/dir: " .. file .. " (matched pattern: " .. pattern .. ")\n") end @@ -154,4 +185,4 @@ function M.get_file_contents(files, conf) return table.concat(sections, "\n") end -return M +return M \ No newline at end of file