-
Notifications
You must be signed in to change notification settings - Fork 11
/
init.lua
126 lines (111 loc) Β· 3.2 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
local M = {}
local features = require "bigfile.features"
---@class rule
---@field size integer file size in MiB
---@field features feature[] array of features
---@class config
---@field rules rule[] rules
local config = {
rules = {
{
size = 1,
features = {
"indent_blankline",
"illuminate",
"lsp",
"treesitter",
"syntax",
"matchparen",
"vimopts",
},
},
{ size = 50, features = { "filetype" } },
},
}
---@param bufnr number
---@return integer|nil size in MiB if buffer is valid, nil otherwise
local function get_buf_size(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
local ok, stats = pcall(function()
return vim.loop.fs_stat(vim.api.nvim_buf_get_name(bufnr))
end)
if not (ok and stats) then
return
end
return math.floor(0.5 + (stats.size / (1024 * 1024)))
end
---@param bufnr number buffer id to match against
---@return feature[] features Features from rules that match the `filesize`
local function match_features(bufnr)
local matched_features = {}
local filesize = get_buf_size(bufnr)
if not filesize then
return matched_features
end
for _, rule in ipairs(config.rules) do
if filesize >= rule.size then
for _, raw_feature in ipairs(rule.features) do
matched_features[#matched_features + 1] = features.get_feature(raw_feature)
end
else -- since rules should be sorted, we can exit early
return matched_features
end
end
return matched_features
end
local function pre_bufread_callback(args)
local status_ok, _ = pcall(vim.api.nvim_buf_get_var, args.buf, "bigfile_detected")
if status_ok then
return -- buffer has already been processed
end
local matched_features = vim.tbl_filter(function(feature)
return type(feature.disable) == "function"
end, match_features(args.bufnr))
if #matched_features == 0 then
vim.api.nvim_buf_set_var(args.buf, "bigfile_detected", 0)
return
end
vim.api.nvim_buf_set_var(args.buf, "bigfile_detected", 1)
-- Categorize features and disable features that don't need deferring
local matched_deferred_features = {}
for _, feature in ipairs(matched_features) do
if feature.opts.defer then
table.insert(matched_deferred_features, feature)
else
feature.disable(args.buf)
end
end
-- Schedule disabling deferred features
if #matched_deferred_features > 0 then
vim.api.nvim_create_autocmd({ "BufReadPost" }, {
callback = function()
for _, feature in ipairs(matched_deferred_features) do
feature.disable(args.buf)
end
end,
buffer = args.buf,
})
end
end
---@param user_config config|nil
function M.setup(user_config)
if type(user_config) == "table" then
if user_config.rules then
config.rules = user_config.rules
end
end
local treesitter_configs = require "nvim-treesitter.configs"
treesitter_configs.setup {
highlight = {
disable = function(_, buf)
return pcall(vim.api.nvim_buf_get_var, buf, "bigfile_detected")
end,
},
}
vim.api.nvim_create_augroup("bigfile", {})
vim.api.nvim_create_autocmd("BufReadPre", {
group = "bigfile",
callback = pre_bufread_callback,
})
end
return M