Skip to content

Commit 1d629a5

Browse files
feat(#2349): add "right_align" option for renderer.icons.*_placement (#2839)
* 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 --------- Co-authored-by: Alexander Courtis <alex@courtis.org>
1 parent f9ff00b commit 1d629a5

File tree

6 files changed

+46
-18
lines changed

6 files changed

+46
-18
lines changed

Diff for: doc/nvim-tree-lua.txt

+10-12
Original file line numberDiff line numberDiff line change
@@ -944,6 +944,12 @@ Configuration options for icons.
944944
Icon order and sign column precedence:
945945
git < modified < bookmarked < diagnostics
946946

947+
`renderer.icons.*_placement` options may be:
948+
- `"before"` : before file/folder, after the file/folders icons
949+
- `"after"` : after file/folder
950+
- `"signcolumn"` : far left, requires |nvim-tree.view.signcolumn| enabled
951+
- `"right_align"` : far right
952+
947953
*nvim-tree.renderer.icons.web_devicons*
948954
Configure optional plugin `"nvim-tree/nvim-web-devicons"`
949955

@@ -972,27 +978,19 @@ Icon order and sign column precedence:
972978
Type: `boolean`, Default: `true`
973979

974980
*nvim-tree.renderer.icons.git_placement*
975-
Place where the git icons will be rendered.
976-
Can be `"after"` or `"before"` filename (after the file/folders icons)
977-
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
981+
Git icons placement.
978982
Type: `string`, Default: `"before"`
979983

980984
*nvim-tree.renderer.icons.diagnostics_placement*
981-
Place where the diagnostics icon will be rendered.
982-
Can be `"after"` or `"before"` filename (after the file/folders icons)
983-
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
985+
Diganostic icon placement.
984986
Type: `string`, Default: `"signcolumn"`
985987

986988
*nvim-tree.renderer.icons.modified_placement*
987-
Place where the modified icon will be rendered.
988-
Can be `"after"` or `"before"` filename (after the file/folders icons)
989-
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
989+
Modified icon placement.
990990
Type: `string`, Default: `"after"`
991991

992992
*nvim-tree.renderer.icons.bookmarks_placement*
993-
Place where the bookmarks icon will be rendered.
994-
Can be `"after"` or `"before"` filename (after the file/folders icons)
995-
or `"signcolumn"` (requires |nvim-tree.view.signcolumn| enabled).
993+
Bookmark icon placement.
996994
Type: `string`, Default: `signcolumn`
997995

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

Diff for: lua/nvim-tree.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -672,10 +672,10 @@ local ACCEPTED_STRINGS = {
672672
highlight_diagnostics = { "none", "icon", "name", "all" },
673673
highlight_clipboard = { "none", "icon", "name", "all" },
674674
icons = {
675-
git_placement = { "before", "after", "signcolumn" },
676-
modified_placement = { "before", "after", "signcolumn" },
677-
diagnostics_placement = { "before", "after", "signcolumn" },
678-
bookmarks_placement = { "before", "after", "signcolumn" },
675+
git_placement = { "before", "after", "signcolumn", "right_align" },
676+
modified_placement = { "before", "after", "signcolumn", "right_align" },
677+
diagnostics_placement = { "before", "after", "signcolumn", "right_align" },
678+
bookmarks_placement = { "before", "after", "signcolumn", "right_align" },
679679
},
680680
},
681681
help = {

Diff for: 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

Diff for: lua/nvim-tree/renderer/builder.lua

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

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

Diff for: 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

Diff for: lua/nvim-tree/renderer/init.lua

+11-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ local namespace_id = vim.api.nvim_create_namespace "NvimTreeHighlights"
1818
---@param lines string[]
1919
---@param hl_args AddHighlightArgs[]
2020
---@param signs string[]
21-
local function _draw(bufnr, lines, hl_args, signs)
21+
local function _draw(bufnr, lines, hl_args, signs, extmarks)
2222
if vim.fn.has "nvim-0.10" == 1 then
2323
vim.api.nvim_set_option_value("modifiable", true, { buf = bufnr })
2424
else
@@ -38,6 +38,15 @@ local function _draw(bufnr, lines, hl_args, signs)
3838
for i, sign_name in pairs(signs) do
3939
vim.fn.sign_place(0, SIGN_GROUP, sign_name, bufnr, { lnum = i + 1 })
4040
end
41+
for i, extname in pairs(extmarks) do
42+
for _, mark in ipairs(extname) do
43+
vim.api.nvim_buf_set_extmark(bufnr, namespace_id, i, -1, {
44+
virt_text = { { mark.str, mark.hl } },
45+
virt_text_pos = "right_align",
46+
hl_mode = "combine",
47+
})
48+
end
49+
end
4150
end
4251

4352
function M.render_hl(bufnr, hl)
@@ -67,7 +76,7 @@ function M.draw()
6776

6877
local builder = Builder:new():build()
6978

70-
_draw(bufnr, builder.lines, builder.hl_args, builder.signs)
79+
_draw(bufnr, builder.lines, builder.hl_args, builder.signs, builder.extmarks)
7180

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

0 commit comments

Comments
 (0)