From 09fafb946aefd8434f479656c22b90d9bc63c684 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Thu, 22 Jun 2023 11:43:28 +0200 Subject: [PATCH] perf(treesitter): better way of loading treesitter-textobjects --- lua/lazyvim/plugins/treesitter.lua | 38 +++++++++++++++++------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/lua/lazyvim/plugins/treesitter.lua b/lua/lazyvim/plugins/treesitter.lua index b565e862..c37de8dd 100644 --- a/lua/lazyvim/plugins/treesitter.lua +++ b/lua/lazyvim/plugins/treesitter.lua @@ -1,32 +1,23 @@ +local load_textobjects = false return { { "nvim-treesitter/nvim-treesitter", version = false, -- last release is way too old and doesn't work on Windows build = ":TSUpdate", event = { "BufReadPost", "BufNewFile" }, - cmd = { "TSUpdateSync" }, dependencies = { { "nvim-treesitter/nvim-treesitter-textobjects", init = function() - -- PERF: no need to load the plugin, if we only need its queries for mini.ai - local plugin = require("lazy.core.config").spec.plugins["nvim-treesitter"] - local opts = require("lazy.core.plugin").values(plugin, "opts", false) - local enabled = false - if opts.textobjects then - for _, mod in ipairs({ "move", "select", "swap", "lsp_interop" }) do - if opts.textobjects[mod] and opts.textobjects[mod].enable then - enabled = true - break - end - end - end - if not enabled then - require("lazy.core.loader").disable_rtp_plugin("nvim-treesitter-textobjects") - end + -- disable rtp plugin, as we only need its queries for mini.ai + -- In case other textobject modules are enabled, we will load them + -- once nvim-treesitter is loaded + require("lazy.core.loader").disable_rtp_plugin("nvim-treesitter-textobjects") + load_textobjects = true end, }, }, + cmd = { "TSUpdateSync" }, keys = { { "", desc = "Increment selection" }, { "", desc = "Decrement selection", mode = "x" }, @@ -79,6 +70,21 @@ return { end, opts.ensure_installed) end require("nvim-treesitter.configs").setup(opts) + + if load_textobjects then + -- PERF: no need to load the plugin, if we only need its queries for mini.ai + if opts.textobjects then + for _, mod in ipairs({ "move", "select", "swap", "lsp_interop" }) do + if opts.textobjects[mod] and opts.textobjects[mod].enable then + local Loader = require("lazy.core.loader") + Loader.disabled_rtp_plugins["nvim-treesitter-textobjects"] = nil + local plugin = require("lazy.core.config").plugins["nvim-treesitter-textobjects"] + require("lazy.core.loader").source_runtime(plugin.dir, "plugin") + break + end + end + end + end end, }, }