Skip to content

Commit 48d0e82

Browse files
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 <[email protected]>
1 parent 82ba116 commit 48d0e82

File tree

6 files changed

+54
-25
lines changed

6 files changed

+54
-25
lines changed

doc/nvim-tree-lua.txt

+11-15
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,12 @@ Configuration options for icons.
955955
Icon order and sign column precedence:
956956
git < hidden < modified < bookmarked < diagnostics
957957

958+
`renderer.icons.*_placement` options may be:
959+
- `"before"` : before file/folder, after the file/folders icons
960+
- `"after"` : after file/folder
961+
- `"signcolumn"` : far left, requires |nvim-tree.view.signcolumn| enabled
962+
- `"right_align"` : far right
963+
958964
*nvim-tree.renderer.icons.web_devicons*
959965
Configure optional plugin `"nvim-tree/nvim-web-devicons"`
960966

@@ -983,33 +989,23 @@ Icon order and sign column precedence:
983989
Type: `boolean`, Default: `true`
984990

985991
*nvim-tree.renderer.icons.git_placement*
986-
Place where the git icons will be rendered.
987-
Can be `"after"` or `"before"` filename (after the file/folders icons)
988-
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
992+
Git icons placement.
989993
Type: `string`, Default: `"before"`
990994

991995
*nvim-tree.renderer.icons.diagnostics_placement*
992-
Place where the diagnostics icon will be rendered.
993-
Can be `"after"` or `"before"` filename (after the file/folders icons)
994-
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
996+
Diganostic icon placement.
995997
Type: `string`, Default: `"signcolumn"`
996998

997999
*nvim-tree.renderer.icons.modified_placement*
998-
Place where the modified icon will be rendered.
999-
Can be `"after"` or `"before"` filename (after the file/folders icons)
1000-
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
1000+
Modified icon placement.
10011001
Type: `string`, Default: `"after"`
10021002

10031003
*nvim-tree.renderer.icons.hidden_placement*
1004-
Place where the hidden (dotfile) icon will be rendered.
1005-
Can be `"after"` or `"before"` filename (after the file/folders icons)
1006-
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
1004+
Hidden icon placement.
10071005
Type: `string`, Default: `"after"`
10081006

10091007
*nvim-tree.renderer.icons.bookmarks_placement*
1010-
Place where the bookmarks icon will be rendered.
1011-
Can be `"after"` or `"before"` filename (after the file/folders icons)
1012-
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
1008+
Bookmark icon placement.
10131009
Type: `string`, Default: `signcolumn`
10141010

10151011
*nvim-tree.renderer.icons.padding*

lua/nvim-tree.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -688,11 +688,11 @@ local ACCEPTED_STRINGS = {
688688
highlight_diagnostics = { "none", "icon", "name", "all" },
689689
highlight_clipboard = { "none", "icon", "name", "all" },
690690
icons = {
691-
git_placement = { "before", "after", "signcolumn" },
692-
modified_placement = { "before", "after", "signcolumn" },
693-
hidden_placement = { "before", "after", "signcolumn" },
694-
diagnostics_placement = { "before", "after", "signcolumn" },
695-
bookmarks_placement = { "before", "after", "signcolumn" },
691+
git_placement = { "before", "after", "signcolumn", "right_align" },
692+
modified_placement = { "before", "after", "signcolumn", "right_align" },
693+
hidden_placement = { "before", "after", "signcolumn", "right_align" },
694+
diagnostics_placement = { "before", "after", "signcolumn", "right_align" },
695+
bookmarks_placement = { "before", "after", "signcolumn", "right_align" },
696696
},
697697
},
698698
help = {

lua/nvim-tree/enum.lua

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ M.ICON_PLACEMENT = {
1616
signcolumn = 1,
1717
before = 2,
1818
after = 3,
19+
right_align = 4,
1920
}
2021

2122
return M

lua/nvim-tree/renderer/builder.lua

+9
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ function Builder:new()
6161
lines = {},
6262
markers = {},
6363
signs = {},
64+
extmarks = {},
6465
}
6566
setmetatable(o, self)
6667
self.__index = self
@@ -229,6 +230,14 @@ function Builder:format_line(indent_markers, arrows, icon, name, node)
229230
add_to_end(line, M.decorators[i]:icons_after(node))
230231
end
231232

