feat(renderer): allow placing git icons after filename (#1203)

This feature allows placing git icons after the filename.
This commit is contained in:
Kiyan
2022-05-07 14:05:51 +02:00
committed by GitHub
parent ec888d08ce
commit a1600e57f2
6 changed files with 74 additions and 48 deletions

View File

@@ -60,6 +60,13 @@ function Builder:configure_git_icons_padding(padding)
return self
end
function Builder:configure_git_icons_placement(where)
where = where or "before"
self.is_git_before = where == "before"
self.is_git_after = not self.is_git_before
return self
end
function Builder:_insert_highlight(group, start, end_)
table.insert(self.highlights, { group, self.index, start, end_ or -1 })
end
@@ -93,16 +100,17 @@ function Builder:_unwrap_git_data(git_icons_and_hl_groups, offset)
return icon
end
function Builder:_build_folder(node, padding, git_hl)
function Builder:_build_folder(node, padding, git_hl, git_icons_tbl)
local offset = string.len(padding)
local name = get_folder_name(node)
local has_children = #node.nodes ~= 0 or node.has_children
local icon = icons.get_folder_icon(node.open, node.link_to ~= nil, has_children)
local git_icon = self:_unwrap_git_data(git.get_icons(node), offset + #icon)
local line = padding .. icon .. git_icon .. name .. self.trailing_slash
local foldername = name .. self.trailing_slash
local git_icons = self:_unwrap_git_data(git_icons_tbl, offset + #icon + (self.is_git_after and #foldername + 1 or 0))
local fname_starts_at = offset + #icon + (self.is_git_before and #git_icons or 0)
local line = self:_format_line(padding .. icon, foldername, git_icons)
self:_insert_line(line)
if #icon > 0 then
@@ -118,76 +126,93 @@ function Builder:_build_folder(node, padding, git_hl)
foldername_hl = "NvimTreeEmptyFolderName"
end
self:_insert_highlight(foldername_hl, offset + #icon + #git_icon, #line)
self:_insert_highlight(foldername_hl, fname_starts_at, fname_starts_at + #foldername)
if git_hl then
self:_insert_highlight(git_hl, offset + #icon + #git_icon, #line)
self:_insert_highlight(git_hl, fname_starts_at, fname_starts_at + #foldername)
end
end
-- TODO: missing git icon for symlinks
function Builder:_build_symlink(node, padding, git_highlight)
function Builder:_format_line(before, after, git_icons)
return string.format(
"%s%s%s %s",
before,
self.is_git_after and "" or git_icons,
after,
self.is_git_after and git_icons or ""
)
end
function Builder:_build_symlink(node, padding, git_highlight, git_icons_tbl)
local offset = string.len(padding)
local icon = icons.i.symlink
local arrow = icons.i.symlink_arrow
local symlink_formatted = node.name .. arrow .. node.link_to
local link_highlight = git_highlight or "NvimTreeSymlink"
local line = padding .. icon .. node.name .. arrow .. node.link_to
self:_insert_highlight(link_highlight, string.len(padding), string.len(line))
local git_icons_starts_at = offset + #icon + (self.is_git_after and #symlink_formatted + 1 or 0)
local git_icons = self:_unwrap_git_data(git_icons_tbl, git_icons_starts_at)
local line = self:_format_line(padding .. icon, symlink_formatted, git_icons)
self:_insert_highlight(link_highlight, offset + (self.is_git_after and 0 or #git_icons), string.len(line))
self:_insert_line(line)
end
function Builder:_build_file_icons(node, offset)
if self.special_map[node.absolute_path] or self.special_map[node.name] then
local git_icons = self:_unwrap_git_data(git.get_icons(node), offset + #icons.i.special)
self:_insert_highlight("NvimTreeSpecialFile", offset + #git_icons)
return icons.i.special, git_icons
else
local icon, hl_group = icons.get_file_icon(node.name, node.extension)
if hl_group then
self:_insert_highlight(hl_group, offset, offset + #icon)
end
return icon, self:_unwrap_git_data(git.get_icons(node), offset + #icon)
function Builder:_build_file_icon(node, offset)
local icon, hl_group = icons.get_file_icon(node.name, node.extension)
if hl_group then
self:_insert_highlight(hl_group, offset, offset + #icon)
end
return icon, false
end
function Builder:_highlight_opened_files(node, offset, icon, git_icons)
function Builder:_highlight_opened_files(node, offset, icon_length, git_icons_length)
local from = offset
local to = offset
if self.open_file_highlight == "icon" then
to = from + #icon
to = from + icon_length
elseif self.open_file_highlight == "name" then
from = offset + #icon + #git_icons
from = offset + icon_length + git_icons_length
to = from + #node.name
elseif self.open_file_highlight == "all" then
to = -1
to = from + icon_length + git_icons_length + #node.name
end
self:_insert_highlight("NvimTreeOpenedFile", from, to)
end
function Builder:_build_file(node, padding, git_highlight)
function Builder:_build_file(node, padding, git_highlight, git_icons_tbl)
local offset = string.len(padding)
local icon, git_icons = self:_build_file_icons(node, offset)
local icon = self:_build_file_icon(node, offset)
self:_insert_line(padding .. icon .. git_icons .. node.name)
local col_start = offset + #icon + #git_icons
local git_icons_starts_at = offset + #icon + (self.is_git_after and #node.name + 1 or 0)
local git_icons = self:_unwrap_git_data(git_icons_tbl, git_icons_starts_at)
if node.executable then
self:_insert_highlight("NvimTreeExecFile", col_start)
self:_insert_line(self:_format_line(padding .. icon, node.name, git_icons))
local git_icons_length = self.is_git_after and 0 or #git_icons
local col_start = offset + #icon + git_icons_length
local col_end = col_start + #node.name
if self.special_map[node.absolute_path] or self.special_map[node.name] then
self:_insert_highlight("NvimTreeSpecialFile", col_start, col_end)
elseif node.executable then
self:_insert_highlight("NvimTreeExecFile", col_start, col_end)
elseif self.picture_map[node.extension] then
self:_insert_highlight("NvimTreeImageFile", col_start)
self:_insert_highlight("NvimTreeImageFile", col_start, col_end)
end
local should_highlight_opened_files = self.open_file_highlight and vim.fn.bufloaded(node.absolute_path) > 0
if should_highlight_opened_files then
self:_highlight_opened_files(node, offset, icon, git_icons)
self:_highlight_opened_files(node, offset, #icon, git_icons_length)
end
if git_highlight then
self:_insert_highlight(git_highlight, col_start)
self:_insert_highlight(git_highlight, col_start, col_end)
end
end
@@ -199,16 +224,17 @@ function Builder:_build_line(tree, node, idx)
end
local git_highlight = git.get_highlight(node)
local git_icons_tbl = git.get_icons(node)
local is_folder = node.nodes ~= nil
local is_symlink = node.link_to ~= nil
if is_folder then
self:_build_folder(node, padding, git_highlight)
self:_build_folder(node, padding, git_highlight, git_icons_tbl)
elseif is_symlink then
self:_build_symlink(node, padding, git_highlight)
self:_build_symlink(node, padding, git_highlight, git_icons_tbl)
else
self:_build_file(node, padding, git_highlight)
self:_build_file(node, padding, git_highlight, git_icons_tbl)
end
self.index = self.index + 1