mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-01 19:17:02 -04:00
73 lines
1.8 KiB
Lua
73 lines
1.8 KiB
Lua
local api = vim.api
|
|
local ts = vim.treesitter
|
|
|
|
local queries = require'nvim-treesitter.query'
|
|
local parsers = require'nvim-treesitter.parsers'
|
|
|
|
local M = {
|
|
highlighters = {}
|
|
}
|
|
|
|
local hlmap = vim.treesitter.TSHighlighter.hl_map
|
|
|
|
-- Misc
|
|
hlmap.error = "Error"
|
|
hlmap["punctuation.delimiter"] = "Delimiter"
|
|
hlmap["punctuation.bracket"] = "Delimiter"
|
|
hlmap["punctuation.special"] = "Delimiter"
|
|
|
|
-- Constants
|
|
hlmap["constant"] = "Constant"
|
|
hlmap["constant.builtin"] = "Special"
|
|
hlmap["constant.macro"] = "Define"
|
|
hlmap["string"] = "String"
|
|
hlmap["string.regex"] = "String"
|
|
hlmap["string.escape"] = "SpecialChar"
|
|
hlmap["character"] = "Character"
|
|
hlmap["number"] = "Number"
|
|
hlmap["boolean"] = "Boolean"
|
|
hlmap["float"] = "Float"
|
|
|
|
-- Functions
|
|
hlmap["function"] = "Function"
|
|
hlmap["function.builtin"] = "Special"
|
|
hlmap["function.macro"] = "Macro"
|
|
hlmap["parameter"] = "Identifier"
|
|
hlmap["method"] = "Function"
|
|
hlmap["field"] = "Identifier"
|
|
hlmap["property"] = "Identifier"
|
|
hlmap["constructor"] = "Special"
|
|
|
|
-- Keywords
|
|
hlmap["conditional"] = "Conditional"
|
|
hlmap["repeat"] = "Repeat"
|
|
hlmap["label"] = "Label"
|
|
hlmap["operator"] = "Operator"
|
|
hlmap["keyword"] = "Keyword"
|
|
hlmap["exception"] = "Exception"
|
|
|
|
hlmap["type"] = "Type"
|
|
hlmap["type.builtin"] = "Type"
|
|
hlmap["structure"] = "Structure"
|
|
hlmap["include"] = "Include"
|
|
|
|
function M.attach(bufnr, lang)
|
|
local bufnr = bufnr or api.nvim_get_current_buf()
|
|
local lang = lang or parsers.ft_to_lang(api.nvim_buf_get_option(bufnr, 'ft'))
|
|
|
|
local query = queries.get_query(lang, "highlights")
|
|
if not query then return end
|
|
|
|
M.highlighters[bufnr] = ts.TSHighlighter.new(query, bufnr, lang)
|
|
end
|
|
|
|
function M.detach(bufnr)
|
|
local buf = bufnr or api.nvim_get_current_buf()
|
|
if M.highlighters[buf] then
|
|
M.highlighters[buf]:set_query("")
|
|
M.highlighters[buf] = nil
|
|
end
|
|
api.nvim_buf_set_option(buf, 'syntax', 'on')
|
|
end
|
|
|
|
return M
|