docs: apply current colorscheme to default highlight groups

Problem: Not all default highlight groups show their actual colors.
Solution: Refactor `vimhelp.lua` and apply it to all relevant lists (UI
groups, syntax groups, treesitter groups, LSP groups, diagnostic groups).
This commit is contained in:
Christian Clason
2023-12-18 16:49:44 +01:00
parent db4b0aeb92
commit cc6a257c8c
5 changed files with 142 additions and 119 deletions

View File

@@ -480,7 +480,7 @@ Use |LspTokenUpdate| and |vim.lsp.semantic_tokens.highlight_token()| for more
complex highlighting.
The following groups are linked by default to standard |group-name|s:
>
@lsp.type.class Structure
@lsp.type.decorator Function
@lsp.type.enum Structure
@@ -496,7 +496,7 @@ The following groups are linked by default to standard |group-name|s:
@lsp.type.type Type
@lsp.type.typeParameter TypeDef
@lsp.type.variable Identifier
<
==============================================================================
EVENTS *lsp-events*

View File

@@ -225,7 +225,7 @@ you can see the actual color, except for "Ignore"):
Type int, long, char, etc.
StorageClass static, register, volatile, etc.
Structure struct, union, enum, etc.
Typedef A typedef
Typedef a typedef
Special any special symbol
SpecialChar special character in a constant

View File

@@ -393,7 +393,7 @@ instance, to highlight comments differently per language: >vim
hi link @comment.doc.java String
<
The following captures are linked by default to standard |group-name|s:
>
@text.literal Comment
@text.reference Identifier
@text.title Title
@@ -444,7 +444,7 @@ The following captures are linked by default to standard |group-name|s:
@preproc PreProc
@debug Debug
@tag Tag
<
*treesitter-highlight-spell*
The special `@spell` capture can be used to indicate that a node should be
spell checked by Nvim's builtin |spell| checker. For example, the following

View File

@@ -2,6 +2,27 @@
vim.treesitter.start()
-- add custom highlights for list in `:h highlight-groups`
if vim.endswith(vim.fs.normalize(vim.api.nvim_buf_get_name(0)), '/doc/syntax.txt') then
require('vim.vimhelp').highlight_groups()
local bufname = vim.fs.normalize(vim.api.nvim_buf_get_name(0))
if vim.endswith(bufname, '/doc/syntax.txt') then
require('vim.vimhelp').highlight_groups({
{ start = [[\*group-name\*]], stop = '^======', match = '^(%w+)\t' },
{ start = [[\*highlight-groups\*]], stop = '^======', match = '^(%w+)\t' },
})
elseif vim.endswith(bufname, '/doc/treesitter.txt') then
require('vim.vimhelp').highlight_groups({
{
start = [[\*treesitter-highlight-groups\*]],
stop = [[\*treesitter-highlight-spell\*]],
match = '^@[%w%p]+',
},
})
elseif vim.endswith(bufname, '/doc/diagnostic.txt') then
require('vim.vimhelp').highlight_groups({
{ start = [[\*diagnostic-highlights\*]], stop = '^======', match = '^(%w+)' },
})
elseif vim.endswith(bufname, '/doc/lsp.txt') then
require('vim.vimhelp').highlight_groups({
{ start = [[\*lsp-highlight\*]], stop = '^------', match = '^(%w+)' },
{ start = [[\*lsp-semantic-highlight\*]], stop = '^======', match = '^@[%w%p]+' },
})
end

View File

@@ -2,28 +2,30 @@
local M = {}
-- Called when editing the doc/syntax.txt file
function M.highlight_groups()
local save_cursor = vim.fn.getcurpos()
local start_lnum = vim.fn.search([[\*highlight-groups\*]], 'c')
if start_lnum == 0 then
return
end
local end_lnum = vim.fn.search('^======')
if end_lnum == 0 then
return
end
--- Apply current colorscheme to lists of default highlight groups
---
--- Note: {patterns} is assumed to be sorted by occurrence in the file.
--- @param patterns {start:string,stop:string,match:string}[]
function M.highlight_groups(patterns)
local ns = vim.api.nvim_create_namespace('vimhelp')
vim.api.nvim_buf_clear_namespace(0, ns, 0, -1)
local save_cursor = vim.fn.getcurpos()
for _, pat in pairs(patterns) do
local start_lnum = vim.fn.search(pat.start, 'c')
local end_lnum = vim.fn.search(pat.stop)
if start_lnum == 0 or end_lnum == 0 then
break
end
for lnum = start_lnum, end_lnum do
local word = vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, true)[1]:match('^(%w+)\t')
local word = vim.api.nvim_buf_get_lines(0, lnum - 1, lnum, true)[1]:match(pat.match)
if vim.fn.hlexists(word) ~= 0 then
vim.api.nvim_buf_set_extmark(0, ns, lnum - 1, 0, { end_col = #word, hl_group = word })
end
end
end
vim.fn.setpos('.', save_cursor)
end