Skip to content

Commit

Permalink
perf(treesitter): better way of loading treesitter-textobjects
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 22, 2023
1 parent 8281bd2 commit 09fafb9
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions lua/lazyvim/plugins/treesitter.lua
Original file line number Diff line number Diff line change
@@ -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 = {
{ "<c-space>", desc = "Increment selection" },
{ "<bs>", desc = "Decrement selection", mode = "x" },
Expand Down Expand Up @@ -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,
},
}

0 comments on commit 09fafb9

Please sign in to comment.