feat: also allow the config file without dot, give an error message at search replace if search was not found in file

This commit is contained in:
2025-01-31 14:35:32 +01:00
parent e9fd204977
commit 5dc568e9e5
5 changed files with 70 additions and 13 deletions

View File

@@ -4,6 +4,7 @@ local uv = vim.loop
local ok_yaml, lyaml = pcall(require, "lyaml")
local prompts = require("chatgpt_nvim.prompts")
-- Determines the Git root or fallback directory.
local function get_project_root()
local current_file = vim.fn.expand("%:p")
local root_dir
@@ -28,14 +29,30 @@ local function get_project_root()
return root_dir
end
-- Attempt to locate either .chatgpt_config.yaml or chatgpt_config.yaml, returning whichever exists first.
local function get_config_path()
local root = get_project_root()
return root .. "/.chatgpt_config.yaml"
local dot_config = root .. "/.chatgpt_config.yaml"
local non_dot_config = root .. "/chatgpt_config.yaml"
local dot_fd = uv.fs_open(dot_config, "r", 438)
if dot_fd then
uv.fs_close(dot_fd)
return dot_config
end
local non_dot_fd = uv.fs_open(non_dot_config, "r", 438)
if non_dot_fd then
uv.fs_close(non_dot_fd)
return non_dot_config
end
return nil
end
function M.load()
-- Attempt to find either config file, else fallback
local path = get_config_path()
local fd = uv.fs_open(path, "r", 438)
local config = {
initial_prompt = "",
directories = { "." },
@@ -62,10 +79,22 @@ function M.load()
}
}
-- If no config file found, alert user and return default config
if not path then
config.initial_prompt = "You are a coding assistant who receives a project's context and user instructions..."
vim.notify(
"No config file found (tried .chatgpt_config.yaml, chatgpt_config.yaml). Using defaults.",
vim.log.levels.WARN
)
return config
end
local fd = uv.fs_open(path, "r", 438)
if fd then
local stat = uv.fs_fstat(fd)
local data = uv.fs_read(fd, stat.size, 0)
uv.fs_close(fd)
if data and ok_yaml then
local ok, result = pcall(lyaml.load, data)
if ok and type(result) == "table" then
@@ -109,7 +138,6 @@ function M.load()
config.enable_step_by_step = result.enable_step_by_step
end
-- auto_lint controls whether we do LSP-based checks
if type(result.auto_lint) == "boolean" then
config.auto_lint = result.auto_lint
end