Skip to content

Commit

Permalink
fix(treesitter): use the new treesitter ft to lang API if availble. F…
Browse files Browse the repository at this point in the history
…ixes folke#378
  • Loading branch information
folke authored and willothy committed Aug 19, 2023
1 parent 554518f commit 660e051
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lua/noice/health.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local require = require("noice.util.lazy")
local Util = require("noice.util")
local Config = require("noice.config")
local Lsp = require("noice.lsp")
local Treesitter = require("noice.text.treesitter")

local M = {}

Expand Down Expand Up @@ -118,7 +119,7 @@ function M.check(opts)
end

for _, lang in ipairs({ "vim", "regex", "lua", "bash", "markdown", "markdown_inline" }) do
if pcall(vim.treesitter.language.require_language, lang) then
if Treesitter.has_lang(lang) then
log.ok("**TreeSitter " .. lang .. "** parser is installed")
else
log.warn(
Expand Down
11 changes: 9 additions & 2 deletions lua/noice/text/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ function M.highlight_markdown(buf, injections)
parser:parse()
end

function M.get_lang(ft)
return vim.treesitter.language.get_lang and vim.treesitter.language.get_lang(ft) or ft
end

function M.has_lang(lang)
if vim.treesitter.language.get_lang then
return vim.treesitter.language.get_lang(lang) ~= nil
end
return vim.treesitter.language.require_language(lang, nil, true)
end

Expand All @@ -30,11 +37,11 @@ end
---@param lang string treesitter language
-- luacheck: no redefined
function M.highlight(buf, ns, range, lang)
lang = M.get_lang(lang)

buf = (buf == 0 or buf == nil) and vim.api.nvim_get_current_buf() or buf
vim.fn.bufload(buf)

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 opts = { injections = { php = "", html = "" } }
Expand Down

0 comments on commit 660e051

Please sign in to comment.