diff --git a/home/private_dot_config/nvim/lua/plugins/treesitter.lua b/home/private_dot_config/nvim/lua/plugins/treesitter.lua index 71b8277..1b92cae 100644 --- a/home/private_dot_config/nvim/lua/plugins/treesitter.lua +++ b/home/private_dot_config/nvim/lua/plugins/treesitter.lua @@ -1,6 +1,57 @@ return { 'nvim-treesitter/nvim-treesitter', + branch = 'main', lazy = false, - build = ':TSUpdate' + build = ':TSUpdate', + config = function() + -- Old `ensure_installed` + require('nvim-treesitter').install({ + -- Neovim config + 'lua', 'vim', 'vimdoc', 'query', + + -- Rust + 'rust', 'toml', + + -- Ansible + 'yaml', + + -- Frontend stuff + 'html', 'css', 'javascript', 'typescript', 'tsx', 'json', + + -- Low level stuff + 'c', 'asm', + }) + + -- Old `auto_install` + local ts_autoinstall_group = vim.api.nvim_create_augroup('TSAutoInstall', { clear = true }) + vim.api.nvim_create_autocmd('FileType', { + group = ts_autoinstall_group, + callback = function(args) + local lang = vim.treesitter.language.get_lang(args.match) or args.match + + -- Try starting right away: if it works, the parser was already installed + if pcall(vim.treesitter.start, args.buf, lang) then + return + end + + -- Didn't work: download the parser in the background + local ok = pcall(function() + require('nvim-treesitter').install({ lang }, { summary = false }) + end) + if not ok then return end + + -- Poll (non-blocking) until start() succeeds, or give up after 9s + local function try_start(attempts) + attempts = attempts or 0 + if pcall(vim.treesitter.start, args.buf, lang) then + return + elseif attempts < 30 then + vim.defer_fn(function() try_start(attempts + 1) end, 300) + end + end + try_start() + end, + }) + end, } diff --git a/setup-script/roles/editor/tasks/main.yaml b/setup-script/roles/editor/tasks/main.yaml index 5b3ac20..dc90abd 100644 --- a/setup-script/roles/editor/tasks/main.yaml +++ b/setup-script/roles/editor/tasks/main.yaml @@ -9,3 +9,14 @@ ansible.builtin.pacman: name: "{{ 'wl-clipboard' if lookup('env', 'XDG_SESSION_TYPE') == 'wayland' else 'xclip' }}" when: chezmoi.osRelease.id == "arch" + +- name: Install plugin dependencies + become: true + ansible.builtin.pacman: + name: + # nvim-treesitter + - tree-sitter-cli + - gcc + - curl + - tar + when: chezmoi.osRelease.id == "arch"