initial commit
This commit is contained in:
67
lua/chatgpt_nvim/context.lua
Normal file
67
lua/chatgpt_nvim/context.lua
Normal file
@@ -0,0 +1,67 @@
|
||||
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
|
||||
|
||||
-- Fallback if not in a git repo
|
||||
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
|
||||
|
||||
-- Helper to find the git root directory
|
||||
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()
|
||||
if root and root ~= "" then
|
||||
return root
|
||||
end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
-- Attempt to read README.md from the root of the git repository
|
||||
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
|
||||
Reference in New Issue
Block a user