Skip to content

Commit cbc3165

Browse files
fix(#2746): background and right aligned icons in floating windows (#3128)
* fix(#2746): fix cursorcolumn and right aligned icons in floating windows * feat: remove right aligned icons from full name float, show float over right aligned icons * refactoring: move `extmarks_length` to utils.lua * fix: decrease `win_width` instead of increasing `text_width` when computing condition for full name float to show --------- Co-authored-by: Alexander Courtis <alex@courtis.org>
1 parent bd54d1d commit cbc3165

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

lua/nvim-tree/renderer/components/full-name.lua

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local M = {}
22

33
local utils = require("nvim-tree.utils")
4+
local view = require("nvim-tree.view")
45

56
local function hide(win)
67
if win then
@@ -32,7 +33,7 @@ local function effective_win_width()
3233
return win_width - win_info[1].textoff
3334
end
3435

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

56+
-- windows width reduced by right aligned icons
57+
local icon_ns_id = vim.api.nvim_get_namespaces()["NvimTreeExtmarks"]
58+
local icon_extmarks = vim.api.nvim_buf_get_extmarks(0, icon_ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = true })
59+
win_width = win_width - utils.extmarks_length(icon_extmarks)
60+
5561
if text_width < win_width then
5662
return
5763
end
@@ -66,6 +72,7 @@ local function show()
6672
style = "minimal",
6773
border = "none"
6874
})
75+
vim.wo[M.popup_win].winhl = view.View.winopts.winhl
6976

7077
local ns_id = vim.api.nvim_get_namespaces()["NvimTreeHighlights"]
7178
local extmarks = vim.api.nvim_buf_get_extmarks(0, ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = true })
@@ -88,7 +95,10 @@ local function show()
8895
end
8996
end
9097
end
91-
vim.cmd([[ setlocal nowrap cursorline noswapfile nobuflisted buftype=nofile bufhidden=wipe ]])
98+
vim.cmd([[ setlocal nowrap noswapfile nobuflisted buftype=nofile bufhidden=wipe ]])
99+
if opts.view.cursorline then
100+
vim.cmd([[ setlocal cursorline cursorlineopt=both ]])
101+
end
92102
end)
93103
end
94104

@@ -114,7 +124,7 @@ M.setup = function(opts)
114124
pattern = { "NvimTree_*" },
115125
callback = function()
116126
if utils.is_nvim_tree_buf(0) then
117-
show()
127+
show(opts)
118128
end
119129
end,
120130
})

lua/nvim-tree/utils.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,21 @@ function M.find_node_line(node)
172172
return -1
173173
end
174174

175+
---@param extmarks vim.api.keyset.get_extmark_item[] as per vim.api.nvim_buf_get_extmarks
176+
---@return number
177+
function M.extmarks_length(extmarks)
178+
local length = 0
179+
for _, extmark in ipairs(extmarks) do
180+
local details = extmark[4]
181+
if details and details.virt_text then
182+
for _, text in ipairs(details.virt_text) do
183+
length = length + vim.fn.strchars(text[1])
184+
end
185+
end
186+
end
187+
return length
188+
end
189+
175190
-- get the node in the tree state depending on the absolute path of the node
176191
-- (grouped or hidden too)
177192
---@param path string

lua/nvim-tree/view.lua

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -329,14 +329,7 @@ local function grow()
329329
local count = vim.fn.strchars(l)
330330
-- also add space for right-aligned icons
331331
local extmarks = vim.api.nvim_buf_get_extmarks(M.get_bufnr(), ns_id, { line_nr, 0 }, { line_nr, -1 }, { details = true })
332-
for _, extmark in ipairs(extmarks) do
333-
local virt_texts = extmark[4].virt_text
334-
if virt_texts then
335-
for _, virt_text in ipairs(virt_texts) do
336-
count = count + vim.fn.strchars(virt_text[1])
337-
end
338-
end
339-
end
332+
count = count + utils.extmarks_length(extmarks)
340333
if resizing_width < count then
341334
resizing_width = count
342335
end

0 commit comments

Comments
 (0)