fix: tools return json (#1263)

This commit is contained in:
yetone 2025-02-13 11:54:54 +08:00 committed by GitHub
parent ce55d7ac9e
commit 76c06ed277
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -42,13 +42,12 @@ function M.list_files(opts, on_log)
add_dirs = true, add_dirs = true,
depth = opts.depth, depth = opts.depth,
}) })
local result = "" local filepaths = {}
for _, file in ipairs(files) do for _, file in ipairs(files) do
local uniform_path = Utils.uniform_path(file) local uniform_path = Utils.uniform_path(file)
result = result .. uniform_path .. "\n" table.insert(filepaths, uniform_path)
end end
result = result:gsub("\n$", "") return vim.json.encode(filepaths), nil
return result, nil
end end
---@param opts { rel_path: string, keyword: string } ---@param opts { rel_path: string, keyword: string }
@ -63,12 +62,11 @@ function M.search_files(opts, on_log)
local files = Utils.scan_directory_respect_gitignore({ local files = Utils.scan_directory_respect_gitignore({
directory = abs_path, directory = abs_path,
}) })
local result = "" local filepaths = {}
for _, file in ipairs(files) do for _, file in ipairs(files) do
if file:find(opts.keyword) then result = result .. file .. "\n" end if file:find(opts.keyword) then table.insert(filepaths, file) end
end end
result = result:gsub("\n$", "") return vim.json.encode(filepaths), nil
return result, nil
end end
---@param opts { rel_path: string, keyword: string } ---@param opts { rel_path: string, keyword: string }
@ -105,7 +103,9 @@ function M.search(opts, on_log)
if on_log then on_log("Running command: " .. cmd) end if on_log then on_log("Running command: " .. cmd) end
local result = vim.fn.system(cmd) local result = vim.fn.system(cmd)
return result or "", nil local filepaths = vim.split(result, "\n")
return vim.json.encode(filepaths), nil
end end
---@param opts { rel_path: string } ---@param opts { rel_path: string }
@ -184,9 +184,10 @@ function M.rename_file(opts, on_log)
end end
---@param opts { rel_path: string, new_rel_path: string } ---@param opts { rel_path: string, new_rel_path: string }
---@param on_log? fun(log: string): nil
---@return boolean success ---@return boolean success
---@return string|nil error ---@return string|nil error
function M.copy_file(opts) function M.copy_file(opts, on_log)
local abs_path = get_abs_path(opts.rel_path) local abs_path = get_abs_path(opts.rel_path)
if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
if not Path:new(abs_path):exists() then return false, "File not found: " .. abs_path end if not Path:new(abs_path):exists() then return false, "File not found: " .. abs_path end
@ -194,38 +195,47 @@ function M.copy_file(opts)
local new_abs_path = get_abs_path(opts.new_rel_path) local new_abs_path = get_abs_path(opts.new_rel_path)
if not has_permission_to_access(new_abs_path) then return false, "No permission to access path: " .. new_abs_path end if not has_permission_to_access(new_abs_path) then return false, "No permission to access path: " .. new_abs_path end
if Path:new(new_abs_path):exists() then return false, "File already exists: " .. new_abs_path end if Path:new(new_abs_path):exists() then return false, "File already exists: " .. new_abs_path end
if on_log then on_log("Copying file: " .. abs_path .. " to " .. new_abs_path) end
Path:new(new_abs_path):write(Path:new(abs_path):read()) Path:new(new_abs_path):write(Path:new(abs_path):read())
return true, nil return true, nil
end end
---@param opts { rel_path: string } ---@param opts { rel_path: string }
---@param on_log? fun(log: string): nil
---@return boolean success ---@return boolean success
---@return string|nil error ---@return string|nil error
function M.delete_file(opts) function M.delete_file(opts, on_log)
local abs_path = get_abs_path(opts.rel_path) local abs_path = get_abs_path(opts.rel_path)
if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
if not Path:new(abs_path):exists() then return false, "File not found: " .. abs_path end if not Path:new(abs_path):exists() then return false, "File not found: " .. abs_path end
if not Path:new(abs_path):is_file() then return false, "Path is not a file: " .. abs_path end if not Path:new(abs_path):is_file() then return false, "Path is not a file: " .. abs_path end
if not M.confirm("Are you sure you want to delete the file: " .. abs_path) then return false, "User canceled" end if not M.confirm("Are you sure you want to delete the file: " .. abs_path) then return false, "User canceled" end
if on_log then on_log("Deleting file: " .. abs_path) end
os.remove(abs_path) os.remove(abs_path)
return true, nil return true, nil
end end
---@param opts { rel_path: string } ---@param opts { rel_path: string }
---@param on_log? fun(log: string): nil
---@return boolean success ---@return boolean success
---@return string|nil error ---@return string|nil error
function M.create_dir(opts) function M.create_dir(opts, on_log)
local abs_path = get_abs_path(opts.rel_path) local abs_path = get_abs_path(opts.rel_path)
if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
if Path:new(abs_path):exists() then return false, "Directory already exists: " .. abs_path end if Path:new(abs_path):exists() then return false, "Directory already exists: " .. abs_path end
if not M.confirm("Are you sure you want to create the directory: " .. abs_path) then
return false, "User canceled"
end
if on_log then on_log("Creating directory: " .. abs_path) end
Path:new(abs_path):mkdir({ parents = true }) Path:new(abs_path):mkdir({ parents = true })
return true, nil return true, nil
end end
---@param opts { rel_path: string, new_rel_path: string } ---@param opts { rel_path: string, new_rel_path: string }
---@param on_log? fun(log: string): nil
---@return boolean success ---@return boolean success
---@return string|nil error ---@return string|nil error
function M.rename_dir(opts) function M.rename_dir(opts, on_log)
local abs_path = get_abs_path(opts.rel_path) local abs_path = get_abs_path(opts.rel_path)
if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
if not Path:new(abs_path):exists() then return false, "Directory not found: " .. abs_path end if not Path:new(abs_path):exists() then return false, "Directory not found: " .. abs_path end
@ -236,14 +246,16 @@ function M.rename_dir(opts)
if not M.confirm("Are you sure you want to rename directory " .. abs_path .. " to " .. new_abs_path .. "?") then if not M.confirm("Are you sure you want to rename directory " .. abs_path .. " to " .. new_abs_path .. "?") then
return false, "User canceled" return false, "User canceled"
end end
if on_log then on_log("Renaming directory: " .. abs_path .. " to " .. new_abs_path) end
os.rename(abs_path, new_abs_path) os.rename(abs_path, new_abs_path)
return true, nil return true, nil
end end
---@param opts { rel_path: string } ---@param opts { rel_path: string }
---@param on_log? fun(log: string): nil
---@return boolean success ---@return boolean success
---@return string|nil error ---@return string|nil error
function M.delete_dir(opts) function M.delete_dir(opts, on_log)
local abs_path = get_abs_path(opts.rel_path) local abs_path = get_abs_path(opts.rel_path)
if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end if not has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
if not Path:new(abs_path):exists() then return false, "Directory not found: " .. abs_path end if not Path:new(abs_path):exists() then return false, "Directory not found: " .. abs_path end
@ -251,6 +263,7 @@ function M.delete_dir(opts)
if not M.confirm("Are you sure you want to delete the directory: " .. abs_path) then if not M.confirm("Are you sure you want to delete the directory: " .. abs_path) then
return false, "User canceled" return false, "User canceled"
end end
if on_log then on_log("Deleting directory: " .. abs_path) end
os.remove(abs_path) os.remove(abs_path)
return true, nil return true, nil
end end