233+
local rights = {}
234+
for i = #M.decorators, 1, -1 do
235+
add_to_end(rights, M.decorators[i]:icons_right_align(node))
236+
end
237+
if #rights > 0 then
238+
self.extmarks[self.index] = rights
239+
end
240+
232241
return line
233242
end
234243

lua/nvim-tree/renderer/decorator/init.lua

+11
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ function Decorator:icons_after(node)
7474
return self:calculate_icons(node)
7575
end
7676

77+
---Icons when ICON_PLACEMENT.right_align
78+
---@param node Node
79+
---@return HighlightedString[]|nil icons
80+
function Decorator:icons_right_align(node)
81+
if not self.enabled or self.icon_placement ~= ICON_PLACEMENT.right_align then
82+
return
83+
end
84+
85+
return self:calculate_icons(node)
86+
end
87+
7788
---Maybe icons, optionally implemented
7889
---@protected
7990
---@param _ Node

lua/nvim-tree/renderer/init.lua

+17-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ local M = {}
1212

1313
local SIGN_GROUP = "NvimTreeRendererSigns"
1414

15-
local namespace_id = vim.api.nvim_create_namespace "NvimTreeHighlights"
15+
local namespace_highlights_id = vim.api.nvim_create_namespace "NvimTreeHighlights"
16+
local namespace_extmarks_id = vim.api.nvim_create_namespace "NvimTreeExtmarks"
1617

1718
---@param bufnr number
1819
---@param lines string[]
1920
---@param hl_args AddHighlightArgs[]
2021
---@param signs string[]
21-
local function _draw(bufnr, lines, hl_args, signs)
22+
local function _draw(bufnr, lines, hl_args, signs, extmarks)
2223
if vim.fn.has "nvim-0.10" == 1 then
2324
vim.api.nvim_set_option_value("modifiable", true, { buf = bufnr })
2425
else
@@ -38,17 +39,28 @@ local function _draw(bufnr, lines, hl_args, signs)
3839
for i, sign_name in pairs(signs) do
3940
vim.fn.sign_place(0, SIGN_GROUP, sign_name, bufnr, { lnum = i + 1 })
4041
end
42+
43+
vim.api.nvim_buf_clear_namespace(bufnr, namespace_extmarks_id, 0, -1)
44+
for i, extname in pairs(extmarks) do
45+
for _, mark in ipairs(extname) do
46+
vim.api.nvim_buf_set_extmark(bufnr, namespace_extmarks_id, i, -1, {
47+
virt_text = { { mark.str, mark.hl } },
48+
virt_text_pos = "right_align",
49+
hl_mode = "combine",
50+
})
51+
end
52+
end
4153
end
4254

4355
function M.render_hl(bufnr, hl)
4456
if not bufnr or not vim.api.nvim_buf_is_loaded(bufnr) then
4557
return
4658
end
47-
vim.api.nvim_buf_clear_namespace(bufnr, namespace_id, 0, -1)
59+
vim.api.nvim_buf_clear_namespace(bufnr, namespace_highlights_id, 0, -1)
4860
for _, data in ipairs(hl) do
4961
if type(data[1]) == "table" then
5062
for _, group in ipairs(data[1]) do
51-
vim.api.nvim_buf_add_highlight(bufnr, namespace_id, group, data[2], data[3], data[4])
63+
vim.api.nvim_buf_add_highlight(bufnr, namespace_highlights_id, group, data[2], data[3], data[4])
5264
end
5365
end
5466
end
@@ -67,7 +79,7 @@ function M.draw()
6779

6880
local builder = Builder:new():build()
6981

70-
_draw(bufnr, builder.lines, builder.hl_args, builder.signs)
82+
_draw(bufnr, builder.lines, builder.hl_args, builder.signs, builder.extmarks)
7183

7284
if cursor and #builder.lines >= cursor[1] then
7385
vim.api.nvim_win_set_cursor(view.get_winnr() or 0, cursor)

0 commit comments

Comments
 (0)