forked from HiPhish/rainbow-delimiters.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.lua
171 lines (144 loc) · 6.53 KB
/
config.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
local yd = require 'yo-dawg'
describe('User settings are respected', function()
local nvim
before_each(function()
nvim = yd.start()
end)
after_each(function()
yd.stop(nvim)
end)
describe('Strategy settings', function()
it('Applies the default strategy to all languages', function()
local strategy = 'default strategy'
nvim:exec2('let g:rainbow_delimiters = {"strategy": {"": "default strategy"}}', {})
local lua_strategy = nvim:exec_lua('return require("rainbow-delimiters.config").strategy.lua', {})
local c_strategy = nvim:exec_lua('return require("rainbow-delimiters.config").strategy.c', {})
assert.is.equal(strategy, lua_strategy)
assert.is.equal(strategy, c_strategy)
end)
it('Overrides the strategy for individual languages', function()
-- I had to use a trick here because we cannot compare dictionaries or
-- functions for identity between Vim script and Lua. Instead I
-- set a string as the strategy and compare for that equality.
nvim:exec_lua('require("rainbow-delimiters.default").strategy[""] = "default strategy"', {})
-- Override the strategy for Vim only
nvim:set_var('rainbow_delimiters', {strategy = {vim = 'vim strategy'}})
local lua_strategy = nvim:exec_lua('return require("rainbow-delimiters.config").strategy.lua', {})
local vim_strategy = nvim:exec_lua('return require("rainbow-delimiters.config").strategy.vim', {})
assert.is.equal('vim strategy', vim_strategy, 'Wrong strategy found for Vim')
assert.is.equal('default strategy', lua_strategy, 'Wrong strategy found for Lua')
end)
describe('Strategies can be thunks', function()
before_each(function()
-- Store strategies in global variables for later reference
nvim:exec_lua('noop = require("rainbow-delimiters").strategy.noop', {})
nvim:exec_lua('the_strategy = require("rainbow-delimiters.strategy.track")(noop)', {})
-- Set a thunk as the strategy
nvim:exec_lua([[
vim.g.rainbow_delimiters = {
strategy = {
[""] = function() return the_strategy end,
vim = function() return nil end
}
}]], {})
end)
it('Uses the strategy returned by the thunk', function()
nvim:exec_lua('TSEnsure(...)', {'lua'})
nvim:buf_set_lines(0, 0, -1, true, {'print "Hello world"', '-- vim:ft=lua'})
nvim:command('filetype detect')
local attachments = nvim:exec_lua('return the_strategy.attachments[1]', {})
assert.is.equal(1, attachments, 'The strategy should be attached to the Lua buffer')
end)
it('Does nothing if the thunk returns nil', function()
nvim:exec_lua('TSEnsure(...)', {'vim'})
nvim:buf_set_lines(0, 0, -1, true, {'echo "Hello world"', '" vim:ft=vim'})
nvim:command('filetype detect')
local attachments = nvim:exec_lua('return the_strategy.attachments[1]', {})
assert.is.equal(0, attachments, 'The strategy should not be attached to the Vim buffer')
end)
end)
end)
it('Overrides the query for an individual language', function()
-- Override the query for one language only
nvim:set_var('rainbow_delimiters', {query = {c = 'other-query'}})
local c_query = nvim:exec_lua('return require("rainbow-delimiters.config").query.c', {})
local lua_query = nvim:exec_lua('return require("rainbow-delimiters.config").query.lua', {})
assert.is.equal('other-query', c_query)
assert.is.equal('rainbow-delimiters', lua_query)
end)
it('Falls back to default highlighting if the highlight table is empty', function()
---The expected highlight groups in order
local hlgroups = {
'RainbowDelimiterRed',
'RainbowDelimiterYellow',
'RainbowDelimiterBlue',
'RainbowDelimiterOrange',
'RainbowDelimiterGreen',
'RainbowDelimiterViolet',
'RainbowDelimiterCyan',
}
-- Set highlight to empty list
nvim:set_var('rainbow_delimiters', {highlight = {}})
for i, expected in ipairs(hlgroups) do
local given = nvim:exec_lua('return require("rainbow-delimiters.config").highlight[...]', {i})
assert.is.equal(expected, given, string.format('Wrong highlight group at index %d', i))
end
end)
describe('White- and blacklist individual languages', function()
it('Has all languages enabled without configuration', function()
nvim:exec_lua('rbc = require("rainbow-delimiters.config")', {})
local lua_enabled = nvim:exec_lua('return rbc.enabled_for("lua")', {})
local vim_enabled = nvim:exec_lua('return rbc.enabled_for("vim")', {})
assert.is_true(lua_enabled, 'Lua should be enabled')
assert.is_true(vim_enabled, 'Vim script should be enabled')
end)
it('Has all languages enabled in blank configuration', function()
nvim:set_var('rainbow_delimiters', {})
nvim:exec_lua('rbc = require("rainbow-delimiters.config")', {})
local lua_enabled = nvim:exec_lua('return rbc.enabled_for("lua")', {})
local vim_enabled = nvim:exec_lua('return rbc.enabled_for("vim")', {})
assert.is_true(lua_enabled, 'Lua should be enabled')
assert.is_true(vim_enabled, 'Vim script should be enabled')
end)
it('Can whitelist individual file types by adding them to our configuration', function()
nvim:set_var('rainbow_delimiters', {whitelist = {'lua'}})
nvim:exec_lua('rbc = require("rainbow-delimiters.config")', {})
local lua_enabled = nvim:exec_lua('return rbc.enabled_for("lua")', {})
local vim_enabled = nvim:exec_lua('return rbc.enabled_for("vim")', {})
assert.is_true( lua_enabled, 'Lua should be enabled')
assert.is_false(vim_enabled, 'Vim script should be disabled')
end)
it('Can blacklist individual file types by adding them to our configuration', function()
nvim:set_var('rainbow_delimiters', {blacklist = {'vim'}})
nvim:exec_lua('rbc = require("rainbow-delimiters.config")', {})
local lua_enabled = nvim:exec_lua('return rbc.enabled_for("lua")', {})
local vim_enabled = nvim:exec_lua('return rbc.enabled_for("vim")', {})
assert.is_true( lua_enabled, 'Lua should be enabled')
assert.is_false(vim_enabled, 'Vim script should be disabled')
end)
end)
describe('The setup function sets configuration indirectly', function()
it('Can call the setup function', function()
nvim:exec_lua([[
require('rainbow-delimiters.setup').setup {
query = {
lua = 'rainbow-blocks'
}
}
]], {})
local lua_query = nvim:eval('g:rainbow_delimiters.query.lua')
assert.is.equal('rainbow-blocks', lua_query)
end)
it('Can call the table itset', function()
nvim:exec_lua([[
require('rainbow-delimiters.setup') {
query = {
lua = 'rainbow-blocks'
}
}
]], {})
local lua_query = nvim:eval('g:rainbow_delimiters.query.lua')
assert.is.equal('rainbow-blocks', lua_query)
end)
end)
end)