feat(view): indicate modified buffers (#1835)

* Outlined new options

* highlight_modified is highlight_opened_files

* prototype with autocmd

* moved modified into glyphs

* show_on_dirs and show_on_open_dirs

* icon placement before & after

* _get_filename_offset

* fixed :wq doesn't update modified indicator

* highlight_modified, signcolumn modified_placement

Refactored to make everything use HighlightedString to remove all the complex `insert_highlight` calculation.
Not tested.

* updated doc to match the reality of no multi char for glyphs.modified

* fixed git signcolumn doesn't show

* fixed highlight_modified gets replaced by highlight_opened_files

* fixed renderer.icons.show.modified = false crash

* updated doc to reflect empty icon not breaking rendering

* removed debounce_delay to implement in a later PR

* doc nit: order placement

* change modified dirs default to be consistent with git

* illegal git & modified placement changed to default

* don't assume icon exist

* nit remove comment

* Noted in doc that glyphs can't have more than 2 characters if in signcolumn

* Don't sign_define if placement isn't signcolumn

Co-authored-by: Alexander Courtis <alex@courtis.org>
This commit is contained in:
Richard Li
2022-12-31 17:54:04 +13:00
committed by GitHub
parent 9ad93b6ac0
commit dcc344cc72
10 changed files with 381 additions and 146 deletions

View File

@@ -1,19 +1,17 @@
local notify = require "nvim-tree.notify"
local explorer_node = require "nvim-tree.explorer.node"
local M = {
SIGN_GROUP = "NvimTreeGitSigns",
}
local M = {}
local function build_icons_table(i)
local icons = {
staged = { icon = i.staged, hl = "NvimTreeGitStaged", ord = 1 },
unstaged = { icon = i.unstaged, hl = "NvimTreeGitDirty", ord = 2 },
renamed = { icon = i.renamed, hl = "NvimTreeGitRenamed", ord = 3 },
deleted = { icon = i.deleted, hl = "NvimTreeGitDeleted", ord = 4 },
unmerged = { icon = i.unmerged, hl = "NvimTreeGitMerge", ord = 5 },
untracked = { icon = i.untracked, hl = "NvimTreeGitNew", ord = 6 },
ignored = { icon = i.ignored, hl = "NvimTreeGitIgnored", ord = 7 },
staged = { str = i.staged, hl = "NvimTreeGitStaged", ord = 1 },
unstaged = { str = i.unstaged, hl = "NvimTreeGitDirty", ord = 2 },
renamed = { str = i.renamed, hl = "NvimTreeGitRenamed", ord = 3 },
deleted = { str = i.deleted, hl = "NvimTreeGitDeleted", ord = 4 },
unmerged = { str = i.unmerged, hl = "NvimTreeGitMerge", ord = 5 },
untracked = { str = i.untracked, hl = "NvimTreeGitNew", ord = 6 },
ignored = { str = i.ignored, hl = "NvimTreeGitIgnored", ord = 7 },
}
return {
["M "] = { icons.staged },
@@ -59,6 +57,8 @@ local function warn_status(git_status)
)
end
---@param node table
---@return HighlightedString[]|nil
local function get_icons_(node)
local git_status = explorer_node.get_git_status(node)
if git_status == nil then
@@ -85,6 +85,11 @@ local function get_icons_(node)
end
end
-- sort icons so it looks slightly better
table.sort(iconss, function(a, b)
return a.ord < b.ord
end)
return iconss
end
@@ -145,7 +150,9 @@ function M.setup(opts)
M.git_icons = build_icons_table(opts.renderer.icons.glyphs.git)
M.setup_signs(opts.renderer.icons.glyphs.git)
if opts.renderer.icons.git_placement == "signcolumn" then
M.setup_signs(opts.renderer.icons.glyphs.git)
end
if opts.renderer.icons.show.git then
M.get_icons = get_icons_

View File

@@ -1,7 +1,7 @@
local M = { i = {} }
local function config_symlinks()
M.i.symlink = #M.config.glyphs.symlink > 0 and M.config.glyphs.symlink .. M.config.padding or ""
M.i.symlink = #M.config.glyphs.symlink > 0 and M.config.glyphs.symlink or ""
M.i.symlink_arrow = M.config.symlink_arrow
end
@@ -28,14 +28,14 @@ local function get_folder_icon(open, is_symlink, has_children)
n = M.config.glyphs.folder.empty
end
end
return n .. M.config.padding
return n
end
local function get_file_icon_default()
local hl_group = "NvimTreeFileIcon"
local icon = M.config.glyphs.default
if #icon > 0 then
return icon .. M.config.padding, hl_group
return icon, hl_group
else
return ""
end
@@ -47,7 +47,7 @@ local function get_file_icon_webdev(fname, extension)
hl_group = "NvimTreeFileIcon"
end
if icon and hl_group ~= "DevIconDefault" then
return icon .. M.config.padding, hl_group
return icon, hl_group
elseif string.match(extension, "%.(.*)") then
-- If there are more extensions to the file, try to grab the icon for them recursively
return get_file_icon_webdev(fname, string.match(extension, "%.(.*)"))

View File

@@ -0,0 +1,41 @@
local modified = require "nvim-tree.modified"
local M = {}
local HIGHLIGHT = "NvimTreeModifiedFile"
---return modified icon if node is modified, otherwise return empty string
---@param node table
---@return HighlightedString|nil modified icon
function M.get_icon(node)
if not modified.is_modified(node) or not M.show_icon then
return nil
end
return { str = M.icon, hl = HIGHLIGHT }
end
function M.setup_signs()
vim.fn.sign_define(HIGHLIGHT, { text = M.icon, texthl = HIGHLIGHT })
end
---@param node table
---@return string|nil
function M.get_highlight(node)
if not modified.is_modified(node) then
return nil
end
return HIGHLIGHT
end
function M.setup(opts)
M.icon = opts.renderer.icons.glyphs.modified
M.show_icon = opts.renderer.icons.show.modified
if opts.renderer.icons.modified_placement == "signcolumn" then
M.setup_signs()
end
end
return M

View File

@@ -68,6 +68,12 @@ local function get_padding_arrows(node, indent)
end
end
---@param depth integer
---@param idx integer
---@param nodes_number integer
---@param node table
---@param markers table
---@return HighlightedString
function M.get_padding(depth, idx, nodes_number, node, markers)
local padding = ""
@@ -86,7 +92,7 @@ function M.get_padding(depth, idx, nodes_number, node, markers)
padding = padding .. get_padding_arrows(node, not show_markers)
end
return padding
return { str = padding, hl = "NvimTreeIndentMarker" }
end
function M.setup(opts)