From 560e8fb4500ac481601d2511b40f730b4515820e Mon Sep 17 00:00:00 2001 From: kiyan Date: Sun, 6 Feb 2022 15:15:42 +0100 Subject: refacto: expose a function to set custom capture for the higlighter BREAKING: deprecate custom_captures from highlight config. This allows plugin authors to extend the map. It also avoids settings user configuration specific values during the highlighter initialization (SOC). Not sure how much value this brings, and might potentially break a lot of people configurations. This is questionable. --- lua/nvim-treesitter/configs.lua | 3 ++- lua/nvim-treesitter/highlight.lua | 19 ++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) (limited to 'lua') diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index 8f0f885a1..7e6b0a7b9 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -22,8 +22,9 @@ local is_initialized = false local builtin_modules = { highlight = { module_path = "nvim-treesitter.highlight", - enable = false, + -- @deprecated: use `highlight.set_custom_captures` instead custom_captures = {}, + enable = false, is_supported = function(lang) return queries.has_highlights(lang) end, diff --git a/lua/nvim-treesitter/highlight.lua b/lua/nvim-treesitter/highlight.lua index ce7cf8117..b71a64eec 100644 --- a/lua/nvim-treesitter/highlight.lua +++ b/lua/nvim-treesitter/highlight.lua @@ -114,10 +114,6 @@ local function enable_syntax(bufnr) api.nvim_buf_set_option(bufnr, "syntax", "ON") end -function M.get_config() - return configs.get_module "highlight" -end - function M.stop(bufnr) if ts.highlighter.active[bufnr] then ts.highlighter.active[bufnr]:destroy() @@ -126,18 +122,13 @@ end function M.start(bufnr, lang) local parser = parsers.get_parser(bufnr, lang) - local config = M.get_config() - - for k, v in pairs(config.custom_captures) do - hlmap[k] = v - end - ts.highlighter.new(parser, {}) end function M.attach(bufnr, lang) + local config = configs.get_module "highlight" M.start(bufnr, lang) - if should_enable_vim_regex(M.get_config(), lang) then + if should_enable_vim_regex(config, lang) then enable_syntax(bufnr) end end @@ -147,4 +138,10 @@ function M.detach(bufnr) enable_syntax(bufnr) end +function M.set_custom_captures(captures) + for k, v in pairs(captures) do + hlmap[k] = v + end +end + return M -- cgit v1.2.3-70-g09d2