feat: add the possibility to delete files. and restrict the editing of files to inside the project
This commit is contained in:
@@ -31,6 +31,12 @@ local function estimate_tokens(text)
|
||||
return math.floor(length / approx_chars_per_token)
|
||||
end
|
||||
|
||||
local function is_subpath(root, path)
|
||||
local root_abs = vim.fn.fnamemodify(root, ":p")
|
||||
local target_abs = vim.fn.fnamemodify(path, ":p")
|
||||
return target_abs:sub(1, #root_abs) == root_abs
|
||||
end
|
||||
|
||||
function M.run_chatgpt_command()
|
||||
local conf = config.load()
|
||||
local user_input = vim.fn.input("Message for O1 Model: ")
|
||||
@@ -90,13 +96,30 @@ function M.run_chatgpt_paste_command()
|
||||
return
|
||||
end
|
||||
|
||||
local root = vim.fn.getcwd()
|
||||
|
||||
for _, fileinfo in ipairs(data.files) do
|
||||
if fileinfo.path and fileinfo.content then
|
||||
if not fileinfo.path then
|
||||
vim.api.nvim_err_writeln("Invalid file entry. Must have 'path'.")
|
||||
goto continue
|
||||
end
|
||||
|
||||
-- Ensure the path is within the project root
|
||||
if not is_subpath(root, fileinfo.path) then
|
||||
vim.api.nvim_err_writeln("Invalid file path outside project root: " .. fileinfo.path)
|
||||
goto continue
|
||||
end
|
||||
|
||||
if fileinfo.delete == true then
|
||||
handler.delete_file(fileinfo.path)
|
||||
print("Deleted file: " .. fileinfo.path)
|
||||
elseif fileinfo.content then
|
||||
handler.write_file(fileinfo.path, fileinfo.content)
|
||||
print("Wrote file: " .. fileinfo.path)
|
||||
else
|
||||
vim.api.nvim_err_writeln("Invalid file entry. Must have 'path' and 'content'.")
|
||||
vim.api.nvim_err_writeln("Invalid file entry. Must have 'content' or 'delete' set to true.")
|
||||
end
|
||||
::continue::
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user