Skip to content

Commit e7a7ad7

Browse files
committed
fix(#2746): fix cursorcolumn and right aligned icons in floating windows
1 parent e7d1b7d commit e7a7ad7

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

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

Lines changed: 32 additions & 8 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
@@ -50,6 +51,12 @@ local function show()
5051
end
5152

5253
local text_width = vim.fn.strdisplaywidth(vim.fn.substitute(line, "[^[:print:]]*$", "", "g"))
54+
55+
-- also make space for right-aligned icons
56+
local icon_ns_id = vim.api.nvim_get_namespaces()["NvimTreeExtmarks"]
57+
local icon_extmarks = vim.api.nvim_buf_get_extmarks(0, icon_ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = true })
58+
text_width = text_width + view.extmarks_length(icon_extmarks)
59+
5360
local win_width = effective_win_width()
5461

5562
if text_width < win_width then
@@ -66,12 +73,26 @@ local function show()
6673
style = "minimal",
6774
border = "none"
6875
})
76+
vim.wo[M.popup_win].winhl = view.View.winopts.winhl
6977

70-
local ns_id = vim.api.nvim_get_namespaces()["NvimTreeHighlights"]
71-
local extmarks = vim.api.nvim_buf_get_extmarks(0, ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = true })
78+
local hl_ns_id = vim.api.nvim_get_namespaces()["NvimTreeHighlights"]
79+
local hl_extmarks = vim.api.nvim_buf_get_extmarks(0, hl_ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = true })
7280
vim.api.nvim_win_call(M.popup_win, function()
7381
vim.api.nvim_buf_set_lines(0, 0, -1, true, { line })
74-
for _, extmark in ipairs(extmarks) do
82+
83+
-- copy also right-aligned icons
84+
for _, extmark in ipairs(icon_extmarks) do
85+
local details = extmark[4]
86+
if details then
87+
vim.api.nvim_buf_set_extmark(0, icon_ns_id, 0, 0, {
88+
virt_text = details.virt_text,
89+
virt_text_pos = details.virt_text_pos,
90+
hl_mode = details.hl_mode,
91+
})
92+
end
93+
end
94+
95+
for _, extmark in ipairs(hl_extmarks) do
7596
-- nvim 0.10 luadoc is incorrect: vim.api.keyset.get_extmark_item is missing the extmark_id at the start
7697

7798
---@cast extmark table
@@ -82,13 +103,16 @@ local function show()
82103

83104
if type(details) == "table" then
84105
if vim.fn.has("nvim-0.11") == 1 and vim.hl and vim.hl.range then
85-
vim.hl.range(0, ns_id, details.hl_group, { 0, col }, { 0, details.end_col, }, {})
106+
vim.hl.range(0, hl_ns_id, details.hl_group, { 0, col }, { 0, details.end_col, }, {})
86107
else
87-
vim.api.nvim_buf_add_highlight(0, ns_id, details.hl_group, 0, col, details.end_col) ---@diagnostic disable-line: deprecated
108+
vim.api.nvim_buf_add_highlight(0, hl_ns_id, details.hl_group, 0, col, details.end_col) ---@diagnostic disable-line: deprecated
88109
end
89110
end
90111
end
91-
vim.cmd([[ setlocal nowrap cursorline noswapfile nobuflisted buftype=nofile bufhidden=wipe ]])
112+
vim.cmd([[ setlocal nowrap noswapfile nobuflisted buftype=nofile bufhidden=wipe ]])
113+
if opts.view.cursorline then
114+
vim.cmd([[ setlocal cursorline cursorlineopt=both ]])
115+
end
92116
end)
93117
end
94118

@@ -114,7 +138,7 @@ M.setup = function(opts)
114138
pattern = { "NvimTree_*" },
115139
callback = function()
116140
if utils.is_nvim_tree_buf(0) then
117-
show()
141+
show(opts)
118142
end
119143
end,
120144
})

lua/nvim-tree/view.lua

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,21 @@ function M.open(options)
302302
log.profile_end(profile)
303303
end
304304

305+
---@param extmarks table
306+
---@return number
307+
function M.extmarks_length(extmarks)
308+
local length = 0
309+
for _, extmark in ipairs(extmarks) do
310+
local details = extmark[4]
311+
if details and details.virt_text then
312+
for _, text in ipairs(details.virt_text) do
313+
length = length + vim.fn.strchars(text[1])
314+
end
315+
end
316+
end
317+
return length
318+
end
319+
305320
local function grow()
306321
local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
307322
local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false)
@@ -329,14 +344,7 @@ local function grow()
329344
local count = vim.fn.strchars(l)
330345
-- also add space for right-aligned icons
331346
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
347+
count = count + M.extmarks_length(extmarks)
340348
if resizing_width < count then
341349
resizing_width = count
342350
end

0 commit comments

Comments
 (0)