Skip to content

Commit

Permalink
Merge branch 'nvim-tree:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
dxrcy authored Jan 30, 2024
2 parents e942899 + f39f7b6 commit be1e6e4
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 90 deletions.
56 changes: 42 additions & 14 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ CONTENTS *nvim-tree*
7. Mappings |nvim-tree-mappings|
7.1 Mappings: Default |nvim-tree-mappings-default|
8. Highlight |nvim-tree-highlight|
8.1 Highlight Overhaul |nvim-tree-highlight-overhaul|
8.1 Highlight Default |nvim-tree-highlight-default|
8.2 Highlight Overhaul |nvim-tree-highlight-overhaul|
9. Events |nvim-tree-events|
10. Prompts |nvim-tree-prompts|
11. OS Specific Restrictions |nvim-tree-os-specific|
Expand Down Expand Up @@ -793,6 +794,7 @@ Use nvim-tree in a floating window.

Highlight precedence, additive:
git < opened < modified < bookmarked < diagnostics < copied < cut
Neovim <= 0.8 will only show the highest.

*nvim-tree.renderer.add_trailing*
Appends a trailing slash to folder names.
Expand Down Expand Up @@ -847,7 +849,7 @@ Value can be `"none"`, `"icon"`, `"name"` or `"all"`.

*nvim-tree.renderer.highlight_opened_files*
Highlight icons and/or names for |bufloaded()| files using the
`NvimTreeOpenedFile` highlight group.
`NvimTreeOpenedHL` highlight group.
See |nvim-tree-api.navigate.opened.next()| and |nvim-tree-api.navigate.opened.prev()|
Value can be `"none"`, `"icon"`, `"name"` or `"all"`.
Type: `string`, Default: `"none"`
Expand Down Expand Up @@ -2241,12 +2243,29 @@ groups.
Example |:highlight| >
:hi NvimTreeSymlink guifg=blue gui=bold,underline
<
It is recommended to enable 'termguicolors' for the more pleasant 24-bit colours.
It is recommended to enable 'termguicolors' for the more pleasant 24-bit
colours.

To view the active highlight groups run `:so $VIMRUNTIME/syntax/hitest.vim`
as per |:highlight|

Default linked group or definition follows name.
The `*HL` groups are additive as per |nvim-tree-opts-renderer| precedence.
Only present attributes will clobber each other.
In this example a modified, opened file will have magenta text, with cyan
undercurl: >
:hi NvimTreeOpenedHL guifg=magenta guisp=red gui=underline
:hi NvimTreeModifiedFileHL guisp=cyan gui=undercurl
<
To prevent usage of a highlight:
- Before setup: link the group to `Normal` e.g.
`:hi NvimTreeExecFile Normal`
- After setup: link it to `NONE`, to override the default link e.g.
`:hi! link NvimTreeExecFile NONE`

==============================================================================
8.1 HIGHLIGHT DEFAULT *nvim-tree-highlight-default*

|:highlight-link| `default` or |:highlight-default| define the groups on setup:

