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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
-- See also: test/old/testdir/test_options.vim
local t = require('test.testutil')
local n = require('test.functional.testnvim')()
local Screen = require('test.functional.ui.screen')
local describe, it, before_each, setup = t.describe, t.it, t.before_each, t.setup
local command, clear = n.command, n.clear
local poke_eventloop = n.poke_eventloop
local source, expect = n.source, n.expect
local eval = n.eval
local exec_lua = n.exec_lua
local matches = t.matches
local pcall_err = t.pcall_err
local eq = t.eq
describe('options', function()
setup(clear)
it('should not throw any exception', function()
command('options')
end)
end)
describe('options :set', function()
before_each(clear)
it("should keep two comma when 'path' is changed", function()
source([[
set path=foo,,bar
set path-=bar
set path+=bar
$put =&path]])
expect([[
foo,,bar]])
end)
it("'winminheight'", function()
local _ = Screen.new(20, 11)
source([[
set wmh=0 stal=2
below sp | wincmd _
below sp | wincmd _
below sp | wincmd _
below sp
]])
matches('E36: Not enough room', pcall_err(command, 'set wmh=1'))
end)
it("'winminheight' with tabline", function()
local _ = Screen.new(20, 11)
source([[
set wmh=0 stal=2
split
split
split
split
tabnew
]])
matches('E36: Not enough room', pcall_err(command, 'set wmh=1'))
end)
it("'scroll'", function()
local screen = Screen.new(42, 16)
source([[
set scroll=2
set laststatus=2
]])
command('verbose set scroll?')
screen:expect([[
|
{1:~ }|*11
{3: }|
scroll=7 |
Last set from changed window size |
{6:Press ENTER or type command to continue}^ |
]])
end)
it('foldcolumn and signcolumn to empty string is disallowed', function()
matches("E474: Invalid value ''.*fdc=", pcall_err(command, 'set fdc='))
matches('E474: Invalid argument: scl=', pcall_err(command, 'set scl='))
end)
end)
describe('options validation', function()
before_each(clear)
it('rejects a restricted callback assignment before evaluating its expression', function()
local filename = t.tmpname()
t.finally(function()
os.remove(filename)
end)
t.write_file(filename, 'vim: set syntax=optiontest :\n')
source([[
set modeline
let g:arg_calls = 0
function! MakeArg()
let g:arg_calls += 1
return 0
endfunction
function! Handler(...)
return 0
endfunction
set operatorfunc=Handler
autocmd Syntax optiontest set operatorfunc=function('Handler',\ [MakeArg()])
]])
-- Syntax handlers triggered by a modeline run in secure mode. Setting operatorfunc
-- must be rejected before MakeArg() runs, even though it only constructs a callback.
matches('E523:', pcall_err(command, 'edit ' .. n.fn.fnameescape(filename)))
eq(0, eval('g:arg_calls'))
eq('Handler', eval('&operatorfunc'))
-- The same assignment is allowed afterwards, outside the modeline's restrictions.
command([[set operatorfunc=function('Handler',\ [MakeArg()])]])
eq(1, eval('g:arg_calls > 0'))
end)
-- Improved error messages for structured "key:value" options ("schema" in options.lua).
it('reports specific errors for structured (schema) options', function()
eq("Vim(set):E474: Unknown item 'foo': diffopt=foo", pcall_err(command, 'set diffopt=foo'))
eq(
"Vim(set):E474: 'context' requires a number: diffopt=context:x",
pcall_err(command, 'set diffopt=context:x')
)
eq(
'Vim(set):E474: '
.. "'algorithm' must be one of: myers, minimal, patience, histogram: diffopt=algorithm:bad",
pcall_err(command, 'set diffopt=algorithm:bad')
)
eq(
"Vim(set):E474: 'filler' does not take a value: diffopt=filler:1",
pcall_err(command, 'set diffopt=filler:1')
)
eq(
"Vim(set):E474: 'ver' number is out of range: mousescroll=ver:99999999999",
pcall_err(command, 'set mousescroll=ver:99999999999')
)
end)
-- Enum / flag-list options name the offending value and list the valid ones.
it('reports specific errors for enum and flag-list options', function()
eq(
"Vim(set):E474: Invalid value 'x', expected one of: single, double: ambiwidth=x",
pcall_err(command, 'set ambiwidth=x')
)
eq(
'Vim(set):E474: '
.. "Invalid value 'x', expected one of: yes, auto, no, breaksymlink, breakhardlink: backupcopy=x",
pcall_err(command, 'set backupcopy=x')
)
end)
end)
describe('callback (func/expr) options', function()
before_each(clear)
it('accept a Lua function, preserving identity and captured upvalues', function()
-- 'operatorfunc' is global; 'tagfunc' is buffer-local: exercise both storage paths.
eq(
{ identity = true, captured = 'CAP', kind = 'function', optset_old = '', optset_new = true },
exec_lua(function()
vim.api.nvim_buf_set_lines(0, 0, -1, false, { 'abcdef' })
local captured = 'CAP'
_G.seen = nil
local fn = function(_)
_G.seen = captured
end
-- OptionSet emits with Lua function as "<Lua …>".
local optset
vim.api.nvim_create_autocmd('OptionSet', {
pattern = 'operatorfunc',
callback = function()
optset = { old = vim.v.option_old, new = vim.v.option_new }
end,
})
vim.o.operatorfunc = fn
vim.cmd('normal! g@l')
return {
identity = vim.o.operatorfunc == fn,
captured = _G.seen,
kind = type(vim.o.operatorfunc),
optset_old = optset.old,
optset_new = tostring(optset.new):match('^<Lua ') ~= nil,
}
end)
)
eq(
true,
exec_lua(function()
local tfn = function(_, _, _)
return {}
end
vim.bo.tagfunc = tfn
return vim.bo.tagfunc == tfn
end)
)
-- Vimscript GC traverses buffer-local callback options; a Lua-function callback must survive it
-- garbagecollect() defers to main loop, so spin it with a motion to actually run collection.
command('call garbagecollect(1)')
n.feed('ll')
poke_eventloop()
eq('function', exec_lua('return type(vim.bo.tagfunc)'))
end)
it('accept a string funcref, read back as a string', function()
eq(
{ type = 'string', value = 'SomeFunc' },
exec_lua(function()
vim.o.operatorfunc = 'SomeFunc'
return { type = type(vim.o.operatorfunc), value = vim.o.operatorfunc }
end)
)
end)
it('reject a non-function, non-string value', function()
matches(
"Invalid 'operatorfunc': expected a valid type, got Integer",
pcall_err(exec_lua, 'vim.o.operatorfunc = 42')
)
-- Only callback (func/expr) options accept a function; others reject it.
matches(
"Invalid 'statusline': expected a valid type, got Function",
pcall_err(exec_lua, 'vim.o.statusline = function() end')
)
matches(
"Invalid 'shiftwidth': expected a valid type, got Function",
pcall_err(exec_lua, 'vim.bo.shiftwidth = function() end')
)
end)
it("preserve a partial's bound args when the value is copied to the global default", function()
-- 'tagfunc' is buffer-local; setting it also copies the value to the global default (which
-- new buffers inherit). A partial must keep its bound args across that copy.
command([[
func! Foo(bound, pat, flags, info)
return []
endfunc
set tagfunc=function('Foo',\ [10])
]])
eq("function('Foo', [10])", eval('&g:tagfunc'))
end)
it('keep the previous callback when set to an invalid function', function()
command('set operatorfunc=GoodFunc')
matches('E700:', pcall_err(command, "set operatorfunc=function('NoSuchFuncXYZ')"))
eq('GoodFunc', eval('&operatorfunc'))
end)
it(':set', function()
command('set operatorfunc=SomeFunc')
command('set quickfixtextfunc={\\ d\\ ->\\ []\\ }')
exec_lua(function()
vim.o.omnifunc = function() end
vim.wo.foldexpr = function() end
end)
command('set all')
n.assert_alive()
-- ":set opt?" shows funcref/lambda name, or "<Lua …>".
local function set_q(opt)
return n.api.nvim_exec2(('set %s?'):format(opt), { output = true }).output
end
eq(' operatorfunc=SomeFunc', set_q('operatorfunc'))
matches("^ quickfixtextfunc=function%('<lambda>%d+'%)$", set_q('quickfixtextfunc'))
matches('^ omnifunc=<Lua .+:%d+>$', set_q('omnifunc'))
matches('^ foldexpr=<Lua .+:%d+>$', set_q('foldexpr'))
eq(set_q('omnifunc'), set_q('omnifunc')) -- Stable: no per-read ref id.
-- ":set opt=<Tab>" completes a funcref value, but not a Lua function.
eq({ 'SomeFunc' }, n.fn.getcompletion('set operatorfunc=', 'cmdline'))
eq({}, n.fn.getcompletion('set omnifunc=', 'cmdline'))
end)
it("expr option ('foldexpr') accepts string, reads back string", function()
eq(
{ kind = 'string', value = 'v:lnum - 1', l1 = 0, l3 = 2 },
exec_lua(function()
vim.api.nvim_buf_set_lines(0, 0, -1, false, { 'a', 'b', 'c', 'd' })
vim.wo.foldmethod = 'expr'
vim.wo.foldexpr = 'v:lnum - 1'
return {
kind = type(vim.wo.foldexpr),
value = vim.wo.foldexpr,
l1 = vim.fn.foldlevel(1),
l3 = vim.fn.foldlevel(3),
}
end)
)
end)
it("expr option ('foldexpr') accepts Lua function, preserving identity", function()
-- Lua-function 'foldexpr' is invoked per line.
eq(
{ identity = true, kind = 'function', l2 = 0, l3 = 1 },
exec_lua(function()
vim.api.nvim_buf_set_lines(0, 0, -1, false, { 'a', 'b', 'c', 'd' })
local fn = function()
return vim.v.lnum >= 3 and 1 or 0
end
vim.wo.foldmethod = 'expr'
vim.wo.foldexpr = fn
return {
identity = vim.wo.foldexpr == fn,
kind = type(vim.wo.foldexpr),
l2 = vim.fn.foldlevel(2),
l3 = vim.fn.foldlevel(3),
}
end)
)
end)
it("buffer-local expr option ('indentexpr') accepts Lua function", function()
-- Exercises the buffer-local Callback path (copy/free) and get_expr_indent via `==`.
eq(
{ identity = true, indent = 8 },
exec_lua(function()
local myfn = function()
return 8
end
vim.bo.indentexpr = myfn
vim.api.nvim_buf_set_lines(0, 0, -1, false, { 'line1', 'line2' })
vim.cmd('normal! 2G==')
return {
identity = vim.bo.indentexpr == myfn,
indent = vim.fn.indent(2),
}
end)
)
end)
it("Lua function 'formatexpr' on another buffer (vim.bo[bufnr])", function()
-- Sets via the non-curbuf API path (context switch), the way LSP configures 'formatexpr'.
eq(
{ identity = true, cur_unchanged = true, invoked = true },
exec_lua(function()
local buf = vim.api.nvim_create_buf(true, false)
local invoked = false
local fexpr = function()
invoked = true
return 0
end
vim.bo[buf].formatexpr = fexpr
vim.api.nvim_buf_set_lines(buf, 0, -1, false, { 'some text to format' })
local identity = vim.bo[buf].formatexpr == fexpr
vim.api.nvim_buf_call(buf, function()
vim.cmd('normal! gqq')
end)
return {
identity = identity,
cur_unchanged = vim.bo.formatexpr == '',
invoked = invoked,
}
end)
)
end)
it("omits a Lua-function value from :mksession/:mkvimrc (no ':set' form)", function()
-- A Lua function has no ":set" repres
|