Skip to content

Commit 714d9c1

Browse files
committed
Only attach after parser is installed
1 parent 2442c57 commit 714d9c1

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

init.lua

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -947,11 +947,10 @@ require('lazy').setup({
947947
config = function()
948948
---@param buf integer
949949
---@param language string
950-
---@return boolean
951-
local function treesitter_attach(buf, language)
952-
-- check if parser exists before starting highlighter
950+
local function treesitter_try_attach(buf, language)
951+
-- check if parser exists and load it
953952
if not vim.treesitter.language.add(language) then
954-
return false
953+
return
955954
end
956955
-- enables syntax highlighting and other treesitter features
957956
vim.treesitter.start(buf, language)
@@ -962,10 +961,9 @@ require('lazy').setup({
962961

963962
-- enables treesitter based indentation
964963
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
965-
return true
966964
end
967965

968-
local available_parsers = require('nvim-treesitter.config').get_available()
966+
local available_parsers = require('nvim-treesitter').get_available()
969967
vim.api.nvim_create_autocmd('FileType', {
970968
callback = function(args)
971969
local buf, filetype = args.buf, args.match
@@ -974,17 +972,19 @@ require('lazy').setup({
974972
return
975973
end
976974

977-
if not (treesitter_attach(buf, language) or vim.tbl_contains(available_parsers, language)) then
975+
-- try to enable treesitter features in case the parser exists but is not available from `nvim-treesitter`
976+
if not vim.tbl_contains(available_parsers, language) then
977+
treesitter_try_attach(buf, language)
978978
return
979979
end
980980

981-
-- automaically install parser for missing languages
982-
-- attempt to install even if it is available accoring to `vim.treesitter.langauge.add()`,
983-
-- to ensure the latest version is installed using `nvim-treesitter`, instead of the outdated vendored parser
984-
if not vim.tbl_contains(require('nvim-treesitter.config').get_installed 'parsers', language) then
985-
-- attempt to start highlighter after installing missing language
986-
require('nvim-treesitter.install').install(language):await(function()
987-
treesitter_attach(buf, language)
981+
-- if a parser is available in `nvim-treesitter` enable it after ensuring it is installed
982+
local installed_parsers = require('nvim-treesitter').get_installed 'parsers'
983+
if vim.tbl_contains(installed_parsers, language) then
984+
treesitter_try_attach(buf, language)
985+
else
986+
require('nvim-treesitter').install(language):await(function()
987+
treesitter_try_attach(buf, language)
988988
end)
989989
end
990990
end,

0 commit comments

Comments
 (0)