Standard: >
NvimTreeNormal Normal
Expand All @@ -2267,14 +2286,13 @@ Standard: >
NvimTreeStatusLineNC StatusLineNC
<
File Text: >
NvimTreeExecFile Constant
NvimTreeImageFile PreProc
NvimTreeOpenedFile Constant
NvimTreeSpecialFile PreProc
NvimTreeSymlink Statement
NvimTreeExecFile SpellCap
NvimTreeImageFile SpellCap
NvimTreeSpecialFile SpellCap
NvimTreeSymlink SpellCap
<
Folder Text: >
NvimTreeRootFolder PreProc
NvimTreeRootFolder Title
NvimTreeFolderName Directory
NvimTreeEmptyFolderName Directory
NvimTreeOpenedFolderName Directory
Expand All @@ -2283,7 +2301,6 @@ Folder Text: >
File Icons: >
NvimTreeFileIcon NvimTreeNormal
NvimTreeSymlinkIcon NvimTreeNormal
NvimTreeOpenedFileIcon NvimTreeOpenedFile
<
Folder Icons: >
NvimTreeFolderIcon guifg=#8094b4 ctermfg=Blue
Expand All @@ -2307,16 +2324,16 @@ Clipboard: >
NvimTreeCutHL SpellBad
<
Bookmarks: >
NvimTreeBookmarkIcon Constant
NvimTreeBookmarkIcon NvimTreeFolderIcon
NvimTreeBookmarkHL SpellLocal
<
Modified: >
NvimTreeModifiedIcon Constant
NvimTreeModifiedIcon Type
NvimTreeModifiedFileHL NvimTreeModifiedIcon
NvimTreeModifiedFolderHL NvimTreeModifiedIcon
<
Opened: >
NvimTreeOpenedHL Constant
NvimTreeOpenedHL Special
<
Git Icon: >
NvimTreeGitDeletedIcon Statement
Expand Down Expand Up @@ -2378,6 +2395,17 @@ Diagnostics Folder Highlight: >
- `highlight_xxx` is additive instead of overwriting. See
|nvim-tree-opts-renderer| for precedence.

2024-01-29: disambiguate default highlights sharing groups:

- NvimTreeRootFolder PreProc -> Title
- NvimTreeModified* Constant -> Type
- NvimTreeOpenedHL Constant -> Special
- NvimTreeBookmarkIcon Constant -> NvimTreeFolderIcon
- NvimTreeExecFile Constant -> SpellCap
- NvimTreeImageFile PreProc -> SpellCap
- NvimTreeSpecialFile PreProc -> SpellCap
- NvimTreeSymlink Statement -> SpellCap

Legacy highlight group are still obeyed when they are defined and the current
highlight group is not, hard linking as follows: >
Expand Down
5 changes: 3 additions & 2 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,12 @@ local function setup_autocommands(opts)
vim.api.nvim_create_autocmd(name, vim.tbl_extend("force", default_opts, custom_opts))
end

-- reset and draw highlights when colorscheme is changed
-- reset and draw (highlights) when colorscheme is changed
create_nvim_tree_autocmd("ColorScheme", {
callback = function()
appearance.setup()
renderer.render_hl(view.get_bufnr())
view.reset_winhl()
renderer.draw()
end,
})

Expand Down
56 changes: 19 additions & 37 deletions lua/nvim-tree/appearance.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
local M = {
-- namespace for all tree window highlights
NS_ID = vim.api.nvim_create_namespace "nvim_tree",
}
local M = {}

