feat(#2349): add "right_align" option for renderer.icons.*_placement (#2846)

* feat(icon_placement): Allow right_align icon_placemente for decorator using ext_marks nvim api

* feat(icon_placement): Allow right_align icon_placemente for decorator using ext_marks nvim api

feat(icon_placement): Allow right_align icon_placemente for decorator using ext_marks nvim api

* feat(icon_placement): consolidate doc

* fix: extra namespace added to avoid colision between right_align and full_name features

* style: rename namespace_id

---------

Co-authored-by: Alexander Courtis <alex@courtis.org>
This commit is contained in:
Everton Jr. 2024-07-28 00:26:22 -03:00 committed by GitHub
parent 82ba116bbd
commit 48d0e82f94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 54 additions and 25 deletions

View File

@ -955,6 +955,12 @@ Configuration options for icons.
Icon order and sign column precedence: Icon order and sign column precedence:
git < hidden < modified < bookmarked < diagnostics git < hidden < modified < bookmarked < diagnostics
`renderer.icons.*_placement` options may be:
- `"before"` : before file/folder, after the file/folders icons
- `"after"` : after file/folder
- `"signcolumn"` : far left, requires |nvim-tree.view.signcolumn| enabled
- `"right_align"` : far right
*nvim-tree.renderer.icons.web_devicons* *nvim-tree.renderer.icons.web_devicons*
Configure optional plugin `"nvim-tree/nvim-web-devicons"` Configure optional plugin `"nvim-tree/nvim-web-devicons"`
@ -983,33 +989,23 @@ Icon order and sign column precedence:
Type: `boolean`, Default: `true` Type: `boolean`, Default: `true`
*nvim-tree.renderer.icons.git_placement* *nvim-tree.renderer.icons.git_placement*
Place where the git icons will be rendered. Git icons placement.
Can be `"after"` or `"before"` filename (after the file/folders icons)
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
Type: `string`, Default: `"before"` Type: `string`, Default: `"before"`
*nvim-tree.renderer.icons.diagnostics_placement* *nvim-tree.renderer.icons.diagnostics_placement*
Place where the diagnostics icon will be rendered. Diganostic icon placement.
Can be `"after"` or `"before"` filename (after the file/folders icons)
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
Type: `string`, Default: `"signcolumn"` Type: `string`, Default: `"signcolumn"`
*nvim-tree.renderer.icons.modified_placement* *nvim-tree.renderer.icons.modified_placement*
Place where the modified icon will be rendered. Modified icon placement.
Can be `"after"` or `"before"` filename (after the file/folders icons)
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
Type: `string`, Default: `"after"` Type: `string`, Default: `"after"`
*nvim-tree.renderer.icons.hidden_placement* *nvim-tree.renderer.icons.hidden_placement*
Place where the hidden (dotfile) icon will be rendered. Hidden icon placement.
Can be `"after"` or `"before"` filename (after the file/folders icons)
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
Type: `string`, Default: `"after"` Type: `string`, Default: `"after"`
*nvim-tree.renderer.icons.bookmarks_placement* *nvim-tree.renderer.icons.bookmarks_placement*
Place where the bookmarks icon will be rendered. Bookmark icon placement.
Can be `"after"` or `"before"` filename (after the file/folders icons)
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
Type: `string`, Default: `signcolumn` Type: `string`, Default: `signcolumn`
*nvim-tree.renderer.icons.padding* *nvim-tree.renderer.icons.padding*

View File

@ -688,11 +688,11 @@ local ACCEPTED_STRINGS = {
highlight_diagnostics = { "none", "icon", "name", "all" }, highlight_diagnostics = { "none", "icon", "name", "all" },
highlight_clipboard = { "none", "icon", "name", "all" }, highlight_clipboard = { "none", "icon", "name", "all" },
icons = { icons = {
git_placement = { "before", "after", "signcolumn" }, git_placement = { "before", "after", "signcolumn", "right_align" },
modified_placement = { "before", "after", "signcolumn" }, modified_placement = { "before", "after", "signcolumn", "right_align" },
hidden_placement = { "before", "after", "signcolumn" }, hidden_placement = { "before", "after", "signcolumn", "right_align" },
diagnostics_placement = { "before", "after", "signcolumn" }, diagnostics_placement = { "before", "after", "signcolumn", "right_align" },
bookmarks_placement = { "before", "after", "signcolumn" }, bookmarks_placement = { "before", "after", "signcolumn", "right_align" },
}, },
}, },
help = { help = {

View File

@ -16,6 +16,7 @@ M.ICON_PLACEMENT = {
signcolumn = 1, signcolumn = 1,
before = 2, before = 2,
after = 3, after = 3,
right_align = 4,
} }
return M return M

View File

@ -61,6 +61,7 @@ function Builder:new()
lines = {}, lines = {},
markers = {}, markers = {},
signs = {}, signs = {},
extmarks = {},
} }
setmetatable(o, self) setmetatable(o, self)
self.__index = self self.__index = self
@ -229,6 +230,14 @@ function Builder:format_line(indent_markers, arrows, icon, name, node)
add_to_end(line, M.decorators[i]:icons_after(node)) add_to_end(line, M.decorators[i]:icons_after(node))
end end
local rights = {}
for i = #M.decorators, 1, -1 do
add_to_end(rights, M.decorators[i]:icons_right_align(node))
end
if #rights > 0 then
self.extmarks[self.index] = rights
end
return line return line
end end

