robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1-- See also: test/old/testdir/test_options.vim 2local t =require ( 'test.testutil' ) 3local n =require ( 'test.functional.testnvim' )() 4local Screen =require ( 'test.functional.ui.screen' ) 5 6local describe , it , before_each , setup =t . describe , t . it , t . before_each , t . setup 7local command , clear =n . command , n . clear 8local poke_eventloop =n . poke_eventloop 9local source , expect =n . source , n . expect 10local eval =n . eval 11local exec_lua =n . exec_lua 12local matches =t . matches 13local pcall_err =t . pcall_err 14local eq =t . eq 15 16describe ( 'options' , function () 17setup ( clear ) 18 19it ( 'should not throw any exception' , function () 20command ( 'options' ) 21end ) 22end ) 23 24describe ( 'options :set' , function () 25before_each ( clear ) 26 27it ( "should keep two comma when 'path' is changed" , function () 28source ( [[ 29set path=foo,,bar 30set path-=bar 31set path+=bar 32$put =&path]] ) 33 34expect ( [[ 35 36foo,,bar]] ) 37end ) 38 39it ( "'winminheight'" , function () 40local _ =Screen . new ( 20 , 11 ) 41source ( [[ 42set wmh=0 stal=2 43below sp | wincmd _ 44below sp | wincmd _ 45below sp | wincmd _ 46below sp 47]] ) 48matches ( 'E36: Not enough room' , pcall_err ( command , 'set wmh=1' )) 49end ) 50 51it ( "'winminheight' with tabline" , function () 52local _ =Screen . new ( 20 , 11 ) 53source ( [[ 54set wmh=0 stal=2 55split 56split 57split 58split 59tabnew 60]] ) 61matches ( 'E36: Not enough room' , pcall_err ( command , 'set wmh=1' )) 62end ) 63 64it ( "'scroll'" , function () 65local screen =Screen . new ( 42 , 16 ) 66source ( [[ 67set scroll=2 68set laststatus=2 69]] ) 70command ( 'verbose set scroll?' ) 71screen : expect ( [[ 72| 73{1:~ }|*11 74{3: }| 75scroll=7 | 76Last set from changed window size | 77{6:Press ENTER or type command to continue}^ | 78]] ) 79end ) 80 81it ( 'foldcolumn and signcolumn to empty string is disallowed' , function () 82matches ( "E474: Invalid value ''.*fdc=" , pcall_err ( command , 'set fdc=' )) 83matches ( 'E474: Invalid argument: scl=' , pcall_err ( command , 'set scl=' )) 84end ) 85end ) 86 87describe ( 'options validation' , function () 88before_each ( clear ) 89 90it ( 'rejects a restricted callback assignment before evaluating its expression' , function () 91local filename =t . tmpname () 92t . finally ( function () 93os . remove ( filename ) 94end ) 95t . write_file ( filename , 'vim: set syntax=optiontest :\n' ) 96source ( [[ 97set modeline 98let g:arg_calls = 0 99function! MakeArg() 100let g:arg_calls += 1 101return 0 102endfunction 103function! Handler(...) 104return 0 105endfunction 106set operatorfunc=Handler 107autocmd Syntax optiontest set operatorfunc=function('Handler',\ [MakeArg()]) 108]] ) 109 110-- Syntax handlers triggered by a modeline run in secure mode. Setting operatorfunc 111-- must be rejected before MakeArg() runs, even though it only constructs a callback. 112matches ( 'E523:' , pcall_err ( command , 'edit ' .. n . fn . fnameescape ( filename ))) 113eq ( 0 , eval ( 'g:arg_calls' )) 114eq ( 'Handler' , eval ( '&operatorfunc' )) 115 116-- The same assignment is allowed afterwards, outside the modeline's restrictions. 117command ( [[set operatorfunc=function('Handler',\ [MakeArg()])]] ) 118eq ( 1 , eval ( 'g:arg_calls > 0' )) 119end ) 120 121-- Improved error messages for structured "key:value" options ("schema" in options.lua). 122it ( 'reports specific errors for structured (schema) options' , function () 123eq ( "Vim(set):E474: Unknown item 'foo': diffopt=foo" , pcall_err ( command , 'set diffopt=foo' )) 124eq ( 125"Vim(set):E474: 'context' requires a number: diffopt=context:x" , 126pcall_err ( command , 'set diffopt=context:x' ) 127) 128eq ( 129'Vim(set):E474: ' 130.. "'algorithm' must be one of: myers, minimal, patience, histogram: diffopt=algorithm:bad" , 131pcall_err ( command , 'set diffopt=algorithm:bad' ) 132) 133eq ( 134"Vim(set):E474: 'filler' does not take a value: diffopt=filler:1" , 135pcall_err ( command , 'set diffopt=filler:1' ) 136) 137eq ( 138"Vim(set):E474: 'ver' number is out of range: mousescroll=ver:99999999999" , 139pcall_err ( command , 'set mousescroll=ver:99999999999' ) 140) 141end ) 142 143-- Enum / flag-list options name the offending value and list the valid ones. 144it ( 'reports specific errors for enum and flag-list options' , function () 145eq ( 146"Vim(set):E474: Invalid value 'x', expected one of: single, double: ambiwidth=x" , 147pcall_err ( command , 'set ambiwidth=x' ) 148) 149eq ( 150'Vim(set):E474: ' 151.. "Invalid value 'x', expected one of: yes, auto, no, breaksymlink, breakhardlink: backupcopy=x" , 152pcall_err ( command , 'set backupcopy=x' ) 153) 154end ) 155end ) 156 157describe ( 'callback (func/expr) options' , function () 158before_each ( clear ) 159 160it ( 'accept a Lua function, preserving identity and captured upvalues' , function () 161-- 'operatorfunc' is global; 'tagfunc' is buffer-local: exercise both storage paths. 162eq ( 163{ identity =true , captured ='CAP' , kind ='function' , optset_old ='' , optset_new =true } , 164exec_lua ( function () 165vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'abcdef' } ) 166local captured ='CAP' 167_G . seen =nil 168local fn =function ( _ ) 169_G . seen =captured 170end 171-- OptionSet emits with Lua function as "<Lua …>". 172local optset 173vim . api . nvim_create_autocmd ( 'OptionSet' , { 174pattern ='operatorfunc' , 175callback =function () 176optset ={ old =vim . v . option_old , new =vim . v . option_new } 177end , 178} ) 179vim . o . operatorfunc =fn 180vim . cmd ( 'normal! g@l' ) 181return { 182identity =vim . o . operatorfunc == fn , 183captured =_G . seen , 184kind =type ( vim . o . operatorfunc ), 185optset_old =optset . old , 186optset_new =tostring ( optset . new ): match ( '^<Lua ' ) ~= nil , 187} 188end ) 189) 190eq ( 191true , 192exec_lua ( function () 193local tfn =function ( _ , _ , _ ) 194return {} 195end 196vim . bo . tagfunc =tfn 197return vim . bo . tagfunc == tfn 198end ) 199) 200-- Vimscript GC traverses buffer-local callback options; a Lua-function callback must survive it 201-- garbagecollect() defers to main loop, so spin it with a motion to actually run collection. 202command ( 'call garbagecollect(1)' ) 203n . feed ( 'll' ) 204poke_eventloop () 205eq ( 'function' , exec_lua ( 'return type(vim.bo.tagfunc)' )) 206end ) 207 208it ( 'accept a string funcref, read back as a string' , function () 209eq ( 210{ type ='string' , value ='SomeFunc' } , 211exec_lua ( function () 212vim . o . operatorfunc ='SomeFunc' 213return { type =type ( vim . o . operatorfunc ), value =vim . o . operatorfunc } 214end ) 215) 216end ) 217 218it ( 'reject a non-function, non-string value' , function () 219matches ( 220"Invalid 'operatorfunc': expected a valid type, got Integer" , 221pcall_err ( exec_lua , 'vim.o.operatorfunc = 42' ) 222) 223-- Only callback (func/expr) options accept a function; others reject it. 224matches ( 225"Invalid 'statusline': expected a valid type, got Function" , 226pcall_err ( exec_lua , 'vim.o.statusline = function() end' ) 227) 228matches ( 229"Invalid 'shiftwidth': expected a valid type, got Function" , 230pcall_err ( exec_lua , 'vim.bo.shiftwidth = function() end' ) 231) 232end ) 233 234it ( "preserve a partial's bound args when the value is copied to the global default" , function () 235-- 'tagfunc' is buffer-local; setting it also copies the value to the global default (which 236-- new buffers inherit). A partial must keep its bound args across that copy. 237command ( [[ 238func! Foo(bound, pat, flags, info) 239return [] 240endfunc 241set tagfunc=function('Foo',\ [10]) 242]] ) 243eq ( "function('Foo', [10])" , eval ( '&g:tagfunc' )) 244end ) 245 246it ( 'keep the previous callback when set to an invalid function' , function () 247command ( 'set operatorfunc=GoodFunc' ) 248matches ( 'E700:' , pcall_err ( command , "set operatorfunc=function('NoSuchFuncXYZ')" )) 249eq ( 'GoodFunc' , eval ( '&operatorfunc' )) 250end ) 251 252it ( ':set' , function () 253command ( 'set operatorfunc=SomeFunc' ) 254command ( 'set quickfixtextfunc={\\ d\\ ->\\ []\\ }' ) 255exec_lua ( function () 256vim . o . omnifunc =function () end 257vim . wo . foldexpr =function () end 258end ) 259command ( 'set all' ) 260n . assert_alive () 261 262-- ":set opt?" shows funcref/lambda name, or "<Lua …>". 263local function set_q ( opt ) 264return n . api . nvim_exec2 (( 'set %s?' ): format ( opt ), { output =true } ). output 265end 266eq ( ' operatorfunc=SomeFunc' , set_q ( 'operatorfunc' )) 267matches ( "^ quickfixtextfunc=function%('<lambda>%d+'%)$" , set_q ( 'quickfixtextfunc' )) 268matches ( '^ omnifunc=<Lua .+:%d+>$' , set_q ( 'omnifunc' )) 269matches ( '^ foldexpr=<Lua .+:%d+>$' , set_q ( 'foldexpr' )) 270eq ( set_q ( 'omnifunc' ), set_q ( 'omnifunc' )) -- Stable: no per-read ref id. 271 272-- ":set opt=<Tab>" completes a funcref value, but not a Lua function. 273eq ( { 'SomeFunc' } , n . fn . getcompletion ( 'set operatorfunc=' , 'cmdline' )) 274eq ( {} , n . fn . getcompletion ( 'set omnifunc=' , 'cmdline' )) 275end ) 276 277it ( "expr option ('foldexpr') accepts string, reads back string" , function () 278eq ( 279{ kind ='string' , value ='v:lnum - 1' , l1 =0 , l3 =2 } , 280exec_lua ( function () 281vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'a' , 'b' , 'c' , 'd' } ) 282vim . wo . foldmethod ='expr' 283vim . wo . foldexpr ='v:lnum - 1' 284return { 285kind =type ( vim . wo . foldexpr ), 286value =vim . wo . foldexpr , 287l1 =vim . fn . foldlevel ( 1 ), 288l3 =vim . fn . foldlevel ( 3 ), 289} 290end ) 291) 292end ) 293 294it ( "expr option ('foldexpr') accepts Lua function, preserving identity" , function () 295-- Lua-function 'foldexpr' is invoked per line. 296eq ( 297{ identity =true , kind ='function' , l2 =0 , l3 =1 } , 298exec_lua ( function () 299vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'a' , 'b' , 'c' , 'd' } ) 300local fn =function () 301return vim . v . lnum >= 3 and 1 or 0 302end 303vim . wo . foldmethod ='expr' 304vim . wo . foldexpr =fn 305return { 306identity =vim . wo . foldexpr == fn , 307kind =type ( vim . wo . foldexpr ), 308l2 =vim . fn . foldlevel ( 2 ), 309l3 =vim . fn . foldlevel ( 3 ), 310} 311end ) 312) 313end ) 314 315it ( "buffer-local expr option ('indentexpr') accepts Lua function" , function () 316-- Exercises the buffer-local Callback path (copy/free) and get_expr_indent via `==`. 317eq ( 318{ identity =true , indent =8 } , 319exec_lua ( function () 320local myfn =function () 321return 8 322end 323vim . bo . indentexpr =myfn 324vim . api . nvim_buf_set_lines ( 0 , 0 , - 1 , false , { 'line1' , 'line2' } ) 325vim . cmd ( 'normal! 2G==' ) 326return { 327identity =vim . bo . indentexpr == myfn , 328indent =vim . fn . indent ( 2 ), 329} 330end ) 331) 332end ) 333 334it ( "Lua function 'formatexpr' on another buffer (vim.bo[bufnr])" , function () 335-- Sets via the non-curbuf API path (context switch), the way LSP configures 'formatexpr'. 336eq ( 337{ identity =true , cur_unchanged =true , invoked =true } , 338exec_lua ( function () 339local buf =vim . api . nvim_create_buf ( true , false ) 340local invoked =false 341local fexpr =function () 342invoked =true 343return 0 344end 345vim . bo [ buf ]. formatexpr =fexpr 346vim . api . nvim_buf_set_lines ( buf , 0 , - 1 , false , { 'some text to format' } ) 347local identity =vim . bo [ buf ]. formatexpr == fexpr 348vim . api . nvim_buf_call ( buf , function () 349vim . cmd ( 'normal! gqq' ) 350end ) 351return { 352identity =identity , 353cur_unchanged =vim . bo . formatexpr == '' , 354invoked =invoked , 355} 356end ) 357) 358end ) 359 360it ( "omits a Lua-function value from :mksession/:mkvimrc (no ':set' form)" , function () 361-- A Lua function has no ":set" representation, so it must be skipped, not crash the writer. 362local session =exec_lua ( function () 363vim . opt . sessionoptions : append ( 'options' ) 364vim . o . operatorfunc ='StrOpFunc' -- string funcref: written 365vim . o . quickfixtextfunc =function () end -- Lua function: omitted 366local f =vim . fn . tempname () 367vim . cmd ( 'mksession! ' .. f ) 368vim . cmd ( 'mkvimrc! ' .. vim . fn . tempname ()) -- also exercises the makeset()/put_set() path 369return table . concat ( vim . fn . readfile ( f ), '\n' ) 370end ) 371matches ( 'operatorfunc=StrOpFunc' , session ) 372eq ( nil , session : find ( 'quickfixtextfunc' )) 373end ) 374end )