Skip to content

Commit 7bf45d6

Browse files
refactor: defer treesitter configuration (#14)
1 parent a34e426 commit 7bf45d6

File tree

2 files changed

+29
-24
lines changed

2 files changed

+29
-24
lines changed

lua/bigfile/features.lua

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,37 @@ feature("lsp", {
4545
end,
4646
})
4747

48+
local is_ts_configured = false
49+
50+
local function configure_treesitter()
51+
local status_ok, ts_config = pcall(require, "nvim-treesitter.configs")
52+
if not status_ok then
53+
return
54+
end
55+
56+
local disable_cb = function(_, buf)
57+
local success, detected = pcall(vim.api.nvim_buf_get_var, buf, "bigfile_disable_treesitter")
58+
return success and detected
59+
end
60+
61+
for _, mod_name in ipairs(ts_config.available_modules()) do
62+
local module_config = ts_config.get_module(mod_name) or {}
63+
local old_disabled = module_config.disable
64+
module_config.disable = function(lang, buf)
65+
return disable_cb(lang, buf)
66+
or (type(old_disabled) == "table" and vim.tbl_contains(old_disabled, lang))
67+
or (type(old_disabled) == "function" and old_disabled(lang, buf))
68+
end
69+
end
70+
71+
is_ts_configured = true
72+
end
4873
feature("treesitter", {
4974
disable = function(buf)
75+
if not is_ts_configured then
76+
configure_treesitter()
77+
end
78+
5079
vim.api.nvim_buf_set_var(buf, "bigfile_disable_treesitter", 1)
5180
end,
5281
})

lua/bigfile/init.lua

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,22 +73,6 @@ local function pre_bufread_callback(bufnr, rule)
7373
})
7474
end
7575

76-
local function configure_treesitter_disable(cb, modules)
77-
local status_ok, ts_config = pcall(require, "nvim-treesitter.configs")
78-
if not status_ok then
79-
return
80-
end
81-
82-
modules = modules or ts_config.available_modules()
83-
for _, mod_name in ipairs(modules) do
84-
local module_config = ts_config.get_module(mod_name) or {}
85-
module_config.disable = module_config.disable or {}
86-
if type(module_config.disable) == "table" and #module_config.disable == 0 then
87-
module_config.disable = cb
88-
end
89-
end
90-
end
91-
9276
---@param overrides config|nil
9377
function M.config(overrides)
9478
default_config = vim.tbl_deep_extend("force", default_config, overrides or {})
@@ -111,14 +95,6 @@ function M.setup(overrides)
11195
config.filesize
11296
),
11397
})
114-
115-
if vim.tbl_contains(config.features, "treesitter") then
116-
local disable_cb = function(_, buf)
117-
local status_ok, detected = pcall(vim.api.nvim_buf_get_var, buf, "bigfile_disable_treesitter")
118-
return status_ok and detected
119-
end
120-
configure_treesitter_disable(disable_cb)
121-
end
12298
end
12399

124100
return M

0 commit comments

Comments
 (0)