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
|
|
|
|
2022-10-31 10:52:52 +00:00
|
|
|
---@param config table
|
|
|
|
|
---@param lang string
|
|
|
|
|
---@return boolean
|
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
|
|
|
|
|
|
2022-10-31 10:52:52 +00:00
|
|
|
---@param bufnr integer
|
2022-02-06 14:46:24 +01:00
|
|
|
local function enable_syntax(bufnr)
|
|
|
|
|
api.nvim_buf_set_option(bufnr, "syntax", "ON")
|
|
|
|
|
end
|
|
|
|
|
|
2022-10-31 10:52:52 +00:00
|
|
|
---@param bufnr integer
|
2022-02-06 14:46:24 +01:00
|
|
|
function M.stop(bufnr)
|
|
|
|
|
if ts.highlighter.active[bufnr] then
|
|
|
|
|
ts.highlighter.active[bufnr]:destroy()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-10-31 10:52:52 +00:00
|
|
|
---@param bufnr integer
|
|
|
|
|
---@param lang string
|
2022-02-06 14:46:24 +01:00
|
|
|
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-10-31 10:52:52 +00:00
|
|
|
---@param bufnr integer
|
|
|
|
|
---@param lang string
|
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-10-31 10:52:52 +00:00
|
|
|
if config and 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
|
|
|
|
|
|
2022-10-31 10:52:52 +00:00
|
|
|
---@param bufnr integer
|
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
|
|
|
|
|
|
2020-04-20 16:18:02 +02:00
|
|
|
return M
|