Skip to content
forked from neovim/neovim

Commit

Permalink
[Backport release-0.7] fix(treesitter): create new parser if language…
Browse files Browse the repository at this point in the history
… is not the same as cached parser (neovim#18220)

* fix(treesitter): create new parser if language is not the same as cached parser

Fixes neovim#18148

(cherry picked from commit 8e35894)

* test: create new parser in vim.treesitter.get_parser() when filetype changes

(cherry picked from commit 30e7b3f)

Co-authored-by: Chinmay Dalal <dalal.chinmay.0101@gmail.com>
  • Loading branch information
github-actions[bot] and p00f committed Apr 22, 2022
1 parent 7f8faac commit f7e2ad7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion runtime/lua/vim/treesitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function M.get_parser(bufnr, lang, opts)
lang = a.nvim_buf_get_option(bufnr, "filetype")
end

if parsers[bufnr] == nil then
if parsers[bufnr] == nil or parsers[bufnr]:lang() ~= lang then
parsers[bufnr] = M._create_parser(bufnr, lang, opts)
end

Expand Down
11 changes: 11 additions & 0 deletions test/functional/treesitter/language_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ local helpers = require('test.functional.helpers')(after_each)

local clear = helpers.clear
local eq = helpers.eq
local command = helpers.command
local exec_lua = helpers.exec_lua
local pcall_err = helpers.pcall_err
local matches = helpers.matches
Expand Down Expand Up @@ -67,5 +68,15 @@ describe('treesitter API', function()
end
eq({true,true}, {has_named,has_anonymous})
end)

it('checks if vim.treesitter.get_parser tries to create a new parser on filetype change', function ()
command("set filetype=c")
-- Should not throw an error when filetype is c
eq('c', exec_lua("return vim.treesitter.get_parser(0):lang()"))
command("set filetype=borklang")
-- Should throw an error when filetype changes to borklang
eq("Error executing lua: .../language.lua:0: no parser for 'borklang' language, see :help treesitter-parsers",
pcall_err(exec_lua, "new_parser = vim.treesitter.get_parser(0)"))
end)
end)

0 comments on commit f7e2ad7

Please sign in to comment.