fix(hl_map): adapt to upstream changes on master

https://github.com/neovim/neovim/pull/19931 adds direct support for
highlighting capture groups (with fallback), obviating the need for
`vim.treesitter.highlighter.hl_map`. Instead of

`hl_map["@keyword"] = "TSKeyword"`

users can simply

`hi link TSKeyword @keyword`

Check for the existence of `hl_map` and either use the former or the
latter.
This commit is contained in:
Christian Clason 2022-08-25 15:55:19 +02:00 committed by Stephan Seitz
parent 6b0ddaa9b3
commit 1baf751fb0

View file

@ -6,116 +6,133 @@ local configs = require "nvim-treesitter.configs"
local M = {} local M = {}
local hlmap = vim.treesitter.highlighter.hl_map
-- nvim-treesitter Highlight Group Mappings -- nvim-treesitter Highlight Group Mappings
-- Note: Some highlight groups may not be applied upstream, some may be experimental -- Note: Some highlight groups may not be applied upstream, some may be experimental
-- TODO(clason): deprecated and will be removed for Nvim 0.8
hlmap["annotation"] = "TSAnnotation" local default_map = {
["annotation"] = "TSAnnotation",
hlmap["attribute"] = "TSAttribute" ["attribute"] = "TSAttribute",
hlmap["boolean"] = "TSBoolean" ["boolean"] = "TSBoolean",
hlmap["character"] = "TSCharacter" ["character"] = "TSCharacter",
hlmap["character.special"] = "TSCharacterSpecial" ["character.special"] = "TSCharacterSpecial",
hlmap["comment"] = "TSComment" ["comment"] = "TSComment",
hlmap["conditional"] = "TSConditional" ["conditional"] = "TSConditional",
hlmap["constant"] = "TSConstant" ["constant"] = "TSConstant",
hlmap["constant.builtin"] = "TSConstBuiltin" ["constant.builtin"] = "TSConstBuiltin",
hlmap["constant.macro"] = "TSConstMacro" ["constant.macro"] = "TSConstMacro",
hlmap["constructor"] = "TSConstructor" ["constructor"] = "TSConstructor",
hlmap["debug"] = "TSDebug" ["debug"] = "TSDebug",
hlmap["define"] = "TSDefine" ["define"] = "TSDefine",
hlmap["error"] = "TSError" ["error"] = "TSError",
hlmap["exception"] = "TSException" ["exception"] = "TSException",
hlmap["field"] = "TSField" ["field"] = "TSField",
hlmap["float"] = "TSFloat" ["float"] = "TSFloat",
hlmap["function"] = "TSFunction" ["function"] = "TSFunction",
hlmap["function.call"] = "TSFunctionCall" ["function.call"] = "TSFunctionCall",
hlmap["function.builtin"] = "TSFuncBuiltin" ["function.builtin"] = "TSFuncBuiltin",
hlmap["function.macro"] = "TSFuncMacro" ["function.macro"] = "TSFuncMacro",
hlmap["include"] = "TSInclude" ["include"] = "TSInclude",
hlmap["keyword"] = "TSKeyword" ["keyword"] = "TSKeyword",
hlmap["keyword.function"] = "TSKeywordFunction" ["keyword.function"] = "TSKeywordFunction",
hlmap["keyword.operator"] = "TSKeywordOperator" ["keyword.operator"] = "TSKeywordOperator",
hlmap["keyword.return"] = "TSKeywordReturn" ["keyword.return"] = "TSKeywordReturn",
hlmap["label"] = "TSLabel" ["label"] = "TSLabel",
hlmap["method"] = "TSMethod" ["method"] = "TSMethod",
hlmap["method.call"] = "TSMethodCall" ["method.call"] = "TSMethodCall",
hlmap["namespace"] = "TSNamespace" ["namespace"] = "TSNamespace",
hlmap["none"] = "TSNone" ["none"] = "TSNone",
hlmap["number"] = "TSNumber" ["number"] = "TSNumber",
hlmap["operator"] = "TSOperator" ["operator"] = "TSOperator",
hlmap["parameter"] = "TSParameter" ["parameter"] = "TSParameter",
hlmap["parameter.reference"] = "TSParameterReference" ["parameter.reference"] = "TSParameterReference",
hlmap["preproc"] = "TSPreProc" ["preproc"] = "TSPreProc",
hlmap["property"] = "TSProperty" ["property"] = "TSProperty",
hlmap["punctuation.delimiter"] = "TSPunctDelimiter" ["punctuation.delimiter"] = "TSPunctDelimiter",
hlmap["punctuation.bracket"] = "TSPunctBracket" ["punctuation.bracket"] = "TSPunctBracket",
hlmap["punctuation.special"] = "TSPunctSpecial" ["punctuation.special"] = "TSPunctSpecial",
hlmap["repeat"] = "TSRepeat" ["repeat"] = "TSRepeat",
hlmap["storageclass"] = "TSStorageClass" ["storageclass"] = "TSStorageClass",
hlmap["string"] = "TSString" ["string"] = "TSString",
hlmap["string.regex"] = "TSStringRegex" ["string.regex"] = "TSStringRegex",
hlmap["string.escape"] = "TSStringEscape" ["string.escape"] = "TSStringEscape",
hlmap["string.special"] = "TSStringSpecial" ["string.special"] = "TSStringSpecial",
hlmap["symbol"] = "TSSymbol" ["symbol"] = "TSSymbol",
hlmap["tag"] = "TSTag" ["tag"] = "TSTag",
hlmap["tag.attribute"] = "TSTagAttribute" ["tag.attribute"] = "TSTagAttribute",
hlmap["tag.delimiter"] = "TSTagDelimiter" ["tag.delimiter"] = "TSTagDelimiter",
hlmap["text"] = "TSText" ["text"] = "TSText",
hlmap["text.strong"] = "TSStrong" ["text.strong"] = "TSStrong",
hlmap["text.emphasis"] = "TSEmphasis" ["text.emphasis"] = "TSEmphasis",
hlmap["text.underline"] = "TSUnderline" ["text.underline"] = "TSUnderline",
hlmap["text.strike"] = "TSStrike" ["text.strike"] = "TSStrike",
hlmap["text.title"] = "TSTitle" ["text.title"] = "TSTitle",
hlmap["text.literal"] = "TSLiteral" ["text.literal"] = "TSLiteral",
hlmap["text.uri"] = "TSURI" ["text.uri"] = "TSURI",
hlmap["text.math"] = "TSMath" ["text.math"] = "TSMath",
hlmap["text.reference"] = "TSTextReference" ["text.reference"] = "TSTextReference",
hlmap["text.environment"] = "TSEnvironment" ["text.environment"] = "TSEnvironment",
hlmap["text.environment.name"] = "TSEnvironmentName" ["text.environment.name"] = "TSEnvironmentName",
hlmap["text.note"] = "TSNote" ["text.note"] = "TSNote",
hlmap["text.warning"] = "TSWarning" ["text.warning"] = "TSWarning",
hlmap["text.danger"] = "TSDanger" ["text.danger"] = "TSDanger",
hlmap["todo"] = "TSTodo" ["todo"] = "TSTodo",
hlmap["type"] = "TSType" ["type"] = "TSType",
hlmap["type.builtin"] = "TSTypeBuiltin" ["type.builtin"] = "TSTypeBuiltin",
hlmap["type.qualifier"] = "TSTypeQualifier" ["type.qualifier"] = "TSTypeQualifier",
hlmap["type.definition"] = "TSTypeDefinition" ["type.definition"] = "TSTypeDefinition",
hlmap["variable"] = "TSVariable" ["variable"] = "TSVariable",
hlmap["variable.builtin"] = "TSVariableBuiltin" ["variable.builtin"] = "TSVariableBuiltin",
}
-- compatibility shim
local link_captures
if ts.highlighter.hl_map then
link_captures = function(capture, hlgroup)
ts.highlighter.hl_map[capture] = hlgroup
end
elseif not vim.g.skip_ts_default_groups then
link_captures = function(capture, hlgroup)
api.nvim_set_hl(0, hlgroup, { link = "@" .. capture })
end
end
for capture, hlgroup in pairs(default_map) do
link_captures(capture, hlgroup)
end
local function should_enable_vim_regex(config, lang) local function should_enable_vim_regex(config, lang)
local additional_hl = config.additional_vim_regex_highlighting local additional_hl = config.additional_vim_regex_highlighting
@ -152,9 +169,10 @@ function M.detach(bufnr)
enable_syntax(bufnr) enable_syntax(bufnr)
end end
-- TODO(clason): remove obsolete function after bump to 0.8
function M.set_custom_captures(captures) function M.set_custom_captures(captures)
for k, v in pairs(captures) do for capture, hlgroup in pairs(captures) do
hlmap[k] = v link_captures(capture, hlgroup)
end end
end end