fix: confirm ui (#1665)

This commit is contained in:
yetone
2025-03-22 01:57:22 +08:00
committed by GitHub
parent 1025d45cfa
commit 1e1cd1cf78
4 changed files with 95 additions and 21 deletions

View File

@@ -32,6 +32,8 @@ local Highlights = {
bg_link = "NormalFloat",
},
AVANTE_SIDEBAR_NORMAL = { name = "AvanteSidebarNormal", link = "NormalFloat" },
AVANTE_COMMENT_FG = { name = "AvanteCommentFg", fg_link = "Comment" },
AVANTE_REVERSED_NORMAL = { name = "AvanteReversedNormal", fg_link_bg = "Normal", bg_link_fg = "Normal" },
}
Highlights.conflict = {
@@ -65,6 +67,8 @@ function M.setup()
local fg = hl.fg
if hl.bg_link ~= nil then bg = api.nvim_get_hl(0, { name = hl.bg_link }).bg end
if hl.fg_link ~= nil then fg = api.nvim_get_hl(0, { name = hl.fg_link }).fg end
if hl.bg_link_fg ~= nil then bg = api.nvim_get_hl(0, { name = hl.bg_link_fg }).fg end
if hl.fg_link_bg ~= nil then fg = api.nvim_get_hl(0, { name = hl.fg_link_bg }).bg end
api.nvim_set_hl(
0,
hl.name,

View File

@@ -1453,7 +1453,7 @@ local base_win_options = {
wrap = false,
cursorline = false,
fillchars = "eob: ",
winhl = "CursorLine:Normal,CursorColumn:Normal,WinSeparator:"
winhighlight = "CursorLine:Normal,CursorColumn:Normal,WinSeparator:"
.. Highlights.AVANTE_SIDEBAR_WIN_SEPARATOR
.. ",Normal:"
.. Highlights.AVANTE_SIDEBAR_NORMAL,

View File

@@ -2,6 +2,7 @@ local Popup = require("nui.popup")
local NuiText = require("nui.text")
local Highlights = require("avante.highlights")
local Utils = require("avante.utils")
local Line = require("avante.ui.line")
---@class avante.ui.Confirm
---@field message string
@@ -39,27 +40,50 @@ function M:open()
local BUTTON_NORMAL = Highlights.BUTTON_DEFAULT
local BUTTON_FOCUS = Highlights.BUTTON_DEFAULT_HOVER
local keybindings_content = "<C-w>f: focus; c: code; r: resp; i: input"
local keybidings_start_col = math.floor((win_width - #keybindings_content) / 2)
local commentfg = Highlights.AVANTE_COMMENT_FG
-- local keybindings_content = "<C-w>f: focus; c: code; r: resp; i: input"
local keybindings_line = Line:new({
{ " <C-w>f ", "visual" },
{ " - focus ", commentfg },
{ " " },
{ " c ", "visual" },
{ " - code ", commentfg },
{ " " },
{ " r ", "visual" },
{ " - resp ", commentfg },
{ " " },
{ " i ", "visual" },
{ " - input ", commentfg },
{ " " },
})
local buttons_content = " Yes No "
local buttons_start_col = math.floor((win_width - #buttons_content) / 2)
local yes_button_pos = { buttons_start_col, buttons_start_col + 5 }
local no_button_pos = { buttons_start_col + 10, buttons_start_col + 14 }
local keybindings_line = string.rep(" ", keybidings_start_col) .. keybindings_content
local buttons_line = string.rep(" ", buttons_start_col) .. buttons_content
local keybindings_line_num = 1 + #vim.split(message, "\n")
local keybindings_line_num = 5 + #vim.split(message, "\n")
local buttons_line_num = 2 + #vim.split(message, "\n")
local content = vim
.iter({
"",
vim.tbl_map(function(line) return " " .. line end, vim.split(message, "\n")),
keybindings_line,
"",
buttons_line,
"",
"",
tostring(keybindings_line),
})
:flatten()
:totable()
local button_row = #content - 1
local win_height = #content
for _, line in ipairs(vim.split(message, "\n")) do
win_height = win_height + math.floor(#line / (win_width - 2))
end
local button_row = buttons_line_num + 1
local container_winid = self._container_winid or vim.api.nvim_get_current_win()
local container_width = vim.api.nvim_win_get_width(container_winid)
@@ -70,23 +94,28 @@ function M:open()
winid = container_winid,
},
position = {
row = vim.o.lines - #content - 3,
col = (container_width - win_width) / 2,
row = vim.o.lines - win_height,
col = math.floor((container_width - win_width) / 2),
},
size = { width = win_width, height = #content + 3 },
size = { width = win_width, height = win_height },
enter = self._focus ~= false,
focusable = true,
border = {
style = "rounded",
padding = { 0, 1 },
text = { top = NuiText(" Confirmation ", Highlights.CONFIRM_TITLE) },
style = { " ", " ", " ", " ", " ", " ", " ", " " },
},
buf_options = {
filetype = "AvanteConfirm",
modifiable = false,
readonly = true,
buftype = "nofile",
},
win_options = {
winblend = 10,
winfixbuf = true,
cursorline = false,
winblend = 5,
winhighlight = "NormalFloat:Normal,FloatBorder:Comment",
},
})
@@ -107,14 +136,7 @@ function M:open()
vim.api.nvim_buf_set_lines(popup.bufnr, 0, -1, false, content)
Utils.lock_buf(popup.bufnr)
vim.api.nvim_buf_add_highlight(
popup.bufnr,
0,
"Comment",
keybindings_line_num,
keybidings_start_col,
keybidings_start_col + #keybindings_content
)
keybindings_line:set_highlights(0, popup.bufnr, keybindings_line_num)
vim.api.nvim_buf_add_highlight(popup.bufnr, 0, yes_style, buttons_line_num, yes_button_pos[1], yes_button_pos[2])
vim.api.nvim_buf_add_highlight(popup.bufnr, 0, no_style, buttons_line_num, no_button_pos[1], no_button_pos[2])
focus_button(buttons_line_num + 1)
@@ -171,6 +193,16 @@ function M:open()
focus_button()
end, { buffer = popup.bufnr })
vim.keymap.set("n", "h", function()
focus_index = 1
focus_button()
end, { buffer = popup.bufnr })
vim.keymap.set("n", "l", function()
focus_index = 2
focus_button()
end, { buffer = popup.bufnr })
vim.keymap.set("n", "<Tab>", function()
focus_index = (focus_index == 1) and 2 or 1
focus_button()
@@ -251,7 +283,7 @@ function M:bind_window_focus_keymaps()
vim.keymap.set({ "n", "i" }, "<C-w>f", function() self:window_focus_handler() end)
end
function M:unbind_window_focus_keymaps() vim.keymap.del({ "n", "i" }, "<C-w>f") end
function M:unbind_window_focus_keymaps() pcall(vim.keymap.del, { "n", "i" }, "<C-w>f") end
function M:cancel()
self.callback(false)

38
lua/avante/ui/line.lua Normal file
View File

@@ -0,0 +1,38 @@
---@alias avante.ui.LineSection string[]
---
---@class avante.ui.Line
---@field sections avante.ui.LineSection[]
local M = {}
M.__index = M
---@param sections avante.ui.LineSection[]
function M:new(sections)
local this = setmetatable({}, M)
this.sections = sections
return this
end
---@param ns_id number
---@param bufnr number
---@param line number
function M:set_highlights(ns_id, bufnr, line)
if not vim.api.nvim_buf_is_valid(bufnr) then return end
local col_start = 0
for _, section in ipairs(self.sections) do
local text = section[1]
local highlight = section[2]
if highlight then vim.api.nvim_buf_add_highlight(bufnr, ns_id, highlight, line, col_start, col_start + #text) end
col_start = col_start + #text
end
end
function M:__tostring()
local content = {}
for _, section in ipairs(self.sections) do
local text = section[1]
table.insert(content, text)
end
return table.concat(content, "")
end
return M