robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1---@diagnostic disable: no-unknown 2local t =require ( 'test.testutil' ) 3local t_lsp =require ( 'test.functional.plugin.lsp.testutil' ) 4local n =require ( 'test.functional.testnvim' )() 5 6local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 7local clear =n . clear 8local eq =t . eq 9local neq =t . neq 10local exec_lua =n . exec_lua 11local feed =n . feed 12local retry =t . retry 13local Screen =require ( 'test.functional.ui.screen' ) 14 15local create_server_definition =t_lsp . create_server_definition 16 17--- Extract only abbr/word from a list of completion items for assertion 18---@param items table 19---@return table 20local function extract_word_abbr ( items ) 21return vim . tbl_map ( function ( x ) 22return { abbr =x . abbr , word =x . word } 23end , items ) 24end 25 26--- Convert completion results. 27--- 28---@param line string line contents. Mark cursor position with `|` 29---@param candidates lsp.CompletionList|lsp.CompletionItem[] 30---@param lnum? integer 0-based, defaults to 0 31---@return {items: table[], server_start_boundary: integer?} 32local function complete ( line , candidates , lnum , server_boundary ) 33lnum =lnum or 0 34-- nvim_win_get_cursor returns 0 based column, line:find returns 1 based 35local cursor_col =line : find ( '|' ) - 1 36line =line : gsub ( '|' , '' ) 37return exec_lua ( function ( result ) 38local line_to_cursor =line : sub ( 1 , cursor_col ) 39local client_start_boundary =vim . fn . match ( line_to_cursor , '\\k*$' ) 40local items , new_server_boundary =require ( 'vim.lsp.completion' ). _convert_results ( 41line , 42lnum , 43cursor_col , 441 , 45client_start_boundary , 46server_boundary , 47result , 48'utf-16' 49) 50return { 51items =items , 52server_start_boundary =new_server_boundary , 53} 54end , candidates ) 55end 56 57--- Wait for pumvisible() to equal `visible` (default 1) 58---@param visible? integer 1 to wait for pum shown, 0 to wait for pum hidden 59local function wait_for_pum ( visible ) 60visible =visible == nil and 1 or visible 61retry ( nil , nil , function () 62eq ( 63visible , 64exec_lua ( function () 65return vim . fn . pumvisible () 66end ) 67) 68end ) 69end 70 71--- Detach client and assert the pum no longer appears. 72---@param client_id integer 73local function assert_cleanup_after_detach ( client_id ) 74feed ( '<Esc>o' ) 75exec_lua ( function () 76vim . lsp . completion . get () 77end ) 78wait_for_pum ( 1 ) 79feed ( '<C-e>' ) 80 81-- Detach then re-trigger under identical conditions. 82exec_lua ( function () 83vim . lsp . buf_detach_client ( 0 , client_id ) 84end ) 85exec_lua ( function () 86vim . lsp . completion . get () 87end ) 88wait_for_pum ( 0 ) 89feed ( '<Esc>' ) 90end 91 92describe ( 'vim.lsp.completion: item conversion' , function () 93before_each ( n . clear ) 94 95-- https://microsoft.github.io/language-server-protocol/specifications/specification-current/#textDocument_completion 96it ( 'prefers textEdit over label as word' , function () 97local range0 ={ 98start ={ line =0 , character =0 } , 99[ 'end' ] ={ line =0 , character =0 } , 100} 101local completion_list ={ 102-- resolves into label 103{ label ='foobar' , sortText ='a' , documentation ='documentation' } , 104{ 105label ='foobar' , 106sortText ='b' , 107documentation ={ value ='documentation' } , 108} , 109-- resolves into insertText 110{ label ='foocar' , sortText ='c' , insertText ='foobar' } , 111{ label ='foocar' , sortText ='d' , insertText ='foobar' } , 112-- resolves into textEdit.newText 113{ 114label ='foocar' , 115sortText ='e' , 116insertText ='foodar' , 117textEdit ={ newText ='foobar' , range =range0 } , 118} , 119{ label ='foocar' , sortText ='f' , textEdit ={ newText ='foobar' , range =range0 } } , 120-- plain text 121{ 122label ='foocar' , 123sortText ='g' , 124insertText ='foodar(${1:var1})' , 125insertTextFormat =1 , 126} , 127{ 128label ='•INT16_C(c)' , 129insertText ='INT16_C(${1:c})' , 130insertTextFormat =2 , 131filterText ='INT16_C' , 132sortText ='h' , 133textEdit ={ 134newText ='INT16_C(${1:c})' , 135range =range0 , 136} , 137} , 138-- "" is unset 139{ 140label ='zoocar_long' , 141sortText ='' , 142insertText ='' , 143insertTextFormat =2 , 144textEdit ={ newText ='foobar($1)' , range =range0 } , 145} , 146} 147local expected ={ 148{ abbr ='foobar' , word ='foobar' } , 149{ abbr ='foobar' , word ='foobar' } , 150{ abbr ='foocar' , word ='foobar' } , 151{ abbr ='foocar' , word ='foobar' } , 152{ abbr ='foocar' , word ='foobar' } , 153{ abbr ='foocar' , word ='foobar' } , 154{ abbr ='foocar' , word ='foodar(${1:var1})' } , -- marked as PlainText, text is used as is 155{ abbr ='•INT16_C(c)' , word ='INT16_C' } , 156{ abbr ='zoocar_long' , word ='foobar' } , 157} 158local result =complete ( '|' , completion_list ) 159eq ( expected , extract_word_abbr ( result . items )) 160end ) 161 162local word_sorter =function ( a , b ) 163return a . word > b . word 164end 165 166it ( 'does not filter if there is a textEdit' , function () 167local range0 ={ 168start ={ line =0 , character =0 } , 169[ 'end' ] ={ line =0 , character =0 } , 170} 171local completion_list ={ 172{ label ='foo' , textEdit ={ newText ='foo' , range =range0 } } , 173{ label ='bar' , textEdit ={ newText ='bar' , range =range0 } } , 174} 175local result =complete ( 'fo|' , completion_list ) 176local expected ={ 177{ abbr ='foo' , word ='foo' } , 178} 179local got =extract_word_abbr ( result . items ) 180table . sort ( expected , word_sorter ) 181table . sort ( got , word_sorter ) 182eq ( expected , got ) 183end ) 184 185it ( 'generate "■" symbol with highlight group for CompletionItemKind.Color' , function () 186local completion_list ={ 187{ label ='text-red-300' , kind =16 , documentation ='color: rgb(252, 165, 165)' } , 188} 189local result =complete ( '|' , completion_list ) 190result =vim . tbl_map ( function ( x ) 191return { 192word =x . word , 193kind_hlgroup =x . kind_hlgroup , 194kind =x . kind , 195} 196end , result . items ) 197eq ( { { word ='text-red-300' , kind_hlgroup ='@lsp.color.fca5a5' , kind ='■' } } , result ) 198end ) 199 200it ( 'uses labelDetails for abbr and menu' , function () 201local completion_list ={ 202{ 203label ='printf' , 204kind =3 , 205detail ='int' , 206sortText ='1' , 207labelDetails ={ detail ='(const char *restrict, ...)' , description ='stdio.h' } , 208} , 209{ 210label =' flush' , 211kind =2 , 212insertText ='flush()' , 213insertTextFormat =2 , 214filterText ='flush' , 215sortText ='2' , 216labelDetails ={ detail ='()' } , 217} , 218} 219local result =complete ( '|' , completion_list ) 220eq ( 'printf(const char *restrict, ...)' , result . items [ 1 ]. abbr ) 221eq ( 'stdio.h' , result . items [ 1 ]. menu ) 222eq ( 'flush' , result . items [ 2 ]. word ) 223end ) 224 225---@param prefix string 226---@param items lsp.CompletionItem[] 227---@param expected table[] 228local assert_completion_matches =function ( prefix , items , expected ) 229local got =extract_word_abbr ( complete ( prefix .. '|' , items ). items ) 230table . sort ( expected , word_sorter ) 231table . sort ( got , word_sorter ) 232eq ( expected , got ) 233end 234 235it ( 'uses filterText as word if label/newText would not match' , function () 236local items ={ 237{ 238filterText ='<module' , 239insertTextFormat =2 , 240kind =10 , 241label ='module' , 242sortText ='module' , 243textEdit ={ 244newText ='<module>$1</module>$0' , 245range ={ 246start ={ character =0 , line =0 } , 247[ 'end' ] ={ character =0 , line =0 } , 248} , 249} , 250} , 251{ 252filterText ='atto' , 253insertTextFormat =1 , 254kind =7 , 255label ='•std::atto' , 256sortText ='atto' , 257textEdit ={ 258newText ='std::atto' , 259range ={ 260start ={ character =0 , line =0 } , 261[ 'end' ] ={ character =0 , line =0 } , 262} , 263} , 264} , 265{ 266filterText ='adopt_lock_t' , 267insertTextFormat =1 , 268kind =7 , 269label ='•std::adopt_lock_t' , 270sortText ='adopt_lock_t' , 271insertText ='std::adopt_lock_t' , 272} , 273} 274assert_completion_matches ( '<mo' , items , { 275{ abbr ='module' , word ='<module' } , 276} ) 277assert_completion_matches ( 'a' , items , { 278{ abbr ='•std::atto' , word ='atto' } , 279{ abbr ='•std::adopt_lock_t' , word ='adopt_lock_t' } , 280} ) 281assert_completion_matches ( '' , items , { 282{ abbr ='module' , word ='module' } , 283{ abbr ='•std::atto' , word ='std::atto' } , 284{ abbr ='•std::adopt_lock_t' , word ='std::adopt_lock_t' } , 285} ) 286end ) 287 288describe ( 'when completeopt has fuzzy matching enabled' , function () 289before_each ( function () 290exec_lua ( function () 291vim . opt . completeopt : append ( 'fuzzy' ) 292end ) 293end ) 294after_each ( function () 295exec_lua ( function () 296vim . opt . completeopt : remove ( 'fuzzy' ) 297end ) 298end ) 299 300it ( 'fuzzy matches on filterText' , function () 301assert_completion_matches ( 'fo' , { 302{ label ='?.foo' , filterText ='foo' } , 303{ label ='faz other' , filterText ='faz other' } , 304{ label ='bar' , filterText ='bar' } , 305} , { 306{ abbr ='faz other' , word ='faz other' } , 307{ abbr ='?.foo' , word ='?.foo' } , 308} ) 309end ) 310 311it ( 'fuzzy matches on label when filterText is missing or empty' , function () 312assert_completion_matches ( 'fo' , { 313{ label ='foo' , filterText ='' } , 314{ label ='faz other' } , 315{ label ='bar' } , 316} , { 317{ abbr ='faz other' , word ='faz other' } , 318{ abbr ='foo' , word ='foo' } , 319} ) 320end ) 321end ) 322 323describe ( 'when smartcase is enabled' , function () 324before_each ( function () 325exec_lua ( function () 326vim . opt . smartcase =true 327end ) 328end ) 329after_each ( function () 330exec_lua ( function () 331vim . opt . smartcase =false 332end ) 333end ) 334 335it ( 'matches filterText case sensitively' , function () 336assert_completion_matches ( 'Fo' , { 337{ label ='foo' , filterText ='foo' } , 338{ label ='?.Foo' , filterText ='Foo' } , 339{ label ='Faz other' , filterText ='Faz other' } , 340{ label ='faz other' , filterText ='faz other' } , 341{ label ='bar' , filterText ='bar' } , 342} , { 343{ abbr ='?.Foo' , word ='?.Foo' } , 344} ) 345end ) 346 347it ( 'matches label case sensitively when filterText is missing' , function () 348assert_completion_matches ( 'Fo' , { 349{ label ='foo' } , 350{ label ='Foo' } , 351{ label ='Faz other' } , 352{ label ='faz other' } , 353{ label ='bar' } , 354} , { 355{ abbr ='Foo' , word ='Foo' } , 356} ) 357end ) 358 359describe ( 'when ignorecase is enabled' , function () 360before_each ( function () 361exec_lua ( function () 362vim . opt . ignorecase =true 363end ) 364end ) 365after_each ( function () 366exec_lua ( function () 367vim . opt . ignorecase =false 368end ) 369end ) 370 371it ( 'matches filterText case insensitively if prefix is lowercase' , function () 372assert_completion_matches ( 'fo' , { 373{ label ='?.foo' , filterText ='foo' } , 374{ label ='?.Foo' , filterText ='Foo' } , 375{ label ='Faz other' , filterText ='Faz other' } , 376{ label ='faz other' , filterText ='faz other' } , 377{ label ='bar' , filterText ='bar' } , 378} , { 379{ abbr ='?.Foo' , word ='?.Foo' } , 380{ abbr ='?.foo' , word ='?.foo' } , 381} ) 382end ) 383 384it ( 385'matches label case insensitively if prefix is lowercase and filterText is missing' , 386function () 387assert_completion_matches ( 'fo' , { 388{ label ='foo' } , 389{ label ='Foo' } , 390{ label ='Faz other' } , 391{ label ='faz other' } , 392{ label ='bar' } , 393} , { 394{ abbr ='Foo' , word ='Foo' } , 395{ abbr ='foo' , word ='foo' } , 396} ) 397end 398) 399 400it ( 'matches filterText case sensitively if prefix has uppercase letters' , function () 401assert_completion_matches ( 'Fo' , { 402{ label ='foo' , filterText ='foo' } , 403{ label ='?.Foo' , filterText ='Foo' } , 404{ label ='Faz other' , filterText ='Faz other' } , 405{ label ='faz other' , filterText ='faz other' } , 406{ label ='bar' , filterText ='bar' } , 407} , { 408{ abbr ='?.Foo' , word ='?.Foo' } , 409} ) 410end ) 411 412it ( 413'matches label case sensitively if prefix has uppercase letters and filterText is missing' , 414function () 415assert_completion_matches ( 'Fo' , { 416{ label ='foo' } , 417{ label ='Foo' } , 418{ label ='Faz other' } , 419{ label ='faz other' } , 420{ label ='bar' } , 421} , { 422{ abbr ='Foo' , word ='Foo' } , 423} ) 424end 425) 426end ) 427end ) 428 429describe ( 'when ignorecase is enabled' , function () 430before_each ( function () 431exec_lua ( function () 432vim . opt . ignorecase =true 433end ) 434end ) 435after_each ( function () 436exec_lua ( function () 437vim . opt . ignorecase =false 438end ) 439end ) 440 441it ( 'matches filterText case insensitively' , function () 442assert_completion_matches ( 'Fo' , { 443{ label ='?.foo' , filterText ='foo' } , 444{ label ='?.Foo' , filterText ='Foo' } , 445{ label ='Faz other' , filterText ='Faz other' } , 446{ label ='faz other' , filterText ='faz other' } , 447{ label ='bar' , filterText ='bar' } , 448} , { 449{ abbr ='?.Foo' , word ='?.Foo' } , 450{ abbr ='?.foo' , word ='?.foo' } , 451} ) 452end ) 453 454it ( 'matches label case insensitively when filterText is missing' , function () 455assert_completion_matches ( 'Fo' , { 456{ label ='foo' } , 457{ label ='Foo' } , 458{ label ='Faz other' } , 459{ label ='faz other' } , 460{ label ='bar' } , 461} , { 462{ abbr ='Foo' , word ='Foo' } , 463{ abbr ='foo' , word ='foo' } , 464} ) 465end ) 466end ) 467 468it ( 'works on non word prefix' , function () 469local completion_list ={ 470{ label =' foo' , insertText ='->foo' , sortText ='1' } , 471{ label =' bar' , insertText ='->bar' , filterText ='bar' , sortText ='2' } , 472} 473local result =complete ( 'wp.|' , completion_list , 0 , 2 ) 474eq ( { 475{ abbr =' foo' , word ='->foo' } , 476{ abbr =' bar' , word ='->bar' } , 477} , extract_word_abbr ( result . items )) 478end ) 479 480it ( 'trims trailing newline or tab from textEdit and insertText' , function () 481local range0 ={ 482start ={ line =0 , character =0 } , 483[ 'end' ] ={ line =0 , character =0 } , 484} 485local items ={ 486{ 487detail ='ansible.builtin' , 488filterText ='lineinfile ansible.builtin.lineinfile builtin ansible' , 489kind =7 , 490label ='ansible.builtin.lineinfile' , 491sortText ='2_ansible.builtin.lineinfile' , 492textEdit ={ 493newText ='ansible.builtin.lineinfile:\n ' , 494range =range0 , 495} , 496} , 497{ 498kind =7 , 499label ='ansible.builtin.copy' , 500sortText ='3_ansible.builtin.copy' , 501insertText ='ansible.builtin.copy:\n ' , 502} , 503} 504eq ( { 505{ abbr ='ansible.builtin.lineinfile' , word ='ansible.builtin.lineinfile:' } , 506{ abbr ='ansible.builtin.copy' , word ='ansible.builtin.copy:' } , 507} , extract_word_abbr ( complete ( '|' , items ). items )) 508end ) 509 510it ( 'handles multiword textEdits' , function () 511local range0 ={ 512start ={ line =0 , character =0 } , 513[ 'end' ] ={ line =0 , character =0 } , 514} 515local items ={ 516{ 517detail ='abc' , 518filterText ='abc' , 519kind =7 , 520label ='abc' , 521sortText ='abc' , 522textEdit ={ 523newText ='abc: Abc' , 524range =range0 , 525} , 526} , 527} 528eq ( { { abbr ='abc' , word ='abc: Abc' } } , extract_word_abbr ( complete ( '|' , items ). items )) 529end ) 530 531it ( 'prefers wordlike components for snippets' , function () 532-- There are two goals here: 533-- 534-- 1. The `word` should match what the user started typing, so that vim.fn.complete() doesn't 535-- filter it away, preventing snippet expansion 536-- 537-- For example, if they type `items@ins`, luals returns `table.insert(items, $0)` as 538-- textEdit.newText and `insert` as label. 539-- There would be no prefix match if textEdit.newText is used as `word` 540-- 541-- 2. If users do not expand a snippet, but continue typing, they should see a somewhat reasonable 542-- `word` getting inserted. 543-- 544-- For example in: 545-- 546-- insertText: "testSuites ${1:Env}" 547-- label: "testSuites" 548-- 549-- "testSuites" should have priority as `word`, as long as the full snippet gets expanded on accept (<c-y>) 550local range0 ={ 551start ={ line =0 , character =0 } , 552[ 'end' ] ={ line =0 , character =0 } , 553} 554local completion_list ={ 555-- luals postfix snippet (typed text: items@ins|) 556{ 557label ='insert' , 558insertTextFormat =2 , 559textEdit ={ 560newText ='table.insert(items, $0)' , 561range =range0 , 562} , 563} , 564-- eclipse.jdt.ls `new` snippet 565{ 566label ='new' , 567insertTextFormat =2 , 568textEdit ={ 569newText ='${1:Object} ${2:foo} = new ${1}(${3});\n${0}' , 570range =range0 , 571} , 572textEditText ='${1:Object} ${2:foo} = new ${1}(${3});\n${0}' , 573} , 574-- eclipse.jdt.ls `List.copyO` function call completion 575{ 576label ='copyOf(Collection<? extends E> coll) : List<E>' , 577insertTextFormat =2 , 578insertText ='copyOf' , 579textEdit ={ 580newText ='copyOf(${1:coll})' , 581range =range0 , 582} , 583} , 584-- luals for snippet 585{ 586insertText ='for ${1:index}, ${2:value} in ipairs(${3:t}) do\n\t$0\nend' , 587insertTextFormat =2 , 588kind =15 , 589label ='for .. ipairs' , 590} , 591} 592local expected ={ 593{ abbr ='copyOf(Collection<? extends E> coll) : List<E>' , word ='copyOf' } , 594{ abbr ='for .. ipairs' , word ='for .. ipairs' } , 595{ abbr ='insert' , word ='insert' } , 596{ abbr ='new' , word ='new' } , 597} 598eq ( expected , extract_word_abbr ( complete ( '|' , completion_list ). items )) 599end ) 600 601it ( 'uses correct start boundary' , function () 602local completion_list ={ 603isIncomplete =false , 604items ={ 605{ 606filterText ='this_thread' , 607insertText ='this_thread' , 608insertTextFormat =1 , 609kind =9 , 610label =' this_thread' , 611score =1.3205767869949 , 612sortText ='4056f757this_thread' , 613textEdit ={ 614newText ='this_thread' , 615range ={ 616start ={ line =0 , character =7 } , 617[ 'end' ] ={ line =0 , character =11 } , 618} , 619} , 620} , 621} , 622} 623local expected ={ 624{ 625abbr =' this_thread' , 626dup =1 , 627empty =1 , 628icase =1 , 629info ='' , 630kind ='Module' , 631menu ='' , 632abbr_hlgroup ='' , 633word ='this_thread' , 634} , 635} 636local result =complete ( ' std::this|' , completion_list ) 637eq ( 7 , result . server_start_boundary ) 638for _ , item in ipairs ( result . items ) do 639item . user_data =nil 640end 641eq ( expected , result . items ) 642end ) 643 644it ( 'should search from start boundary to cursor position' , function () 645local completion_list ={ 646isIncomplete =false , 647items ={ 648{ 649filterText ='this_thread' , 650insertText ='this_thread' , 651insertTextFormat =1 , 652kind =9 , 653label =' this_thread' , 654score =1.3205767869949 , 655sortText ='4056f757this_thread' , 656textEdit ={ 657newText ='this_thread' , 658range ={ 659start ={ line =0 , character =7 } , 660[ 'end' ] ={ line =0 , character =11 } , 661} , 662} , 663} , 664{ 665filterText ='no_match' , 666insertText ='notthis_thread' , 667insertTextFormat =1 , 668kind =9 , 669label =' notthis_thread' , 670score =1.3205767869949 , 671sortText ='4056f757this_thread' , 672textEdit ={ 673newText ='notthis_thread' , 674range ={ 675start ={ line =0 , character =7 } , 676[ 'end' ] ={ line =0 , character =11 } , 677} , 678} , 679} , 680} , 681} 682local expected ={ 683abbr =' this_thread' , 684dup =1 , 685empty =1 , 686icase =1 , 687info ='' , 688kind ='Module' , 689menu ='' , 690abbr_hlgroup ='' , 691word ='this_thread' , 692} 693local result =complete ( ' std::this|is' , completion_list ) 694eq ( 1 , # result . items ) 695local item =result . items [ 1 ] 696item . user_data =nil 697eq ( expected , item ) 698end ) 699 700it ( 'uses defaults from itemDefaults' , function () 701--- @type lsp.CompletionList 702local completion_list ={ 703isIncomplete =false , 704itemDefaults ={ 705editRange ={ 706start ={ line =1 , character =1 } , 707[ 'end' ] ={ line =1 , character =4 } , 708} , 709insertTextFormat =2 , 710data ='foobar' , 711} , 712items ={ 713{ 714label ='hello' , 715data ='item-property-has-priority' , 716textEditText ='hello' , 717} , 718} , 719} 720local result =complete ( '|' , completion_list ) 721eq ( 1 , # result . items ) 722local item =result . items [ 1 ]. user_data . nvim . lsp . completion_item --- @type lsp.CompletionItem 723eq ( 2 , item . insertTextFormat ) 724eq ( 'item-property-has-priority' , item . data ) 725eq ( { line =1 , character =1 } , item . textEdit . range . start ) 726end ) 727 728it ( 729'uses insertText as textEdit.newText if there are editRange defaults but no textEditText' , 730function () 731--- @type lsp.CompletionList 732local completion_list ={ 733isIncomplete =false , 734itemDefaults ={ 735editRange ={ 736start ={ line =1 , character =1 } , 737[ 'end' ] ={ line =1 , character =4 } , 738} , 739insertTextFormat =2 , 740data ='foobar' , 741} , 742items ={ 743{ 744insertText ='the-insertText' , 745label ='hello' , 746data ='item-property-has-priority' , 747} , 748} , 749} 750local result =complete ( '|' , completion_list ) 751eq ( 1 , # result . items ) 752eq ( 'the-insertText' , result . items [ 1 ]. user_data . nvim . lsp . completion_item . textEdit . newText ) 753end 754) 755 756it ( 757'defaults to label as textEdit.newText if insertText or textEditText are not present' , 758function () 759local completion_list ={ 760isIncomplete =false , 761itemDefaults ={ 762editRange ={ 763start ={ line =1 , character =1 } , 764[ 'end' ] ={ line =1 , character =4 } , 765} , 766insertTextFormat =2 , 767data ='foobar' , 768} , 769items ={ 770{ 771label ='hello' , 772data ='item-property-has-priority' , 773insertText ='' , 774} , 775} , 776} 777local result =complete ( '|' , completion_list ) 778eq ( 1 , # result . items ) 779eq ( 'hello' , result . items [ 1 ]. user_data . nvim . lsp . completion_item . textEdit . newText ) 780end 781) 782 783it ( 'uses the start boundary from an insertReplace response' , function () 784local completion_list ={ 785isIncomplete =false , 786items ={ 787{ 788data ={ cacheId =1 } , 789kind =2 , 790label ='foobar' , 791sortText ='11' , 792textEdit ={ 793insert ={ 794start ={ character =4 , line =4 } , 795[ 'end' ] ={ character =8 , line =4 } , 796} , 797newText ='foobar' , 798replace ={ 799start ={ character =4 , line =4 } , 800[ 'end' ] ={ character =8 , line =4 } , 801} , 802} , 803} , 804{ 805data ={ cacheId =2 } , 806kind =2 , 807label ='bazqux' , 808sortText ='11' , 809textEdit ={ 810insert ={ 811start ={ character =4 , line =4 } , 812[ 'end' ] ={ character =5 , line =4 } , 813} , 814newText ='bazqux' , 815replace ={ 816start ={ character =4 , line =4 } , 817[ 'end' ] ={ character =5 , line =4 } , 818} , 819} , 820} , 821} , 822} 823 824local result =complete ( 'foo.f|' , completion_list ) 825eq ( 1 , # result . items ) 826eq ( 'foobar' , result . items [ 1 ]. user_data . nvim . lsp . completion_item . textEdit . newText ) 827end ) 828 829--- @param candidates lsp.CompletionList 830--- @return table<string, lsp.CompletionItem> 831local function convert ( candidates ) 832local items ={} --- @type table<string, lsp.CompletionItem> 833for _ , match in ipairs ( complete ( '|' , candidates ). items ) do 834local item =match . user_data . nvim . lsp . completion_item 835items [ item . label ] =item 836end 837return items 838end 839 840it ( 'itemDefaults are replaced by the item by default' , function () 841local items =convert ( { 842isIncomplete =false , 843itemDefaults ={ commitCharacters ={ '.' , ';' } , data ={ a =1 } } , 844items ={ 845{ label ='own' , commitCharacters ={ '(' } , data ={ b =2 } } , 846{ label ='absent' } , 847{ label ='empty' , commitCharacters ={} , data =false } , 848} , 849} ) 850eq ( { '(' } , items . own . commitCharacters ) 851eq ( { b =2 } , items . own . data ) 852eq ( { '.' , ';' } , items . absent . commitCharacters ) 853eq ( { a =1 } , items . absent . data ) 854eq ( {} , items . empty . commitCharacters ) 855eq ( false , items . empty . data ) 856end ) 857 858it ( 'applyKind=Merge merges the item with itemDefaults' , function () 859local Merge =2 860local items =convert ( { 861isIncomplete =false , 862applyKind ={ commitCharacters =Merge , data =Merge } , 863itemDefaults ={ commitCharacters ={ '.' , ';' } , data ={ a =1 , nested ={ x =1 } } } , 864items ={ 865{ label ='own' , commitCharacters ={ '(' } , data ={ a =9 , nested ={ y =2 } } } , 866{ label ='absent' } , 867} , 868} ) 869eq ( { '(' , '.' , ';' } , items . own . commitCharacters ) 870eq ( { a =9 , nested ={ y =2 } } , items . own . data ) 871eq ( { '.' , ';' } , items . absent . commitCharacters ) 872eq ( { a =1 , nested ={ x =1 } } , items . absent . data ) 873end ) 874end ) 875 876--- @param name string 877--- @param completion_result vim.lsp.CompletionResult 878--- @param opts? {trigger_chars?: string[], resolve_result?: lsp.CompletionItem|lsp.CompletionItem[], delay?: integer, cmp?: string} 879--- @return integer 880local function create_server ( name , completion_result , opts ) 881opts =opts or {} 882return exec_lua ( function () 883local server =_G . _create_server ( { 884capabilities ={ 885completionProvider ={ 886triggerCharacters =opts . trigger_chars or { '.' } , 887resolveProvider =opts . resolve_result ~= nil , 888} , 889} , 890handlers ={ 891[ 'textDocument/completion' ] =function ( _ , _ , callback ) 892if opts . delay then 893-- simulate delay in completion request, needed for some of these tests 894vim . defer_fn ( function () 895callback ( nil , completion_result ) 896end , opts . delay ) 897else 898callback ( nil , completion_result ) 899end 900end , 901[ 'completionItem/resolve' ] =function ( _ , request_item , callback ) 902if type ( opts . resolve_result ) == 'table' and not opts . resolve_result . label then 903local selected =vim . fn . complete_info ( { 'selected' } ). selected 904callback ( nil , opts . resolve_result [ selected + 1 ] or request_item ) 905else 906callback ( nil , opts . resolve_result ) 907end 908end , 909} , 910} ) 911 912local bufnr =vim . api . nvim_get_current_buf () 913vim . api . nvim_win_set_buf ( 0 , bufnr ) 914local cmp_fn 915if opts . cmp then 916cmp_fn =assert ( loadstring ( opts . cmp )) 917end 918return vim . lsp . start ( { 919name =name , 920cmd =server . cmd , 921on_attach =function ( client , bufnr0 ) 922vim . lsp . completion . enable ( true , client . id , bufnr0 , { 923autotrigger =opts . trigger_chars ~= nil , 924convert =function ( item ) 925return { abbr =item . label : gsub ( '%b()' , '' ) } 926end , 927cmp =cmp_fn , 928} ) 929end , 930} ) 931end ) 932end 933 934describe ( 'vim.lsp.completion: protocol' , function () 935before_each ( function () 936clear () 937exec_lua ( create_server_definition ) 938exec_lua ( function () 939_G . capture ={} 940--- @diagnostic disable-next-line:duplicate-set-field 941vim . fn . complete =function ( col , matches ) 942_G . capture . col =col 943_G . capture . matches =matches 944end 945end ) 946end ) 947 948local function assert_matches ( fn ) 949retry ( nil , nil , function () 950fn ( exec_lua ( 'return _G.capture.matches' )) 951end ) 952end 953 954--- @param pos [integer, integer] 955local function trigger_at_pos ( pos ) 956exec_lua ( function () 957local win =vim . api . nvim_get_current_win () 958vim . api . nvim_win_set_cursor ( win , pos ) 959vim . lsp . completion . get () 960end ) 961 962retry ( nil , nil , function () 963neq ( nil , exec_lua ( 'return _G.capture.col' )) 964end ) 965end 966 967it ( 'does not error on :checktime after client restart' , function () 968local fname ='Xtest-lsp-completion-reload' 969t . write_file ( fname , 'foo' ) 970t . finally ( function () 971os . remove ( fname ) 972end ) 973local mtime =os . time () - 10 974vim . uv . fs_utime ( fname , mtime , mtime ) 975n . command ( 'edit ' .. fname ) 976n . command ( 'set autoread' ) 977 978local client_id =create_server ( 'dummy' , { isIncomplete =false , items ={} } ) 979exec_lua ( function () 980vim . lsp . get_client_by_id ( client_id ): stop ( true ) 981vim . wait ( 1000 , function () 982return vim . lsp . get_client_by_id ( client_id ) == nil 983end ) 984end ) 985create_server ( 'dummy' , { isIncomplete =false , items ={} } ) 986 987t . write_file ( fname , 'bar' ) 988n . command ( 'checktime' ) 989eq ( { 'bar' } , n . api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 990end ) 991 992it ( 'fetches completions and shows them using complete on trigger' , function () 993create_server ( 'dummy' , { 994isIncomplete =false , 995items ={ 996{ label ='hello' } , 997{ label ='hercules' , tags ={ 1 } } , -- 1 represents Deprecated tag 998{ label ='hero' , deprecated =true } , 999} , 1000} ) 1001 1002feed ( 'ih' ) 1003trigger_at_pos ( { 1 , 1 } ) 1004 1005assert_matches ( function ( matches ) 1006eq ( { 1007{ 1008abbr ='hello' , 1009dup =1 , 1010empty =1 , 1011icase =1 , 1012info ='' , 1013kind ='Unknown' , 1014menu ='' , 1015abbr_hlgroup ='' , 1016user_data ={ 1017nvim ={ 1018lsp ={ 1019client_id =1 , 1020completion_item ={ label ='hello' } , 1021info_kind ='markdown' , 1022completion_item_needs_resolving =false , 1023} , 1024} , 1025} , 1026word ='hello' , 1027} , 1028{ 1029abbr ='hercules' , 1030dup =1 , 1031empty =1 , 1032icase =1 , 1033info ='' , 1034kind ='Unknown' , 1035menu ='' , 1036abbr_hlgroup ='DiagnosticDeprecated' , 1037user_data ={ 1038nvim ={ 1039lsp ={ 1040client_id =1 , 1041completion_item ={ label ='hercules' , tags ={ 1 } } , 1042info_kind ='markdown' , 1043completion_item_needs_resolving =false , 1044} , 1045} , 1046} , 1047word ='hercules' , 1048} , 1049{ 1050abbr ='hero' , 1051dup =1 , 1052empty =1 , 1053icase =1 , 1054info ='' , 1055kind ='Unknown' , 1056menu ='' , 1057abbr_hlgroup ='DiagnosticDeprecated' , 1058user_data ={ 1059nvim ={ 1060lsp ={ 1061client_id =1 , 1062completion_item ={ label ='hero' , deprecated =true } , 1063info_kind ='markdown' , 1064completion_item_needs_resolving =false , 1065} , 1066} , 1067} , 1068word ='hero' , 1069} , 1070} , matches ) 1071end ) 1072end ) 1073 1074it ( 'merges results from multiple clients' , function () 1075create_server ( 'dummy1' , { isIncomplete =false , items ={ { label ='hello' } } } ) 1076create_server ( 'dummy2' , { isIncomplete =false , items ={ { label ='hallo' } } } ) 1077create_server ( 'dummy3' , { { label ='hallo' } } ) 1078 1079feed ( 'ih' ) 1080trigger_at_pos ( { 1 , 1 } ) 1081 1082assert_matches ( function ( matches ) 1083eq ( 3 , # matches ) 1084eq ( 'hello' , matches [ 1 ]. word ) 1085eq ( 'hallo' , matches [ 2 ]. word ) 1086eq ( 'hallo' , matches [ 3 ]. word ) 1087end ) 1088end ) 1089 1090it ( 'insert char triggers clients matching trigger characters' , function () 1091create_server ( 'dummy1' , { 1092isIncomplete =false , 1093items ={ { label ='hello' } } , 1094} , { trigger_chars ={ 'e' } } ) 1095create_server ( 'dummy2' , { 1096isIncomplete =false , 1097items ={ { label ='hallo' } } , 1098} , { trigger_chars ={ 'h' } } ) 1099 1100feed ( 'h' ) 1101exec_lua ( function () 1102vim . v . char ='h' 1103vim . cmd . startinsert () 1104vim . api . nvim_exec_autocmds ( 'InsertCharPre' , {} ) 1105end ) 1106 1107assert_matches ( function ( matches ) 1108eq ( 1 , # matches ) 1109eq ( 'hallo' , matches [ 1 ]. word ) 1110end ) 1111end ) 1112 1113it ( 'reads completionProvider from a dynamic registration' , function () 1114exec_lua ( function () 1115local server =_G . _create_server ( { 1116capabilities ={} , 1117handlers ={ 1118[ 'textDocument/completion' ] =function ( _ , _ , callback ) 1119callback ( nil , { isIncomplete =false , items ={ { label ='hello' } } } ) 1120end , 1121} , 1122} ) 1123local client_id =assert ( vim . lsp . start ( { 1124name ='dummy' , 1125cmd =server . cmd , 1126capabilities ={ 1127textDocument ={ completion ={ dynamicRegistration =true } } , 1128} , 1129} )) 1130assert ( vim . lsp . get_client_by_id ( client_id )): _register ( { 1131{ 1132id ='nvim.test.completion' , 1133method ='textDocument/completion' , 1134registerOptions ={ 1135triggerCharacters ={ '.' } , 1136resolveProvider =true , 1137allCommitCharacters ={ ';' } , 1138} , 1139} , 1140} ) 1141vim . lsp . completion . enable ( true , client_id , 0 , { autotrigger =true } ) 1142end ) 1143 1144feed ( 'i.' ) 1145 1146assert_matches ( function ( matches ) 1147eq ( 1 , # matches ) 1148eq ( 'hello' , matches [ 1 ]. word ) 1149eq ( ';' , matches [ 1 ]. commit_chars ) 1150eq ( true , matches [ 1 ]. user_data . nvim . lsp . completion_item_needs_resolving ) 1151end ) 1152end ) 1153 1154it ( 'treats 2-triggers-at-once as "last char wins"' , function () 1155create_server ( 'dummy1' , { 1156isIncomplete =false , 1157items ={ { label ='first' } } , 1158} , { trigger_chars ={ '-' } } ) 1159create_server ( 'dummy2' , { 1160isIncomplete =false , 1161items ={ { label ='second' } } , 1162} , { trigger_chars ={ '>' } } ) 1163 1164feed ( 'i->' ) 1165 1166assert_matches ( function ( matches ) 1167eq ( 1 , # matches ) 1168eq ( 'second' , matches [ 1 ]. word ) 1169end ) 1170end ) 1171 1172it ( 'executes commands' , function () 1173local completion_list ={ 1174isIncomplete =false , 1175items ={ 1176{ 1177label ='hello' , 1178command ={ arguments ={ '1' , '0' } , command ='dummy' , title ='' } , 1179} , 1180} , 1181} 1182local client_id =create_server ( 'dummy' , completion_list ) 1183 1184exec_lua ( function () 1185_G . called =false 1186local client =assert ( vim . lsp . get_client_by_id ( client_id )) 1187client . commands . dummy =function () 1188_G . called =true 1189end 1190end ) 1191 1192feed ( 'ih' ) 1193trigger_at_pos ( { 1 , 1 } ) 1194 1195local item =completion_list . items [ 1 ] 1196exec_lua ( function () 1197vim . v . completed_item ={ 1198user_data ={ 1199nvim ={ 1200lsp ={ client_id =client_id , completion_item =item } , 1201} , 1202} , 1203} 1204end ) 1205 1206feed ( '<C-x><C-o><C-y>' ) 1207 1208assert_matches ( function ( matches ) 1209eq ( 1 , # matches ) 1210eq ( 'hello' , matches [ 1 ]. word ) 1211eq ( true , exec_lua ( 'return _G.called' )) 1212end ) 1213end ) 1214 1215it ( 'resolves and executes commands' , function () 1216local completion_list ={ 1217isIncomplete =false , 1218items ={ { label ='hello' } } , 1219} 1220local client_id =create_server ( 'dummy' , completion_list , { 1221resolve_result ={ 1222label ='hello' , 1223command ={ arguments ={ '1' , '0' } , command ='dummy' , title ='' } , 1224} , 1225} ) 1226exec_lua ( function () 1227_G . called =false 1228local client =assert ( vim . lsp . get_client_by_id ( client_id )) 1229client . commands . dummy =function () 1230_G . called =true 1231end 1232end ) 1233 1234feed ( 'ih' ) 1235trigger_at_pos ( { 1 , 1 } ) 1236 1237local item =completion_list . items [ 1 ] 1238exec_lua ( function () 1239vim . v . completed_item ={ 1240user_data ={ 1241nvim ={ 1242lsp ={ client_id =client_id , completion_item =item } , 1243} , 1244} , 1245} 1246end ) 1247 1248feed ( '<C-x><C-o><C-y>' ) 1249 1250assert_matches ( function ( matches ) 1251eq ( 1 , # matches ) 1252eq ( 'hello' , matches [ 1 ]. word ) 1253eq ( true , exec_lua ( 'return _G.called' )) 1254end ) 1255end ) 1256 1257it ( 'enable(…,{convert=fn}) custom word/abbr format' , function () 1258create_server ( 'dummy' , { 1259isIncomplete =false , 1260items ={ { label ='foo(bar)' } } , 1261} ) 1262 1263feed ( 'ifo' ) 1264trigger_at_pos ( { 1 , 1 } ) 1265assert_matches ( function ( matches ) 1266eq ( 'foo' , matches [ 1 ]. abbr ) 1267end ) 1268end ) 1269 1270it ( 'enable(…,{cmp=fn}) custom sort order' , function () 1271create_server ( 'dummy' , { 1272isIncomplete =false , 1273items ={ 1274{ label ='zzz' , sortText ='a' } , 1275{ label ='aaa' , sortText ='z' } , 1276{ label ='mmm' , sortText ='m' } , 1277} , 1278} , { 1279cmp =string . dump ( function ( a , b ) 1280return a . abbr < b . abbr 1281end ), 1282} ) 1283feed ( 'i' ) 1284trigger_at_pos ( { 1 , 0 } ) 1285assert_matches ( function ( matches ) 1286eq ( 3 , # matches ) 1287eq ( 'aaa' , matches [ 1 ]. abbr ) 1288eq ( 'mmm' , matches [ 2 ]. abbr ) 1289eq ( 'zzz' , matches [ 3 ]. abbr ) 1290end ) 1291end ) 1292 1293it ( 'sends completion context when invoked' , function () 1294local params =exec_lua ( function () 1295local params 1296local server =_G . _create_server ( { 1297capabilities ={ completionProvider ={} } , 1298handlers ={ 1299[ 'textDocument/completion' ] =function ( _ , params0 , callback ) 1300params =params0 1301callback ( nil , nil ) 1302end , 1303} , 1304} ) 1305 1306local bufnr =vim . api . nvim_get_current_buf () 1307vim . api . nvim_win_set_buf ( 0 , bufnr ) 1308vim . lsp . start ( { 1309name ='dummy' , 1310cmd =server . cmd , 1311on_attach =function ( client , bufnr0 ) 1312vim . lsp . completion . enable ( true , client . id , bufnr0 ) 1313end , 1314} ) 1315 1316vim . lsp . completion . get () 1317 1318return params 1319end ) 1320 1321eq ( { triggerKind =1 } , params . context ) 1322end ) 1323 1324it ( 'sends completion context with trigger characters' , function () 1325exec_lua ( function () 1326local server =_G . _create_server ( { 1327capabilities ={ 1328completionProvider ={ triggerCharacters ={ 'h' } } , 1329} , 1330handlers ={ 1331[ 'textDocument/completion' ] =function ( _ , params , callback ) 1332_G . params =params 1333callback ( nil , { isIncomplete =false , items ={ label ='hello' } } ) 1334end , 1335} , 1336} ) 1337 1338local bufnr =vim . api . nvim_get_current_buf () 1339vim . api . nvim_win_set_buf ( 0 , bufnr ) 1340vim . lsp . start ( { 1341name ='dummy' , 1342cmd =server . cmd , 1343on_attach =function ( client , bufnr0 ) 1344vim . lsp . completion . enable ( true , client . id , bufnr0 , { autotrigger =true } ) 1345end , 1346} ) 1347end ) 1348 1349feed ( 'ih' ) 1350 1351retry ( 100 , nil , function () 1352eq ( { triggerKind =2 , triggerCharacter ='h' } , exec_lua ( 'return _G.params.context' )) 1353end ) 1354end ) 1355 1356it ( 'errors on invalid items=null in completion response #39400' , function () 1357create_server ( 'dummy' , { 1358isIncomplete =false , 1359items =vim . NIL , 1360} ) 1361feed ( 'ih' ) 1362local err =t . pcall_err ( function () 1363exec_lua ( function () 1364vim . api . nvim_win_set_cursor ( 0 , { 1 , 1 } ) 1365vim . lsp . completion . get () 1366vim . wait ( 1000 , function () 1367return false 1368end ) 1369end ) 1370end ) 1371t . matches ( 'items=null' , err ) 1372end ) 1373 1374it ( 'keeps requerying while the completion list is incomplete #40096' , function () 1375exec_lua ( function () 1376_G . contexts ={} 1377local server =_G . _create_server ( { 1378capabilities ={ 1379completionProvider ={ triggerCharacters ={ 'h' } } , 1380} , 1381handlers ={ 1382[ 'textDocument/completion' ] =function ( _ , params , callback ) 1383_G . contexts [ # _G . contexts + 1 ] =params . context 1384callback ( nil , { isIncomplete =true , items ={ { label ='hello' } } } ) 1385end , 1386} , 1387} ) 1388local bufnr =vim . api . nvim_get_current_buf () 1389vim . api . nvim_win_set_buf ( 0 , bufnr ) 1390vim . lsp . start ( { 1391name ='dummy' , 1392cmd =server . cmd , 1393on_attach =function ( client , bufnr0 ) 1394vim . lsp . completion . enable ( true , client . id , bufnr0 , { autotrigger =true } ) 1395end , 1396} ) 1397end ) 1398feed ( 'ih' ) 1399assert_matches ( function ( matches ) 1400eq ( 'hello' , matches [ 1 ]. word ) 1401end ) 1402eq ( { triggerKind =2 , triggerCharacter ='h' } , exec_lua ( 'return _G.contexts[1]' )) 1403 1404exec_lua ( '_G.capture = {}' ) 1405feed ( 'e' ) 1406assert_matches ( function ( matches ) 1407eq ( 'hello' , matches [ 1 ]. word ) 1408end ) 1409eq ( { triggerKind =3 } , exec_lua ( 'return _G.contexts[2]' )) 1410end ) 1411end ) 1412 1413describe ( 'vim.lsp.completion: integration' , function () 1414before_each ( function () 1415clear () 1416exec_lua ( create_server_definition ) 1417exec_lua ( function () 1418vim . fn . complete =vim . schedule_wrap ( vim . fn . complete ) 1419end ) 1420end ) 1421 1422it ( 'puts cursor at the end of completed word' , function () 1423local completion_list ={ 1424isIncomplete =false , 1425items ={ 1426{ 1427label ='hello' , 1428insertText ='${1:hello} friends' , 1429insertTextFormat =2 , 1430} , 1431} , 1432} 1433exec_lua ( function () 1434vim . o . completeopt ='menuone,noselect' 1435end ) 1436local client_id =create_server ( 'dummy' , completion_list ) 1437feed ( 'i world<esc>0ih<c-x><c-o>' ) 1438wait_for_pum () 1439feed ( '<C-n><C-y>' ) 1440eq ( 1441{ true , { 'hello friends world' } } , 1442exec_lua ( function () 1443return { 1444vim . snippet . active ( { direction =1 } ), 1445vim . api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ), 1446} 1447end ) 1448) 1449exec_lua ( function () 1450vim . snippet . jump ( 1 ) 1451end ) 1452eq ( 1453# 'hello friends' , 1454exec_lua ( function () 1455return vim . api . nvim_win_get_cursor ( 0 )[ 2 ] 1456end ) 1457) 1458assert_cleanup_after_detach ( client_id ) 1459end ) 1460 1461it ( 'expands a snippet before a slow resolve answers' , function () 1462exec_lua ( function () 1463vim . o . completeopt ='menuone,noselect' 1464local server =_G . _create_server ( { 1465capabilities ={ completionProvider ={ resolveProvider =true } } , 1466handlers ={ 1467[ 'textDocument/completion' ] =function ( _ , _ , callback ) 1468callback ( nil , { 1469isIncomplete =false , 1470items ={ { label ='hello' , insertText ='hello($0)' , insertTextFormat =2 } } , 1471} ) 1472end , 1473[ 'completionItem/resolve' ] =function ( _ , item , callback ) 1474vim . defer_fn ( function () 1475callback ( nil , item ) 1476end , 100 ) 1477end , 1478} , 1479} ) 1480local client_id =assert ( vim . lsp . start ( { name ='dummy' , cmd =server . cmd } )) 1481vim . lsp . completion . enable ( true , client_id , 0 ) 1482end ) 1483feed ( 'S<C-x><C-o>' ) 1484wait_for_pum () 1485feed ( '<C-n><C-y>x' ) 1486eq ( { 'hello(x)' } , n . api . nvim_buf_get_lines ( 0 , 0 , - 1 , true )) 1487end ) 1488 1489it ( 'clear multiple-lines word' , function () 1490local completion_list ={ 1491isIncomplete =false , 1492items ={ 1493{ 1494label ='then...end' , 1495sortText ='0001' , 1496insertText ='then\n\t$0\nend' , 1497kind =15 , 1498insertTextFormat =2 , 1499} , 1500} , 1501} 1502exec_lua ( function () 1503vim . o . completeopt ='menuone,noselect' 1504end ) 1505local client_id =create_server ( 'dummy' , completion_list ) 1506feed ( 'Sif true <C-X><C-O>' ) 1507wait_for_pum () 1508feed ( '<C-n><C-y>' ) 1509eq ( 1510{ false , { 'if true then' , '\t' , 'end' } } , 1511exec_lua ( function () 1512return { 1513vim . snippet . active ( { direction =1 } ), 1514vim . api . nvim_buf_get_lines ( 0 , 0 , - 1 , true ), 1515} 1516end ) 1517) 1518assert_cleanup_after_detach ( client_id ) 1519end ) 1520 1521it ( 'prepends prefix for items with different start positions' , function () 1522local completion_list ={ 1523isIncomplete =false , 1524items ={ 1525{ 1526label ='div.foo' , 1527insertTextFormat =2 , 1528textEdit ={ 1529newText ='<div class="foo">$0</div>' , 1530range ={ start ={ line =0 , character =0 } , [ 'end' ] ={ line =0 , character =7 } } , 1531} , 1532} , 1533{ 1534label ='foobar' , 1535insertTextFormat =1 , 1536} , 1537} , 1538} 1539exec_lua ( function () 1540vim . o . completeopt ='menu,menuone,noinsert' 1541end ) 1542local client_id =create_server ( 'dummy' , completion_list ) 1543feed ( 'Adiv.foo<C-x><C-O>' ) 1544wait_for_pum () 1545feed ( '<C-Y>' ) 1546eq ( '<div class="foo"></div>' , n . api . nvim_get_current_line ()) 1547eq ( { 1 , 17 } , n . api . nvim_win_get_cursor ( 0 )) 1548 1549feed ( '<Esc>ccdiv.foo<C-x><C-O>' ) 1550wait_for_pum () 1551feed ( '<C-N><C-Y>' ) 1552eq ( 'div.foobar' , n . api . nvim_get_current_line ()) 1553 1554assert_cleanup_after_detach ( client_id ) 1555end ) 1556 1557it ( 'does not empty server start boundary' , function () 1558local completion_list ={ 1559isIncomplete =false , 1560items ={ 1561{ 1562label ='div.foo' , 1563insertTextFormat =2 , 1564textEdit ={ 1565newText ='<div class="foo">$0</div>' , 1566range ={ 1567start ={ line =0 , character =0 } , 1568[ 'end' ] ={ line =0 , character =7 } , 1569} , 1570} , 1571} , 1572} , 1573} 1574local completion_list2 ={ 1575isIncomplete =false , 1576items ={ { insertTextFormat =1 , label ='foo' } } , 1577} 1578exec_lua ( function () 1579vim . o . completeopt ='menu,menuone,noinsert' 1580end ) 1581create_server ( 'dummy' , completion_list ) 1582create_server ( 'dummy2' , completion_list2 ) 1583create_server ( 'dummy3' , { isIncomplete =false , items ={} } ) 1584feed ( 'Adiv.foo<C-x><C-O>' ) 1585wait_for_pum () 1586feed ( '<C-Y>' ) 1587eq ( '<div class="foo"></div>' , n . api . nvim_get_current_line ()) 1588eq ( { 1 , 17 } , n . api . nvim_win_get_cursor ( 0 )) 1589end ) 1590 1591it ( 'sorts items when fuzzy is enabled and prefix not empty #33610' , function () 1592local completion_list ={ 1593isIncomplete =false , 1594items ={ 1595{ 1596kind =21 , 1597label ='-row-end-1' , 1598sortText ='0327' , 1599textEdit ={ 1600newText ='-row-end-1' , 1601range ={ 1602[ 'end' ] ={ character =1 , line =0 } , 1603start ={ character =0 , line =0 } , 1604} , 1605} , 1606} , 1607{ 1608kind =21 , 1609label ='w-1/2' , 1610sortText ='3052' , 1611textEdit ={ 1612newText ='w-1/2' , 1613range ={ 1614[ 'end' ] ={ character =1 , line =0 } , 1615start ={ character =0 , line =0 } , 1616} , 1617} , 1618} , 1619} , 1620} 1621exec_lua ( function () 1622vim . o . completeopt ='menuone,fuzzy' 1623end ) 1624create_server ( 'dummy' , completion_list , { trigger_chars ={ '-' } } ) 1625feed ( 'Sw-' ) 1626wait_for_pum () 1627feed ( '<C-y>' ) 1628eq ( 'w-1/2' , n . api . nvim_get_current_line ()) 1629end ) 1630 1631describe ( 'selecting an item triggers (snippet) preview' , function () 1632---@type lsp.CompletionItem[] 1633local incomplete_items ={ 1634{ 1635-- detail populated but not documentation 1636detail ='(method) nvim__id_array_1(arr: any[]): any[]' , 1637insertText ='nvim__id_array_1' , 1638insertTextFormat =1 , 1639kind =3 , 1640label ='nvim__id_array_1(arr)' , 1641sortText ='0001' , 1642} , 1643{ 1644-- documentation populated but not detail 1645documentation ={ 1646kind ='markdown' , 1647value =[[```lua\nfunction vim.api.nvim__id_array_2(arr: any[])\n -> any[]\n```]] , 1648} , 1649insertText ='nvim__id_array_2' , 1650insertTextFormat =1 , 1651kind =3 , 1652label ='nvim__id_array_2(arr)' , 1653sortText ='0002' , 1654} , 1655{ 1656insertText ='for ${1:i} = ${2:1}, ${3:10, 1} do\n\t$0\nend' , 1657insertTextFormat =2 , 1658kind =15 , 1659label ='for i = ..' , 1660sortText ='0003' , 1661} , 1662{ 1663textEdit ={ 1664newText ='for ${1:j} = ${2:1}, ${3:10, 1} do\n\t$0\nend' , 1665range ={ 1666start ={ character =0 , line =0 } , 1667[ 'end' ] ={ character =0 , line =0 } , 1668} , 1669} , 1670insertTextFormat =2 , 1671kind =15 , 1672label ='for j = ..' , 1673sortText ='0004' , 1674} , 1675{ 1676insertText ='_assert_integer(${1:x}, ${2:base?})' , 1677insertTextFormat =2 , 1678kind =3 , 1679label ='_assert_integer(x, base)' , 1680sortText ='0005' , 1681} , 1682} 1683---@type lsp.CompletionItem[] 1684local complete_items ={ 1685{ 1686-- detail not in documentation, should be prepended as code block 1687detail ='(method) nvim__id_array_1(arr: any[]): any[]' , 1688documentation ={ 1689kind ='markdown' , 1690value =[[```lua\nfunction vim.api.nvim__id_array_1(arr: any[])\n -> any[]\n```]] , 1691} , 1692insertText ='nvim__id_array_1' , 1693insertTextFormat =1 , 1694kind =3 , 1695label ='nvim__id_array_1(arr)' , 1696sortText ='0001' , 1697} , 1698{ 1699-- detail not in documentation, should be prepended as code block 1700detail ='(method) nvim__id_array_2(arr: any[]): any[]' , 1701documentation ={ 1702kind ='markdown' , 1703value =[[```lua\nfunction vim.api.nvim__id_array_2(arr: any[])\n -> any[]\n```]] , 1704} , 1705insertText ='nvim__id_array_2' , 1706insertTextFormat =1 , 1707kind =3 , 1708label ='nvim__id_array_2(arr)' , 1709sortText ='0002' , 1710} , 1711{ 1712-- snippet populated in insertText 1713insertText ='for ${1:i} = ${2:1}, ${3:10, 1} do\n\t$0\nend' , 1714insertTextFormat =2 , 1715kind =15 , 1716label ='for i = ..' , 1717sortText ='0003' , 1718} , 1719{ 1720-- snippet populated in textEdit.newText 1721textEdit ={ 1722newText ='for ${1:j} = ${2:1}, ${3:10, 1} do\n\t$0\nend' , 1723range ={ 1724start ={ character =0 , line =0 } , 1725[ 'end' ] ={ character =0 , line =0 } , 1726} , 1727} , 1728insertTextFormat =2 , 1729kind =15 , 1730label ='for j = ..' , 1731sortText ='0004' , 1732} , 1733{ 1734-- detail is in documentation, should not be duplicated 1735detail ='_assert_integer' , 1736documentation ={ 1737kind ='markdown' , 1738value =[[```lua\nmore doc for vim._assert_integer\n```]] , 1739} , 1740insertText ='_assert_integer(${1:x}, ${2:base?})' , 1741insertTextFormat =2 , 1742kind =3 , 1743label ='_assert_integer(x, base)' , 1744sortText ='0005' , 1745} , 1746} 1747 1748---@param opts {items:lsp.CompletionItem[], resolved_items:lsp.CompletionItem[]} 1749local function run_test ( opts ) 1750local screen =Screen . new ( 50 , 20 ) 1751screen : add_extra_attr_ids ( { 1752[ 100 ] ={ background =Screen . colors . Plum1 , foreground =Screen . colors . Blue } , 1753} ) 1754local completion_list ={ 1755isIncomplete =false , 1756items =opts . items , 1757} 1758exec_lua ( function () 1759vim . o . completeopt ='menuone,popup' 1760end ) 1761create_server ( 'dummy' , completion_list , { 1762resolve_result =opts . resolved_items , 1763} ) 1764 1765feed ( 'S<C-X><C-O>' ) 1766retry ( nil , nil , function () 1767local info =exec_lua ( function () 1768local data =vim . fn . complete_info ( { 'selected' } ) 1769if 1770not data . preview_winid 1771or not vim . api . nvim_win_is_valid ( data . preview_winid ) 1772or not data . preview_bufnr 1773or not vim . api . nvim_buf_is_valid ( data . preview_bufnr ) 1774then 1775error ( 'preview not ready' ) 1776end 1777return table . concat ( vim . api . nvim_buf_get_lines ( data . preview_bufnr , 0 , - 1 , false ), '\n' ) 1778end ) 1779-- item 1: detail is not in documentation, should be prepended 1780neq ( nil , info : find ( '(method) nvim__id_array_1(arr: any[]): any[]' , 1 , true )) 1781neq ( nil , info : find ( 'function vim.api.nvim__id_array_1' , 1 , true )) 1782end ) 1783screen : expect ( [[ 1784nvim__id_array_1^ | 1785{12:nvim__id_array_1 Function }{100:(method) nvim__id_array}{1: }| 1786{4:nvim__id_array_2 Function }{100:_1(arr: any[]): any[]}{4: }{1: }| 1787{4:for i = .. Snippet }{100:lua\nfunction vim.ap}{4: }{1: }| 1788{4:for j = .. Snippet }{100:i.nvim__id_array_1(arr:}{1: }| 1789{4:_assert_integer Function }{100: any[])\n -> any[]\n}{4: }{1: }| 1790{1:~ }{4: }{1: }| 1791{1:~ }|*12 1792{5:-- INSERT --} | 1793]] ) 1794feed ( '<C-N>' ) 1795screen : expect ( [[ 1796nvim__id_array_2^ | 1797{4:nvim__id_array_1 Function }{100:(method) nvim__id_array}{1: }| 1798{12:nvim__id_array_2 Function }{100:_2(arr: any[]): any[]}{4: }{1: }| 1799{4:for i = .. Snippet }{100:lua\nfunction vim.ap}{4: }{1: }| 1800{4:for j = .. Snippet }{100:i.nvim__id_array_2(arr:}{1: }| 1801{4:_assert_integer Function }{100: any[])\n -> any[]\n}{4: }{1: }| 1802{1:~ }{4: }{1: }| 1803{1:~ }|*12 1804{5:-- INSERT --} | 1805]] ) 1806feed ( '<C-N>' ) 1807screen : expect ( [[ 1808for i = ..^ | 1809{4:nvim__id_array_1 Function }{100:for i = 1, 10, 1 do}{1: }| 1810{4:nvim__id_array_2 Function }{100: }{4: }{1: }| 1811{12:for i = .. Snippet }{100:end}{4: }{1: }| 1812{4:for j = .. Snippet }{1: }| 1813{4:_assert_integer Function }{1: }| 1814{1:~ }|*13 1815{5:-- INSERT --} | 1816]] ) 1817feed ( '<C-N>' ) 1818screen : expect ( [[ 1819for j = ..^ | 1820{4:nvim__id_array_1 Function }{100:for j = 1, 10, 1 do}{1: }| 1821{4:nvim__id_array_2 Function }{100: }{4: }{1: }| 1822{4:for i = .. Snippet }{100:end}{4: }{1: }| 1823{12:for j = .. Snippet }{1: }| 1824{4:_assert_integer Function }{1: }| 1825{1:~ }|*13 1826{5:-- INSERT --} | 1827]] ) 1828feed ( '<C-N>' ) 1829retry ( nil , nil , function () 1830local info =exec_lua ( function () 1831local data =vim . fn . complete_info ( { 'selected' } ) 1832if not data . preview_bufnr or not vim . api . nvim_buf_is_valid ( data . preview_bufnr ) then 1833error ( 'preview not ready' ) 1834end 1835return table . concat ( vim . api . nvim_buf_get_lines ( data . preview_bufnr , 0 , - 1 , false ), '\n' ) 1836end ) 1837neq ( nil , info : find ( 'more doc for vim._assert_integer' , 1 , true )) 1838local _ , count =info : gsub ( '_assert_integer' , '' ) 1839-- item 3: detail '_assert_integer' is in documentation, should not be duplicated 1840eq ( 1 , count ) 1841end ) 1842screen : expect ( [[ 1843_assert_integer(x, base)^ | 1844{4:nvim__id_array_1 Function }{100:lua\nmore doc for vi}{4: }{1: }| 1845{4:nvim__id_array_2 Function }{100:m._assert_integer\n}{4: }{1: }| 1846{4:for i = .. Snippet }{1: }| 1847{4:for j = .. Snippet }{1: }| 1848{12:_assert_integer Function }{1: }| 1849{1:~ }|*13 1850{5:-- INSERT --} | 1851]] ) 1852end 1853 1854it ( 'when server supports completionItem/resolve' , function () 1855run_test ( { items =incomplete_items , resolved_items =complete_items } ) 1856end ) 1857 1858it ( 'when server does not support completionItem/resolve' , function () 1859run_test ( { items =complete_items } ) 1860end ) 1861end ) 1862 1863it ( 'omnifunc works without enable() #38252' , function () 1864local completion_list ={ 1865isIncomplete =false , 1866items ={ 1867{ label ='hello' } , 1868{ label ='hallo' } , 1869} , 1870} 1871exec_lua ( function () 1872local server =_G . _create_server ( { 1873capabilities ={ 1874completionProvider ={ 1875triggerCharacters ={ '.' } , 1876} , 1877} , 1878handlers ={ 1879[ 'textDocument/completion' ] =function ( _ , _ , callback ) 1880callback ( nil , completion_list ) 1881end , 1882} , 1883} ) 1884local bufnr =vim . api . nvim_get_current_buf () 1885local id =vim . lsp . start ( { 1886name ='dummy' , 1887cmd =server . cmd , 1888} ) 1889if id then 1890vim . lsp . buf_attach_client ( bufnr , id ) 1891vim . bo [ bufnr ]. omnifunc ='v:lua.vim.lsp.omnifunc' 1892end 1893end ) 1894feed ( 'ih<C-x><C-o>' ) 1895wait_for_pum () 1896feed ( '<C-y>' ) 1897eq ( 'hallo' , n . api . nvim_get_current_line ()) 1898end ) 1899 1900it ( 'CompletionItem.preselect' , function () 1901local completion_list ={ 1902isIncomplete =false , 1903items ={ 1904{ label ='aaa' } , 1905{ label ='zzz' , preselect =true } , 1906{ label ='mmm' } , 1907} , 1908} 1909exec_lua ( function () 1910vim . o . completeopt ='menuone,noselect,preselect' 1911end ) 1912create_server ( 'dummy' , completion_list ) 1913feed ( 'i<C-x><C-o>' ) 1914wait_for_pum () 1915eq ( 19162 , 1917exec_lua ( function () 1918return vim . fn . complete_info ( { 'selected' } ). selected 1919end ) 1920) 1921end ) 1922 1923it ( 'support commitCharacters' , function () 1924n . command ( 'set completeopt=menuone,menu,noinsert' ) 1925-- from typescript-language-server 1926local completion_list ={ 1927isIncomplete =false , 1928items ={ 1929{ 1930-- Only whole characters are kept: '\0' and '\169x' are dropped, '=>' becomes '='. 1931commitCharacters ={ '.' , ',' , ';' , '\0' , '(' , '=>' , '\169x' } , 1932data ={ 1933cacheId =1 , 1934} , 1935filterText ='.bar' , 1936kind =2 , 1937label ='bar' , 1938sortText ='11' , 1939textEdit ={ 1940newText ='.bar' , 1941range ={ 1942[ 'end' ] ={ 1943character =2 , 1944line =0 , 1945} , 1946start ={ 1947character =1 , 1948line =0 , 1949} , 1950} , 1951} , 1952} , 1953} , 1954} 1955create_server ( 'dummy' , completion_list , { trigger_chars ={ '.' } } ) 1956feed ( 'Sf.' ) 1957wait_for_pum () 1958feed ( '(' ) 1959eq ( 'f.bar(' , n . api . nvim_get_current_line ()) 1960 1961n . command ( 'set completeopt+=noselect' ) 1962feed ( '<ESC>Sf.' ) 1963wait_for_pum () 1964feed ( '(' ) 1965eq ( 'f.(' , n . api . nvim_get_current_line ()) 1966 1967-- Test that a dropped fragment did not leak: 'x' must not commit, '=' must. 1968n . command ( 'set completeopt=menuone,menu,noinsert' ) 1969feed ( '<ESC>Sf.' ) 1970wait_for_pum () 1971feed ( 'x' ) 1972eq ( 'f.x' , n . api . nvim_get_current_line ()) 1973 1974feed ( '<ESC>Sf.' ) 1975wait_for_pum () 1976feed ( '=' ) 1977eq ( 'f.bar=' , n . api . nvim_get_current_line ()) 1978end ) 1979end ) 1980 1981describe ( "vim.lsp.completion: omnifunc + 'autocomplete'" , function () 1982before_each ( function () 1983clear () 1984exec_lua ( create_server_definition ) 1985exec_lua ( function () 1986-- enable buffer and omnifunc autocompletion 1987-- omnifunc will be the lsp omnifunc 1988vim . o . complete ='.,o' 1989vim . o . autocomplete =true 1990end ) 1991 1992local completion_list ={ 1993isIncomplete =false , 1994items ={ 1995{ label ='hello' } , 1996{ label ='hallo' } , 1997} , 1998} 1999create_server ( 'dummy' , completion_list , { delay =50 } ) 2000end ) 2001 2002local function assert_matches ( expected ) 2003retry ( nil , nil , function () 2004local matches =vim . tbl_map ( function ( m ) 2005return m . word 2006end , exec_lua ( 'return vim.fn.complete_info({ "items" })' ). items ) 2007eq ( expected , matches ) 2008end ) 2009end 2010 2011it ( 'merges with other completions' , function () 2012feed ( 'ihillo<cr><esc>ih' ) 2013assert_matches ( { 'hillo' , 'hallo' , 'hello' } ) 2014end ) 2015 2016it ( 'fuzzy matches without duplication' , function () 2017-- wait for one completion request to start and then request another before 2018-- the first one finishes, then wait for both to finish 2019feed ( 'ihillo<cr>h' ) 2020vim . uv . sleep ( 1 ) 2021feed ( 'e' ) 2022 2023assert_matches ( { 'hello' } ) 2024end ) 2025end )