From 933030d3d08aa6bd4d277baf477e5dc794948640 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 11 Sep 2022 10:37:58 +0200 Subject: [PATCH] feat: make new capture groups work on Neovim 0.7 --- lua/tokyonight/treesitter.lua | 131 ++++++++++++++++++++++++++++++++++ lua/tokyonight/util.lua | 9 +-- 2 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 lua/tokyonight/treesitter.lua diff --git a/lua/tokyonight/treesitter.lua b/lua/tokyonight/treesitter.lua new file mode 100644 index 0000000..ce523ce --- /dev/null +++ b/lua/tokyonight/treesitter.lua @@ -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 diff --git a/lua/tokyonight/util.lua b/lua/tokyonight/util.lua index 286b5aa..311cd74 100644 --- a/lua/tokyonight/util.lua +++ b/lua/tokyonight/util.lua @@ -1,3 +1,5 @@ +local ts = require("tokyonight.treesitter") + local M = {} M.bg = "#000000" @@ -28,6 +30,7 @@ end function M.darken(hex, amount, bg) return M.blend(hex, bg or M.bg, math.abs(amount)) end + function M.lighten(hex, amount, fg) return M.blend(hex, fg or M.fg, math.abs(amount)) end @@ -45,12 +48,10 @@ function M.invert_color(color) return color end --- new in 0.8 something -local has_new_group_names = vim.fn.hlexists("@comment") ~= 0 - ---@param group string 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 end local hl = { fg = color.fg, bg = color.bg, sp = color.sp, link = color.link }