Skip to content

Commit 3aa1fe0

Browse files
committed
refactor: update treesitter configuration to match the tool's new API
1 parent 8be54e3 commit 3aa1fe0

2 files changed

Lines changed: 69 additions & 36 deletions

File tree

init.lua

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -945,28 +945,61 @@ require('lazy').setup({
945945
},
946946
{ -- Highlight, edit, and navigate code
947947
'nvim-treesitter/nvim-treesitter',
948+
lazy = false,
948949
build = ':TSUpdate',
949-
main = 'nvim-treesitter.configs', -- Sets main module to use for opts
950-
-- [[ Configure Treesitter ]] See `:help nvim-treesitter`
951-
opts = {
952-
ensure_installed = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' },
953-
-- Autoinstall languages that are not installed
954-
auto_install = true,
955-
highlight = {
956-
enable = true,
957-
-- Some languages depend on vim's regex highlighting system (such as Ruby) for indent rules.
958-
-- If you are experiencing weird indenting issues, add the language to
959-
-- the list of additional_vim_regex_highlighting and disabled languages for indent.
960-
additional_vim_regex_highlighting = { 'ruby' },
961-
},
962-
indent = { enable = true, disable = { 'ruby' } },
963-
},
964-
-- There are additional nvim-treesitter modules that you can use to interact
965-
-- with nvim-treesitter. You should go explore a few and see what interests you:
966-
--
967-
-- - Incremental selection: Included, see `:help nvim-treesitter-incremental-selection-mod`
968-
-- - Show your current context: https://github.com/nvim-treesitter/nvim-treesitter-context
969-
-- - Treesitter + textobjects: https://github.com/nvim-treesitter/nvim-treesitter-textobjects
950+
branch = 'main',
951+
-- [[ Configure Treesitter ]] See `:help nvim-treesitter-intro`
952+
config = function()
953+
-- ensure basic parser are installed
954+
local parsers = { 'bash', 'c', 'diff', 'html', 'lua', 'luadoc', 'markdown', 'markdown_inline', 'query', 'vim', 'vimdoc' }
955+
require('nvim-treesitter').install(parsers)
956+
957+
---@param buf integer
958+
---@param language string
959+
local function treesitter_try_attach(buf, language)
960+
-- check if parser exists and load it
961+
if not vim.treesitter.language.add(language) then
962+
return
963+
end
964+
-- enables syntax highlighting and other treesitter features
965+
vim.treesitter.start(buf, language)
966+
967+
-- enables treesitter based folds
968+
-- for more info on folds see `:help folds`
969+
-- vim.wo.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
970+
-- vim.wo.foldmethod = 'expr'
971+
972+
-- enables treesitter based indentation
973+
vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
974+
end
975+
976+
local available_parsers = require('nvim-treesitter').get_available()
977+
vim.api.nvim_create_autocmd('FileType', {
978+
callback = function(args)
979+
local buf, filetype = args.buf, args.match
980+
981+
local language = vim.treesitter.language.get_lang(filetype)
982+
if not language then
983+
return
984+
end
985+
986+
local installed_parsers = require('nvim-treesitter').get_installed 'parsers'
987+
988+
if vim.tbl_contains(installed_parsers, language) then
989+
-- enable the parser if it is installed
990+
treesitter_try_attach(buf, language)
991+
elseif vim.tbl_contains(available_parsers, language) then
992+
-- if a parser is available in `nvim-treesitter` auto install it, and enable it after the installation is done
993+
require('nvim-treesitter').install(language):await(function()
994+
treesitter_try_attach(buf, language)
995+
end)
996+
else
997+
-- try to enable treesitter features in case the parser exists but is not available from `nvim-treesitter`
998+
treesitter_try_attach(buf, language)
999+
end
1000+
end,
1001+
})
1002+
end,
9701003
},
9711004

9721005
-- The following comments only work if you have downloaded the kickstart repo, not just copy pasted the

lazy-lock.json

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
{
22
"LuaSnip": { "branch": "master", "commit": "5a1e39223db9a0498024a77b8441169d260c8c25" },
3-
"blink.cmp": { "branch": "main", "commit": "b19413d214068f316c78978b08264ed1c41830ec" },
4-
"conform.nvim": { "branch": "master", "commit": "238f542a118984a88124fc915d5b981680418707" },
5-
"fidget.nvim": { "branch": "main", "commit": "7fa433a83118a70fe24c1ce88d5f0bd3453c0970" },
6-
"gitsigns.nvim": { "branch": "main", "commit": "42d6aed4e94e0f0bbced16bbdcc42f57673bd75e" },
3+
"blink.cmp": { "branch": "main", "commit": "78336bc89ee5365633bcf754d93df01678b5c08f" },
4+
"conform.nvim": { "branch": "master", "commit": "086a40dc7ed8242c03be9f47fbcee68699cc2395" },
5+
"fidget.nvim": { "branch": "main", "commit": "889e2e96edef4e144965571d46f7a77bcc4d0ddf" },
6+
"gitsigns.nvim": { "branch": "main", "commit": "4ed47e8c4c66c921dc1d6643977e0526e1f44396" },
77
"guess-indent.nvim": { "branch": "main", "commit": "84a4987ff36798c2fc1169cbaff67960aed9776f" },
8-
"lazy.nvim": { "branch": "main", "commit": "85c7ff3711b730b4030d03144f6db6375044ae82" },
9-
"lazydev.nvim": { "branch": "main", "commit": "5231c62aa83c2f8dc8e7ba957aa77098cda1257d" },
10-
"mason-lspconfig.nvim": { "branch": "main", "commit": "80c0130c5f16b551865a69e832f1feadeedb5fbe" },
11-
"mason-tool-installer.nvim": { "branch": "main", "commit": "517ef5994ef9d6b738322664d5fdd948f0fdeb46" },
8+
"lazy.nvim": { "branch": "main", "commit": "306a05526ada86a7b30af95c5cc81ffba93fef97" },
9+
"lazydev.nvim": { "branch": "main", "commit": "ff2cbcba459b637ec3fd165a2be59b7bbaeedf0d" },
10+
"mason-lspconfig.nvim": { "branch": "main", "commit": "25f609e7fca78af7cede4f9fa3af8a94b1c4950b" },
11+
"mason-tool-installer.nvim": { "branch": "main", "commit": "443f1ef8b5e6bf47045cb2217b6f748a223cf7dc" },
1212
"mason.nvim": { "branch": "main", "commit": "44d1e90e1f66e077268191e3ee9d2ac97cc18e65" },
13-
"mini.nvim": { "branch": "main", "commit": "19973fa2c13399d68bf8d2cd98b0cb7367a8a72a" },
14-
"neo-tree.nvim": { "branch": "main", "commit": "f3df514fff2bdd4318127c40470984137f87b62e" },
13+
"mini.nvim": { "branch": "main", "commit": "402ee6c6ec8ea44b22330446c8fb4e615fd3953e" },
14+
"neo-tree.nvim": { "branch": "main", "commit": "84c75e7a7e443586f60508d12fc50f90d9aee14e" },
1515
"neoscroll.nvim": { "branch": "master", "commit": "c8d29979cb0cb3a2437a8e0ae683fd82f340d3b8" },
1616
"nui.nvim": { "branch": "main", "commit": "de740991c12411b663994b2860f1a4fd0937c130" },
17-
"nvim-lspconfig": { "branch": "master", "commit": "92ee7d42320edfbb81f3cad851314ab197fa324a" },
18-
"nvim-treesitter": { "branch": "main", "commit": "42fc28ba918343ebfd5565147a42a26580579482" },
19-
"nvim-web-devicons": { "branch": "master", "commit": "d7462543c9e366c0d196c7f67a945eaaf5d99414" },
17+
"nvim-lspconfig": { "branch": "master", "commit": "9ccd58a7949091c0cc2777d4e92a45a209c808c1" },
18+
"nvim-treesitter": { "branch": "main", "commit": "4916d6592ede8c07973490d9322f187e07dfefac" },
19+
"nvim-web-devicons": { "branch": "master", "commit": "40e9d5a6cc3db11b309e39593fc7ac03bb844e38" },
2020
"plenary.nvim": { "branch": "master", "commit": "b9fd5226c2f76c951fc8ed5923d85e4de065e509" },
2121
"telescope-fzf-native.nvim": { "branch": "main", "commit": "6fea601bd2b694c6f2ae08a6c6fab14930c60e2c" },
2222
"telescope-ui-select.nvim": { "branch": "master", "commit": "6e51d7da30bd139a6950adf2a47fda6df9fa06d2" },
23-
"telescope.nvim": { "branch": "master", "commit": "a8c2223ea6b185701090ccb1ebc7f4e41c4c9784" },
23+
"telescope.nvim": { "branch": "master", "commit": "cfb85dcf7f822b79224e9e6aef9e8c794211b20b" },
2424
"todo-comments.nvim": { "branch": "main", "commit": "31e3c38ce9b29781e4422fc0322eb0a21f4e8668" },
25-
"tokyonight.nvim": { "branch": "main", "commit": "5da1b76e64daf4c5d410f06bcb6b9cb640da7dfd" },
25+
"tokyonight.nvim": { "branch": "main", "commit": "cdc07ac78467a233fd62c493de29a17e0cf2b2b6" },
2626
"which-key.nvim": { "branch": "main", "commit": "3aab2147e74890957785941f0c1ad87d0a44c15a" }
2727
}

0 commit comments

Comments
 (0)