robin/artio.nvim
minimal ui2 fuzzy finder for Neovim
git clone https://git.robinwobin.dev/robin/artio.nvim
8a29ed7
main
1local cmdline =require ( "vim._core.ui2.cmdline" ) 2local ui2 =require ( "vim._core.ui2" ) 3 4local _log ={} 5local _loglevel =vim . log . levels . ERROR 6---@private 7---@param msgwrapped { [1]: string, [2]: string? } 8---@param level? integer 9local function logadd ( msgwrapped , level ) 10level =level or vim . log . levels . DEBUG 11if level < _loglevel then 12return 13end 14_log [ # _log + 1 ] =msgwrapped 15end 16---@private 17---@param msg string 18local function logdebug ( msg ) 19logadd ( { msg .. "\n" } ) 20end 21---@private 22---@param msg string 23---@param v any 24local function logdbg ( msg , v ) 25logdebug ( string . format ( "%s: %s\n" , msg , ( vim . inspect ( v )))) 26end 27---@private 28---@param msg string 29local function logerror ( msg ) 30logadd ( { msg .. "\n" , "ErrorMsg" } , vim . log . levels . ERROR ) 31end 32 33local prompt_hl_id =vim . api . nvim_get_hl_id_by_name ( "ArtioPrompt" ) 34 35---@class artio.View 36---@field picker artio.Picker 37---@field closed boolean 38---@field opts table<'win'|'buf'|'g',table<string,any>> 39---@field marks table<string|integer, integer> 40---@field win artio.View.win 41---@field preview_win integer 42local View ={} 43View . __index =View 44 45---@param picker artio.Picker 46function View : new ( picker ) 47---@diagnostic disable-next-line: undefined-field 48if picker . log_level then 49---@diagnostic disable-next-line: undefined-field 50_loglevel =picker . log_level 51end 52 53return setmetatable ( { 54picker =picker , 55closed =false , 56opts ={} , 57marks ={} , 58win ={ 59height =1 , 60} , 61} , View ) 62end 63 64---@class artio.View.win 65---@field height integer 66 67local prompthl_id =- 1 68 69--- gets updated before draw 70local before_draw_tick =0 71--- gets updated after changedtick event 72local last_draw_tick =0 73local function get_changedtick () 74return vim . api . nvim_buf_get_changedtick ( ui2 . bufs . cmd ) 75end 76 77local cmdbuff ="" ---@type string Stored cmdline used to calculate translation offset. 78local promptlen =0 -- Current length of the last line in the prompt. 79local promptidx =0 80--- Concatenate content chunks and set the text for the current row in the cmdline buffer. 81--- 82---@param content CmdContent 83---@param prompt string 84function View : setprompttext ( content , prompt ) 85local lines ={} ---@type string[] 86for line in ( prompt .. "\n" ): gmatch ( "(.-)\n" ) do 87lines [ # lines + 1 ] =vim . fn . strtrans ( line ) 88end 89 90local promptstr =lines [ # lines ] 91promptlen =# lines [ # lines ] 92 93cmdbuff ="" 94for _ , chunk in ipairs ( content ) do 95cmdbuff =cmdbuff .. chunk [ 2 ] 96end 97lines [ # lines ] =( "%s%s" ): format ( promptstr , vim . fn . strtrans ( cmdbuff )) 98 99self : promptpos () 100self : setlines ( promptidx , promptidx + 1 , lines ) 101if vim . fn . prompt_getprompt ( ui2 . bufs . cmd ) ~= promptstr then 102vim . fn . prompt_setprompt ( ui2 . bufs . cmd , promptstr ) 103end 104vim . schedule ( function () 105local ok , result =pcall ( vim . api . nvim_buf_set_mark , ui2 . bufs . cmd , ":" , promptidx + 1 , promptlen , {} ) 106if not ok then 107logerror (( "Failed to set mark %d:%d\n\t%s" ): format ( promptidx , promptlen , result )) 108return 109end 110end ) 111end 112 113--- Set the cmdline buffer text and cursor position. 114--- 115---@param content CmdContent 116---@param pos? integer 117---@param firstc string 118---@param prompt string 119---@param indent integer 120---@param level integer 121---@param hl_id integer 122function View : show ( content , pos , firstc , prompt , indent , level , hl_id ) 123cmdline . level , cmdline . indent =level , indent 124if cmdline . highlighter and cmdline . highlighter . active then 125cmdline . highlighter . active [ ui2 . bufs . cmd ] =nil 126end 127if ui2 . msg . cmd . msg_row ~= - 1 then 128ui2 . msg . msg_clear () 129end 130ui2 . msg . virt . last ={ {} , {} , {} , {} } 131 132self : clear () 133prompthl_id =hl_id 134 135local cmd_text ="" 136for _ , chunk in ipairs ( content ) do 137cmd_text =cmd_text .. chunk [ 2 ] 138end 139 140self : showmatches () 141 142self : setprompttext ( content , ( "%s%s%s" ): format ( firstc , prompt , ( " " ): rep ( indent ))) 143self : updatecursor ( pos ) 144 145self : updatewinheight () 146 147self : drawprompt () 148self : hlselect () 149end 150 151--- Set the 'cmdheight' and cmdline window height. Reposition message windows. 152--- 153---@param win integer Cmdline window in the current tabpage. 154---@param hide boolean Whether to hide or show the window. 155---@param height integer (Text)height of the cmdline window. 156function View : win_config ( win , hide , height ) 157if ui2 . cmdheight == 0 and vim . api . nvim_win_get_config ( win ). hide ~= hide then 158vim . api . nvim_win_set_config ( win , { hide =hide , height =not hide and height or nil } ) 159elseif vim . api . nvim_win_get_height ( win ) ~= height then 160vim . api . nvim_win_set_height ( win , height ) 161end 162 163if not hide and self . picker . win . hidestatusline then 164height =0 165end 166 167if vim . o . cmdheight ~= height then 168-- Avoid moving the cursor with 'splitkeep' = "screen", and altering the user 169-- configured value with noautocmd. 170vim . _with ( { noautocmd =true , o ={ splitkeep ="screen" } } , function () 171vim . o . cmdheight =height 172end ) 173ui2 . msg . set_pos () 174end 175 176if self . preview_win and vim . api . nvim_win_is_valid ( self . preview_win ) then 177vim . api . nvim_win_set_config ( self . preview_win , self : previewconfig ()) 178end 179end 180 181---@param predicted? integer The predicted height of the cmdline window 182function View : updatewinheight ( predicted ) 183local height =math . max ( 1 , predicted or vim . api . nvim_win_text_height ( ui2 . wins . cmd , {} ). all ) 184height =math . min ( height , self . win . height ) 185self : win_config ( ui2 . wins . cmd , false , height ) 186end 187 188function View : saveview () 189self . save =vim . fn . winsaveview () 190self . prevwin =vim . api . nvim_get_current_win () 191end 192 193function View : restoreview () 194vim . api . nvim_set_current_win ( self . prevwin ) 195vim . fn . winrestview ( self . save ) 196end 197 198local ext_winhl ="Search:,CurSearch:,IncSearch:" 199 200---@param restore? boolean 201function View : setopts ( restore ) 202local opts ={ 203win ={ 204eventignorewin ="all,-FileType,-InsertCharPre,-TextChangedI,-CursorMovedI" , 205winhighlight ="Normal:ArtioNormal," .. ext_winhl , 206signcolumn ="no" , 207wrap =false , 208} , 209buf ={ 210filetype ="artio-picker" , 211buftype ="prompt" , 212autocomplete =false , 213} , 214g ={ 215showmode =false , 216showcmd =false , 217} , 218} 219 220for level , o in pairs ( opts ) do 221self . opts [ level ] =self . opts [ level ] or {} 222local props ={ 223scope =level == "g" and "global" or "local" , 224buf =level == "buf" and ui2 . bufs . cmd or nil , 225win =level == "win" and ui2 . wins . cmd or nil , 226} 227 228for name , value in pairs ( o ) do 229if restore then 230vim . api . nvim_set_option_value ( name , self . opts [ level ][ name ], props ) 231else 232self . opts [ level ][ name ] =vim . api . nvim_get_option_value ( name , props ) 233vim . api . nvim_set_option_value ( name , value , props ) 234end 235end 236end 237end 238 239local maxlistheight =1 -- Max height of the matches list (`self.win.height - 1`) 240 241function View : on_resized () 242logdebug ( "on_resized" ) 243 244if self . picker . win . height > 1 then 245self . win . height =self . picker . win . height 246else 247self . win . height =vim . o . lines * self . picker . win . height 248end 249self . win . height =math . max ( math . ceil ( self . win . height ), 1 ) 250 251maxlistheight =math . max ( self . win . height - 1 , 1 ) 252end 253 254function View : open () 255if not self . picker then 256return 257end 258_log =nil 259_log ={} 260 261vim . schedule ( function () 262self . augroup =vim . api . nvim_create_augroup ( "@artio.view" , { clear =true } ) 263 264vim . api . nvim_create_autocmd ( "CmdlineLeave" , { 265group =self . augroup , 266once =true , 267callback =function () 268self : close () 269end , 270} ) 271 272vim . api . nvim_create_autocmd ( "ModeChanged" , { 273group =self . augroup , 274callback =function ( ev ) 275if string . match ( ev . match , "^i:" ) then 276self : close () 277end 278end , 279} ) 280 281vim . api . nvim_create_autocmd ( { "VimResized" , "WinEnter" } , { 282group =self . augroup , 283callback =function () 284self : on_resized () 285end , 286} ) 287 288vim . api . nvim_create_autocmd ( "WinEnter" , { 289group =self . augroup , 290callback =function () 291self : update ( true ) 292end , 293} ) 294 295vim . api . nvim_create_autocmd ( "TextChangedI" , { 296group =self . augroup , 297buffer =ui2 . bufs . cmd , 298callback =function () 299self : update () 300end , 301} ) 302 303vim . api . nvim_create_autocmd ( "CursorMovedI" , { 304group =self . augroup , 305buffer =ui2 . bufs . cmd , 306callback =function () 307self : updatecursor () 308end , 309} ) 310end ) 311 312cmdline . prompt =false 313cmdline . srow =0 314cmdline . indent =1 315cmdline . level =1 316 317self : saveview () 318 319-- initial render 320self : trigger_show () 321 322vim . _with ( { noautocmd =true } , function () 323vim . api . nvim_set_current_win ( ui2 . wins . cmd ) 324end ) 325 326self : setopts () 327 328-- start insert *before* registering events 329self : updatecursor ( # cmdbuff ) 330vim . _with ( { noautocmd =true } , function () 331vim . cmd . startinsert ( { bang =true } ) 332end ) 333 334-- trigger after registering events 335vim . schedule ( function () 336vim . _with ( { win =ui2 . wins . cmd , wo ={ eventignorewin ="" } } , function () 337vim . api . nvim_exec_autocmds ( "WinEnter" , {} ) 338end ) 339end ) 340end 341 342function View : close () 343if self . closed then 344return 345end 346self : closepreview () 347vim . schedule ( function () 348pcall ( vim . api . nvim_del_augroup_by_id , self . augroup ) 349pcall ( vim . api . nvim_buf_detach , ui2 . bufs . cmd ) 350 351vim . cmd . stopinsert () 352 353-- prepare state 354self : setopts ( true ) 355 356-- reset state 357self : clear () 358cmdline . srow =0 359cmdline . erow =0 360 361-- restore ui 362self : hide () 363self : restoreview () 364vim . cmd . redraw () 365 366self . closed =true 367 368self . picker : close () 369 370vim . api . nvim_echo ( _log , true , {} ) 371end ) 372end 373 374function View : hide () 375vim . fn . clearmatches ( ui2 . wins . cmd ) -- Clear matchparen highlights. 376vim . api . nvim_win_set_cursor ( ui2 . wins . cmd , { 1 , 0 } ) 377vim . api . nvim_buf_set_lines ( ui2 . bufs . cmd , 0 , - 1 , false , {} ) 378 379local clear =vim . schedule_wrap ( function ( was_prompt ) 380-- Avoid clearing prompt window when it is re-entered before the next event 381-- loop iteration. E.g. when a non-choice confirm button is pressed. 382if was_prompt and not cmdline . prompt then 383pcall ( function () 384vim . api . nvim_buf_set_lines ( ui2 . bufs . cmd , 0 , - 1 , false , {} ) 385vim . api . nvim_buf_set_lines ( ui2 . bufs . dialog , 0 , - 1 , false , {} ) 386vim . api . nvim_win_set_config ( ui2 . wins . dialog , { hide =true } ) 387vim . on_key ( nil , ui2 . msg . dialog_on_key ) 388end ) 389end 390-- Messages emitted as a result of a typed command are treated specially: 391-- remember if the cmdline was used this event loop iteration. 392-- NOTE: Message event callbacks are themselves scheduled, so delay two iterations. 393vim . schedule ( function () 394cmdline . level =- 1 395end ) 396end ) 397clear ( cmdline . prompt ) 398 399cmdline . prompt , cmdline . level =false , 0 400self : win_config ( ui2 . wins . cmd , true , ui2 . cmdheight ) 401end 402 403function View : trigger_show () 404logdebug ( "trigger_show" ) 405local input 406if self . picker . live then 407input =self . picker . liveinput 408else 409input =self . picker . input 410end 411self : show ( { { 0 , input } } , - 1 , "" , self . picker . prompttext , cmdline . indent , cmdline . level , prompt_hl_id ) 412end 413 414---@param force? boolean 415function View : update ( force ) 416if not force and before_draw_tick < last_draw_tick and before_draw_tick == get_changedtick () - 1 then 417logdebug ( "update (skip-redraw)" ) 418return self : drawprompt () 419end 420 421logdebug ( "update" ) 422 423local text =vim . api . nvim_get_current_line () 424text =text : sub ( promptlen + 1 ) 425 426if self . picker . live then 427self . picker . liveinput =text 428else 429self . picker . input =text 430end 431 432vim . schedule ( coroutine . wrap ( function () 433logdebug ( "getmatches" ) 434self . picker : getmatches () 435 436if self . closed then 437return 438end 439 440vim . schedule_wrap ( self . trigger_show )( self ) 441end )) 442end 443 444local curpos ={ 0 , 0 } -- Last drawn cursor position. absolute 445---@param pos? integer relative to prompt 446function View : updatecursor ( pos ) 447logdebug ( "updatecursor" ) 448 449self : promptpos () 450 451if not pos or pos < 0 then 452local cursorpos =vim . api . nvim_win_get_cursor ( ui2 . wins . cmd ) 453pos =cursorpos [ 2 ] - promptlen 454end 455 456-- set cursor pos to *at least* the prompt length 457curpos [ 2 ] =math . max ( curpos [ 2 ], promptlen ) 458 459if curpos [ 1 ] == promptidx + 1 and curpos [ 2 ] == promptlen + pos then 460return 461end 462 463if pos < 0 then 464-- reset to last known position 465pos =curpos [ 2 ] - promptlen 466end 467 468curpos [ 1 ], curpos [ 2 ] =promptidx + 1 , promptlen + pos 469 470vim . _with ( { noautocmd =true } , function () 471local ok , _ =pcall ( vim . api . nvim_win_set_cursor , ui2 . wins . cmd , curpos ) 472if not ok then 473logerror (( "Failed to set cursor %d:%d" ): format ( curpos [ 1 ], curpos [ 2 ])) 474end 475end ) 476end 477 478local srow =0 479 480function View : clear () 481srow =self . picker . opts . bottom and 0 or 1 482cmdline . erow =srow 483self : setlines ( 0 , - 1 , {} ) 484end 485 486function View : promptpos () 487promptidx =self . picker . opts . bottom and cmdline . erow or 0 488end 489 490function View : setlines ( posstart , posend , lines ) 491-- update winheight to prevent wrong scroll when increasing from 1 492local diff =# lines - ( posend - posstart ) 493if diff ~= 0 then 494local height =vim . api . nvim_win_text_height ( ui2 . wins . cmd , {} ). all 495local predicted =height + diff 496self : updatewinheight ( predicted ) 497end 498 499before_draw_tick =get_changedtick () 500vim . api . nvim_buf_set_lines ( ui2 . bufs . cmd , posstart , posend , false , lines ) 501last_draw_tick =get_changedtick () 502end 503 504local view_ns =vim . api . nvim_create_namespace ( "@artio.view.ns" ) 505local ext_priority ={ 506prompt =1 , 507info =2 , 508select =4 , 509marker =8 , 510hl =16 , 511icon =32 , 512match =64 , 513} 514 515---@param id? string|integer 516---@param line integer 0-based 517---@param col integer 0-based 518---@param opts vim.api.keyset.set_extmark 519---@return integer 520function View : mark ( id , line , col , opts ) 521if id and self . marks [ id ] then 522vim . _with ( { noautocmd =true } , function () 523vim . api . nvim_buf_del_extmark ( ui2 . bufs . cmd , view_ns , self . marks [ id ]) 524end ) 525self . marks [ id ] =nil 526end 527 528opts . hl_mode ="combine" 529opts . invalidate =true 530 531local ok , result 532vim . _with ( { noautocmd =true } , function () 533ok , result =pcall ( vim . api . nvim_buf_set_extmark , ui2 . bufs . cmd , view_ns , line , col , opts ) 534end ) 535if not ok then 536logerror (( "Failed to add extmark %d:%d\n\t%s" ): format ( line , col , result )) 537return - 1 538end 539 540if id and result >= 0 then 541self . marks [ id ] =result 542end 543 544return result 545end 546 547---@param p artio.Picker 548---@param info 'index'|'list'|string 549---@return string 550local function getpromptinfo ( p , info ) 551if info == "index" then 552return ( "[%d]" ): format ( p . idx ) 553elseif info == "list" then 554return ( "(%d/%d)" ): format ( # p . matches , p . items and # p . items or 0 ) 555end 556return "" 557end 558 559function View : drawprompt () 560logdebug ( "drawprompt" ) 561 562self : promptpos () 563if promptlen > 0 and prompthl_id > 0 then 564self : mark ( "prompthl" , promptidx , 0 , { hl_group =prompthl_id , end_col =promptlen , priority =ext_priority . prompt } ) 565self : mark ( "promptinfo" , promptidx , 0 , { 566virt_text ={ 567{ 568table . concat ( 569vim 570. iter ( self . picker . opts . infolist ) 571: map ( function ( info ) 572return getpromptinfo ( self . picker , info ) 573end ) 574: totable (), 575" " 576), 577"InfoText" , 578} , 579} , 580virt_text_pos ="eol_right_align" , 581priority =ext_priority . info , 582} ) 583end 584end 585 586local offset =0 587 588function View : updateoffset () 589self . picker : fix () 590if self . picker . idx == 0 then 591offset =0 592return 593end 594 595local _offset =self . picker . idx - maxlistheight 596if _offset > offset then 597offset =_offset 598elseif self . picker . idx <= offset then 599offset =self . picker . idx - 1 600end 601 602offset =math . min ( math . max ( 0 , offset ), math . max ( 0 , # self . picker . matches - maxlistheight )) 603end 604 605local icon_pad =2 606 607function View : showmatches () 608local indent =vim . fn . strdisplaywidth ( self . picker . opts . pointer ) + 1 609local prefix =( " " ): rep ( indent ) 610local icon_pad_str =( " " ): rep ( icon_pad ) 611 612self : updateoffset () 613 614local lines ={} ---@type string[] 615local hls ={} 616local icons ={} ---@type ([string, string]|false)[] 617local custom_hls ={} ---@type (artio.Picker.hl[]|false)[] 618local marks ={} ---@type boolean[] 619for i =1 + offset , math . min ( # self . picker . matches , maxlistheight + offset ) do 620local match =self . picker . matches [ i ] 621local item =self . picker . items [ match [ 1 ]] 622 623local icon , icon_hl =item . icon , item . icon_hl 624if not ( icon and icon_hl ) and vim . is_callable ( self . picker . get_icon ) then 625icon , icon_hl =self . picker . get_icon ( item ) 626item . icon , item . icon_hl =icon , icon_hl 627end 628icons [ # icons + 1 ] =icon and { icon , icon_hl } or false 629icon =icon and ( "%s%s" ): format ( item . icon , icon_pad_str ) or "" 630 631local hl =item . hls 632if not hl and vim . is_callable ( self . picker . hl_item ) then 633hl =self . picker . hl_item ( item ) 634item . hls =hl 635end 636custom_hls [ # custom_hls + 1 ] =hl or false 637 638marks [ # marks + 1 ] =self . picker . marked [ item . id ] or false 639 640lines [ # lines + 1 ] =( "%s%s%s" ): format ( prefix , icon , item . text ) 641hls [ # hls + 1 ] =match [ 2 ] 642end 643 644if not self . picker . opts . shrink then 645for _ =1 , ( maxlistheight - # lines ) do 646lines [ # lines + 1 ] ="" 647end 648end 649self : setlines ( srow , cmdline . erow , lines ) 650cmdline . erow =srow + # lines 651 652for i =1 , # lines do 653local has_icon =icons [ i ] and icons [ i ][ 1 ] and true 654local icon_indent =has_icon and ( # icons [ i ][ 1 ] + icon_pad ) or 0 655 656if has_icon and icons [ i ][ 2 ] then 657self : mark ( nil , srow + i - 1 , indent , { 658end_col =indent + icon_indent , 659hl_group =icons [ i ][ 2 ], 660priority =ext_priority . icon , 661} ) 662end 663 664local line_hls =custom_hls [ i ] 665if line_hls then 666for j =1 , # line_hls do 667local hl =line_hls [ j ] 668self : mark ( nil , srow + i - 1 , indent + icon_indent + hl [ 1 ][ 1 ], { 669end_col =indent + icon_indent + hl [ 1 ][ 2 ], 670hl_group =hl [ 2 ], 671priority =ext_priority . hl , 672} ) 673end 674end 675 676if marks [ i ] then 677self : mark ( nil , srow + i - 1 , indent - 1 , { 678virt_text ={ { self . picker . opts . marker , "ArtioMark" } } , 679virt_text_pos ="overlay" , 680priority =ext_priority . marker , 681} ) 682self : mark ( nil , srow + i - 1 , 0 , { 683hl_group ="ArtioMarkLine" , 684hl_eol =true , 685end_row =srow + i , 686end_col =0 , 687 688priority =ext_priority . marker , 689} ) 690end 691 692if hls [ i ] then 693for j =1 , # hls [ i ] do 694local col =indent + icon_indent + hls [ i ][ j ] 695self : mark ( nil , srow + i - 1 , col , { 696hl_group ="ArtioMatch" , 697end_col =col + 1 , 698priority =ext_priority . match , 699} ) 700end 701end 702end 703end 704 705function View : hlselect () 706self : softupdatepreview () 707 708self . picker : fix () 709local idx =self . picker . idx 710if idx == 0 then 711return 712end 713 714self : updateoffset () 715local row =math . max ( 0 , math . min ( srow + ( idx - offset ), cmdline . erow ) - 1 ) 716 717self : mark ( "hlselect" , row , 0 , { 718virt_text ={ { self . picker . opts . pointer , "ArtioPointer" } } , 719virt_text_pos ="overlay" , 720 721hl_group ="ArtioSel" , 722hl_eol =true , 723end_row =row + 1 , 724end_col =0 , 725 726priority =ext_priority . select , 727} ) 728end 729 730function View : togglepreview () 731if self . preview_win then 732self : closepreview () 733return 734end 735 736self : updatepreview () 737end 738 739---@return {buf?:integer, pos?:[integer,integer], pos_end?:[integer,integer]}? 740function View : openpreview () 741if self . picker . idx == 0 then 742return 743end 744 745local match =self . picker . matches [ self . picker . idx ] 746local item =self . picker . items [ match [ 1 ]] 747 748if not item or not ( self . picker . preview_item and vim . is_callable ( self . picker . preview_item )) then 749return 750end 751 752return self . picker . preview_item ( item . v ) 753end 754 755function View : previewconfig () 756local previewopts =self . picker . win . preview_opts 757and vim . is_callable ( self . picker . win . preview_opts ) 758and self . picker . win . preview_opts ( self ) 759local cmdheight =vim . api . nvim_win_get_height ( ui2 . wins . cmd ) 760 761local winborder =previewopts and previewopts . border or vim . o . winborder 762return vim . tbl_extend ( "force" , { 763relative ="editor" , 764focusable =false , 765width =vim . o . columns , 766height =self . win . height , 767col =0 , 768row =vim . o . lines 769- ( self . win . height + cmdheight ) 770- (( winborder == "none" or winborder == "" ) and 0 or 2 ) 771- ( self . picker . win . hidestatusline and 0 or 1 ), 772} , previewopts or {} ) 773end 774 775function View : updatepreview () 776local pr =self : openpreview () 777if not pr or not pr . buf then 778return 779end 780 781if not self . preview_win then 782self . preview_win =vim . api . nvim_open_win ( pr . buf , false , self : previewconfig ()) 783else 784vim . api . nvim_win_set_buf ( self . preview_win , pr . buf ) 785end 786 787vim . _with ( { win =self . preview_win , noautocmd =true } , function () 788vim . api . nvim_set_option_value ( "previewwindow" , true , { scope ="local" } ) 789vim . api . nvim_set_option_value ( "eventignorewin" , "all,-FileType" , { scope ="local" } ) 790 791local sameline =pr . pos ~= nil and pr . pos_end == nil or pr . pos_end [ 1 ] == pr . pos [ 1 ] 792vim . api . nvim_set_option_value ( "cursorline" , sameline , { scope ="local" } ) 793 794if pr . pos then 795vim . api . nvim_win_set_cursor ( self . preview_win , pr . pos ) 796end 797end ) 798end 799 800function View : softupdatepreview () 801if self . picker . idx == 0 then 802self : closepreview () 803end 804 805if not self . preview_win then 806return 807end 808 809self : updatepreview () 810end 811 812function View : closepreview () 813if not self . preview_win then 814return 815end 816 817vim . api . nvim_win_close ( self . preview_win , true ) 818self . preview_win =nil 819end 820 821return View