2020-04-20 16:18:02 +02:00
|
|
|
local api = vim.api
|
2020-05-08 11:22:59 +02:00
|
|
|
|
2021-07-04 16:12:17 -05:00
|
|
|
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
|
|
|
|
|
---@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-12-25 22:41:59 +01:00
|
|
|
vim.treesitter.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-12-25 22:41:59 +01:00
|
|
|
vim.treesitter.stop(bufnr)
|
2022-02-06 14:46:24 +01:00
|
|
|
enable_syntax(bufnr)
|
2020-04-22 11:13:05 +02:00
|
|
|
end
|
|
|
|
|
|
2022-12-25 22:41:59 +01:00
|
|
|
---@deprecated
|
|
|
|
|
function M.start(...)
|
|
|
|
|
vim.notify(
|
|
|
|
|
"`nvim-treesitter.highlight.start` is deprecated: use `nvim-treesitter.highlight.attach` or `vim.treesitter.start`",
|
|
|
|
|
vim.log.levels.WARN
|
|
|
|
|
)
|
|
|
|
|
M.attach(...)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
---@deprecated
|
|
|
|
|
function M.stop(...)
|
|
|
|
|
vim.notify(
|
|
|
|
|
"`nvim-treesitter.highlight.stop` is deprecated: use `nvim-treesitter.highlight.detach` or `vim.treesitter.stop`",
|
|
|
|
|
vim.log.levels.WARN
|
|
|
|
|
)
|
|
|
|
|
M.detach(...)
|
|
|
|
|
end
|
|
|
|
|
|
2020-04-20 16:18:02 +02:00
|
|
|
return M
|