View File

@ -74,6 +74,17 @@ function Decorator:icons_after(node)
return self:calculate_icons(node) return self:calculate_icons(node)
end end
---Icons when ICON_PLACEMENT.right_align
---@param node Node
---@return HighlightedString[]|nil icons
function Decorator:icons_right_align(node)
if not self.enabled or self.icon_placement ~= ICON_PLACEMENT.right_align then
return
end
return self:calculate_icons(node)
end
---Maybe icons, optionally implemented ---Maybe icons, optionally implemented
---@protected ---@protected
---@param _ Node ---@param _ Node

View File

@ -12,13 +12,14 @@ local M = {}
local SIGN_GROUP = "NvimTreeRendererSigns" local SIGN_GROUP = "NvimTreeRendererSigns"
local namespace_id = vim.api.nvim_create_namespace "NvimTreeHighlights" local namespace_highlights_id = vim.api.nvim_create_namespace "NvimTreeHighlights"
local namespace_extmarks_id = vim.api.nvim_create_namespace "NvimTreeExtmarks"
---@param bufnr number ---@param bufnr number
---@param lines string[] ---@param lines string[]
---@param hl_args AddHighlightArgs[] ---@param hl_args AddHighlightArgs[]
---@param signs string[] ---@param signs string[]
local function _draw(bufnr, lines, hl_args, signs) local function _draw(bufnr, lines, hl_args, signs, extmarks)
if vim.fn.has "nvim-0.10" == 1 then if vim.fn.has "nvim-0.10" == 1 then
vim.api.nvim_set_option_value("modifiable", true, { buf = bufnr }) vim.api.nvim_set_option_value("modifiable", true, { buf = bufnr })
else else
@ -38,17 +39,28 @@ local function _draw(bufnr, lines, hl_args, signs)
for i, sign_name in pairs(signs) do for i, sign_name in pairs(signs) do
vim.fn.sign_place(0, SIGN_GROUP, sign_name, bufnr, { lnum = i + 1 }) vim.fn.sign_place(0, SIGN_GROUP, sign_name, bufnr, { lnum = i + 1 })
end end
vim.api.nvim_buf_clear_namespace(bufnr, namespace_extmarks_id, 0, -1)
for i, extname in pairs(extmarks) do
for _, mark in ipairs(extname) do
vim.api.nvim_buf_set_extmark(bufnr, namespace_extmarks_id, i, -1, {
virt_text = { { mark.str, mark.hl } },
virt_text_pos = "right_align",
hl_mode = "combine",
})
end
end
end end
function M.render_hl(bufnr, hl) function M.render_hl(bufnr, hl)
if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
return return
end end
vim.api.nvim_buf_clear_namespace(bufnr, namespace_id, 0, -1) vim.api.nvim_buf_clear_namespace(bufnr, namespace_highlights_id, 0, -1)
for _, data in ipairs(hl) do for _, data in ipairs(hl) do
if type(data[1]) == "table" then if type(data[1]) == "table" then
for _, group in ipairs(data[1]) do for _, group in ipairs(data[1]) do
vim.api.nvim_buf_add_highlight(bufnr, namespace_id, group, data[2], data[3], data[4]) vim.api.nvim_buf_add_highlight(bufnr, namespace_highlights_id, group, data[2], data[3], data[4])
end end
end end
end end
@ -67,7 +79,7 @@ function M.draw()
local builder = Builder:new():build() local builder = Builder:new():build()
_draw(bufnr, builder.lines, builder.hl_args, builder.signs) _draw(bufnr, builder.lines, builder.hl_args, builder.signs, builder.extmarks)
if cursor and #builder.lines >= cursor[1] then if cursor and #builder.lines >= cursor[1] then
vim.api.nvim_win_set_cursor(view.get_winnr() or 0, cursor) vim.api.nvim_win_set_cursor(view.get_winnr() or 0, cursor)