robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
3b00ddb601
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4 5local describe , it , before_each , pending =t . describe , t . it , t . before_each , t . pending 6local fn =n . fn 7local api =n . api 8local command =n . command 9local eq =t . eq 10local exec_lua =n . exec_lua 11local exec_capture =n . exec_capture 12local matches =t . matches 13local pcall_err =t . pcall_err 14 15describe ( 'vim._with' , function () 16before_each ( function () 17n . clear () 18exec_lua ( [[ 19_G.fn = vim.fn 20_G.api = vim.api 21 22_G.setup_buffers = function() 23return api.nvim_create_buf(false, true), api.nvim_get_current_buf() 24end 25 26_G.setup_windows = function() 27local other_win = api.nvim_get_current_win() 28vim.cmd.new() 29return other_win, api.nvim_get_current_win() 30end 31]] ) 32end ) 33 34local assert_events_trigger =function () 35local out =exec_lua [[ 36-- Needs three global values defined: 37-- - `test_events` - array of events which are tested. 38-- - `test_context` - context to be tested. 39-- - `test_trig_event` - callable triggering at least one tested event. 40_G.n_events = 0 41local opts = { callback = function() _G.n_events = _G.n_events + 1 end } 42api.nvim_create_autocmd(_G.test_events, opts) 43 44local context = { bo = { commentstring = '-- %s' } } 45 46-- Should not trigger events on its own 47vim._with(_G.test_context, function() end) 48local is_no_events = _G.n_events == 0 49 50-- Should trigger events if specifically asked inside callback 51local is_events = vim._with(_G.test_context, function() 52_G.test_trig_event() 53return _G.n_events > 0 54end) 55return { is_no_events, is_events } 56]] 57eq ( { true , true } , out ) 58end 59 60describe ( '`bo` context' , function () 61before_each ( function () 62exec_lua [[ 63_G.other_buf, _G.cur_buf = setup_buffers() 64 65-- 'commentstring' is local to buffer and string 66vim.bo[other_buf].commentstring = '## %s' 67vim.bo[cur_buf].commentstring = '// %s' 68vim.go.commentstring = '$$ %s' 69 70-- 'undolevels' is global or local to buffer (global-local) and number 71vim.bo[other_buf].undolevels = 100 72vim.bo[cur_buf].undolevels = 250 73vim.go.undolevels = 500 74 75-- 'autoread' is global or local to buffer (global-local) and boolean 76vim.bo[other_buf].autoread = false 77vim.bo[cur_buf].autoread = false 78vim.go.autoread = true 79 80_G.get_state = function() 81return { 82bo = { 83cms_cur = vim.bo[cur_buf].commentstring, 84cms_other = vim.bo[other_buf].commentstring, 85ul_cur = vim.bo[cur_buf].undolevels, 86ul_other = vim.bo[other_buf].undolevels, 87ar_cur = vim.bo[cur_buf].autoread, 88ar_other = vim.bo[other_buf].autoread, 89}, 90go = { 91cms = vim.go.commentstring, 92ul = vim.go.undolevels, 93ar = vim.go.autoread, 94}, 95} 96end 97]] 98end ) 99 100it ( 'works' , function () 101local out =exec_lua [[ 102local context = { bo = { commentstring = '-- %s', undolevels = 0, autoread = true } } 103 104local before = get_state() 105local inner = vim._with(context, function() 106assert(api.nvim_get_current_buf() == cur_buf) 107return get_state() 108end) 109 110return { before = before, inner = inner, after = get_state() } 111]] 112 113eq ( { 114bo ={ 115cms_cur ='-- %s' , 116cms_other ='## %s' , 117ul_cur =0 , 118ul_other =100 , 119ar_cur =true , 120ar_other =false , 121} , 122go ={ cms ='$$ %s' , ul =500 , ar =true } , 123} , out . inner ) 124eq ( out . before , out . after ) 125end ) 126 127it ( 'sets options in `buf` context' , function () 128local out =exec_lua [[ 129local context = { buf = other_buf, bo = { commentstring = '-- %s', undolevels = 0, autoread = true } } 130 131local before = get_state() 132local inner = vim._with(context, function() 133assert(api.nvim_get_current_buf() == other_buf) 134return get_state() 135end) 136 137return { before = before, inner = inner, after = get_state() } 138]] 139 140eq ( { 141bo ={ 142cms_cur ='// %s' , 143cms_other ='-- %s' , 144ul_cur =250 , 145ul_other =0 , 146ar_cur =false , 147ar_other =true , 148} , 149go ={ cms ='$$ %s' , ul =500 , ar =true } , 150} , out . inner ) 151eq ( out . before , out . after ) 152end ) 153 154it ( 'restores only options from context' , function () 155local out =exec_lua [[ 156local context = { bo = { commentstring = '-- %s' } } 157 158local inner = vim._with(context, function() 159assert(api.nvim_get_current_buf() == cur_buf) 160vim.bo[cur_buf].undolevels = 750 161vim.bo[cur_buf].commentstring = '!! %s' 162return get_state() 163end) 164 165return { inner = inner, after = get_state() } 166]] 167 168eq ( { 169bo ={ 170cms_cur ='!! %s' , 171cms_other ='## %s' , 172ul_cur =750 , 173ul_other =100 , 174ar_cur =false , 175ar_other =false , 176} , 177go ={ cms ='$$ %s' , ul =500 , ar =true } , 178} , out . inner ) 179eq ( { 180bo ={ 181cms_cur ='// %s' , 182cms_other ='## %s' , 183ul_cur =750 , 184ul_other =100 , 185ar_cur =false , 186ar_other =false , 187} , 188go ={ cms ='$$ %s' , ul =500 , ar =true } , 189} , out . after ) 190end ) 191 192it ( 'does not trigger events' , function () 193exec_lua [[ 194_G.test_events = { 'BufEnter', 'BufLeave', 'BufWinEnter', 'BufWinLeave' } 195_G.test_context = { bo = { commentstring = '-- %s' } } 196_G.test_trig_event = function() vim.cmd.new() end 197]] 198assert_events_trigger () 199end ) 200 201it ( 'can be nested' , function () 202local out =exec_lua [[ 203local before, before_inner, after_inner = get_state(), nil, nil 204vim._with({ bo = { commentstring = '-- %s', undolevels = 0, autoread = true } }, function() 205before_inner = get_state() 206inner = vim._with({ bo = { commentstring = '!! %s', autoread = false } }, get_state) 207after_inner = get_state() 208end) 209return { 210before = before, before_inner = before_inner, 211inner = inner, 212after_inner = after_inner, after = get_state(), 213} 214]] 215eq ( '!! %s' , out . inner . bo . cms_cur ) 216eq ( 0 , out . inner . bo . ul_cur ) 217eq ( false , out . inner . bo . ar_cur ) 218eq ( out . before_inner , out . after_inner ) 219eq ( out . before , out . after ) 220end ) 221end ) 222 223describe ( '`buf` context' , function () 224it ( 'works' , function () 225local out =exec_lua [[ 226local other_buf, cur_buf = setup_buffers() 227local inner = vim._with({ buf = other_buf }, function() 228return api.nvim_get_current_buf() 229end) 230return { inner == other_buf, api.nvim_get_current_buf() == cur_buf } 231]] 232eq ( { true , true } , out ) 233end ) 234 235it ( 'does not trigger events' , function () 236exec_lua [[ 237_G.test_events = { 'BufEnter', 'BufLeave', 'BufWinEnter', 'BufWinLeave' } 238_G.test_context = { buf = other_buf } 239_G.test_trig_event = function() vim.cmd.new() end 240]] 241assert_events_trigger () 242end ) 243 244it ( 'can access buffer options' , function () 245local out =exec_lua [[ 246other_buf, cur_buf = setup_buffers() 247vim.bo[other_buf].commentstring = '## %s' 248vim.bo[cur_buf].commentstring = '// %s' 249 250vim._with({ buf = other_buf }, function() 251vim.cmd.set('commentstring=--\\ %s') 252end) 253 254return vim.bo[other_buf].commentstring == '-- %s' and 255vim.bo[cur_buf].commentstring == '// %s' 256]] 257eq ( true , out ) 258end ) 259 260it ( 'works with different kinds of buffers' , function () 261exec_lua [[ 262local assert_buf = function(buf) 263vim._with({ buf = buf }, function() 264assert(api.nvim_get_current_buf() == buf) 265end) 266end 267 268-- Current 269assert_buf(api.nvim_get_current_buf()) 270 271local buf = api.nvim_get_current_buf() 272vim._with({ buf = 0 }, function() 273assert(api.nvim_get_current_buf() == buf) 274end) 275assert(api.nvim_get_current_buf() == buf) 276 277-- Hidden listed 278local listed = api.nvim_create_buf(true, true) 279assert_buf(listed) 280 281-- Visible 282local other_win, cur_win = setup_windows() 283api.nvim_win_set_buf(other_win, listed) 284assert_buf(listed) 285 286-- Shown but not visible 287vim.cmd.tabnew() 288assert_buf(listed) 289 290-- Shown in several windows 291api.nvim_win_set_buf(0, listed) 292assert_buf(listed) 293 294-- Shown in floating window 295local float_buf = api.nvim_create_buf(false, true) 296local config = { relative = 'editor', row = 1, col = 1, width = 5, height = 5 } 297api.nvim_open_win(float_buf, false, config) 298assert_buf(float_buf) 299]] 300end ) 301 302it ( 'does not cause ml_get errors with invalid visual selection' , function () 303exec_lua [[ 304api.nvim_buf_set_lines(0, 0, -1, true, { 'a', 'b', 'c' }) 305api.nvim_feedkeys(vim.keycode('G<C-V>'), 'txn', false) 306local other_buf, _ = setup_buffers() 307vim._with({ buf = buf }, function() vim.cmd.redraw() end) 308]] 309end ) 310 311it ( 'can be nested' , function () 312exec_lua [[ 313local other_buf, cur_buf = setup_buffers() 314vim._with({ buf = other_buf }, function() 315assert(api.nvim_get_current_buf() == other_buf) 316inner = vim._with({ buf = cur_buf }, function() 317assert(api.nvim_get_current_buf() == cur_buf) 318end) 319assert(api.nvim_get_current_buf() == other_buf) 320end) 321assert(api.nvim_get_current_buf() == cur_buf) 322]] 323end ) 324 325it ( 'can be nested crazily with hidden buffers' , function () 326local out =exec_lua ( [[ 327local n = 0 328local function with_recursive_nested_bufs() 329n = n + 1 330if n > 20 then return true end 331 332local other_buf, _ = setup_buffers() 333vim.bo[other_buf].commentstring = '## %s' 334local callback = function() 335return api.nvim_get_current_buf() == other_buf 336and vim.bo[other_buf].commentstring == '## %s' 337and with_recursive_nested_bufs() 338end 339return vim._with({ buf = other_buf }, callback) and 340api.nvim_buf_delete(other_buf, {}) == nil 341end 342 343return with_recursive_nested_bufs() 344]] ) 345eq ( true , out ) 346end ) 347 348it ( 'keeps ":bcd" on the target buffer' , function () 349local out =exec_lua [[ 350local other_buf, cur_buf = setup_buffers() 351local err_buf = api.nvim_create_buf(false, true) 352local cwd = fn.getcwd() 353local dir = vim.fs.joinpath(cwd, 'test') 354vim._with({ buf = other_buf }, function() vim.cmd.bcd(dir) end) 355pcall(vim._with, { buf = err_buf }, function() 356vim.cmd.bcd(dir) 357error('oops') 358end) 359return { 360fn.haslocaldir(-1, -1, other_buf), 361fn.haslocaldir(-1, -1, err_buf), 362fn.haslocaldir(-1, -1, cur_buf), 363fn.getcwd() == cwd, -- Caller's CWD is unaffected: the target is not current. 364} 365]] 366eq ( { 1 , 1 , 0 , true } , out ) 367end ) 368end ) 369 370describe ( '`cwd` context' , function () 371it ( 'works' , function () 372local out =exec_lua [[ 373local cwd = vim.uv.cwd() 374local temp_cwd = vim.fs.joinpath(cwd, 'test') 375local test_cwd 376vim._with({ cwd = temp_cwd }, function() test_cwd = vim.uv.cwd() end) 377-- Use `fs_realpath` to resolve symlinks from how tests are set up 378return { vim.uv.cwd() == cwd, vim.uv.fs_realpath(test_cwd) == vim.uv.fs_realpath(temp_cwd) } 379]] 380eq ( { true , true } , out ) 381end ) 382 383it ( 'can be nested' , function () 384local out =exec_lua [[ 385local cwd = vim.uv.cwd() 386local temp_cwd = vim.fs.joinpath(cwd, 'test') 387local temp_cwd2 = vim.fs.joinpath(cwd, 'src') 388local test_cwd_before, test_cwd, test_cwd_after 389vim._with({ cwd = temp_cwd }, function() 390test_cwd_before = vim.uv.cwd() 391vim._with({ cwd = temp_cwd2 }, function() 392test_cwd = vim.uv.cwd() 393end) 394test_cwd_after = vim.uv.cwd() 395end) 396return { 397vim.uv.cwd() == cwd, 398vim.uv.fs_realpath(test_cwd_before) == vim.uv.fs_realpath(temp_cwd), 399vim.uv.fs_realpath(test_cwd) == vim.uv.fs_realpath(temp_cwd2), 400vim.uv.fs_realpath(test_cwd_after) == vim.uv.fs_realpath(temp_cwd), 401} 402]] 403eq ( { true , true , true , true } , out ) 404end ) 405 406it ( 'does not modify global CWD' , function () 407local out =exec_lua [[ 408local other_buf, _ = setup_buffers() 409local cwd = fn.getcwd() 410-- Activate a window-local dir, so that the global dir must be remembered. 411vim.cmd.lcd(vim.fs.joinpath(cwd, 'test')) 412local lcd_cwd = fn.getcwd() -- Not necessarily `cwd .. '/test'`: symlinks are resolved. 413vim._with({ buf = other_buf, cwd = vim.fs.joinpath(cwd, 'src') }, function() end) 414return { fn.getcwd() == lcd_cwd, fn.getcwd(-1, -1) == cwd } 415]] 416eq ( { true , true } , out ) 417end ) 418end ) 419 420describe ( '`keepcwd` context' , function () 421it ( 'undoes chdir at every scope' , function () 422local out =exec_lua [[ 423local cwd = fn.getcwd() 424local dir = vim.fs.joinpath(cwd, 'test') 425vim._with({ keepcwd = true }, function() 426vim.cmd.cd(dir) 427vim.cmd.tcd(dir) 428vim.cmd.bcd(dir) 429vim.cmd.lcd(dir) 430end) 431return { 432fn.haslocaldir(), -- window 433fn.haslocaldir(-1, 0), -- tabpage 434fn.haslocaldir(-1, -1, 0), -- buffer 435fn.getcwd() == cwd, 436fn.getcwd(-1, -1) == cwd, -- global 437} 438]] 439eq ( { 0 , 0 , 0 , true , true } , out ) 440end ) 441 442it ( 'discards ":bcd"/":lcd" that a `buf`/`win` context would keep' , function () 443local out =exec_lua [[ 444local other_buf, _ = setup_buffers() 445local other_win, _ = setup_windows() 446local cwd = fn.getcwd() 447local dir = vim.fs.joinpath(cwd, 'test') 448vim._with({ buf = other_buf, keepcwd = true }, function() vim.cmd.bcd(dir) end) 449vim._with({ win = other_win, keepcwd = true }, function() vim.cmd.lcd(dir) end) 450return { 451fn.haslocaldir(-1, -1, other_buf), 452fn.haslocaldir(fn.win_id2win(other_win)), 453fn.getcwd() == cwd, 454} 455]] 456eq ( { 0 , 0 , true } , out ) 457end ) 458 459it ( 'restores nothing else: the callback may switch window' , function () 460local out =exec_lua [[ 461local other_win, _ = setup_windows() 462vim._with({ keepcwd = true }, function() api.nvim_set_current_win(other_win) end) 463return api.nvim_get_current_win() == other_win 464]] 465eq ( true , out ) 466end ) 467end ) 468 469describe ( '`emsg_silent` context' , function () 470pending ( 'works' , function () 471local ok =pcall ( 472exec_lua , 473[[ 474_G.f = function() 475error('This error should not interfere with execution', 0) 476end 477-- Should not produce error same as `vim.cmd('silent! lua _G.f()')` 478vim._with({ emsg_silent = true }, f) 479]] 480) 481eq ( true , ok ) 482 483-- Should properly report errors afterwards 484ok =pcall ( exec_lua , 'lua _G.f()' ) 485eq ( false , ok ) 486end ) 487 488it ( 'can be nested' , function () 489local ok =pcall ( 490exec_lua , 491[[ 492_G.f = function() 493error('This error should not interfere with execution', 0) 494end 495-- Should produce error same as `_G.f()` 496vim._with({ emsg_silent = true }, function() 497vim._with( { emsg_silent = false }, f) 498end) 499]] 500) 501eq ( false , ok ) 502end ) 503end ) 504 505describe ( '`env` context' , function () 506before_each ( function () 507exec_lua [[ 508vim.fn.setenv('aaa', 'hello') 509_G.get_state = function() 510return { aaa = vim.fn.getenv('aaa'), bbb = vim.fn.getenv('bbb') } 511end 512]] 513end ) 514 515it ( 'works' , function () 516local out =exec_lua [[ 517local context = { env = { aaa = 'inside', bbb = 'wow' } } 518local before = get_state() 519local inner = vim._with(context, get_state) 520return { before = before, inner = inner, after = get_state() } 521]] 522 523eq ( { aaa ='inside' , bbb ='wow' } , out . inner ) 524eq ( out . before , out . after ) 525end ) 526 527it ( 'restores only variables from context' , function () 528local out =exec_lua [[ 529local context = { env = { bbb = 'wow' } } 530local before = get_state() 531local inner = vim._with(context, function() 532vim.env.aaa = 'inside' 533return get_state() 534end) 535return { before = before, inner = inner, after = get_state() } 536]] 537 538eq ( { aaa ='inside' , bbb ='wow' } , out . inner ) 539eq ( { aaa ='inside' , bbb =vim . NIL } , out . after ) 540end ) 541 542it ( 'can be nested' , function () 543local out =exec_lua [[ 544local before, before_inner, after_inner = get_state(), nil, nil 545vim._with({ env = { aaa = 'inside', bbb = 'wow' } }, function() 546before_inner = get_state() 547inner = vim._with({ env = { aaa = 'more inside' } }, get_state) 548after_inner = get_state() 549end) 550return { 551before = before, before_inner = before_inner, 552inner = inner, 553after_inner = after_inner, after = get_state(), 554} 555]] 556eq ( 'more inside' , out . inner . aaa ) 557eq ( 'wow' , out . inner . bbb ) 558eq ( out . before_inner , out . after_inner ) 559eq ( out . before , out . after ) 560end ) 561end ) 562 563describe ( '`go` context' , function () 564before_each ( function () 565exec_lua [[ 566vim.bo.commentstring = '## %s' 567vim.go.commentstring = '$$ %s' 568vim.wo.winblend = 25 569vim.go.winblend = 50 570vim.go.langmap = 'xy,yx' 571vim.go.confirm = false 572 573_G.get_state = function() 574return { 575bo = { cms = vim.bo.commentstring }, 576wo = { winbl = vim.wo.winblend }, 577go = { 578cms = vim.go.commentstring, 579winbl = vim.go.winblend, 580lmap = vim.go.langmap, 581cf = vim.go.confirm, 582}, 583} 584end 585]] 586end ) 587 588it ( 'works' , function () 589local out =exec_lua [[ 590local context = { 591go = { commentstring = '-- %s', winblend = 75, langmap = 'ab,ba', cf = true }, 592} 593local before = get_state() 594local inner = vim._with(context, get_state) 595return { before = before, inner = inner, after = get_state() } 596]] 597 598eq ( { 599bo ={ cms ='## %s' } , 600wo ={ winbl =25 } , 601go ={ cms ='-- %s' , winbl =75 , lmap ='ab,ba' , cf =true } , 602} , out . inner ) 603eq ( out . before , out . after ) 604end ) 605 606it ( 'works with `eventignore`' , function () 607-- This might be an issue if saving and restoring option context is done 608-- to account for triggering `OptionSet`, but in not a good way 609local out =exec_lua [[ 610vim.go.eventignore = 'ModeChanged' 611local inner = vim._with({ go = { eventignore = 'CursorMoved' } }, function() 612return vim.go.eventignore 613end) 614return { inner = inner, after = vim.go.eventignore } 615]] 616eq ( { inner ='CursorMoved' , after ='ModeChanged' } , out ) 617end ) 618 619it ( 'restores only options from context' , function () 620local out =exec_lua [[ 621local context = { go = { langmap = 'ab,ba' } } 622 623local inner = vim._with(context, function() 624vim.go.commentstring = '!! %s' 625vim.go.winblend = 75 626vim.go.langmap = 'uv,vu' 627return get_state() 628end) 629 630return { inner = inner, after = get_state() } 631]] 632 633eq ( { 634bo ={ cms ='## %s' } , 635wo ={ winbl =25 } , 636go ={ cms ='!! %s' , winbl =75 , lmap ='uv,vu' , cf =false } , 637} , out . inner ) 638eq ( { 639bo ={ cms ='## %s' } , 640wo ={ winbl =25 } , 641go ={ cms ='!! %s' , winbl =75 , lmap ='xy,yx' , cf =false } , 642} , out . after ) 643end ) 644 645it ( 'does not trigger events' , function () 646exec_lua [[ 647_G.test_events = { 648'BufEnter', 'BufLeave', 'BufWinEnter', 'BufWinLeave', 'WinEnter', 'WinLeave' 649} 650_G.test_context = { go = { commentstring = '-- %s', winblend = 75, langmap = 'ab,ba' } } 651_G.test_trig_event = function() vim.cmd.new() end 652]] 653assert_events_trigger () 654end ) 655 656it ( 'can be nested' , function () 657local out =exec_lua [[ 658local before, before_inner, after_inner = get_state(), nil, nil 659vim._with({ go = { langmap = 'ab,ba', commentstring = '-- %s' } }, function() 660before_inner = get_state() 661inner = vim._with({ go = { langmap = 'uv,vu' } }, get_state) 662after_inner = get_state() 663end) 664return { 665before = before, before_inner = before_inner, 666inner = inner, 667after_inner = after_inner, after = get_state(), 668} 669]] 670eq ( 'uv,vu' , out . inner . go . lmap ) 671eq ( '-- %s' , out . inner . go . cms ) 672eq ( out . before_inner , out . after_inner ) 673eq ( out . before , out . after ) 674end ) 675end ) 676 677describe ( '`hide` context' , function () 678pending ( 'works' , function () 679local ok =pcall ( 680exec_lua , 681[[ 682vim.o.hidden = false 683vim.bo.modified = true 684local init_buf = api.nvim_get_current_buf() 685-- Should not produce error same as `vim.cmd('hide enew')` 686vim._with({ hide = true }, function() 687vim.cmd.enew() 688end) 689assert(api.nvim_get_current_buf() ~= init_buf) 690]] 691) 692eq ( true , ok ) 693end ) 694 695it ( 'can be nested' , function () 696local ok =pcall ( 697exec_lua , 698[[ 699vim.o.hidden = false 700vim.bo.modified = true 701-- Should produce error same as `vim.cmd.enew()` 702vim._with({ hide = true }, function() 703vim._with({ hide = false }, function() 704vim.cmd.enew() 705end) 706end) 707]] 708) 709eq ( false , ok ) 710end ) 711end ) 712 713describe ( '`horizontal` context' , function () 714local is_approx_eq =function ( dim , id_1 , id_2 ) 715local f =dim == 'height' and api . nvim_win_get_height or api . nvim_win_get_width 716return math . abs ( f ( id_1 ) - f ( id_2 )) <= 1 717end 718 719local win_id_1 , win_id_2 , win_id_3 720before_each ( function () 721win_id_1 =api . nvim_get_current_win () 722command ( 'wincmd v | wincmd 5>' ) 723win_id_2 =api . nvim_get_current_win () 724command ( 'wincmd s | wincmd 5+' ) 725win_id_3 =api . nvim_get_current_win () 726 727eq ( false , is_approx_eq ( 'width' , win_id_1 , win_id_2 )) 728eq ( false , is_approx_eq ( 'height' , win_id_3 , win_id_2 )) 729end ) 730 731pending ( 'works' , function () 732exec_lua [[ 733-- Should be same as `vim.cmd('horizontal wincmd =')` 734vim._with({ horizontal = true }, function() 735vim.cmd.wincmd('=') 736end) 737]] 738eq ( true , is_approx_eq ( 'width' , win_id_1 , win_id_2 )) 739eq ( false , is_approx_eq ( 'height' , win_id_3 , win_id_2 )) 740end ) 741 742pending ( 'can be nested' , function () 743exec_lua [[ 744-- Should be same as `vim.cmd.wincmd('=')` 745vim._with({ horizontal = true }, function() 746vim._with({ horizontal = false }, function() 747vim.cmd.wincmd('=') 748end) 749end) 750]] 751eq ( true , is_approx_eq ( 'width' , win_id_1 , win_id_2 )) 752eq ( true , is_approx_eq ( 'height' , win_id_3 , win_id_2 )) 753end ) 754end ) 755 756describe ( '`keepalt` context' , function () 757pending ( 'works' , function () 758local out =exec_lua [[ 759vim.cmd('edit alt') 760vim.cmd('edit new') 761assert(fn.bufname('#') == 'alt') 762 763-- Should work as `vim.cmd('keepalt edit very-new')` 764vim._with({ keepalt = true }, function() 765vim.cmd.edit('very-new') 766end) 767return fn.bufname('#') == 'alt' 768]] 769eq ( true , out ) 770end ) 771 772it ( 'can be nested' , function () 773local out =exec_lua [[ 774vim.cmd('edit alt') 775vim.cmd('edit new') 776assert(fn.bufname('#') == 'alt') 777 778-- Should work as `vim.cmd.edit('very-new')` 779vim._with({ keepalt = true }, function() 780vim._with({ keepalt = false }, function() 781vim.cmd.edit('very-new') 782end) 783end) 784return fn.bufname('#') == 'alt' 785]] 786eq ( false , out ) 787end ) 788end ) 789 790describe ( '`keepjumps` context' , function () 791pending ( 'works' , function () 792local out =exec_lua [[ 793api.nvim_buf_set_lines(0, 0, -1, false, { 'aaa', 'bbb', 'ccc' }) 794local jumplist_before = fn.getjumplist() 795-- Should work as `vim.cmd('keepjumps normal! Ggg')` 796vim._with({ keepjumps = true }, function() 797vim.cmd('normal! Ggg') 798end) 799return vim.deep_equal(jumplist_before, fn.getjumplist()) 800]] 801eq ( true , out ) 802end ) 803 804it ( 'can be nested' , function () 805local out =exec_lua [[ 806api.nvim_buf_set_lines(0, 0, -1, false, { 'aaa', 'bbb', 'ccc' }) 807local jumplist_before = fn.getjumplist() 808vim._with({ keepjumps = true }, function() 809vim._with({ keepjumps = false }, function() 810vim.cmd('normal! Ggg') 811end) 812end) 813return vim.deep_equal(jumplist_before, fn.getjumplist()) 814]] 815eq ( false , out ) 816end ) 817end ) 818 819describe ( '`keepmarks` context' , function () 820pending ( 'works' , function () 821local out =exec_lua [[ 822vim.cmd('set cpoptions+=R') 823api.nvim_buf_set_lines(0, 0, -1, false, { 'bbb', 'ccc', 'aaa' }) 824api.nvim_buf_set_mark(0, 'm', 2, 2, {}) 825 826-- Should be the same as `vim.cmd('keepmarks %!sort')` 827vim._with({ keepmarks = true }, function() 828vim.cmd('%!sort') 829end) 830return api.nvim_buf_get_mark(0, 'm') 831]] 832eq ( { 2 , 2 } , out ) 833end ) 834 835it ( 'can be nested' , function () 836local out =exec_lua [[ 837vim.cmd('set cpoptions+=R') 838api.nvim_buf_set_lines(0, 0, -1, false, { 'bbb', 'ccc', 'aaa' }) 839api.nvim_buf_set_mark(0, 'm', 2, 2, {}) 840 841vim._with({ keepmarks = true }, function() 842vim._with({ keepmarks = false }, function() 843vim.cmd('%!sort') 844end) 845end) 846return api.nvim_buf_get_mark(0, 'm') 847]] 848eq ( { 0 , 2 } , out ) 849end ) 850end ) 851 852describe ( '`keepatterns` context' , function () 853pending ( 'works' , function () 854local out =exec_lua [[ 855api.nvim_buf_set_lines(0, 0, -1, false, { 'aaa', 'bbb' }) 856vim.cmd('/aaa') 857-- Should be the same as `vim.cmd('keeppatterns /bbb')` 858vim._with({ keeppatterns = true }, function() 859vim.cmd('/bbb') 860end) 861return fn.getreg('/') 862]] 863eq ( 'aaa' , out ) 864end ) 865 866it ( 'can be nested' , function () 867local out =exec_lua [[ 868api.nvim_buf_set_lines(0, 0, -1, false, { 'aaa', 'bbb' }) 869vim.cmd('/aaa') 870vim._with({ keeppatterns = true }, function() 871vim._with({ keeppatterns = false }, function() 872vim.cmd('/bbb') 873end) 874end) 875return fn.getreg('/') 876]] 877eq ( 'bbb' , out ) 878end ) 879end ) 880 881describe ( '`lockmarks` context' , function () 882it ( 'works' , function () 883local mark =exec_lua [[ 884api.nvim_buf_set_lines(0, 0, 0, false, { 'aaa', 'bbb', 'ccc' }) 885api.nvim_buf_set_mark(0, 'm', 2, 2, {}) 886-- Should be same as `:lockmarks lua api.nvim_buf_set_lines(...)` 887vim._with({ lockmarks = true }, function() 888api.nvim_buf_set_lines(0, 0, 2, false, { 'uuu', 'vvv', 'www' }) 889end) 890return api.nvim_buf_get_mark(0, 'm') 891]] 892eq ( { 2 , 2 } , mark ) 893end ) 894 895it ( 'can be nested' , function () 896local mark =exec_lua [[ 897api.nvim_buf_set_lines(0, 0, 0, false, { 'aaa', 'bbb', 'ccc' }) 898api.nvim_buf_set_mark(0, 'm', 2, 2, {}) 899vim._with({ lockmarks = true }, function() 900vim._with({ lockmarks = false }, function() 901api.nvim_buf_set_lines(0, 0, 2, false, { 'uuu', 'vvv', 'www' }) 902end) 903end) 904return api.nvim_buf_get_mark(0, 'm') 905]] 906eq ( { 0 , 2 } , mark ) 907end ) 908end ) 909 910describe ( '`noautocmd` context' , function () 911it ( 'works' , function () 912local out =exec_lua [[ 913_G.n_events = 0 914vim.cmd('au ModeChanged * lua _G.n_events = _G.n_events + 1') 915-- Should be the same as `vim.cmd('noautocmd normal! vv')` 916vim._with({ noautocmd = true }, function() 917vim.cmd('normal! vv') 918end) 919return _G.n_events 920]] 921eq ( 0 , out ) 922end ) 923 924it ( 'works with User events' , function () 925local out =exec_lua [[ 926_G.n_events = 0 927vim.cmd('au User MyEvent lua _G.n_events = _G.n_events + 1') 928-- Should be the same as `vim.cmd('noautocmd doautocmd User MyEvent')` 929vim._with({ noautocmd = true }, function() 930api.nvim_exec_autocmds('User', { pattern = 'MyEvent' }) 931end) 932return _G.n_events 933]] 934eq ( 0 , out ) 935end ) 936 937pending ( 'can be nested' , function () 938local out =exec_lua [[ 939_G.n_events = 0 940vim.cmd('au ModeChanged * lua _G.n_events = _G.n_events + 1') 941vim._with({ noautocmd = true }, function() 942vim._with({ noautocmd = false }, function() 943vim.cmd('normal! vv') 944end) 945end) 946return _G.n_events 947]] 948eq ( 2 , out ) 949end ) 950end ) 951 952describe ( '`o` context' , function () 953before_each ( function () 954exec_lua [[ 955_G.other_win, _G.cur_win = setup_windows() 956_G.other_buf, _G.cur_buf = setup_buffers() 957 958vim.bo[other_buf].commentstring = '## %s' 959vim.bo[cur_buf].commentstring = '// %s' 960vim.go.commentstring = '$$ %s' 961 962vim.bo[other_buf].undolevels = 100 963vim.bo[cur_buf].undolevels = 250 964vim.go.undolevels = 500 965 966vim.wo[other_win].virtualedit = 'block' 967vim.wo[cur_win].virtualedit = 'insert' 968vim.go.virtualedit = 'none' 969 970vim.wo[other_win].winblend = 10 971vim.wo[cur_win].winblend = 25 972vim.go.winblend = 50 973 974vim.go.langmap = 'xy,yx' 975vim.go.confirm = false 976 977_G.get_state = function() 978return { 979bo = { 980cms_cur = vim.bo[cur_buf].commentstring, 981cms_other = vim.bo[other_buf].commentstring, 982ul_cur = vim.bo[cur_buf].undolevels, 983ul_other = vim.bo[other_buf].undolevels, 984}, 985wo = { 986ve_cur = vim.wo[cur_win].virtualedit, 987ve_other = vim.wo[other_win].virtualedit, 988winbl_cur = vim.wo[cur_win].winblend, 989winbl_other = vim.wo[other_win].winblend, 990}, 991go = { 992cms = vim.go.commentstring, 993ul = vim.go.undolevels, 994ve = vim.go.virtualedit, 995winbl = vim.go.winblend, 996lmap = vim.go.langmap, 997cf = vim.go.confirm, 998}, 999} 1000end 1001]] 1002end ) 1003 1004it ( 'works' , function () 1005local out =exec_lua [[ 1006local context = { 1007o = { 1008commentstring = '-- %s', 1009undolevels = 0, 1010virtualedit = 'all', 1011winblend = 75, 1012langmap = 'ab,ba', 1013confirm = true, 1014}, 1015} 1016 1017local before = get_state() 1018local inner = vim._with(context, function() 1019assert(api.nvim_get_current_buf() == cur_buf) 1020assert(api.nvim_get_current_win() == cur_win) 1021return get_state() 1022end) 1023 1024return { before = before, inner = inner, after = get_state() } 1025]] 1026 1027-- Options in context are set with `vim.o`, so usually both local 1028-- and global values are affected. Yet all of them should be later 1029-- restored to pre-context values. 1030eq ( { 1031bo ={ cms_cur ='-- %s' , cms_other ='## %s' , ul_cur =- 123456 , ul_other =100 } , 1032wo ={ ve_cur ='all' , ve_other ='block' , winbl_cur =75 , winbl_other =10 } , 1033go ={ cms ='-- %s' , ul =0 , ve ='all' , winbl =75 , lmap ='ab,ba' , cf =true } , 1034} , out . inner ) 1035eq ( out . before , out . after ) 1036end ) 1037 1038it ( 'sets options in `buf` context' , function () 1039local out =exec_lua [[ 1040local context = { buf = other_buf, o = { commentstring = '-- %s', undolevels = 0 } } 1041 1042local before = get_state() 1043local inner = vim._with(context, function() 1044assert(api.nvim_get_current_buf() == other_buf) 1045return get_state() 1046end) 1047 1048return { before = before, inner = inner, after = get_state() } 1049]] 1050 1051eq ( { 1052bo ={ cms_cur ='// %s' , cms_other ='-- %s' , ul_cur =250 , ul_other =- 123456 } , 1053wo ={ ve_cur ='insert' , ve_other ='block' , winbl_cur =25 , winbl_other =10 } , 1054-- Global `winbl` inside context ideally should be untouched and equal 1055-- to 50. It seems to be equal to 0 because `context.buf` uses 1056-- `ctx_switch` C approach which has no guarantees about window or 1057-- window option values inside context. 1058go ={ cms ='-- %s' , ul =0 , ve ='none' , winbl =0 , lmap ='xy,yx' , cf =false } , 1059} , out . inner ) 1060eq ( out . before , out . after ) 1061end ) 1062 1063it ( 'sets options in `win` context' , function () 1064local out =exec_lua [[ 1065local context = { win = other_win, o = { winblend = 75, virtualedit = 'all' } } 1066 1067local before = get_state() 1068local inner = vim._with(context, function() 1069assert(api.nvim_get_current_win() == other_win) 1070return get_state() 1071end) 1072 1073return { before = before, inner = inner, after = get_state() } 1074]] 1075 1076eq ( { 1077bo ={ cms_cur ='// %s' , cms_other ='## %s' , ul_cur =250 , ul_other =100 } , 1078wo ={ winbl_cur =25 , winbl_other =75 , ve_cur ='insert' , ve_other ='all' } , 1079go ={ cms ='$$ %s' , ul =500 , winbl =75 , ve ='all' , lmap ='xy,yx' , cf =false } , 1080} , out . inner ) 1081eq ( out . before , out . after ) 1082end ) 1083 1084it ( 'restores only options from context' , function () 1085local out =exec_lua [[ 1086local context = { o = { undolevels = 0, winblend = 75, langmap = 'ab,ba' } } 1087 1088local inner = vim._with(context, function() 1089assert(api.nvim_get_current_buf() == cur_buf) 1090assert(api.nvim_get_current_win() == cur_win) 1091 1092vim.o.commentstring = '!! %s' 1093vim.o.undolevels = 750 1094vim.o.virtualedit = 'onemore' 1095vim.o.winblend = 99 1096vim.o.langmap = 'uv,vu' 1097return get_state() 1098end) 1099 1100return { inner = inner, after = get_state() } 1101]] 1102 1103eq ( { 1104bo ={ cms_cur ='!! %s' , cms_other ='## %s' , ul_cur =- 123456 , ul_other =100 } , 1105wo ={ ve_cur ='onemore' , ve_other ='block' , winbl_cur =99 , winbl_other =10 } , 1106go ={ cms ='!! %s' , ul =750 , ve ='onemore' , winbl =99 , lmap ='uv,vu' , cf =false } , 1107} , out . inner ) 1108eq ( { 1109bo ={ cms_cur ='!! %s' , cms_other ='## %s' , ul_cur =250 , ul_other =100 } , 1110wo ={ ve_cur ='onemore' , ve_other ='block' , winbl_cur =25 , winbl_other =10 } , 1111go ={ cms ='!! %s' , ul =500 , ve ='onemore' , winbl =50 , lmap ='xy,yx' , cf =false } , 1112} , out . after ) 1113end ) 1114 1115it ( 'does not trigger events' , function () 1116exec_lua [[ 1117_G.test_events = { 1118'BufEnter', 'BufLeave', 'WinEnter', 'WinLeave', 'BufWinEnter', 'BufWinLeave' 1119} 1120_G.test_context = { o = { undolevels = 0, winblend = 75, langmap = 'ab,ba' } } 1121_G.test_trig_event = function() vim.cmd.new() end 1122]] 1123assert_events_trigger () 1124end ) 1125 1126it ( 'can be nested' , function () 1127local out =exec_lua [[ 1128local before, before_inner, after_inner = get_state(), nil, nil 1129local cxt_o = { commentstring = '-- %s', winblend = 75, langmap = 'ab,ba', undolevels = 0 } 1130vim._with({ o = cxt_o }, function() 1131before_inner = get_state() 1132local inner_cxt_o = { commentstring = '!! %s', winblend = 99, langmap = 'uv,vu' } 1133inner = vim._with({ o = inner_cxt_o }, get_state) 1134after_inner = get_state() 1135end) 1136return { 1137before = before, before_inner = before_inner, 1138inner = inner, 1139after_inner = after_inner, after = get_state(), 1140} 1141]] 1142eq ( '!! %s' , out . inner . bo . cms_cur ) 1143eq ( 99 , out . inner . wo . winbl_cur ) 1144eq ( 'uv,vu' , out . inner . go . lmap ) 1145eq ( 0 , out . inner . go . ul ) 1146eq ( out . before_inner , out . after_inner ) 1147eq ( out . before , out . after ) 1148end ) 1149end ) 1150 1151describe ( '`sandbox` context' , function () 1152it ( 'works' , function () 1153local ok , err =pcall ( 1154exec_lua , 1155[[ 1156-- Should work as `vim.cmd('sandbox call append(0, "aaa")')` 1157vim._with({ sandbox = true }, function() 1158fn.append(0, 'aaa') 1159end) 1160]] 1161) 1162eq ( false , ok ) 1163matches ( 'Not allowed in sandbox' , err ) 1164end ) 1165 1166it ( 'can NOT be nested' , function () 1167-- This behavior is intentionally different from other flags as allowing 1168-- disabling `sandbox` from nested function seems to be against the point 1169-- of using `sandbox` context in the first place 1170local ok , err =pcall ( 1171exec_lua , 1172[[ 1173vim._with({ sandbox = true }, function() 1174vim._with({ sandbox = false }, function() 1175fn.append(0, 'aaa') 1176end) 1177end) 1178]] 1179) 1180eq ( false , ok ) 1181matches ( 'Not allowed in sandbox' , err ) 1182end ) 1183end ) 1184 1185describe ( '`silent` context' , function () 1186it ( 'works' , function () 1187exec_lua [[ 1188-- Should be same as `vim.cmd('silent lua print("aaa")')` 1189vim._with({ silent = true }, function() print('aaa') end) 1190]] 1191eq ( '' , exec_capture ( 'messages' )) 1192 1193exec_lua [[ vim._with({ silent = true }, function() vim.cmd.echomsg('"bbb"') end) ]] 1194eq ( '' , exec_capture ( 'messages' )) 1195 1196local screen =Screen . new ( 20 , 5 ) 1197exec_lua [[ vim._with({ silent = true }, function() vim.cmd.echo('"ccc"') end) ]] 1198screen : expect [[ 1199^ | 1200{1:~ }|*3 1201| 1202]] 1203end ) 1204 1205pending ( 'can be nested' , function () 1206exec_lua [[ vim._with({ silent = true }, function() 1207vim._with({ silent = false }, function() 1208print('aaa') 1209end) 1210end)]] 1211eq ( 'aaa' , exec_capture ( 'messages' )) 1212end ) 1213end ) 1214 1215describe ( '`unsilent` context' , function () 1216it ( 'works' , function () 1217exec_lua [[ 1218_G.f = function() 1219-- Should be same as `vim.cmd('unsilent lua print("aaa")')` 1220vim._with({ unsilent = true }, function() print('aaa') end) 1221end 1222]] 1223command ( 'silent lua f()' ) 1224eq ( 'aaa' , exec_capture ( 'messages' )) 1225end ) 1226 1227pending ( 'can be nested' , function () 1228exec_lua [[ 1229_G.f = function() 1230vim._with({ unsilent = true }, function() 1231vim._with({ unsilent = false }, function() print('aaa') end) 1232end) 1233end 1234]] 1235command ( 'silent lua f()' ) 1236eq ( '' , exec_capture ( 'messages' )) 1237end ) 1238end ) 1239 1240describe ( '`win` context' , function () 1241it ( 'works' , function () 1242local out =exec_lua [[ 1243local other_win, cur_win = setup_windows() 1244local inner = vim._with({ win = other_win }, function() 1245return api.nvim_get_current_win() 1246end) 1247return { inner == other_win, api.nvim_get_current_win() == cur_win } 1248]] 1249eq ( { true , true } , out ) 1250end ) 1251 1252it ( 'does not trigger events' , function () 1253exec_lua [[ 1254_G.test_events = { 'WinEnter', 'WinLeave', 'BufWinEnter', 'BufWinLeave' } 1255_G.test_context = { win = other_win } 1256_G.test_trig_event = function() vim.cmd.new() end 1257]] 1258assert_events_trigger () 1259end ) 1260 1261it ( 'can access window options' , function () 1262local out =exec_lua [[ 1263local other_win, cur_win = setup_windows() 1264vim.wo[other_win].winblend = 10 1265vim.wo[cur_win].winblend = 25 1266 1267vim._with({ win = other_win }, function() 1268vim.cmd.setlocal('winblend=0') 1269end) 1270 1271return vim.wo[other_win].winblend == 0 and vim.wo[cur_win].winblend == 25 1272]] 1273eq ( true , out ) 1274end ) 1275 1276it ( 'works with different kinds of windows' , function () 1277exec_lua [[ 1278local assert_win = function(win) 1279vim._with({ win = win }, function() 1280assert(api.nvim_get_current_win() == win) 1281end) 1282end 1283 1284-- Current 1285assert_win(api.nvim_get_current_win()) 1286 1287local win = api.nvim_get_current_win() 1288vim._with({ win = 0 }, function() 1289assert(api.nvim_get_current_win() == win) 1290-- Should restore context window if that changed 1291vim.cmd.tabnew() 1292end) 1293assert(api.nvim_get_current_win() == win) 1294 1295-- Not visible 1296local other_win, cur_win = setup_windows() 1297vim.cmd.tabnew() 1298assert_win(other_win) 1299 1300-- Floating 1301local float_win = api.nvim_open_win( 1302api.nvim_create_buf(false, true), 1303false, 1304{ relative = 'editor', row = 1, col = 1, height = 5, width = 5} 1305) 1306assert_win(float_win) 1307]] 1308end ) 1309 1310it ( 'does not cause ml_get errors with invalid visual selection' , function () 1311exec_lua [[ 1312local feedkeys = function(keys) api.nvim_feedkeys(vim.keycode(keys), 'txn', false) end 1313 1314-- Add lines to the current buffer and make another window looking into an empty buffer. 1315local win_empty, win_lines = setup_windows() 1316api.nvim_buf_set_lines(0, 0, -1, true, { 'a', 'b', 'c' }) 1317 1318-- Start Visual in current window, redraw in other window with fewer lines. 1319-- Should be fixed by vim-patch:8.2.4018. 1320feedkeys('G<C-V>') 1321vim._with({ win = win_empty }, function() vim.cmd.redraw() end) 1322 1323-- Start Visual in current window, extend it in other window with more lines. 1324-- Fixed for win_execute by vim-patch:8.2.4026, but nvim_win_call should also not be affected. 1325feedkeys('<Esc>gg') 1326api.nvim_set_current_win(win_empty) 1327feedkeys('gg<C-V>') 1328vim._with({ win = win_lines }, function() feedkeys('G<C-V>') end) 1329vim.cmd.redraw() 1330]] 1331end ) 1332 1333it ( 'can be nested' , function () 1334exec_lua [[ 1335local other_win, cur_win = setup_windows() 1336vim._with({ win = other_win }, function() 1337assert(api.nvim_get_current_win() == other_win) 1338inner = vim._with({ win = cur_win }, function() 1339assert(api.nvim_get_current_win() == cur_win) 1340end) 1341assert(api.nvim_get_current_win() == other_win) 1342end) 1343assert(api.nvim_get_current_win() == cur_win) 1344]] 1345end ) 1346 1347it ( 'updates ruler if cursor moved' , function () 1348local screen =Screen . new ( 30 , 5 ) 1349exec_lua [[ 1350vim.opt.ruler = true 1351local lines = {} 1352for i = 0, 499 do lines[#lines + 1] = tostring(i) end 1353api.nvim_buf_set_lines(0, 0, -1, true, lines) 1354api.nvim_win_set_cursor(0, { 20, 0 }) 1355vim.cmd 'split' 1356_G.win = api.nvim_get_current_win() 1357vim.cmd "wincmd w | redraw" 1358]] 1359screen : expect [[ 136019 | 1361{2:< Name] [+] 20,1 3%}| 1362^19 | 1363{3:< Name] [+] 20,1 3%}| 1364| 1365]] 1366exec_lua [[ 1367vim._with({ win = win }, function() api.nvim_win_set_cursor(0, { 100, 0 }) end) 1368vim.cmd "redraw" 1369]] 1370screen : expect [[ 137199 | 1372{2:< Name] [+] 100,1 19%}| 1373^19 | 1374{3:< Name] [+] 20,1 3%}| 1375| 1376]] 1377end ) 1378 1379it ( 'layout in current tabpage does not affect windows in others' , function () 1380command ( 'tab split' ) 1381local t2_move_win =api . nvim_get_current_win () 1382command ( 'vsplit' ) 1383local t2_other_win =api . nvim_get_current_win () 1384command ( 'tabprevious' ) 1385matches ( 'E36: Not enough room$' , pcall_err ( command , 'execute "split|"->repeat(&lines)' )) 1386command ( 'vsplit' ) 1387 1388exec_lua ( 'vim._with({ win = ... }, function() vim.cmd.wincmd "J" end)' , t2_move_win ) 1389eq ( { 'col' , { { 'leaf' , t2_other_win } , { 'leaf' , t2_move_win } } } , fn . winlayout ( 2 )) 1390end ) 1391 1392it ( 'keeps ":lcd" on the target window, but restores the CWD' , function () 1393local out =exec_lua [[ 1394local other_win, cur_win = setup_windows() 1395local cwd = fn.getcwd() 1396vim._with({ win = other_win }, function() 1397vim.cmd.lcd(vim.fs.joinpath(cwd, 'test')) 1398end) 1399return { fn.haslocaldir(fn.win_id2win(other_win)), fn.getcwd() == cwd } 1400]] 1401eq ( { 1 , true } , out ) 1402end ) 1403end ) 1404 1405describe ( '`wo` context' , function () 1406before_each ( function () 1407exec_lua [[ 1408_G.other_win, _G.cur_win = setup_windows() 1409 1410-- 'virtualedit' is global or local to window (global-local) and string 1411vim.wo[other_win].virtualedit = 'block' 1412vim.wo[cur_win].virtualedit = 'insert' 1413vim.go.virtualedit = 'none' 1414 1415-- 'winblend' is local to window and number 1416vim.wo[other_win].winblend = 10 1417vim.wo[cur_win].winblend = 25 1418vim.go.winblend = 50 1419 1420-- 'number' is local to window and boolean 1421vim.wo[other_win].number = false 1422vim.wo[cur_win].number = false 1423vim.go.number = true 1424 1425_G.get_state = function() 1426return { 1427wo = { 1428ve_cur = vim.wo[cur_win].virtualedit, 1429ve_other = vim.wo[other_win].virtualedit, 1430winbl_cur = vim.wo[cur_win].winblend, 1431winbl_other = vim.wo[other_win].winblend, 1432nu_cur = vim.wo[cur_win].number, 1433nu_other = vim.wo[other_win].number, 1434}, 1435go = { 1436ve = vim.go.virtualedit, 1437winbl = vim.go.winblend, 1438nu = vim.go.number, 1439}, 1440} 1441end 1442]] 1443end ) 1444 1445it ( 'works' , function () 1446local out =exec_lua [[ 1447local context = { wo = { virtualedit = 'all', winblend = 75, number = true } } 1448 1449local before = get_state() 1450local inner = vim._with(context, function() 1451assert(api.nvim_get_current_win() == cur_win) 1452return get_state() 1453end) 1454 1455return { before = before, inner = inner, after = get_state() } 1456]] 1457 1458eq ( { 1459wo ={ 1460ve_cur ='all' , 1461ve_other ='block' , 1462winbl_cur =75 , 1463winbl_other =10 , 1464nu_cur =true , 1465nu_other =false , 1466} , 1467go ={ ve ='none' , winbl =75 , nu =true } , 1468} , out . inner ) 1469eq ( out . before , out . after ) 1470end ) 1471 1472it ( 'sets options in `win` context' , function () 1473local out =exec_lua [[ 1474local context = { win = other_win, wo = { virtualedit = 'all', winblend = 75, number = true } } 1475 1476local before = get_state() 1477local inner = vim._with(context, function() 1478assert(api.nvim_get_current_win() == other_win) 1479return get_state() 1480end) 1481 1482return { before = before, inner = inner, after = get_state() } 1483]] 1484 1485eq ( { 1486wo ={ 1487ve_cur ='insert' , 1488ve_other ='all' , 1489winbl_cur =25 , 1490winbl_other =75 , 1491nu_cur =false , 1492nu_other =true , 1493} , 1494go ={ ve ='none' , winbl =75 , nu =true } , 1495} , out . inner ) 1496eq ( out . before , out . after ) 1497end ) 1498 1499it ( 'restores only options from context' , function () 1500local out =exec_lua [[ 1501local context = { wo = { winblend = 75 } } 1502 1503local inner = vim._with(context, function() 1504assert(api.nvim_get_current_win() == cur_win) 1505vim.wo[cur_win].virtualedit = 'onemore' 1506vim.wo[cur_win].winblend = 99 1507return get_state() 1508end) 1509 1510return { inner = inner, after = get_state() } 1511]] 1512 1513eq ( { 1514wo ={ 1515ve_cur ='onemore' , 1516ve_other ='block' , 1517winbl_cur =99 , 1518winbl_other =10 , 1519nu_cur =false , 1520nu_other =false , 1521} , 1522go ={ ve ='none' , winbl =99 , nu =true } , 1523} , out . inner ) 1524eq ( { 1525wo ={ 1526ve_cur ='onemore' , 1527ve_other ='block' , 1528winbl_cur =25 , 1529winbl_other =10 , 1530nu_cur =false , 1531nu_other =false , 1532} , 1533go ={ ve ='none' , winbl =50 , nu =true } , 1534} , out . after ) 1535end ) 1536 1537it ( 'does not trigger events' , function () 1538exec_lua [[ 1539_G.test_events = { 'WinEnter', 'WinLeave', 'BufWinEnter', 'BufWinLeave' } 1540_G.test_context = { wo = { winblend = 75 } } 1541_G.test_trig_event = function() vim.cmd.new() end 1542]] 1543assert_events_trigger () 1544end ) 1545 1546it ( 'can be nested' , function () 1547local out =exec_lua [[ 1548local before, before_inner, after_inner = get_state(), nil, nil 1549vim._with({ wo = { winblend = 75, virtualedit = 'all', number = true } }, function() 1550before_inner = get_state() 1551inner = vim._with({ wo = { winblend = 99, number = false } }, get_state) 1552after_inner = get_state() 1553end) 1554return { 1555before = before, before_inner = before_inner, 1556inner = inner, 1557after_inner = after_inner, after = get_state(), 1558} 1559]] 1560eq ( 99 , out . inner . wo . winbl_cur ) 1561eq ( 'all' , out . inner . wo . ve_cur ) 1562eq ( false , out . inner . wo . nu_cur ) 1563eq ( out . before_inner , out . after_inner ) 1564eq ( out . before , out . after ) 1565end ) 1566end ) 1567 1568it ( 'returns what callback returns' , function () 1569local out_verify =exec_lua [[ 1570out = { vim._with({}, function() 1571return 'a', 2, nil, { 4 }, function() end 1572end) } 1573return { 1574out[1] == 'a', out[2] == 2, out[3] == nil, 1575vim.deep_equal(out[4], { 4 }), 1576type(out[5]) == 'function', 1577vim.tbl_count(out), 1578} 1579]] 1580eq ( { true , true , true , true , true , 4 } , out_verify ) 1581end ) 1582 1583it ( 'can return values by reference' , function () 1584local out =exec_lua [[ 1585local val = { 4, 10 } 1586local ref = vim._with({}, function() return val end) 1587ref[1] = 7 1588return val 1589]] 1590eq ( { 7 , 10 } , out ) 1591end ) 1592 1593it ( 'can not work with conflicting `buf` and `win`' , function () 1594local out =exec_lua [[ 1595local other_buf, cur_buf = setup_buffers() 1596local other_win, cur_win = setup_windows() 1597assert(api.nvim_win_get_buf(other_win) ~= other_buf) 1598local _, err = pcall(vim._with, { buf = other_buf, win = other_win }, function() end) 1599return err 1600]] 1601matches ( 'Can not set both `buf` and `win`' , out ) 1602end ) 1603 1604it ( 'works with several contexts at once' , function () 1605local out =exec_lua [[ 1606local other_buf, cur_buf = setup_buffers() 1607vim.bo[other_buf].commentstring = '## %s' 1608api.nvim_buf_set_lines(other_buf, 0, -1, false, { 'aaa', 'bbb', 'ccc' }) 1609api.nvim_buf_set_mark(other_buf, 'm', 2, 2, {}) 1610 1611vim.go.commentstring = '// %s' 1612vim.go.langmap = 'xy,yx' 1613 1614local context = { 1615buf = other_buf, 1616bo = { commentstring = '-- %s' }, 1617go = { langmap = 'ab,ba' }, 1618lockmarks = true, 1619} 1620 1621local inner = vim._with(context, function() 1622api.nvim_buf_set_lines(0, 0, -1, false, { 'uuu', 'vvv', 'www' }) 1623return { 1624buf = api.nvim_get_current_buf(), 1625bo = { cms = vim.bo.commentstring }, 1626go = { cms = vim.go.commentstring, lmap = vim.go.langmap }, 1627mark = api.nvim_buf_get_mark(0, 'm') 1628} 1629end) 1630 1631local after = { 1632buf = api.nvim_get_current_buf(), 1633bo = { cms = vim.bo[other_buf].commentstring }, 1634go = { cms = vim.go.commentstring, lmap = vim.go.langmap }, 1635mark = api.nvim_buf_get_mark(other_buf, 'm') 1636} 1637 1638return { 1639context_buf = other_buf, cur_buf = cur_buf, 1640inner = inner, after = after 1641} 1642]] 1643 1644eq ( { 1645buf =out . context_buf , 1646bo ={ cms ='-- %s' } , 1647go ={ cms ='// %s' , lmap ='ab,ba' } , 1648mark ={ 2 , 2 } , 1649} , out . inner ) 1650eq ( { 1651buf =out . cur_buf , 1652bo ={ cms ='## %s' } , 1653go ={ cms ='// %s' , lmap ='xy,yx' } , 1654mark ={ 2 , 2 } , 1655} , out . after ) 1656end ) 1657 1658it ( 'works with same option set in different contexts' , function () 1659local out =exec_lua [[ 1660local get_state = function() 1661return { 1662bo = { cms = vim.bo.commentstring }, 1663wo = { ve = vim.wo.virtualedit }, 1664go = { cms = vim.go.commentstring, ve = vim.go.virtualedit }, 1665} 1666end 1667 1668vim.bo.commentstring = '// %s' 1669vim.go.commentstring = '$$ %s' 1670vim.wo.virtualedit = 'insert' 1671vim.go.virtualedit = 'none' 1672 1673local before = get_state() 1674local context_no_go = { 1675o = { commentstring = '-- %s', virtualedit = 'all' }, 1676bo = { commentstring = '!! %s' }, 1677wo = { virtualedit = 'onemore' }, 1678} 1679local inner_no_go = vim._with(context_no_go, get_state) 1680local middle = get_state() 1681local context_with_go = { 1682o = { commentstring = '-- %s', virtualedit = 'all' }, 1683bo = { commentstring = '!! %s' }, 1684wo = { virtualedit = 'onemore' }, 1685go = { commentstring = '@@ %s', virtualedit = 'block' }, 1686} 1687local inner_with_go = vim._with(context_with_go, get_state) 1688return { 1689before = before, 1690inner_no_go = inner_no_go, 1691middle = middle, 1692inner_with_go = inner_with_go, 1693after = get_state(), 1694} 1695]] 1696 1697-- Should prefer explicit local scopes instead of `o` 1698eq ( { 1699bo ={ cms ='!! %s' } , 1700wo ={ ve ='onemore' } , 1701go ={ cms ='-- %s' , ve ='all' } , 1702} , out . inner_no_go ) 1703eq ( out . before , out . middle ) 1704 1705-- Should prefer explicit global scopes instead of `o` 1706eq ( { 1707bo ={ cms ='!! %s' } , 1708wo ={ ve ='onemore' } , 1709go ={ cms ='@@ %s' , ve ='block' } , 1710} , out . inner_with_go ) 1711eq ( out . middle , out . after ) 1712end ) 1713 1714pending ( 'can forward command modifiers to user command' , function () 1715local out =exec_lua [[ 1716local test_flags = { 1717'emsg_silent', 1718'hide', 1719'keepalt', 1720'keepjumps', 1721'keepmarks', 1722'keeppatterns', 1723'lockmarks', 1724'noautocmd', 1725'silent', 1726'unsilent', 1727} 1728 1729local used_smods 1730local command = function(data) 1731used_smods = data.smods 1732end 1733api.nvim_create_user_command('DummyLog', command, {}) 1734 1735local res = {} 1736for _, flag in ipairs(test_flags) do 1737used_smods = nil 1738vim._with({ [flag] = true }, function() vim.cmd('DummyLog') end) 1739res[flag] = used_smods[flag] 1740end 1741return res 1742]] 1743for k , v in pairs ( out ) do 1744eq ( { k , true } , { k , v } ) 1745end 1746end ) 1747 1748it ( 'handles error in callback' , function () 1749-- Should still restore initial context 1750local out_buf =exec_lua [[ 1751local other_buf, cur_buf = setup_buffers() 1752vim.bo[other_buf].commentstring = '## %s' 1753 1754local context = { buf = other_buf, bo = { commentstring = '-- %s' } } 1755local ok, err = pcall(vim._with, context, function() error('Oops buf', 0) end) 1756 1757return { 1758ok, 1759err, 1760api.nvim_get_current_buf() == cur_buf, 1761vim.bo[other_buf].commentstring, 1762} 1763]] 1764eq ( { false , 'Oops buf' , true , '## %s' } , out_buf ) 1765 1766local out_win =exec_lua [[ 1767local other_win, cur_win = setup_windows() 1768vim.wo[other_win].winblend = 25 1769 1770local context = { win = other_win, wo = { winblend = 50 } } 1771local ok, err = pcall(vim._with, context, function() error('Oops win', 0) end) 1772 1773return { 1774ok, 1775err, 1776api.nvim_get_current_win() == cur_win, 1777vim.wo[other_win].winblend, 1778} 1779]] 1780eq ( { false , 'Oops win' , true , 25 } , out_win ) 1781end ) 1782 1783it ( 'handles not supported option' , function () 1784local out =exec_lua [[ 1785-- Should still restore initial state 1786vim.bo.commentstring = '## %s' 1787 1788local context = { o = { commentstring = '-- %s' }, bo = { winblend = 10 } } 1789local ok, err = pcall(vim._with, context, function() end) 1790 1791return { ok = ok, err = err, cms = vim.bo.commentstring } 1792]] 1793eq ( false , out . ok ) 1794matches ( 'window.*option.*winblend' , out . err ) 1795eq ( '## %s' , out . cms ) 1796end ) 1797 1798it ( 'validates arguments' , function () 1799exec_lua [[ 1800_G.get_error = function(...) 1801local _, err = pcall(vim._with, ...) 1802return err or '' 1803end 1804]] 1805local get_error =function ( string_args ) 1806return exec_lua ( 'return get_error(' .. string_args .. ')' ) 1807end 1808 1809matches ( 'context.*table' , get_error ( "'a', function() end" )) 1810matches ( 'f.*function' , get_error ( '{}, 1' )) 1811 1812local assert_context =function ( bad_context , expected_type ) 1813local bad_field =vim . tbl_keys ( bad_context )[ 1 ] 1814matches ( 1815'context%.' .. bad_field .. '.*' .. expected_type , 1816get_error ( vim . inspect ( bad_context ) .. ', function() end' ) 1817) 1818end 1819 1820assert_context ( { bo =1 } , 'table' ) 1821assert_context ( { buf ='a' } , 'number' ) 1822assert_context ( { emsg_silent =1 } , 'boolean' ) 1823assert_context ( { env =1 } , 'table' ) 1824assert_context ( { go =1 } , 'table' ) 1825assert_context ( { hide =1 } , 'boolean' ) 1826assert_context ( { keepalt =1 } , 'boolean' ) 1827assert_context ( { keepjumps =1 } , 'boolean' ) 1828assert_context ( { keepmarks =1 } , 'boolean' ) 1829assert_context ( { keeppatterns =1 } , 'boolean' ) 1830assert_context ( { lockmarks =1 } , 'boolean' ) 1831assert_context ( { noautocmd =1 } , 'boolean' ) 1832assert_context ( { o =1 } , 'table' ) 1833assert_context ( { sandbox =1 } , 'boolean' ) 1834assert_context ( { silent =1 } , 'boolean' ) 1835assert_context ( { unsilent =1 } , 'boolean' ) 1836assert_context ( { win ='a' } , 'number' ) 1837assert_context ( { wo =1 } , 'table' ) 1838 1839matches ( 'Invalid buffer' , get_error ( '{ buf = -1 }, function() end' )) 1840matches ( 'Invalid window' , get_error ( '{ win = -1 }, function() end' )) 1841end ) 1842 1843it ( 'no double-free when called from :filter browse oldfiles #31501' , function () 1844exec_lua ( [=[ 1845vim.api.nvim_create_autocmd('BufEnter', { 1846callback = function() 1847vim._with({ lockmarks = true }, function() end) 1848end, 1849}) 1850vim.cmd([[ 1851let v:oldfiles = ['Xoldfile'] 1852call nvim_input('1<CR>') 1853noswapfile filter /Xoldfile/ browse oldfiles 1854]]) 1855]=] ) 1856n . assert_alive () 1857eq ( 'Xoldfile' , fn . bufname ( '%' )) 1858end ) 1859end )