Skip to content

Commit b7522c6

Browse files
committed
refactor(rainbow-delims): separate concerns with condition API
Move parser health checks (nil guard, error count) to rainbow-delimiters native `condition` API. Simplify `init_strategy` to only select global/local based on line count. pcall handles both nvim 0.11 (throws) and 0.12+ (returns nil) get_parser behavior.
1 parent 5a8fa0f commit b7522c6

1 file changed

Lines changed: 23 additions & 18 deletions

File tree

lua/modules/configs/editor/rainbow_delims.lua

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,13 @@
11
return function()
2+
local rd = require("rainbow-delimiters")
3+
24
---@param threshold number @Use global strategy if nr of lines exceeds this value
35
local function init_strategy(threshold)
46
return function()
5-
-- Disable on very large files
6-
local line_count = vim.api.nvim_buf_line_count(0)
7-
if line_count > 15000 then
8-
return nil
9-
end
10-
11-
-- Disable on parser error
12-
local errors = 200
13-
vim.treesitter.get_parser():for_each_tree(function(lt)
14-
if lt:root():has_error() and errors >= 0 then
15-
errors = errors - 1
16-
end
17-
end)
18-
if errors < 0 then
19-
return nil
7+
if vim.api.nvim_buf_line_count(0) > threshold then
8+
return rd.strategy["global"]
209
end
21-
22-
return line_count > threshold and require("rainbow-delimiters").strategy["global"]
23-
or require("rainbow-delimiters").strategy["local"]
10+
return rd.strategy["local"]
2411
end
2512
end
2613

@@ -33,6 +20,24 @@ return function()
3320
vimdoc = init_strategy(300),
3421
vim = init_strategy(300),
3522
},
23+
---@param buf number
24+
condition = function(buf)
25+
if vim.api.nvim_buf_line_count(buf) > 15000 then
26+
return false
27+
end
28+
-- pcall handles both nvim 0.11 (throws) and 0.12+ (returns nil)
29+
local ok, parser = pcall(vim.treesitter.get_parser, buf)
30+
if not ok or not parser then
31+
return false
32+
end
33+
local errors = 200
34+
parser:for_each_tree(function(lt)
35+
if lt:root():has_error() and errors >= 0 then
36+
errors = errors - 1
37+
end
38+
end)
39+
return errors >= 0
40+
end,
3641
query = {
3742
[""] = "rainbow-delimiters",
3843
latex = "rainbow-blocks",

0 commit comments

Comments
 (0)