From 365f0eb75f4b8de4655476388c99aaa97f149ded Mon Sep 17 00:00:00 2001 From: Jonas Strittmatter <40792180+smjonas@users.noreply.github.com> Date: Fri, 8 Jul 2022 09:36:54 +0200 Subject: feat: add option to auto-install missing parsers (#3130) --- README.md | 5 ++++- doc/nvim-treesitter.txt | 3 +++ lua/nvim-treesitter/configs.lua | 6 ++++++ lua/nvim-treesitter/install.lua | 12 ++++++++++++ 4 files changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c8659fa9a..be070d5c4 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,9 @@ require'nvim-treesitter.configs'.setup { -- Install parsers synchronously (only applied to `ensure_installed`) sync_install = false, + -- Automatically install missing parsers when entering buffer + auto_install = true, + -- List of parsers to ignore installing (for "all") ignore_install = { "javascript" }, @@ -381,7 +384,7 @@ This directory must be writeable and must be explicitly added to the If this option is not included in the setup options, or is explicitly set to `nil` then the default install directories will be used. If this value is set -the default directories will be ignored. +the default directories will be ignored. Bear in mind that any parser installed into a parser folder on the runtime path will still be considered installed. (For example if diff --git a/doc/nvim-treesitter.txt b/doc/nvim-treesitter.txt index e870ab352..6fc65d001 100644 --- a/doc/nvim-treesitter.txt +++ b/doc/nvim-treesitter.txt @@ -51,6 +51,9 @@ To enable supported features, put this in your `init.lua` file: -- Install parsers synchronously (only applied to `ensure_installed`) sync_install = false, + -- Automatically install missing parsers when entering buffer + auto_install = false, + -- List of parsers to ignore installing (for "all") ignore_install = { "javascript" }, diff --git a/lua/nvim-treesitter/configs.lua b/lua/nvim-treesitter/configs.lua index ee01f3ede..41310f544 100644 --- a/lua/nvim-treesitter/configs.lua +++ b/lua/nvim-treesitter/configs.lua @@ -13,6 +13,7 @@ local config = { modules = {}, sync_install = false, ensure_installed = {}, + auto_install = false, ignore_install = {}, update_strategy = "lockfile", parser_install_dir = nil, @@ -386,6 +387,11 @@ function M.setup(user_data) config.parser_install_dir = vim.fn.expand(config.parser_install_dir, ":p") end + config.auto_install = user_data.auto_install or false + if config.auto_install then + require("nvim-treesitter.install").setup_auto_install() + end + local ensure_installed = user_data.ensure_installed or {} if #ensure_installed > 0 then if user_data.sync_install then diff --git a/lua/nvim-treesitter/install.lua b/lua/nvim-treesitter/install.lua index e709849ad..be980ca0f 100644 --- a/lua/nvim-treesitter/install.lua +++ b/lua/nvim-treesitter/install.lua @@ -425,6 +425,18 @@ local function install(options) end end +function M.setup_auto_install() + vim.api.nvim_create_autocmd("FileType", { + pattern = { "*" }, + callback = function() + local lang = parsers.get_buf_lang() + if parsers.get_parser_configs()[lang] and not parsers.has_parser(lang) then + install { ask_reinstall = true } { lang } + end + end, + }) +end + function M.update(options) options = options or {} return function(...) -- cgit v1.2.3-70-g09d2