2020-04-20 16:18:02 +02:00
|
|
|
local api = vim.api
|
|
|
|
|
local ts = vim.treesitter
|
2020-05-08 11:22:59 +02:00
|
|
|
|
2020-04-22 11:13:05 +02:00
|
|
|
local queries = require'nvim-treesitter.query'
|
2020-06-20 12:21:42 +02:00
|
|
|
local parsers = require'nvim-treesitter.parsers'
|
2020-07-12 14:51:11 +02:00
|
|
|
local configs = require'nvim-treesitter.configs'
|
2020-04-20 16:18:02 +02:00
|
|
|
|
2020-04-22 09:29:33 +02:00
|
|
|
local M = {
|
2020-04-22 11:13:05 +02:00
|
|
|
highlighters = {}
|
2020-04-22 09:29:33 +02:00
|
|
|
}
|
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
|
|
|
|
|
|
|
|
-- Misc
|
2020-06-21 19:43:02 +02:00
|
|
|
hlmap.error = "TSError"
|
|
|
|
|
hlmap["punctuation.delimiter"] = "TSPunctDelimiter"
|
|
|
|
|
hlmap["punctuation.bracket"] = "TSPunctBracket"
|
|
|
|
|
hlmap["punctuation.special"] = "TSPunctSpecial"
|
2020-05-05 15:03:00 +02:00
|
|
|
|
|
|
|
|
-- Constants
|
2020-06-21 19:43:02 +02:00
|
|
|
hlmap["constant"] = "TSConstant"
|
|
|
|
|
hlmap["constant.builtin"] = "TSConstBuiltin"
|
|
|
|
|
hlmap["constant.macro"] = "TSConstMacro"
|
|
|
|
|
hlmap["string"] = "TSString"
|
|
|
|
|
hlmap["string.regex"] = "TSStringRegex"
|
|
|
|
|
hlmap["string.escape"] = "TSStringEscape"
|
|
|
|
|
hlmap["character"] = "TSCharacter"
|
|
|
|
|
hlmap["number"] = "TSNumber"
|
|
|
|
|
hlmap["boolean"] = "TSBoolean"
|
|
|
|
|
hlmap["float"] = "TSFloat"
|
2020-07-23 15:37:04 +01:00
|
|
|
hlmap["annotation"] = "TSAnnotation"
|
2020-05-05 15:03:00 +02:00
|
|
|
|
|
|
|
|
-- Functions
|
2020-06-21 19:43:02 +02:00
|
|
|
hlmap["function"] = "TSFunction"
|
|
|
|
|
hlmap["function.builtin"] = "TSFuncBuiltin"
|
|
|
|
|
hlmap["function.macro"] = "TSFuncMacro"
|
2020-08-10 14:09:09 +02:00
|
|
|
hlmap["parameter"] = "TSParameter"
|
2020-08-15 09:24:24 -05:00
|
|
|
hlmap["parameter.reference"] = "TSParameterReference"
|
2020-06-21 19:43:02 +02:00
|
|
|
hlmap["method"] = "TSMethod"
|
|
|
|
|
hlmap["field"] = "TSField"
|
|
|
|
|
hlmap["property"] = "TSProperty"
|
|
|
|
|
hlmap["constructor"] = "TSConstructor"
|
2020-05-05 15:03:00 +02:00
|
|
|
|
|
|
|
|
-- Keywords
|
2020-06-21 19:43:02 +02:00
|
|
|
hlmap["conditional"] = "TSConditional"
|
|
|
|
|
hlmap["repeat"] = "TSRepeat"
|
|
|
|
|
hlmap["label"] = "TSLabel"
|
|
|
|
|
hlmap["operator"] = "TSOperator"
|
|
|
|
|
hlmap["keyword"] = "TSKeyword"
|
2020-08-03 21:40:23 -05:00
|
|
|
hlmap["keyword.function"] = "TSKeywordFunction"
|
2020-06-21 19:43:02 +02:00
|
|
|
hlmap["exception"] = "TSException"
|
2020-05-05 15:03:00 +02:00
|
|
|
|
2020-06-21 19:43:02 +02:00
|
|
|
hlmap["type"] = "TSType"
|
|
|
|
|
hlmap["type.builtin"] = "TSTypeBuiltin"
|
|
|
|
|
hlmap["structure"] = "TSStructure"
|
|
|
|
|
hlmap["include"] = "TSInclude"
|
2020-05-05 15:03:00 +02:00
|
|
|
|
2020-08-09 15:13:37 +01:00
|
|
|
-- variable
|
|
|
|
|
hlmap["variable"] = "TSVariable"
|
|
|
|
|
hlmap["variable.builtin"] = "TSVariableBuiltin"
|
|
|
|
|
|
2020-07-26 09:38:53 -05:00
|
|
|
-- Text
|
|
|
|
|
hlmap["text"] = "TSText"
|
|
|
|
|
hlmap["text.strong"] = "TSStrong"
|
|
|
|
|
hlmap["text.emphasis"] = "TSEmphasis"
|
|
|
|
|
hlmap["text.underline"] = "TSUnderline"
|
|
|
|
|
hlmap["text.title"] = "TSTitle"
|
|
|
|
|
hlmap["text.literal"] = "TSLiteral"
|
|
|
|
|
hlmap["text.uri"] = "TSURI"
|
|
|
|
|
|
2020-09-14 21:00:03 +02:00
|
|
|
hlmap["none"] = "TSNone"
|
|
|
|
|
|
2020-06-20 12:21:42 +02:00
|
|
|
function M.attach(bufnr, lang)
|
2020-07-10 22:17:51 +02:00
|
|
|
local lang = lang or parsers.get_buf_lang(bufnr)
|
2020-07-12 14:51:11 +02:00
|
|
|
local config = configs.get_module('highlight')
|
|
|
|
|
|
|
|
|
|
for k, v in pairs(config.custom_captures) do
|
|
|
|
|
hlmap[k] = v
|
|
|
|
|
end
|
2020-04-20 16:18:02 +02:00
|
|
|
|
2020-06-20 12:21:42 +02:00
|
|
|
local query = queries.get_query(lang, "highlights")
|
2020-04-20 16:18:02 +02:00
|
|
|
if not query then return end
|
|
|
|
|
|
2020-08-11 23:20:21 +02:00
|
|
|
M.highlighters[bufnr] = ts.highlighter.new(query, bufnr, lang)
|
2020-04-20 16:18:02 +02:00
|
|
|
end
|
|
|
|
|
|
2020-04-22 11:13:05 +02:00
|
|
|
function M.detach(bufnr)
|
2020-08-21 07:49:06 -05:00
|
|
|
if M.highlighters[bufnr] then
|
|
|
|
|
M.highlighters[bufnr]:set_query("")
|
|
|
|
|
M.highlighters[bufnr] = nil
|
2020-04-22 11:13:05 +02:00
|
|
|
end
|
2020-08-21 07:49:06 -05:00
|
|
|
api.nvim_buf_set_option(bufnr, 'syntax', 'on')
|
2020-04-22 11:13:05 +02:00
|
|
|
end
|
|
|
|
|
|
2020-04-20 16:18:02 +02:00
|
|
|
return M
|