fix: floating window highlights (#149)

This commit is contained in:
yetone 2024-08-22 14:46:08 +08:00 committed by GitHub
parent 9211babf09
commit 8c8d864637
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 42 additions and 32 deletions

View File

@ -640,11 +640,11 @@ end
function M.choose(side)
local bufnr = api.nvim_get_current_buf()
if vim.fn.mode() == "v" or vim.fn.mode() == "V" or vim.fn.mode() == "" then
vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes("<Esc>", true, false, true), "n", true)
api.nvim_feedkeys(api.nvim_replace_termcodes("<Esc>", true, false, true), "n", true)
-- have to defer so that the < and > marks are set
vim.defer_fn(function()
local start = vim.api.nvim_buf_get_mark(0, "<")[1]
local finish = vim.api.nvim_buf_get_mark(0, ">")[1]
local start = api.nvim_buf_get_mark(0, "<")[1]
local finish = api.nvim_buf_get_mark(0, ">")[1]
local position = find_position(bufnr, function(line, pos)
local left = pos.current.range_start >= start - 1
local right = pos.incoming.range_end <= finish + 1

View File

@ -1,3 +1,10 @@
local api = vim.api
local namespace = api.nvim_create_namespace("avante_floating_window")
api.nvim_set_hl(namespace, "NormalFloat", { link = "Normal" })
api.nvim_set_hl(namespace, "FloatBorder", { link = "Normal" })
---@class FloatingWindow
---@field enter boolean | nil
---@field winid integer | nil
@ -35,28 +42,30 @@ end
---@return nil
function FloatingWindow:mount()
self.bufnr = vim.api.nvim_create_buf(false, true)
self.bufnr = api.nvim_create_buf(false, true)
for option, value in pairs(self.buf_options) do
vim.api.nvim_set_option_value(option, value, { buf = self.bufnr })
api.nvim_set_option_value(option, value, { buf = self.bufnr })
end
self.winid = vim.api.nvim_open_win(self.bufnr, self.enter, self.float_options)
self.winid = api.nvim_open_win(self.bufnr, self.enter, self.float_options)
for option, value in pairs(self.win_options) do
vim.api.nvim_set_option_value(option, value, { win = self.winid })
api.nvim_set_option_value(option, value, { win = self.winid })
end
api.nvim_win_set_hl_ns(self.winid, namespace)
end
---@return nil
function FloatingWindow:unmount()
if self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr) then
vim.api.nvim_buf_delete(self.bufnr, { force = true })
if self.bufnr and api.nvim_buf_is_valid(self.bufnr) then
api.nvim_buf_delete(self.bufnr, { force = true })
self.bufnr = nil
end
if self.winid and vim.api.nvim_win_is_valid(self.winid) then
vim.api.nvim_win_close(self.winid, true)
if self.winid and api.nvim_win_is_valid(self.winid) then
api.nvim_win_close(self.winid, true)
self.winid = nil
end
end
@ -66,7 +75,7 @@ end
---@param options? table<"'once'" | "'nested'", boolean>
---@return nil
function FloatingWindow:on(event, handler, options)
vim.api.nvim_create_autocmd(event, {
api.nvim_create_autocmd(event, {
buffer = self.bufnr,
callback = handler,
once = options and options["once"] or false,

View File

@ -1,3 +1,5 @@
local api = vim.api
local M = {
TITLE = "AvanteTitle",
REVERSED_TITLE = "AvanteReversedTitle",
@ -9,14 +11,14 @@ local M = {
}
M.setup = function()
local normal = vim.api.nvim_get_hl(0, { name = "Normal" })
vim.api.nvim_set_hl(0, M.REVERSED_NORMAL, { fg = normal.bg })
vim.api.nvim_set_hl(0, M.TITLE, { fg = "#1e222a", bg = "#98c379" })
vim.api.nvim_set_hl(0, M.REVERSED_TITLE, { fg = "#98c379" })
vim.api.nvim_set_hl(0, M.SUBTITLE, { fg = "#1e222a", bg = "#56b6c2" })
vim.api.nvim_set_hl(0, M.REVERSED_SUBTITLE, { fg = "#56b6c2" })
vim.api.nvim_set_hl(0, M.THRIDTITLE, { fg = "#ABB2BF", bg = "#353B45" })
vim.api.nvim_set_hl(0, M.REVERSED_THRIDTITLE, { fg = "#353B45" })
local normal = api.nvim_get_hl(0, { name = "Normal" })
api.nvim_set_hl(0, M.REVERSED_NORMAL, { fg = normal.bg })
api.nvim_set_hl(0, M.TITLE, { fg = "#1e222a", bg = "#98c379" })
api.nvim_set_hl(0, M.REVERSED_TITLE, { fg = "#98c379" })
api.nvim_set_hl(0, M.SUBTITLE, { fg = "#1e222a", bg = "#56b6c2" })
api.nvim_set_hl(0, M.REVERSED_SUBTITLE, { fg = "#56b6c2" })
api.nvim_set_hl(0, M.THRIDTITLE, { fg = "#ABB2BF", bg = "#353B45" })
api.nvim_set_hl(0, M.REVERSED_THRIDTITLE, { fg = "#353B45" })
end
return M

View File

@ -63,7 +63,7 @@ Replace lines: {{start_line}}-{{end_line}}
Remember: Accurate line numbers are CRITICAL. The range start_line to end_line must include ALL lines to be replaced, from the very first to the very last. Double-check every range before finalizing your response, paying special attention to the start_line to ensure it hasn't shifted down. Ensure that your line numbers perfectly match the original code structure without any overall shift.
]]
local group = vim.api.nvim_create_augroup("AvanteLLM", { clear = true })
local group = api.nvim_create_augroup("AvanteLLM", { clear = true })
local active_job = nil
---@param question string

View File

@ -11,7 +11,7 @@ local Selection = {}
Selection.did_setup = false
---@param id integer the tabpage id retrieved from vim.api.nvim_get_current_tabpage()
---@param id integer the tabpage id retrieved from api.nvim_get_current_tabpage()
function Selection:new(id)
return setmetatable({
hints_popup_extmark_id = nil,
@ -46,7 +46,7 @@ function Selection:show_hints_popup()
local virt_text_line = self:get_virt_text_line()
self.hints_popup_extmark_id = vim.api.nvim_buf_set_extmark(0, NAMESPACE, virt_text_line, -1, {
self.hints_popup_extmark_id = api.nvim_buf_set_extmark(0, NAMESPACE, virt_text_line, -1, {
virt_text = { { hint_text, "Keyword" } },
virt_text_pos = "eol",
priority = PRIORITY,
@ -55,7 +55,7 @@ end
function Selection:close_hints_popup()
if self.hints_popup_extmark_id then
vim.api.nvim_buf_del_extmark(0, NAMESPACE, self.hints_popup_extmark_id)
api.nvim_buf_del_extmark(0, NAMESPACE, self.hints_popup_extmark_id)
self.hints_popup_extmark_id = nil
end
end
@ -93,7 +93,7 @@ end
function Selection:delete_autocmds()
if self.augroup then
vim.api.nvim_del_augroup_by_id(self.augroup)
api.nvim_del_augroup_by_id(self.augroup)
end
self.augroup = nil
Selection.did_setup = false

View File

@ -38,7 +38,7 @@ local Sidebar = {}
---@field input_container NuiSplit | nil
---@field input NuiSplit | nil
---@param id integer the tabpage id retrieved from vim.api.nvim_get_current_tabpage()
---@param id integer the tabpage id retrieved from api.nvim_get_current_tabpage()
function Sidebar:new(id)
return setmetatable({
id = id,
@ -65,7 +65,7 @@ end
function Sidebar:delete_autocmds()
if self.augroup then
vim.api.nvim_del_augroup_by_id(self.augroup)
api.nvim_del_augroup_by_id(self.augroup)
end
self.augroup = nil
end
@ -603,7 +603,7 @@ function Sidebar:on_mount()
api.nvim_create_autocmd("WinLeave", {
group = self.augroup,
callback = function()
previous_winid = vim.api.nvim_get_current_win()
previous_winid = api.nvim_get_current_win()
end,
})
@ -1141,8 +1141,8 @@ function Sidebar:get_selected_code_size()
end
local function create_floating_window_for_split(split_winid, buf_opts, win_opts, float_opts)
local height = vim.api.nvim_win_get_height(split_winid)
local width = vim.api.nvim_win_get_width(split_winid)
local height = api.nvim_win_get_height(split_winid)
local width = api.nvim_win_get_width(split_winid)
local float_opts_ = vim.tbl_deep_extend("force", {
relative = "win",
@ -1182,7 +1182,6 @@ function Sidebar:render()
buf_options = buf_options,
win_options = get_win_options(),
size = {
height = math.max(0, sidebar_height - selected_code_size - 8),
width = string.format("%d%%", Config.windows.width),
},
})

View File

@ -159,7 +159,7 @@ function M.notify(msg, opts)
vim.wo[win].conceallevel = 3
vim.wo[win].concealcursor = ""
vim.wo[win].spell = false
local buf = vim.api.nvim_win_get_buf(win)
local buf = api.nvim_win_get_buf(win)
if not pcall(vim.treesitter.start, buf, lang) then
vim.bo[buf].filetype = lang
vim.bo[buf].syntax = lang