From 4282cda0ac336b436edfbfb7bad4065fb8e6e4fd Mon Sep 17 00:00:00 2001 From: Aaron Pham Date: Tue, 29 Oct 2024 07:00:05 -0400 Subject: [PATCH] feat(health): support checkhealth for plugins and providers (#769) --- lua/avante/health.lua | 44 +++++++++++++++++++++++ lua/avante/init.lua | 83 ------------------------------------------- plugin/avante.lua | 73 ++++++++++++++++++++++++++++++++++--- 3 files changed, 113 insertions(+), 87 deletions(-) create mode 100644 lua/avante/health.lua diff --git a/lua/avante/health.lua b/lua/avante/health.lua new file mode 100644 index 0000000..357db84 --- /dev/null +++ b/lua/avante/health.lua @@ -0,0 +1,44 @@ +local M = {} +local H = require("vim.health") +local Utils = require("avante.utils") +local Config = require("avante.config") + +M.check = function() + H.start("avante.nvim") + + -- Required dependencies + local required_plugins = { + ["nvim-treesitter"] = "nvim-treesitter/nvim-treesitter", + ["dressing.nvim"] = "stevearc/dressing.nvim", + ["plenary.nvim"] = "nvim-lua/plenary.nvim", + ["nui.nvim"] = "MunifTanjim/nui.nvim", + } + + for plugin_name, plugin_path in pairs(required_plugins) do + if Utils.has(plugin_name) then + H.ok(string.format("Found required plugin: %s", plugin_path)) + else + H.error(string.format("Missing required plugin: %s", plugin_path)) + end + end + + -- Optional dependencies + local has_devicons = Utils.has("nvim-web-devicons") + local has_mini_icons = Utils.has("mini.icons") or Utils.has("mini.nvim") + if has_devicons or has_mini_icons then + H.ok("Found icons plugin (nvim-web-devicons or mini.icons)") + else + H.warn("No icons plugin found (nvim-web-devicons or mini.icons). Icons will not be displayed") + end + + -- Check Copilot if configured + if Config.providers and Config.providers == "copilot" then + if Utils.has("copilot.lua") or Utils.has("copilot.vim") then + H.ok("Found Copilot plugin") + else + H.error("Copilot provider is configured but neither copilot.lua nor copilot.vim is installed") + end + end +end + +return M diff --git a/lua/avante/init.lua b/lua/avante/init.lua index bcc9ebe..c5e97ea 100644 --- a/lua/avante/init.lua +++ b/lua/avante/init.lua @@ -23,87 +23,6 @@ M.did_setup = false local H = {} -H.commands = function() - ---@param n string - ---@param c vim.api.keyset.user_command.callback - ---@param o vim.api.keyset.user_command.opts - local cmd = function(n, c, o) - o = vim.tbl_extend("force", { nargs = 0 }, o or {}) - api.nvim_create_user_command("Avante" .. n, c, o) - end - - cmd("Ask", function(opts) - ---@type AskOptions - local args = { question = nil, win = {} } - local q_parts = {} - local q_ask = nil - for _, arg in ipairs(opts.fargs) do - local value = arg:match("position=(%w+)") - local ask = arg:match("ask=(%w+)") - if ask ~= nil then - q_ask = ask == "true" - elseif value then - args.win.position = value - else - table.insert(q_parts, arg) - end - end - require("avante.api").ask( - vim.tbl_deep_extend( - "force", - args, - { ask = q_ask, question = #q_parts > 0 and table.concat(q_parts, " ") or nil } - ) - ) - end, { - desc = "avante: ask AI for code suggestions", - nargs = "*", - complete = function(_, _, _) - local candidates = {} ---@type string[] - vim.list_extend( - candidates, - ---@param x string - vim.tbl_map(function(x) return "position=" .. x end, { "left", "right", "top", "bottom" }) - ) - vim.list_extend(candidates, vim.tbl_map(function(x) return "ask=" .. x end, { "true", "false" })) - return candidates - end, - }) - cmd("Chat", function() require("avante.api").ask({ ask = false }) end, { desc = "avante: chat with the codebase" }) - cmd("Toggle", function() M.toggle() end, { desc = "avante: toggle AI panel" }) - cmd( - "Edit", - function(opts) require("avante.api").edit(vim.trim(opts.args)) end, - { desc = "avante: edit selected block", nargs = "*" } - ) - cmd("Refresh", function() require("avante.api").refresh() end, { desc = "avante: refresh windows" }) - cmd("Focus", function() require("avante.api").focus() end, { desc = "avante: switch focus windows" }) - cmd("Build", function(opts) - local args = {} - for _, arg in ipairs(opts.fargs) do - local key, value = arg:match("(%w+)=(%w+)") - if key and value then args[key] = value == "true" end - end - if args.source == nil then args.source = false end - - require("avante.api").build(args) - end, { - desc = "avante: build dependencies", - nargs = "*", - complete = function(_, _, _) return { "source=true", "source=false" } end, - }) - cmd("SwitchProvider", function(opts) require("avante.api").switch_provider(vim.trim(opts.args or "")) end, { - nargs = 1, - desc = "avante: switch provider", - complete = function(_, line, _) - local prefix = line:match("AvanteSwitchProvider%s*(.*)$") or "" - ---@param key string - return vim.tbl_filter(function(key) return key:find(prefix, 1, true) == 1 end, Config.providers) - end, - }) - cmd("Clear", function() require("avante.path").clear() end, { desc = "avante: clear all chat history" }) -end - H.keymaps = function() vim.keymap.set({ "n", "v" }, "(AvanteAsk)", function() require("avante.api").ask() end, { noremap = true }) vim.keymap.set( @@ -115,7 +34,6 @@ H.keymaps = function() vim.keymap.set("v", "(AvanteEdit)", function() require("avante.api").edit() end, { noremap = true }) vim.keymap.set("n", "(AvanteRefresh)", function() require("avante.api").refresh() end, { noremap = true }) vim.keymap.set("n", "(AvanteFocus)", function() require("avante.api").focus() end, { noremap = true }) - vim.keymap.set("n", "(AvanteBuild)", function() require("avante.api").build() end, { noremap = true }) vim.keymap.set("n", "(AvanteToggle)", function() M.toggle() end, { noremap = true }) vim.keymap.set("n", "(AvanteToggleDebug)", function() M.toggle.debug() end) vim.keymap.set("n", "(AvanteToggleHint)", function() M.toggle.hint() end) @@ -446,7 +364,6 @@ function M.setup(opts) -- setup helpers H.autocmds() - H.commands() H.keymaps() H.signs() diff --git a/plugin/avante.lua b/plugin/avante.lua index 86a4a6e..1595c2c 100644 --- a/plugin/avante.lua +++ b/plugin/avante.lua @@ -8,13 +8,17 @@ if vim.fn.has("nvim-0.10") == 0 then vim.cmd([[quit]]) end ---- NOTE: We will override vim.paste if img-clip.nvim is available to work with avante.nvim internal logic paste +if vim.g.avante ~= nil then return end +vim.g.avante = 1 + +--- NOTE: We will override vim.paste if img-clip.nvim is available to work with avante.nvim internal logic paste local Clipboard = require("avante.clipboard") local Config = require("avante.config") +local api = vim.api if Config.support_paste_image() then - vim.paste = (function(overriden) + vim.paste = (function(overridden) ---@param lines string[] ---@param phase -1|1|2|3 return function(lines, phase) @@ -22,13 +26,13 @@ if Config.support_paste_image() then local bufnr = vim.api.nvim_get_current_buf() local filetype = vim.api.nvim_get_option_value("filetype", { buf = bufnr }) - if filetype ~= "AvanteInput" then return overriden(lines, phase) end + if filetype ~= "AvanteInput" then return overridden(lines, phase) end ---@type string local line = lines[1] local ok = Clipboard.paste_image(line) - if not ok then return overriden(lines, phase) end + if not ok then return overridden(lines, phase) end -- After pasting, insert a new line and set cursor to this line vim.api.nvim_buf_set_lines(bufnr, -1, -1, false, { "" }) @@ -37,3 +41,64 @@ if Config.support_paste_image() then end end)(vim.paste) end + +---@param n string +---@param c vim.api.keyset.user_command.callback +---@param o vim.api.keyset.user_command.opts +local cmd = function(n, c, o) + o = vim.tbl_extend("force", { nargs = 0 }, o or {}) + api.nvim_create_user_command("Avante" .. n, c, o) +end + +cmd("Ask", function(opts) + ---@type AskOptions + local args = { question = nil, win = {} } + local q_parts = {} + local q_ask = nil + for _, arg in ipairs(opts.fargs) do + local value = arg:match("position=(%w+)") + local ask = arg:match("ask=(%w+)") + if ask ~= nil then + q_ask = ask == "true" + elseif value then + args.win.position = value + else + table.insert(q_parts, arg) + end + end + require("avante.api").ask( + vim.tbl_deep_extend("force", args, { ask = q_ask, question = #q_parts > 0 and table.concat(q_parts, " ") or nil }) + ) +end, { + desc = "avante: ask AI for code suggestions", + nargs = "*", + complete = function(_, _, _) + local candidates = {} ---@type string[] + vim.list_extend( + candidates, + ---@param x string + vim.tbl_map(function(x) return "position=" .. x end, { "left", "right", "top", "bottom" }) + ) + vim.list_extend(candidates, vim.tbl_map(function(x) return "ask=" .. x end, { "true", "false" })) + return candidates + end, +}) +cmd("Chat", function() require("avante.api").ask({ ask = false }) end, { desc = "avante: chat with the codebase" }) +cmd("Toggle", function() require("avante").toggle() end, { desc = "avante: toggle AI panel" }) +cmd( + "Edit", + function(opts) require("avante.api").edit(vim.trim(opts.args)) end, + { desc = "avante: edit selected block", nargs = "*" } +) +cmd("Refresh", function() require("avante.api").refresh() end, { desc = "avante: refresh windows" }) +cmd("Focus", function() require("avante.api").focus() end, { desc = "avante: switch focus windows" }) +cmd("SwitchProvider", function(opts) require("avante.api").switch_provider(vim.trim(opts.args or "")) end, { + nargs = 1, + desc = "avante: switch provider", + complete = function(_, line, _) + local prefix = line:match("AvanteSwitchProvider%s*(.*)$") or "" + ---@param key string + return vim.tbl_filter(function(key) return key:find(prefix, 1, true) == 1 end, Config.providers) + end, +}) +cmd("Clear", function() require("avante.path").clear() end, { desc = "avante: clear all chat history" })