-- directly defined groups, please keep these to an absolute minimum
local DEFAULT_DEFS = {
Expand Down Expand Up @@ -32,14 +29,13 @@ local DEFAULT_LINKS = {
NvimTreeStatusLineNC = "StatusLineNC",

-- File Text
NvimTreeExecFile = "Constant",
NvimTreeImageFile = "PreProc",
NvimTreeOpenedFile = "Constant",
NvimTreeSpecialFile = "PreProc",
NvimTreeSymlink = "Statement",
NvimTreeExecFile = "SpellCap",
NvimTreeImageFile = "SpellCap",
NvimTreeSpecialFile = "SpellCap",
NvimTreeSymlink = "SpellCap",

-- Folder Text
NvimTreeRootFolder = "PreProc",
NvimTreeRootFolder = "Title",
NvimTreeFolderName = "Directory",
NvimTreeEmptyFolderName = "Directory",
NvimTreeOpenedFolderName = "Directory",
Expand All @@ -48,7 +44,6 @@ local DEFAULT_LINKS = {
-- File Icons
NvimTreeFileIcon = "NvimTreeNormal",
NvimTreeSymlinkIcon = "NvimTreeNormal",
NvimTreeOpenedFileIcon = "NvimTreeOpenedFile",

-- Folder Icons
NvimTreeOpenedFolderIcon = "NvimTreeFolderIcon",
Expand All @@ -57,7 +52,7 @@ local DEFAULT_LINKS = {
NvimTreeFolderArrowOpen = "NvimTreeIndentMarker",

-- Indent
NvimTreeIndentMarker = "NvimTreeFileIcon",
NvimTreeIndentMarker = "NvimTreeFolderIcon",

-- LiveFilter
NvimTreeLiveFilterPrefix = "PreProc",
Expand All @@ -68,16 +63,16 @@ local DEFAULT_LINKS = {
NvimTreeCopiedHL = "SpellRare",

-- Bookmark
NvimTreeBookmarkIcon = "Constant",
NvimTreeBookmarkIcon = "NvimTreeFolderIcon",
NvimTreeBookmarkHL = "SpellLocal",

-- Modified
NvimTreeModifiedIcon = "Constant",
NvimTreeModifiedIcon = "Type",
NvimTreeModifiedFileHL = "NvimTreeModifiedIcon",
NvimTreeModifiedFolderHL = "NvimTreeModifiedFileHL",

-- Opened
NvimTreeOpenedHL = "Constant",
NvimTreeOpenedHL = "Special",

-- Git Icon
NvimTreeGitDeletedIcon = "Statement",
Expand Down Expand Up @@ -125,21 +120,6 @@ local DEFAULT_LINKS = {
NvimTreeDiagnosticHintFolderHL = "NvimTreeDiagnosticHintFileHL",
}

-- namespace standard links
local NS_LINKS = {
EndOfBuffer = "NvimTreeEndOfBuffer",
CursorLine = "NvimTreeCursorLine",
CursorLineNr = "NvimTreeCursorLineNr",
LineNr = "NvimTreeLineNr",
WinSeparator = "NvimTreeWinSeparator",
StatusLine = "NvimTreeStatusLine",
StatusLineNC = "NvimTreeStatuslineNC",
SignColumn = "NvimTreeSignColumn",
Normal = "NvimTreeNormal",
NormalNC = "NvimTreeNormalNC",
NormalFloat = "NvimTreeNormalFloat",
}

-- nvim-tree highlight groups to legacy
local LEGACY_LINKS = {
NvimTreeModifiedIcon = "NvimTreeModifiedFile",
Expand Down Expand Up @@ -196,8 +176,15 @@ function M.setup()

-- hard link override when legacy only is present
for from, to in pairs(LEGACY_LINKS) do
local hl_from = vim.api.nvim_get_hl(0, { name = from })
local hl_to = vim.api.nvim_get_hl(0, { name = to })
local hl_from
local hl_to
if vim.fn.has "nvim-0.9" == 1 then
hl_from = vim.api.nvim_get_hl(0, { name = from })
hl_to = vim.api.nvim_get_hl(0, { name = to })
else
hl_from = vim.api.nvim__get_hl_defs(0)[from] or {}
hl_to = vim.api.nvim__get_hl_defs(0)[to] or {}
end
if vim.tbl_isempty(hl_from) and not vim.tbl_isempty(hl_to) then
vim.api.nvim_command("hi link " .. from .. " " .. to)
end
Expand All @@ -207,11 +194,6 @@ function M.setup()
for from, to in pairs(DEFAULT_LINKS) do
vim.api.nvim_command("hi def link " .. from .. " " .. to)
end

-- window standard; this doesn't appear to clear on ColorScheme however we err on the side of caution
for from, to in pairs(NS_LINKS) do
vim.api.nvim_set_hl(M.NS_ID, from, { link = to })
end
end

return M
42 changes: 22 additions & 20 deletions lua/nvim-tree/renderer/builder.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local appearance = require "nvim-tree.appearance"
local core = require "nvim-tree.core"
local live_filter = require "nvim-tree.live-filter"
local notify = require "nvim-tree.notify"
Expand Down Expand Up @@ -46,7 +45,7 @@ local M = {
---@field private root_cwd string absolute path
---@field private index number
---@field private depth number
---@field private combined_groups string[] combined group names
---@field private combined_groups table<string, boolean> combined group names
---@field private markers boolean[] indent markers
local Builder = {}

Expand Down Expand Up @@ -246,22 +245,17 @@ function Builder:build_signs(node)
end
end

---Combined group name less than the 200 byte limit of highlight group names
---@private
---@param groups string[] highlight group names
---@return string name "NvimTreeCombinedHL" .. sha256
function Builder:combined_group_name(groups)
return string.format("NvimTreeCombinedHL%s", vim.fn.sha256(table.concat(groups)))
end

---Create a highlight group for groups with later groups overriding previous.
---Combined group name is less than the 200 byte limit of highlight group names
---@private
---@param groups string[] highlight group names
---@return string group_name "NvimTreeCombinedHL" .. sha256
function Builder:create_combined_group(groups)
local combined_name = self:combined_group_name(groups)
local combined_name = string.format("NvimTreeCombinedHL%s", vim.fn.sha256(table.concat(groups)))

-- only create if necessary
if not vim.tbl_contains(self.combined_groups, combined_name) then
if not self.combined_groups[combined_name] then
self.combined_groups[combined_name] = true
local combined_hl = {}

-- build the highlight, overriding values
Expand All @@ -270,11 +264,13 @@ function Builder:create_combined_group(groups)
combined_hl = vim.tbl_extend("force", combined_hl, hl)
end

-- highlight directly in the namespace
vim.api.nvim_set_hl(appearance.NS_ID, combined_name, combined_hl)
-- add highlights to the global namespace
vim.api.nvim_set_hl(0, combined_name, combined_hl)

table.insert(self.combined_groups, combined_name)
end

return combined_name
end

---Calculate highlight group for icon and name. A combined highlight group will be created
Expand All @@ -299,18 +295,24 @@ function Builder:add_highlights(node)
table.insert(name_groups, name)
end

-- one or many icon groups
-- one or many icon groups; <= 0.8 always uses highest due to lack of a practical nvim_get_hl equivalent
if #icon_groups > 1 then
icon_hl_group = self:combined_group_name(icon_groups)
self:create_combined_group(icon_groups)
if vim.fn.has "nvim-0.9" == 1 then
icon_hl_group = self:create_combined_group(icon_groups)
else
icon_hl_group = icon_groups[#icon_groups]
end
else
icon_hl_group = icon_groups[1]
end

-- one or many name groups
-- one or many name groups; <= 0.8 always uses highest due to lack of a practical nvim_get_hl equivalent
if #name_groups > 1 then
name_hl_group = self:combined_group_name(name_groups)
self:create_combined_group(name_groups)
if vim.fn.has "nvim-0.9" == 1 then
name_hl_group = self:create_combined_group(name_groups)
else
name_hl_group = name_groups[#name_groups]
end
else
name_hl_group = name_groups[1]
end
Expand Down
7 changes: 3 additions & 4 deletions lua/nvim-tree/renderer/components/full-name.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
local appearance = require "nvim-tree.appearance"

local M = {}

local utils = require "nvim-tree.utils"
Expand Down Expand Up @@ -68,12 +66,13 @@ local function show()
style = "minimal",
})

local extmarks = vim.api.nvim_buf_get_extmarks(0, appearance.NS_ID, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = 1 })
local ns_id = vim.api.nvim_get_namespaces()["NvimTreeHighlights"]
local extmarks = vim.api.nvim_buf_get_extmarks(0, ns_id, { line_nr - 1, 0 }, { line_nr - 1, -1 }, { details = 1 })
vim.api.nvim_win_call(M.popup_win, function()
vim.api.nvim_buf_set_lines(0, 0, -1, true, { line })
for _, extmark in ipairs(extmarks) do
local hl = extmark[4]
vim.api.nvim_buf_add_highlight(0, appearance.NS_ID, hl.hl_group, 0, extmark[3], hl.end_col)
vim.api.nvim_buf_add_highlight(0, ns_id, hl.hl_group, 0, extmark[3], hl.end_col)
end
vim.cmd [[ setlocal nowrap cursorline noswapfile nobuflisted buftype=nofile bufhidden=hide ]]
end)
Expand Down
Loading

0 comments on commit be1e6e4

Please sign in to comment.