2020-04-20 16:18:02 +02:00
|
|
|
local api = vim.api
|
|
|
|
|
local ts = vim.treesitter
|
2020-05-08 11:22:59 +02:00
|
|
|
|
2021-07-04 16:12:17 -05:00
|
|
|
local parsers = require "nvim-treesitter.parsers"
|
|
|
|
|
local configs = require "nvim-treesitter.configs"
|
2020-04-20 16:18:02 +02:00
|
|
|
|
2021-07-04 16:12:17 -05:00
|
|
|
local M = {}
|
2020-04-20 16:18:02 +02:00
|
|
|
|
2020-08-11 23:20:21 +02:00
|
|
|
local hlmap = vim.treesitter.highlighter.hl_map
|
2020-05-05 15:03:00 +02:00
|
|
|
|
2020-11-19 17:32:14 -05:00
|
|
|
-- nvim-treesitter Highlight Group Mappings
|
2020-11-19 17:33:13 -05:00
|
|
|
-- Note: Some highlight groups may not be applied upstream, some may be experimental
|
2020-11-19 17:32:14 -05:00
|
|
|
|
2020-11-19 17:09:02 -05:00
|
|
|
hlmap["annotation"] = "TSAnnotation"
|
|
|
|
|
|
|
|
|
|
hlmap["attribute"] = "TSAttribute"
|
|
|
|
|
|
|
|
|
|
hlmap["boolean"] = "TSBoolean"
|
|
|
|
|
|
|
|
|
|
hlmap["character"] = "TSCharacter"
|
|
|
|
|
|
2021-04-24 20:41:16 +06:00
|
|
|
hlmap["comment"] = "TSComment"
|
|
|
|
|
|
2020-11-19 17:09:02 -05:00
|
|
|
hlmap["conditional"] = "TSConditional"
|
|
|
|
|
|
2020-06-21 19:43:02 +02:00
|
|
|
hlmap["constant"] = "TSConstant"
|
|
|
|
|
hlmap["constant.builtin"] = "TSConstBuiltin"
|
|
|
|
|
hlmap["constant.macro"] = "TSConstMacro"
|
2020-11-19 17:09:02 -05:00
|
|
|
|
|
|
|
|
hlmap["constructor"] = "TSConstructor"
|
|
|
|
|
|
2021-04-24 20:41:16 +06:00
|
|
|
hlmap["error"] = "TSError"
|
2020-11-19 17:09:02 -05:00
|
|
|
hlmap["exception"] = "TSException"
|
|
|
|
|
|
|
|
|
|
hlmap["field"] = "TSField"
|
|
|
|
|
|
2020-06-21 19:43:02 +02:00
|
|
|
hlmap["float"] = "TSFloat"
|
2020-05-05 15:03:00 +02:00
|
|
|
|
2020-06-21 19:43:02 +02:00
|
|
|
hlmap["function"] = "TSFunction"
|
|
|
|
|
hlmap["function.builtin"] = "TSFuncBuiltin"
|
|
|
|
|
hlmap["function.macro"] = "TSFuncMacro"
|
2020-05-05 15:03:00 +02:00
|
|
|
|
2020-11-19 17:09:02 -05:00
|
|
|
hlmap["include"] = "TSInclude"
|
|
|
|
|
|
2020-06-21 19:43:02 +02:00
|
|
|
hlmap["keyword"] = "TSKeyword"
|
2020-08-03 21:40:23 -05:00
|
|
|
hlmap["keyword.function"] = "TSKeywordFunction"
|
2020-09-13 15:08:11 +02:00
|
|
|
hlmap["keyword.operator"] = "TSKeywordOperator"
|
2021-07-04 19:55:59 +03:00
|
|
|
hlmap["keyword.return"] = "TSKeywordReturn"
|
2020-11-19 17:09:02 -05:00
|
|
|
|
|
|
|
|
hlmap["label"] = "TSLabel"
|
|
|
|
|
|
|
|
|
|
hlmap["method"] = "TSMethod"
|
|
|
|
|
|
|
|
|
|
hlmap["namespace"] = "TSNamespace"
|
|
|
|
|
|
|
|
|
|
hlmap["none"] = "TSNone"
|
|
|
|
|
hlmap["number"] = "TSNumber"
|
|
|
|
|
|
2020-09-13 15:08:11 +02:00
|
|
|
hlmap["operator"] = "TSOperator"
|
2020-05-05 15:03:00 +02:00
|
|
|
|
2020-11-19 17:09:02 -05:00
|
|
|
hlmap["parameter"] = "TSParameter"
|
|
|
|
|
hlmap["parameter.reference"] = "TSParameterReference"
|
|
|
|
|
|
|
|
|
|
hlmap["property"] = "TSProperty"
|
|
|
|
|
|
|
|
|
|
hlmap["punctuation.delimiter"] = "TSPunctDelimiter"
|
|
|
|
|
hlmap["punctuation.bracket"] = "TSPunctBracket"
|
|
|
|
|
hlmap["punctuation.special"] = "TSPunctSpecial"
|
|
|
|
|
|
|
|
|
|
hlmap["repeat"] = "TSRepeat"
|
|
|
|
|
|
|
|
|
|
hlmap["string"] = "TSString"
|
|
|
|
|
hlmap["string.regex"] = "TSStringRegex"
|
|
|
|
|
hlmap["string.escape"] = "TSStringEscape"
|
2021-07-11 15:59:53 -05:00
|
|
|
hlmap["string.special"] = "TSStringSpecial"
|
2020-11-19 17:09:02 -05:00
|
|
|
|
2021-02-28 15:24:16 +11:00
|
|
|
hlmap["symbol"] = "TSSymbol"
|
|
|
|
|
|
2020-11-19 17:09:02 -05:00
|
|
|
hlmap["tag"] = "TSTag"
|
2021-07-13 20:21:47 -04:00
|
|
|
hlmap["tag.attribute"] = "TSTagAttribute"
|
2020-11-19 17:09:02 -05:00
|
|
|
hlmap["tag.delimiter"] = "TSTagDelimiter"
|
2020-08-09 15:13:37 +01:00
|
|
|
|
2020-07-26 09:38:53 -05:00
|
|
|
hlmap["text"] = "TSText"
|
|
|
|
|
hlmap["text.strong"] = "TSStrong"
|
|
|
|
|
hlmap["text.emphasis"] = "TSEmphasis"
|
|
|
|
|
hlmap["text.underline"] = "TSUnderline"
|
2021-02-06 17:27:42 -05:00
|
|
|
hlmap["text.strike"] = "TSStrike"
|
2020-07-26 09:38:53 -05:00
|
|
|
hlmap["text.title"] = "TSTitle"
|
|
|
|
|
hlmap["text.literal"] = "TSLiteral"
|
|
|
|
|
hlmap["text.uri"] = "TSURI"
|
2021-03-28 01:42:47 +01:00
|
|
|
hlmap["text.math"] = "TSMath"
|
2021-03-28 21:06:53 +02:00
|
|
|
hlmap["text.reference"] = "TSTextReference"
|
2021-06-21 23:08:32 +02:00
|
|
|
hlmap["text.environment"] = "TSEnvironment"
|
|
|
|
|
hlmap["text.environment.name"] = "TSEnvironmentName"
|
2020-07-26 09:38:53 -05:00
|
|
|
|
2021-03-12 11:21:46 -05:00
|
|
|
hlmap["text.note"] = "TSNote"
|
|
|
|
|
hlmap["text.warning"] = "TSWarning"
|
|
|
|
|
hlmap["text.danger"] = "TSDanger"
|
|
|
|
|
|
2020-11-19 17:09:02 -05:00
|
|
|
hlmap["type"] = "TSType"
|
|
|
|
|
hlmap["type.builtin"] = "TSTypeBuiltin"
|
2020-09-15 22:24:33 -04:00
|
|
|
|
2020-11-19 17:09:02 -05:00
|
|
|
hlmap["variable"] = "TSVariable"
|
|
|
|
|
hlmap["variable.builtin"] = "TSVariableBuiltin"
|
2020-09-14 21:00:03 +02:00
|
|
|
|
2022-02-06 14:46:24 +01:00
|
|
|
local function should_enable_vim_regex(config, lang)
|
|
|
|
|
local additional_hl = config.additional_vim_regex_highlighting
|
|
|
|
|
local is_table = type(additional_hl) == "table"
|
|
|
|
|
|
|
|
|
|
return additional_hl and (not is_table or vim.tbl_contains(additional_hl, lang))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
local function enable_syntax(bufnr)
|
|
|
|
|
api.nvim_buf_set_option(bufnr, "syntax", "ON")
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function M.stop(bufnr)
|
|
|
|
|
if ts.highlighter.active[bufnr] then
|
|
|
|
|
ts.highlighter.active[bufnr]:destroy()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function M.start(bufnr, lang)
|
2020-09-29 18:33:03 +02:00
|
|
|
local parser = parsers.get_parser(bufnr, lang)
|
2021-01-14 11:25:19 +01:00
|
|
|
ts.highlighter.new(parser, {})
|
2022-02-06 14:46:24 +01:00
|
|
|
end
|
2021-03-17 16:51:43 +01:00
|
|
|
|
2022-02-06 14:46:24 +01:00
|
|
|
function M.attach(bufnr, lang)
|
2022-02-06 15:15:42 +01:00
|
|
|
local config = configs.get_module "highlight"
|
2022-02-06 14:46:24 +01:00
|
|
|
M.start(bufnr, lang)
|
2022-02-06 15:15:42 +01:00
|
|
|
if should_enable_vim_regex(config, lang) then
|
2022-02-06 14:46:24 +01:00
|
|
|
enable_syntax(bufnr)
|
2021-03-17 13:08:29 +01:00
|
|
|
end
|
2020-04-20 16:18:02 +02:00
|
|
|
end
|
|
|
|
|
|
2020-04-22 11:13:05 +02:00
|
|
|
function M.detach(bufnr)
|
2022-02-06 14:46:24 +01:00
|
|
|
M.stop(bufnr)
|
|
|
|
|
enable_syntax(bufnr)
|
2020-04-22 11:13:05 +02:00
|
|
|
end
|
|
|
|
|
|
2022-02-06 15:15:42 +01:00
|
|
|
function M.set_custom_captures(captures)
|
|
|
|
|
for k, v in pairs(captures) do
|
|
|
|
|
hlmap[k] = v
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-04-20 16:18:02 +02:00
|
|
|
return M
|