feat: extract text editor tools (#1726)
This commit is contained in:
@@ -3,7 +3,6 @@ local Utils = require("avante.utils")
|
||||
local Path = require("plenary.path")
|
||||
local Config = require("avante.config")
|
||||
local RagService = require("avante.rag_service")
|
||||
local Highlights = require("avante.highlights")
|
||||
local Helpers = require("avante.llm_tools.helpers")
|
||||
|
||||
local M = {}
|
||||
@@ -30,15 +29,6 @@ function M.str_replace_editor(opts, on_log, on_complete, session_ctx)
|
||||
if not on_complete then return false, "on_complete not provided" end
|
||||
local abs_path = Helpers.get_abs_path(opts.path)
|
||||
if not Helpers.has_permission_to_access(abs_path) then return false, "No permission to access path: " .. abs_path end
|
||||
local sidebar = require("avante").get()
|
||||
if not sidebar then return false, "Avante sidebar not found" end
|
||||
local get_bufnr = function()
|
||||
local current_winid = vim.api.nvim_get_current_win()
|
||||
vim.api.nvim_set_current_win(sidebar.code.winid)
|
||||
local bufnr = Utils.get_or_create_buffer_with_filepath(abs_path)
|
||||
vim.api.nvim_set_current_win(current_winid)
|
||||
return bufnr
|
||||
end
|
||||
if opts.command == "view" then
|
||||
local view = require("avante.llm_tools.view")
|
||||
local opts_ = { path = opts.path }
|
||||
@@ -54,85 +44,9 @@ function M.str_replace_editor(opts, on_log, on_complete, session_ctx)
|
||||
if opts.command == "str_replace" then
|
||||
return require("avante.llm_tools.str_replace").func(opts, on_log, on_complete)
|
||||
end
|
||||
if opts.command == "create" then
|
||||
if on_log then on_log("path: " .. vim.inspect(opts.path)) end
|
||||
if opts.file_text == nil then return false, "file_text not provided" end
|
||||
if Path:new(abs_path):exists() then return false, "File already exists: " .. abs_path end
|
||||
local lines = vim.split(opts.file_text, "\n")
|
||||
local bufnr = get_bufnr()
|
||||
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
||||
Helpers.confirm("Are you sure you want to create this file?", function(ok)
|
||||
if not ok then
|
||||
-- close the buffer
|
||||
vim.api.nvim_buf_delete(bufnr, { force = true })
|
||||
on_complete(false, "User canceled")
|
||||
return
|
||||
end
|
||||
-- save the file
|
||||
local current_winid = vim.api.nvim_get_current_win()
|
||||
local winid = Utils.get_winid(bufnr)
|
||||
vim.api.nvim_set_current_win(winid)
|
||||
vim.cmd("write")
|
||||
vim.api.nvim_set_current_win(current_winid)
|
||||
on_complete(true, nil)
|
||||
end)
|
||||
return
|
||||
end
|
||||
if opts.command == "insert" then
|
||||
if on_log then on_log("path: " .. vim.inspect(opts.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 opts.insert_line == nil then return false, "insert_line not provided" end
|
||||
if opts.new_str == nil then return false, "new_str not provided" end
|
||||
local ns_id = vim.api.nvim_create_namespace("avante_insert_diff")
|
||||
local bufnr = get_bufnr()
|
||||
local function clear_highlights() vim.api.nvim_buf_clear_namespace(bufnr, ns_id, 0, -1) end
|
||||
local new_lines = vim.split(opts.new_str, "\n")
|
||||
local max_col = vim.o.columns
|
||||
local virt_lines = vim
|
||||
.iter(new_lines)
|
||||
:map(function(line)
|
||||
--- append spaces to the end of the line
|
||||
local line_ = line .. string.rep(" ", max_col - #line)
|
||||
return { { line_, Highlights.INCOMING } }
|
||||
end)
|
||||
:totable()
|
||||
vim.api.nvim_buf_set_extmark(bufnr, ns_id, opts.insert_line - 1, 0, {
|
||||
virt_lines = virt_lines,
|
||||
hl_eol = true,
|
||||
hl_mode = "combine",
|
||||
})
|
||||
Helpers.confirm("Are you sure you want to insert these lines?", function(ok)
|
||||
clear_highlights()
|
||||
if not ok then
|
||||
on_complete(false, "User canceled")
|
||||
return
|
||||
end
|
||||
vim.api.nvim_buf_set_lines(bufnr, opts.insert_line - 1, opts.insert_line - 1, false, new_lines)
|
||||
on_complete(true, nil)
|
||||
end)
|
||||
return
|
||||
end
|
||||
if opts.command == "undo_edit" then
|
||||
if on_log then on_log("path: " .. vim.inspect(opts.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
|
||||
local bufnr = get_bufnr()
|
||||
Helpers.confirm("Are you sure you want to undo edit this file?", function(ok)
|
||||
if not ok then
|
||||
on_complete(false, "User canceled")
|
||||
return
|
||||
end
|
||||
local current_winid = vim.api.nvim_get_current_win()
|
||||
local winid = Utils.get_winid(bufnr)
|
||||
vim.api.nvim_set_current_win(winid)
|
||||
-- run undo
|
||||
vim.cmd("undo")
|
||||
vim.api.nvim_set_current_win(current_winid)
|
||||
on_complete(true, nil)
|
||||
end)
|
||||
return
|
||||
end
|
||||
if opts.command == "create" then return require("avante.llm_tools.create").func(opts, on_log, on_complete) end
|
||||
if opts.command == "insert" then return require("avante.llm_tools.insert").func(opts, on_log, on_complete) end
|
||||
if opts.command == "undo_edit" then return require("avante.llm_tools.undo_edit").func(opts, on_log, on_complete) end
|
||||
return false, "Unknown command: " .. opts.command
|
||||
end
|
||||
|
||||
@@ -797,6 +711,9 @@ M._tools = {
|
||||
},
|
||||
require("avante.llm_tools.str_replace"),
|
||||
require("avante.llm_tools.view"),
|
||||
require("avante.llm_tools.create"),
|
||||
require("avante.llm_tools.insert"),
|
||||
require("avante.llm_tools.undo_edit"),
|
||||
{
|
||||
name = "read_global_file",
|
||||
description = "Read the contents of a file in the global scope. If the file content is already in the context, do not use this tool.",
|
||||
|
||||
Reference in New Issue
Block a user