Skip to content

fix(#2746): background and right aligned icons in floating windows #3128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lua/nvim-tree/renderer/components/full-name.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local M = {}

local utils = require("nvim-tree.utils")
local view = require("nvim-tree.view")

local function hide(win)
if win then
Expand Down Expand Up @@ -32,7 +33,7 @@ local function effective_win_width()
return win_width - win_info[1].textoff
end

local function show()
local function show(opts)
local line_nr = vim.api.nvim_win_get_cursor(0)[1]
if vim.wo.wrap then
return
Expand All @@ -52,6 +53,11 @@ local function show()
local text_width = vim.fn.strdisplaywidth(vim.fn.substitute(line, "[^[:print:]]*$", "", "g"))
local win_width = effective_win_width()

-- windows width reduced by right aligned icons
local icon_ns_id = vim.api.nvim_get_namespaces()["NvimTreeExtmarks"]
local icon_extmarks = vim.api.nvim_buf_get_extmarks(0, icon_ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = true })
win_width = win_width - utils.extmarks_length(icon_extmarks)

if text_width < win_width then
return
end
Expand All @@ -66,6 +72,7 @@ local function show()
style = "minimal",
border = "none"
})
vim.wo[M.popup_win].winhl = view.View.winopts.winhl

local ns_id = vim.api.nvim_get_namespaces()["NvimTreeHighlights"]
local extmarks = vim.api.nvim_buf_get_extmarks(0, ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = true })
Expand All @@ -88,7 +95,10 @@ local function show()
end
end
end
vim.cmd([[ setlocal nowrap cursorline noswapfile nobuflisted buftype=nofile bufhidden=wipe ]])
vim.cmd([[ setlocal nowrap noswapfile nobuflisted buftype=nofile bufhidden=wipe ]])
if opts.view.cursorline then
vim.cmd([[ setlocal cursorline cursorlineopt=both ]])
end
end)
end

Expand All @@ -114,7 +124,7 @@ M.setup = function(opts)
pattern = { "NvimTree_*" },
callback = function()
if utils.is_nvim_tree_buf(0) then
show()
show(opts)
end
end,
})
Expand Down
15 changes: 15 additions & 0 deletions lua/nvim-tree/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ function M.find_node_line(node)
return -1
end

---@param extmarks vim.api.keyset.get_extmark_item[] as per vim.api.nvim_buf_get_extmarks
---@return number
function M.extmarks_length(extmarks)
local length = 0
for _, extmark in ipairs(extmarks) do
local details = extmark[4]
if details and details.virt_text then
for _, text in ipairs(details.virt_text) do
length = length + vim.fn.strchars(text[1])
end
end
end
return length
end

-- get the node in the tree state depending on the absolute path of the node
-- (grouped or hidden too)
---@param path string
Expand Down
9 changes: 1 addition & 8 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -329,14 +329,7 @@ local function grow()
local count = vim.fn.strchars(l)
-- also add space for right-aligned icons
local extmarks = vim.api.nvim_buf_get_extmarks(M.get_bufnr(), ns_id, { line_nr, 0 }, { line_nr, -1 }, { details = true })
for _, extmark in ipairs(extmarks) do
local virt_texts = extmark[4].virt_text
if virt_texts then
for _, virt_text in ipairs(virt_texts) do
count = count + vim.fn.strchars(virt_text[1])
end
end
end
count = count + utils.extmarks_length(extmarks)
if resizing_width < count then
resizing_width = count
end
Expand Down
Loading