feat: allow custom input provider, removing dressing.nvim (#2173)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: yetone <yetoneful@gmail.com>
This commit is contained in:
48
lua/avante/ui/input/init.lua
Normal file
48
lua/avante/ui/input/init.lua
Normal file
@@ -0,0 +1,48 @@
|
||||
local Utils = require("avante.utils")
|
||||
|
||||
---@class avante.ui.InputOption
|
||||
---@field provider avante.InputProvider
|
||||
---@field title string
|
||||
---@field default string | nil
|
||||
---@field completion string | nil
|
||||
---@field provider_opts table | nil
|
||||
---@field on_submit fun(result: string | nil)
|
||||
---@field conceal boolean | nil -- Whether to conceal input (for passwords)
|
||||
|
||||
---@class avante.ui.Input
|
||||
---@field provider avante.InputProvider
|
||||
---@field title string
|
||||
---@field default string | nil
|
||||
---@field completion string | nil
|
||||
---@field provider_opts table | nil
|
||||
---@field on_submit fun(result: string | nil)
|
||||
---@field conceal boolean | nil
|
||||
local Input = {}
|
||||
Input.__index = Input
|
||||
|
||||
---@param opts avante.ui.InputOption
|
||||
function Input:new(opts)
|
||||
local o = {}
|
||||
setmetatable(o, Input)
|
||||
o.provider = opts.provider
|
||||
o.title = opts.title
|
||||
o.default = opts.default or ""
|
||||
o.completion = opts.completion
|
||||
o.provider_opts = opts.provider_opts or {}
|
||||
o.on_submit = opts.on_submit
|
||||
o.conceal = opts.conceal or false
|
||||
return o
|
||||
end
|
||||
|
||||
function Input:open()
|
||||
if type(self.provider) == "function" then
|
||||
self.provider(self)
|
||||
return
|
||||
end
|
||||
|
||||
local ok, provider = pcall(require, "avante.ui.input.providers." .. self.provider)
|
||||
if not ok then Utils.error("Unknown input provider: " .. self.provider) end
|
||||
provider.show(self)
|
||||
end
|
||||
|
||||
return Input
|
||||
66
lua/avante/ui/input/providers/dressing.lua
Normal file
66
lua/avante/ui/input/providers/dressing.lua
Normal file
@@ -0,0 +1,66 @@
|
||||
local api = vim.api
|
||||
local fn = vim.fn
|
||||
|
||||
local M = {}
|
||||
|
||||
---@param input avante.ui.Input
|
||||
function M.show(input)
|
||||
local ok, dressing_input = pcall(require, "dressing.input")
|
||||
if not ok then
|
||||
vim.notify("dressing.nvim not found, falling back to native input", vim.log.levels.WARN)
|
||||
require("avante.ui.input.providers.native").show(input)
|
||||
return
|
||||
end
|
||||
|
||||
-- Store state for concealing functionality
|
||||
local state = { winid = nil, input_winid = nil, input_bufnr = nil }
|
||||
|
||||
local function setup_concealing()
|
||||
if not input.conceal then return end
|
||||
|
||||
vim.defer_fn(function()
|
||||
-- Find the dressing input window
|
||||
for _, winid in ipairs(api.nvim_list_wins()) do
|
||||
local bufnr = api.nvim_win_get_buf(winid)
|
||||
if vim.bo[bufnr].filetype == "DressingInput" then
|
||||
state.input_winid = winid
|
||||
state.input_bufnr = bufnr
|
||||
vim.wo[winid].conceallevel = 2
|
||||
vim.wo[winid].concealcursor = "nvi"
|
||||
|
||||
-- Set up concealing syntax
|
||||
local prompt_length = api.nvim_strwidth(fn.prompt_getprompt(state.input_bufnr))
|
||||
api.nvim_buf_call(
|
||||
state.input_bufnr,
|
||||
function()
|
||||
vim.cmd(string.format(
|
||||
[[
|
||||
syn region SecretValue start=/^/ms=s+%s end=/$/ contains=SecretChar
|
||||
syn match SecretChar /./ contained conceal cchar=*
|
||||
]],
|
||||
prompt_length
|
||||
))
|
||||
end
|
||||
)
|
||||
break
|
||||
end
|
||||
end
|
||||
end, 50)
|
||||
end
|
||||
|
||||
-- Enhanced functionality for concealed input
|
||||
vim.ui.input({
|
||||
prompt = input.title,
|
||||
default = input.default,
|
||||
completion = input.completion,
|
||||
}, function(result)
|
||||
input.on_submit(result)
|
||||
-- Close the dressing input window after submission if we have concealing
|
||||
if input.conceal then pcall(dressing_input.close) end
|
||||
end)
|
||||
|
||||
-- Set up concealing if needed
|
||||
setup_concealing()
|
||||
end
|
||||
|
||||
return M
|
||||
23
lua/avante/ui/input/providers/native.lua
Normal file
23
lua/avante/ui/input/providers/native.lua
Normal file
@@ -0,0 +1,23 @@
|
||||
local M = {}
|
||||
|
||||
---@param input avante.ui.Input
|
||||
function M.show(input)
|
||||
local opts = {
|
||||
prompt = input.title,
|
||||
default = input.default,
|
||||
completion = input.completion,
|
||||
}
|
||||
|
||||
-- Note: Native vim.ui.input doesn't support concealing
|
||||
-- For password input, users should use dressing or snacks providers
|
||||
if input.conceal then
|
||||
vim.notify_once(
|
||||
"Native input provider doesn't support concealed input. Consider using 'dressing' or 'snacks' provider for password input.",
|
||||
vim.log.levels.WARN
|
||||
)
|
||||
end
|
||||
|
||||
vim.ui.input(opts, input.on_submit)
|
||||
end
|
||||
|
||||
return M
|
||||
23
lua/avante/ui/input/providers/snacks.lua
Normal file
23
lua/avante/ui/input/providers/snacks.lua
Normal file
@@ -0,0 +1,23 @@
|
||||
local M = {}
|
||||
|
||||
---@param input avante.ui.Input
|
||||
function M.show(input)
|
||||
local ok, snacks_input = pcall(require, "snacks.input")
|
||||
if not ok then
|
||||
vim.notify("snacks.nvim not found, falling back to native input", vim.log.levels.WARN)
|
||||
require("avante.ui.input.providers.native").show(input)
|
||||
return
|
||||
end
|
||||
|
||||
local opts = vim.tbl_deep_extend("force", {
|
||||
prompt = input.title,
|
||||
default = input.default,
|
||||
}, input.provider_opts)
|
||||
|
||||
-- Add concealing support if needed
|
||||
if input.conceal then opts.password = true end
|
||||
|
||||
snacks_input(opts, input.on_submit)
|
||||
end
|
||||
|
||||
return M
|
||||
Reference in New Issue
Block a user