Add option to dynamically disable for a buffer

Based on a GitHub discussion[1], this option lets users specify a
predicate to dynamically enable or disable rainbow delimiters for a
buffer.

[1] https://github.com/HiPhish/rainbow-delimiters.nvim/discussions/130
This commit is contained in:
HiPhish 2024-07-25 23:40:01 +02:00
parent a727bd368e
commit a74553b331
6 changed files with 133 additions and 5 deletions

View file

@ -9,6 +9,16 @@ is based on `Keep a Changelog`_ and this project adheres to `Semantic
Versioning`_.
Unreleased
##########
Added
=====
- New option `condition`: allows users to dynamically enable or disable rainbow
delimiters for a buffer.
[0.5.0] - 2024-07-29
####################

View file

@ -78,6 +78,17 @@ keys are recognized:
List of Tree-sitter languages for which to disabled rainbow delimiters.
Rainbow delimiters will be enabled for all other languages.
`condition`
Function which receives the current buffer number and returns a boolean; can
be used to dynamically decide whether to enable (true) or disable (false)
rainbow delimiters for a given buffer. You could for example use this
predicate to disable rainbow delimiters for large files.
Superceeded by the blacklist, but superceedes the whitelist. This means if
the file type is blacklisted the condtion is ignored, but if the file type
is whitelisted or not blacklisted the condition will be evaluated and
honoured.
`log`
Settings for logging information. This is a dictionary which contains
further settings. See |rb-delimiters-logging| for details.

View file

@ -35,6 +35,8 @@
---@field whitelist rainbow_delimiters.language[]?
---Blacklist for languages not to highlight
---@field blacklist rainbow_delimiters.language[]?
---Dynamic condition whether to enable rainbow highlighting
---@field condition (fun(bufnr: number): boolean)?
---Logging with log file and log level
---@field log rainbow_delimiters.logging?

View file

@ -81,6 +81,13 @@ local M = {
end
return true
end,
enabled_when = function(bufnr)
local conf = vim.g.rainbow_delimiters
if not conf or not conf.condition then
return true
end
return conf.condition(bufnr)
end
}

View file

@ -58,9 +58,11 @@ create_autocmd('FileType', {
group = rb_augroup,
callback = function(args)
local lang = get_lang(args.match)
local bufnr = args.buf
if not config.enabled_for(lang) then return end
if not config.enabled_when(bufnr) then return end
lib.attach(args.buf)
lib.attach(bufnr)
end,
})

View file

@ -18,14 +18,24 @@ describe('We can disable rainbow delimiters for certain languages', function()
end)
it('Does not run when blacklisted', function()
nvim:exec_lua('vim.g.rainbow_delimiters = {strategy = {[""] = the_strategy}, blacklist = {"lua"}}', {})
nvim:exec_lua([[
vim.g.rainbow_delimiters = {
strategy = {[""] = the_strategy},
blacklist = {"lua"},
}
]], {})
nvim:command('filetype detect')
local attachments = nvim:exec_lua('return the_strategy.attachments[1]', {})
assert.is.equal(0, attachments)
end)
it('Runs when whitelisted', function()
nvim:exec_lua('vim.g.rainbow_delimiters = {strategy = {[""] = the_strategy}, whitelist = {"lua"}}', {})
nvim:exec_lua([[
vim.g.rainbow_delimiters = {
strategy = {[""] = the_strategy},
whitelist = {"lua"},
}
]], {})
nvim:command('filetype detect')
local attachments = nvim:exec_lua('return the_strategy.attachments[1]', {})
assert.is.equal(1, attachments)
@ -38,14 +48,100 @@ describe('We can disable rainbow delimiters for certain languages', function()
end)
it('Runs when not blacklisted', function()
nvim:exec_lua('vim.g.rainbow_delimiters = {strategy = {[""] = the_strategy}, blacklist = {"lua"}}', {})
nvim:exec_lua([[
vim.g.rainbow_delimiters = {
strategy = {[""] = the_strategy},
blacklist = {"lua"},
}
]], {})
nvim:command('filetype detect')
local attachments = nvim:exec_lua('return the_strategy.attachments[1]', {})
assert.is.equal(1, attachments)
end)
it('Does not run when not whitelisted', function()
nvim:exec_lua('vim.g.rainbow_delimiters = {strategy = {[""] = the_strategy}, whitelist = {"lua"}}', {})
nvim:exec_lua([[
vim.g.rainbow_delimiters = {
strategy = {[""] = the_strategy},
whitelist = {"lua"}
}
]], {})
nvim:command('filetype detect')
local attachments = nvim:exec_lua('return the_strategy.attachments[1]', {})
assert.is.equal(0, attachments)
end)
end)
describe('For dynamic condition', function()
before_each(function()
nvim:buf_set_lines(0, 0, -1, true, {'print "Hello world"', '-- vim:ft=lua'})
end)
it('Runs when no condition is set', function()
nvim:exec_lua([[
vim.g.rainbow_delimiters = {
strategy = {[""] = the_strategy},
condition = nil,
}
]], {})
nvim:command('filetype detect')
local attachments = nvim:exec_lua('return the_strategy.attachments[1]', {})
assert.is.equal(1, attachments)
end)
it('Runs when condition is met', function()
nvim:exec_lua([[
vim.g.rainbow_delimiters = {
strategy = {[""] = the_strategy},
condition = function(bufnr)
return true
end,
}
]], {})
nvim:command('filetype detect')
local attachments = nvim:exec_lua('return the_strategy.attachments[1]', {})
assert.is.equal(1, attachments)
end)
it('Does not run when condition is unmet', function()
nvim:exec_lua([[
vim.g.rainbow_delimiters = {
strategy = {[""] = the_strategy},
condition = function(_bufnr)
return false
end,
}
]], {})
nvim:command('filetype detect')
local attachments = nvim:exec_lua('return the_strategy.attachments[1]', {})
assert.is.equal(0, attachments)
end)
it('Is ignored for blacklisted buffers', function()
nvim:exec_lua([[
vim.g.rainbow_delimiters = {
strategy = {[""] = the_strategy},
blacklist = {'lua'},
condition = function(_bufnr)
return true
end,
}
]], {})
nvim:command('filetype detect')
local attachments = nvim:exec_lua('return the_strategy.attachments[1]', {})
assert.is.equal(0, attachments)
end)
it('Is takes prededence over the whitelist', function()
nvim:exec_lua([[
vim.g.rainbow_delimiters = {
strategy = {[""] = the_strategy},
whitelist = {'lua'},
condition = function(_bufnr)
return false
end,
}
]], {})
nvim:command('filetype detect')
local attachments = nvim:exec_lua('return the_strategy.attachments[1]', {})
assert.is.equal(0, attachments)