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 2 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 })
text_width = text_width + view.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
24 changes: 16 additions & 8 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,21 @@ function M.open(options)
log.profile_end(profile)
end

---@param extmarks table
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
---@param extmarks table
---@param vim.api.keyset.get_extmark_item[] as per vim.api.nvim_buf_get_extmarks

---@return number
function M.extmarks_length(extmarks)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for abstracting this.

Small request: move this to utils.lua. View is being converted into a stateful explorer member as part of the multi-instance view initiative: #3109

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

local function grow()
local starts_at = M.is_root_folder_visible(require("nvim-tree.core").get_cwd()) and 1 or 0
local lines = vim.api.nvim_buf_get_lines(M.get_bufnr(), starts_at, -1, false)
Expand Down Expand Up @@ -329,14 +344,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 + M.extmarks_length(extmarks)
if resizing_width < count then
resizing_width = count
end
Expand Down
Loading