fix: try to find the config file better

This commit is contained in:
2024-12-12 20:49:05 +01:00
parent d51742459c
commit e7e20500ec

View File

@@ -1,6 +1,7 @@
-- lua/chatgpt_nvim/config.lua -- lua/chatgpt_nvim/config.lua
-- Modified to load the config file from the git root of the currently opened file -- Modified to:
-- If not in a git repository, fallback to current working directory -- 1) Determine the Git root based on the currently opened file.
-- 2) If no file is open or not in Git repo, fallback to current working directory.
local M = {} local M = {}
local uv = vim.loop local uv = vim.loop
@@ -8,19 +9,40 @@ local uv = vim.loop
local ok_yaml, lyaml = pcall(require, "lyaml") local ok_yaml, lyaml = pcall(require, "lyaml")
local function get_project_root() local function get_project_root()
-- Attempt to find git root -- Get the directory of the currently opened file.
local git_root = vim.fn.systemlist('git rev-parse --show-toplevel') -- If no file is open, we fallback to current working directory.
if vim.v.shell_error == 0 and git_root and #git_root > 0 then local current_file = vim.fn.expand("%:p")
return git_root[1] local root_dir
if current_file == "" then
-- No file opened, fallback to cwd
root_dir = vim.fn.getcwd()
else
-- Extract directory from current file path
local file_dir = current_file:match("(.*)/")
if not file_dir then
-- If something went wrong extracting the directory, fallback to cwd
root_dir = vim.fn.getcwd()
else
-- Attempt to find git root from the file's directory
-- We run git rev-parse with `cd` to the file's directory.
local cmd = string.format("cd %s && git rev-parse --show-toplevel 2>/dev/null", vim.fn.shellescape(file_dir))
local git_root = vim.fn.systemlist(cmd)
if vim.v.shell_error == 0 and git_root and #git_root > 0 then
root_dir = git_root[1]
else
-- Not a git repo or failed to find git root, fallback to the file's directory
root_dir = file_dir
end
end
end end
-- Fallback to current working directory if git root not found
return vim.fn.getcwd() return root_dir
end end
local function get_config_path() local function get_config_path()
local root = get_project_root() local root = get_project_root()
local config_path = root .. "/.chatgpt_config.yaml" return root .. "/.chatgpt_config.yaml"
return config_path
end end
function M.load() function M.load()
@@ -48,7 +70,7 @@ function M.load()
end end
end end
return { return {
initial_prompt = " You are a coding assistant who receives a projects context and user instructions. The user will provide a prompt, and you will return modifications to the project in a YAML structure. This YAML must have a top-level key named files, which should be a list of file changes. Each element in files must be a mapping with the keys path and content. The path should be the file path relative to the projects root directory, and content should contain the new file content as a multiline string (using the YAML | literal style). Do not include additional commentary outside of the YAML.\n Here is the structure expected in your final answer:\n files:\n - path: \"relative/path/to/file1.ext\"\n content: |\n <full file content here>\n - path: \"relative/path/to/file2.ext\"\n content: |\n <full file content here>\n Based on the prompt and project context provided, you must only return the YAML structure shown above (with actual file paths and contents substituted in). Any file that should be created or modified must appear as one of the files entries. The content should contain the complete and final code for that file, reflecting all changes requested by the user.", initial_prompt = "You are a coding assistant who receives a projects context and user instructions. The user will provide a prompt, and you will return modifications to the project in a YAML structure. This YAML must have a top-level key named files, which should be a list of file changes. Each element in files must be a mapping with the keys path and content. The path should be the file path relative to the projects root directory, and content should contain the new file content as a multiline string (using the YAML | literal style). Do not include additional commentary outside of the YAML.\n Here is the structure expected in your final answer:\n files:\n - path: \"relative/path/to/file1.ext\"\n content: |\n <full file content here>\n - path: \"relative/path/to/file2.ext\"\n content: |\n <full file content here>\n Based on the prompt and project context provided, you must only return the YAML structure shown above (with actual file paths and contents substituted in). Any file that should be created or modified must appear as one of the files entries. The content should contain the complete and final code for that file, reflecting all changes requested by the user.",
directories = { "." }, directories = { "." },
} }
end end