From c248827792f188c5586ab6294f4a690d518ca2a9 Mon Sep 17 00:00:00 2001 From: Dreaded_X Date: Mon, 26 May 2025 03:27:33 +0200 Subject: [PATCH] Auto install and auto start treesitter --- nvim/dot-config/nvim/lua/autocmds.lua | 24 ++++++++++++------- .../nvim/lua/plugins/treesitter.lua | 4 ---- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/nvim/dot-config/nvim/lua/autocmds.lua b/nvim/dot-config/nvim/lua/autocmds.lua index fe6eb83..5a2af57 100644 --- a/nvim/dot-config/nvim/lua/autocmds.lua +++ b/nvim/dot-config/nvim/lua/autocmds.lua @@ -33,14 +33,22 @@ vim.api.nvim_create_autocmd({ "InsertEnter", "WinLeave" }, { pattern = "*", }) +-- Auto install treesitter parser and start them vim.api.nvim_create_autocmd("FileType", { - pattern = require("tools.highlight"), - callback = function() - -- syntax highlighting, provided by Neovim - vim.treesitter.start() - -- folds, provided by Neovim - vim.wo.foldexpr = "v:lua.vim.treesitter.foldexpr()" - -- indentation, provided by nvim-treesitter - vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()" + desc = "Enable Treesitter", + group = vim.api.nvim_create_augroup("enable_treesitter", {}), + callback = function(event) + local has_treesitter, treesitter = pcall(require, "nvim-treesitter") + if has_treesitter then + local language = vim.treesitter.language.get_lang(event.match) + treesitter.install(language):await(function() + if pcall(vim.treesitter.start) then + -- Use Treesitter indentation and folds. + vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()" + vim.wo.foldmethod = "expr" + vim.wo.foldexpr = "v:lua.vim.treesitter.foldexpr()" + end + end) + end end, }) diff --git a/nvim/dot-config/nvim/lua/plugins/treesitter.lua b/nvim/dot-config/nvim/lua/plugins/treesitter.lua index 1210635..8ded268 100644 --- a/nvim/dot-config/nvim/lua/plugins/treesitter.lua +++ b/nvim/dot-config/nvim/lua/plugins/treesitter.lua @@ -11,12 +11,8 @@ return { branch = "main", build = ":TSUpdate", opts = { - -- Add languages to be installed here that you want installed for treesitter ensure_installed = require("tools.highlight"), - -- Autoinstall languages that are not installed. Defaults to false (but you can change for yourself!) - auto_install = true, - highlight = { enable = true }, indent = { enable = true }, incremental_selection = {