feat: make new capture groups work on Neovim 0.7

This commit is contained in:
Folke Lemaitre
2022-09-11 10:37:58 +02:00
parent ffa77681c3
commit 933030d3d0
2 changed files with 136 additions and 4 deletions

View File

@@ -0,0 +1,131 @@
local M = {}
-- new in 0.8 something
local has_new_group_names = vim.fn.hlexists("@comment") ~= 0
function M.get(group)
if group:sub(1, 1) ~= "@" or has_new_group_names then
return group
end
group = group:sub(2)
local lang
while group do
if M.fallbacks[group] then
return (lang or "") .. M.fallbacks[group]
end
group, lang = group:match("(.*)%.(.*)")
end
end
-- taken from https://github.com/nvim-treesitter/nvim-treesitter/blob/master/lua/nvim-treesitter/highlight.lua
M.fallbacks = {
["annotation"] = "TSAnnotation",
["attribute"] = "TSAttribute",
["boolean"] = "TSBoolean",
["character"] = "TSCharacter",
["character.special"] = "TSCharacterSpecial",
["comment"] = "TSComment",
["conditional"] = "TSConditional",
["constant"] = "TSConstant",
["constant.builtin"] = "TSConstBuiltin",
["constant.macro"] = "TSConstMacro",
["constructor"] = "TSConstructor",
["debug"] = "TSDebug",
["define"] = "TSDefine",
["error"] = "TSError",
["exception"] = "TSException",
["field"] = "TSField",
["float"] = "TSFloat",
["function"] = "TSFunction",
["function.call"] = "TSFunctionCall",
["function.builtin"] = "TSFuncBuiltin",
["function.macro"] = "TSFuncMacro",
["include"] = "TSInclude",
["keyword"] = "TSKeyword",
["keyword.function"] = "TSKeywordFunction",
["keyword.operator"] = "TSKeywordOperator",
["keyword.return"] = "TSKeywordReturn",
["label"] = "TSLabel",
["method"] = "TSMethod",
["method.call"] = "TSMethodCall",
["namespace"] = "TSNamespace",
["none"] = "TSNone",
["number"] = "TSNumber",
["operator"] = "TSOperator",
["parameter"] = "TSParameter",
["parameter.reference"] = "TSParameterReference",
["preproc"] = "TSPreProc",
["property"] = "TSProperty",
["punctuation.delimiter"] = "TSPunctDelimiter",
["punctuation.bracket"] = "TSPunctBracket",
["punctuation.special"] = "TSPunctSpecial",
["repeat"] = "TSRepeat",
["storageclass"] = "TSStorageClass",
["string"] = "TSString",
["string.regex"] = "TSStringRegex",
["string.escape"] = "TSStringEscape",
["string.special"] = "TSStringSpecial",
["symbol"] = "TSSymbol",
["tag"] = "TSTag",
["tag.attribute"] = "TSTagAttribute",
["tag.delimiter"] = "TSTagDelimiter",
["text"] = "TSText",
["text.strong"] = "TSStrong",
["text.emphasis"] = "TSEmphasis",
["text.underline"] = "TSUnderline",
["text.strike"] = "TSStrike",
["text.title"] = "TSTitle",
["text.literal"] = "TSLiteral",
["text.uri"] = "TSURI",
["text.math"] = "TSMath",
["text.reference"] = "TSTextReference",
["text.environment"] = "TSEnvironment",
["text.environment.name"] = "TSEnvironmentName",
["text.note"] = "TSNote",
["text.warning"] = "TSWarning",
["text.danger"] = "TSDanger",
["todo"] = "TSTodo",
["type"] = "TSType",
["type.builtin"] = "TSTypeBuiltin",
["type.qualifier"] = "TSTypeQualifier",
["type.definition"] = "TSTypeDefinition",
["variable"] = "TSVariable",
["variable.builtin"] = "TSVariableBuiltin",
}
return M

View File

@@ -1,3 +1,5 @@
local ts = require("tokyonight.treesitter")
local M = {} local M = {}
M.bg = "#000000" M.bg = "#000000"
@@ -28,6 +30,7 @@ end
function M.darken(hex, amount, bg) function M.darken(hex, amount, bg)
return M.blend(hex, bg or M.bg, math.abs(amount)) return M.blend(hex, bg or M.bg, math.abs(amount))
end end
function M.lighten(hex, amount, fg) function M.lighten(hex, amount, fg)
return M.blend(hex, fg or M.fg, math.abs(amount)) return M.blend(hex, fg or M.fg, math.abs(amount))
end end
@@ -45,12 +48,10 @@ function M.invert_color(color)
return color return color
end end
-- new in 0.8 something
local has_new_group_names = vim.fn.hlexists("@comment") ~= 0
---@param group string ---@param group string
function M.highlight(group, color) function M.highlight(group, color)
if group:sub(1, 1) == "@" and not has_new_group_names then group = ts.get(group)
if not group then
return return
end end
local hl = { fg = color.fg, bg = color.bg, sp = color.sp, link = color.link } local hl = { fg = color.fg, bg = color.bg, sp = color.sp, link = color.link }