Skip to content

Commit

Permalink
fix(treesitter): dont allow recursive injections. Fixes #286
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Dec 31, 2022
1 parent 6aa6585 commit a31b41a
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions lua/noice/text/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ function M.highlight_markdown(buf, injections)
end

function M.has_lang(lang)
local language = require("vim.treesitter.language")
return pcall(language.require_language, lang) == true
return vim.treesitter.language.require_language(lang, nil, true)
end

--- Highlights a region of the buffer with a given language
Expand All @@ -34,14 +33,13 @@ function M.highlight(buf, ns, range, lang)
buf = (buf == 0 or buf == nil) and vim.api.nvim_get_current_buf() or buf
vim.fn.bufload(buf)

-- vim.api.nvim_buf_clear_namespace(buf, ns, range[1], range[3] + 1)

local language = require("vim.treesitter.language")
language.require_language(lang)
vim.treesitter.language.require_language(lang)

-- we can't use a cached parser here since that could interfer with the existing parser of the buffer
local LanguageTree = require("vim.treesitter.languagetree")
local parser = LanguageTree.new(buf, lang)
local opts = { injections = { [lang] = "" } }
local parser = LanguageTree.new(buf, lang, opts)

parser:set_included_regions({ { range } })
parser:parse()

Expand Down

0 comments on commit a31b41a

Please sign in to comment.