feat(#1917): add diagnostic highlighting and icon placement (#2396)

* feat(#1917): add renderer.highlight_diagnostics

* feat(#1917): add renderer.highlight_diagnostics

* feat(#1917): add enderer.icons.diagnostics_placement

* feat(#1917): add renderer.icons.show.diagnostics

* feat(#1917): document highlight overrides
This commit is contained in:
Alexander Courtis
2023-09-03 12:29:33 +10:00
committed by GitHub
parent 28c3980b25
commit 323f65cb9c
7 changed files with 148 additions and 64 deletions

View File

@@ -5,6 +5,7 @@ local git = require "nvim-tree.renderer.components.git"
local pad = require "nvim-tree.renderer.components.padding"
local icons = require "nvim-tree.renderer.components.icons"
local modified = require "nvim-tree.renderer.components.modified"
local diagnostics = require "nvim-tree.renderer.components.diagnostics"
local Builder = {}
Builder.__index = Builder
@@ -72,6 +73,14 @@ function Builder:configure_git_icons_placement(where)
return self
end
function Builder:configure_diagnostics_icon_placement(where)
if where ~= "after" and where ~= "before" and where ~= "signcolumn" then
where = "before" -- default before
end
self.diagnostics_placement = where
return self
end
function Builder:configure_modified_placement(where)
if where ~= "after" and where ~= "before" and where ~= "signcolumn" then
where = "after" -- default after
@@ -206,20 +215,29 @@ end
function Builder:_get_git_icons(node)
local git_icons = git.get_icons(node)
if git_icons and #git_icons > 0 and self.git_placement == "signcolumn" then
local sign = git_icons[1]
table.insert(self.signs, { sign = sign.hl, lnum = self.index + 1, priority = 1 })
table.insert(self.signs, { sign = git_icons[1].hl, lnum = self.index + 1, priority = 1 })
git_icons = nil
end
return git_icons
end
---@param node table
---@return HighlightedString[]|nil icon
function Builder:_get_diagnostics_icon(node)
local diagnostics_icon = diagnostics.get_icon(node)
if diagnostics_icon and self.diagnostics_placement == "signcolumn" then
table.insert(self.signs, { sign = diagnostics_icon.hl, lnum = self.index + 1, priority = 2 })
diagnostics_icon = nil
end
return diagnostics_icon
end
---@param node table
---@return HighlightedString|nil icon
function Builder:_get_modified_icon(node)
local modified_icon = modified.get_icon(node)
if modified_icon and self.modified_placement == "signcolumn" then
local sign = modified_icon
table.insert(self.signs, { sign = sign.hl, lnum = self.index + 1, priority = 3 })
table.insert(self.signs, { sign = modified_icon.hl, lnum = self.index + 1, priority = 3 })
modified_icon = nil
end
return modified_icon
@@ -263,6 +281,12 @@ function Builder:_get_highlight_override(node, unloaded_bufnr)
end
end
-- diagnostic status
local diagnostic_highlight = diagnostics.get_highlight(node)
if diagnostic_highlight then
name_hl = diagnostic_highlight
end
return icon_hl, name_hl
end
@@ -270,9 +294,10 @@ end
---@param icon HighlightedString
---@param name HighlightedString
---@param git_icons HighlightedString[]|nil
---@param diagnostics_icon HighlightedString|nil
---@param modified_icon HighlightedString|nil
---@return HighlightedString[]
function Builder:_format_line(padding, icon, name, git_icons, modified_icon)
function Builder:_format_line(padding, icon, name, git_icons, diagnostics_icon, modified_icon)
local added_len = 0
local function add_to_end(t1, t2)
for _, v in ipairs(t2) do
@@ -298,13 +323,21 @@ function Builder:_format_line(padding, icon, name, git_icons, modified_icon)
if modified_icon and self.modified_placement == "before" then
add_to_end(line, { modified_icon })
end
if diagnostics_icon and self.diagnostics_placement == "before" then
add_to_end(line, { diagnostics_icon })
end
add_to_end(line, { name })
if git_icons and self.git_placement == "after" then
add_to_end(line, git_icons)
end
if modified_icon and self.modified_placement == "after" then
add_to_end(line, { modified_icon })
end
if diagnostics_icon and self.diagnostics_placement == "after" then
add_to_end(line, { diagnostics_icon })
end
return line
end
@@ -314,6 +347,7 @@ function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
local padding = pad.get_padding(self.depth, idx, num_children, node, self.markers)
local git_icons = self:_get_git_icons(node)
local modified_icon = self:_get_modified_icon(node)
local diagnostics_icon = self:_get_diagnostics_icon(node)
-- main components
local is_folder = node.nodes ~= nil
@@ -336,7 +370,7 @@ function Builder:_build_line(node, idx, num_children, unloaded_bufnr)
name.hl = name_hl
end
local line = self:_format_line(padding, icon, name, git_icons, modified_icon)
local line = self:_format_line(padding, icon, name, git_icons, diagnostics_icon, modified_icon)
self:_insert_line(self:_unwrap_highlighted_strings(line))
self.index = self.index + 1

View File

@@ -0,0 +1,58 @@
local M = {}
local H = {}
local I = {}
---diagnostics text highlight group if there is a status
---@param node table
---@return string|nil highlight
function M.get_highlight(node)
if M.config.diagnostics.enable and M.config.renderer.highlight_diagnostics then
return H[node.diag_status]
end
end
---diagnostics icon if there is a status
---@param node table
---@return HighlightedString|nil modified icon
function M.get_icon(node)
if M.config.diagnostics.enable and M.config.renderer.icons.show.diagnostics then
return I[node.diag_status]
end
end
function M.setup(opts)
M.config = {
diagnostics = opts.diagnostics,
renderer = opts.renderer,
}
H[vim.diagnostic.severity.ERROR] = "NvimTreeLspDiagnosticsErrorText"
H[vim.diagnostic.severity.WARN] = "NvimTreeLspDiagnosticsWarningText"
H[vim.diagnostic.severity.INFO] = "NvimTreeLspDiagnosticsInfoText"
H[vim.diagnostic.severity.HINT] = "NvimTreeLspDiagnosticsHintText"
I[vim.diagnostic.severity.ERROR] = {
str = M.config.diagnostics.icons.error,
hl = "NvimTreeLspDiagnosticsError",
}
I[vim.diagnostic.severity.WARN] = {
str = M.config.diagnostics.icons.warning,
hl = "NvimTreeLspDiagnosticsWarning",
}
I[vim.diagnostic.severity.INFO] = {
str = M.config.diagnostics.icons.info,
hl = "NvimTreeLspDiagnosticsInfo",
}
I[vim.diagnostic.severity.HINT] = {
str = M.config.diagnostics.icons.hint,
hl = "NvimTreeLspDiagnosticsHint",
}
for _, i in ipairs(I) do
vim.fn.sign_define(i.hl, { text = i.str, texthl = i.hl })
end
end
return M

View File

@@ -1,5 +1,4 @@
local core = require "nvim-tree.core"
local diagnostics = require "nvim-tree.diagnostics"
local log = require "nvim-tree.log"
local view = require "nvim-tree.view"
local events = require "nvim-tree.events"
@@ -9,6 +8,7 @@ local _padding = require "nvim-tree.renderer.components.padding"
local icon_component = require "nvim-tree.renderer.components.icons"
local full_name = require "nvim-tree.renderer.components.full-name"
local git = require "nvim-tree.renderer.components.git"
local diagnostics = require "nvim-tree.renderer.components.diagnostics"
local Builder = require "nvim-tree.renderer.builder"
local live_filter = require "nvim-tree.live-filter"
local marks = require "nvim-tree.marks"
@@ -69,6 +69,7 @@ function M.draw(unloaded_bufnr)
:configure_modified_highlighting(M.config.highlight_modified)
:configure_icon_padding(M.config.icons.padding)
:configure_git_icons_placement(M.config.icons.git_placement)
:configure_diagnostics_icon_placement(M.config.icons.diagnostics_placement)
:configure_modified_placement(M.config.icons.modified_placement)
:configure_symlink_destination(M.config.symlink_destination)
:configure_filter(live_filter.filter, live_filter.prefix)
@@ -84,7 +85,6 @@ function M.draw(unloaded_bufnr)
vim.api.nvim_win_set_cursor(view.get_winnr(), cursor)
end
diagnostics.update()
marks.draw()
view.grow_from_content()
@@ -102,6 +102,7 @@ function M.setup(opts)
full_name.setup(opts)
git.setup(opts)
modified.setup(opts)
diagnostics.setup(opts)
icon_component.setup(opts)
end