Files
chatgpt.vim/lua/chatgpt_nvim/context.lua

62 lines
1.3 KiB
Lua

local M = {}
function M.get_current_file()
local buf = vim.api.nvim_get_current_buf()
local path = vim.api.nvim_buf_get_name(buf)
if path == "" then
path = "untitled"
end
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
local content = table.concat(lines, "\n")
return content, path
end
function M.get_project_structure()
local handle = io.popen("git ls-files 2>/dev/null")
if handle then
local result = handle:read("*a")
handle:close()
if result and result ~= "" then
return "Files:\n" .. result
end
end
local dhandle = io.popen("find . -type f")
if dhandle then
local dresult = dhandle:read("*a")
dhandle:close()
return "Files:\n" .. (dresult or "")
end
return "No files found."
end
local function get_git_root()
local handle = io.popen("git rev-parse --show-toplevel 2>/dev/null")
if handle then
local root = handle:read("*l")
handle:close()
return root
end
return nil
end
function M.get_readme_content()
local root = get_git_root()
if not root then
return nil
end
local readme_path = root .. "/README.md"
local f = io.open(readme_path, "r")
if f then
local content = f:read("*a")
f:close()
if content and content ~= "" then
return content
end
end
return nil
end
return M