robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local t =require ( 'test.testutil' ) 2local n =require ( 'test.functional.testnvim' )() 3local Screen =require ( 'test.functional.ui.screen' ) 4local t_lsp =require ( 'test.functional.plugin.lsp.testutil' ) 5 6local describe , it , before_each , after_each =t . describe , t . it , t . before_each , t . after_each 7local command =n . command 8local dedent =t . dedent 9local eq =t . eq 10local exec_lua =n . exec_lua 11local feed =n . feed 12local insert =n . insert 13local api =n . api 14 15local clear_notrace =t_lsp . clear_notrace 16local create_server_definition =t_lsp . create_server_definition 17 18local function create_start_server () 19function _G . _start_server ( server ) 20return vim . lsp . start ( { 21name ='dummy' , 22cmd =server . cmd , 23flags ={ 24allow_incremental_sync =false , 25debounce_text_changes =0 , -- disable debounce to make tests faster 26} , 27} ) 28end 29end 30 31before_each ( function () 32clear_notrace () 33exec_lua ( create_server_definition ) 34exec_lua ( create_start_server ) 35exec_lua ( function () 36vim . lsp . semantic_tokens . __STHighlighter . debounce =0 -- disable internal debounce to make tests faster 37end ) 38end ) 39 40after_each ( function () 41api . nvim_exec_autocmds ( 'VimLeavePre' , { modeline =false } ) 42end ) 43 44describe ( 'semantic token highlighting' , function () 45local screen --- @type test.functional.ui.screen 46before_each ( function () 47screen =Screen . new ( 40 , 16 ) 48screen : set_default_attr_ids { 49[ 1 ] ={ bold =true , foreground =Screen . colors . Blue1 } , 50[ 2 ] ={ foreground =Screen . colors . DarkCyan } , 51[ 3 ] ={ foreground =Screen . colors . SlateBlue } , 52[ 4 ] ={ bold =true , foreground =Screen . colors . SeaGreen } , 53[ 5 ] ={ foreground =tonumber ( '0x6a0dad' ) } , 54[ 6 ] ={ foreground =Screen . colors . Blue1 } , 55[ 7 ] ={ bold =true , foreground =Screen . colors . DarkCyan } , 56[ 8 ] ={ bold =true , foreground =Screen . colors . SlateBlue } , 57[ 9 ] ={ bold =true , foreground =tonumber ( '0x6a0dad' ) } , 58[ 10 ] ={ bold =true , foreground =Screen . colors . Brown } , 59[ 11 ] ={ foreground =Screen . colors . Magenta1 } , 60} 61command ( [[ hi link @lsp.type.namespace Type ]] ) 62command ( [[ hi link @lsp.type.function Special ]] ) 63command ( [[ hi link @lsp.type.comment Comment ]] ) 64command ( [[ hi @lsp.mod.declaration gui=bold ]] ) 65end ) 66 67describe ( 'general' , function () 68local text =dedent ( [[ 69#include <iostream> 70 71int main() 72{ 73int x; 74#ifdef __cplusplus 75std::cout << x << "\n"; 76#else 77printf("%d\n", x); 78#endif 79} 80}]] ) 81 82local legend =[[{ 83"tokenTypes": [ 84"variable", "variable", "parameter", "function", "method", "function", "property", "variable", "class", "interface", "enum", "enumMember", "type", "type", "unknown", "namespace", "typeParameter", "concept", "type", "macro", "comment" 85], 86"tokenModifiers": [ 87"declaration", "deprecated", "deduced", "readonly", "static", "abstract", "virtual", "dependentName", "defaultLibrary", "usedAsMutableReference", "functionScope", "classScope", "fileScope", "globalScope" 88] 89}]] 90 91local response =[[{ 92"data": [ 2, 4, 4, 3, 8193, 2, 8, 1, 1, 1025, 1, 7, 11, 19, 8192, 1, 4, 3, 15, 8448, 0, 5, 4, 0, 8448, 0, 8, 1, 1, 1024, 1, 0, 5, 20, 0, 1, 0, 22, 20, 0, 1, 0, 6, 20, 0 ], 93"resultId": "1" 94}]] 95 96local range_response =[[{ 97"data": [ 2, 4, 4, 3, 8193, 2, 8, 1, 1, 1025 ], 98"resultId": "2" 99}]] 100 101local edit_response =[[{ 102"edits": [ {"data": [ 2, 8, 1, 3, 8193, 1, 7, 11, 19, 8192, 1, 4, 3, 15, 8448, 0, 5, 4, 0, 8448, 0, 8, 1, 3, 8192 ], "deleteCount": 25, "start": 5 } ], 103"resultId": "3" 104}]] 105 106before_each ( function () 107exec_lua ( function () 108_G . server =_G . _create_server ( { 109capabilities ={ 110textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 111semanticTokensProvider ={ 112full ={ delta =true } , 113range =false , 114legend =vim . fn . json_decode ( legend ), 115} , 116} , 117handlers ={ 118[ 'textDocument/semanticTokens/full' ] =function ( _ , _ , callback ) 119callback ( nil , vim . fn . json_decode ( response )) 120end , 121[ 'textDocument/semanticTokens/full/delta' ] =function ( _ , _ , callback ) 122callback ( nil , vim . fn . json_decode ( edit_response )) 123end , 124} , 125} ) 126end ) 127end ) 128 129it ( 'buffer is highlighted when attached' , function () 130insert ( text ) 131 132exec_lua ( function () 133local bufnr =vim . api . nvim_get_current_buf () 134vim . api . nvim_win_set_buf ( 0 , bufnr ) 135vim . bo [ bufnr ]. filetype ='some-filetype' 136_G . _start_server ( _G . server ) 137end ) 138 139screen : expect { 140grid =[[ 141#include <iostream> | 142| 143int {8:main}() | 144{ | 145int {7:x}; | 146#ifdef {5:__cplusplus} | 147{4:std}::{2:cout} << {2:x} << "\n"; | 148{6:#else} | 149{6: printf("%d\n", x);} | 150{6:#endif} | 151} | 152^} | 153{1:~ }|*3 154| 155]] , 156} 157end ) 158 159it ( 'buffer is highlighted with multiline tokens' , function () 160insert ( text ) 161 162exec_lua ( function () 163_G . server2 =_G . _create_server ( { 164capabilities ={ 165textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 166semanticTokensProvider ={ 167full ={ delta =false } , 168legend =vim . fn . json_decode ( legend ), 169} , 170} , 171handlers ={ 172[ 'textDocument/semanticTokens/full' ] =function ( _ , _ , callback ) 173callback ( nil , { 174data ={ 5 , 0 , 82 , 0 , 0 } , 175resultId =1 , 176} ) 177end , 178} , 179} ) 180end , legend , response , edit_response ) 181exec_lua ( function () 182local bufnr =vim . api . nvim_get_current_buf () 183vim . api . nvim_win_set_buf ( 0 , bufnr ) 184vim . bo [ bufnr ]. fileformat ='unix' 185vim . bo [ bufnr ]. filetype ='some-filetype' 186_G . test_client_id =_G . _start_server ( _G . server2 ) 187end ) 188 189screen : expect { 190grid =[[ 191#include <iostream> | 192| 193int main() | 194{ | 195int x; | 196{2:#ifdef __cplusplus} | 197{2: std::cout << x << "\n";} | 198{2:#else} | 199{2: printf("%d\n", x);} | 200{2:#endif} | 201} | 202^} | 203{1:~ }|*3 204| 205]] , 206} 207 208-- The same token reaches four characters less far in a 'dos' buffer, because each 209-- of the four line endings it spans takes two characters instead of one. 210exec_lua ( function () 211vim . lsp . get_client_by_id ( assert ( _G . test_client_id )): stop () 212vim . bo [ vim . api . nvim_get_current_buf ()]. fileformat ='dos' 213_G . _start_server ( _G . server2 ) 214end ) 215 216screen : expect { 217grid =[[ 218#include <iostream> | 219| 220int main() | 221{ | 222int x; | 223{2:#ifdef __cplusplus} | 224{2: std::cout << x << "\n";} | 225{2:#else} | 226{2: printf("%d\n", x);} | 227{2:#e}ndif | 228} | 229^} | 230{1:~ }|*3 231| 232]] , 233} 234end ) 235 236it ( 'calls both range and full when range is supported' , function () 237insert ( text ) 238 239exec_lua ( function () 240_G . server_range =_G . _create_server ( { 241capabilities ={ 242semanticTokensProvider ={ 243full ={ delta =false } , 244range =true , 245legend =vim . fn . json_decode ( legend ), 246} , 247} , 248handlers ={ 249[ 'textDocument/semanticTokens/range' ] =function ( _ , _ , callback ) 250callback ( nil , vim . fn . json_decode ( range_response )) 251end , 252[ 'textDocument/semanticTokens/full' ] =function ( _ , _ , callback ) 253callback ( nil , vim . fn . json_decode ( response )) 254end , 255} , 256} ) 257end ) 258exec_lua ( function () 259local bufnr =vim . api . nvim_get_current_buf () 260vim . api . nvim_win_set_buf ( 0 , bufnr ) 261_G . _start_server ( _G . server_range ) 262end ) 263 264screen : expect { 265grid =[[ 266#include <iostream> | 267| 268int {8:main}() | 269{ | 270int {7:x}; | 271#ifdef {5:__cplusplus} | 272{4:std}::{2:cout} << {2:x} << "\n"; | 273{6:#else} | 274{6: printf("%d\n", x);} | 275{6:#endif} | 276} | 277^} | 278{1:~ }|*3 279| 280]] , 281} 282 283local messages =exec_lua ( 'return _G.server_range.messages' ) 284local called_range =false 285local called_full =false 286for _ , m in ipairs ( messages ) do 287if m . method == 'textDocument/semanticTokens/range' then 288called_range =true 289end 290if m . method == 'textDocument/semanticTokens/full' then 291called_full =true 292end 293end 294eq ( true , called_range ) 295eq ( true , called_full ) 296end ) 297 298it ( 'does not call range when only full is supported' , function () 299insert ( text ) 300 301exec_lua ( function () 302_G . server_full =_G . _create_server ( { 303capabilities ={ 304semanticTokensProvider ={ 305full ={ delta =false } , 306range =false , 307legend =vim . fn . json_decode ( legend ), 308} , 309} , 310handlers ={ 311[ 'textDocument/semanticTokens/full' ] =function ( _ , _ , callback ) 312callback ( nil , vim . fn . json_decode ( response )) 313end , 314[ 'textDocument/semanticTokens/range' ] =function ( _ , _ , callback ) 315callback ( nil , vim . fn . json_decode ( range_response )) 316end , 317} , 318} ) 319return _G . _start_server ( _G . server_full ) 320end ) 321 322local messages =exec_lua ( 'return _G.server_full.messages' ) 323local called_full =false 324local called_range =false 325for _ , m in ipairs ( messages ) do 326if m . method == 'textDocument/semanticTokens/full' then 327called_full =true 328end 329if m . method == 'textDocument/semanticTokens/range' then 330called_range =true 331end 332end 333eq ( true , called_full ) 334eq ( false , called_range ) 335end ) 336 337it ( 'does not call range after full request received' , function () 338insert ( text ) 339 340exec_lua ( function () 341_G . server_full =_G . _create_server ( { 342capabilities ={ 343textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 344semanticTokensProvider ={ 345full ={ delta =false } , 346range =true , 347legend =vim . fn . json_decode ( legend ), 348} , 349} , 350handlers ={ 351[ 'textDocument/semanticTokens/full' ] =function ( _ , _ , callback ) 352callback ( nil , vim . fn . json_decode ( response )) 353end , 354[ 'textDocument/semanticTokens/range' ] =function ( _ , _ , callback ) 355callback ( nil , vim . fn . json_decode ( range_response )) 356end , 357} , 358} ) 359return _G . _start_server ( _G . server_full ) 360end ) 361 362-- ensure initial semantic token requests have been sent before feeding input 363n . poke_eventloop () 364-- modify the buffer 365feed ( 'o<ESC>' ) 366 367n . poke_eventloop () 368 369local messages =exec_lua ( 'return _G.server_full.messages' ) 370local called_full =0 371local called_range =0 372for _ , m in ipairs ( messages ) do 373if m . method == 'textDocument/semanticTokens/full' then 374called_full =called_full + 1 375end 376if m . method == 'textDocument/semanticTokens/range' then 377called_range =called_range + 1 378end 379end 380eq ( 2 , called_full ) 381eq ( 1 , called_range ) 382end ) 383 384it ( 'range requests preserve highlights outside updated range' , function () 385screen : try_resize ( 40 , 6 ) 386insert ( text ) 387feed ( 'gg' ) 388 389local client_id , bufnr =exec_lua ( function () 390_G . response =[[{ 391"data": [ 2, 4, 4, 3, 8193, 2, 8, 1, 1, 1025 ] 392}]] 393_G . server2 =_G . _create_server ( { 394capabilities ={ 395textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 396semanticTokensProvider ={ 397range =true , 398legend =vim . fn . json_decode ( legend ), 399} , 400} , 401handlers ={ 402[ 'textDocument/semanticTokens/range' ] =function ( _ , _ , callback ) 403callback ( nil , vim . fn . json_decode ( _G . response )) 404end , 405} , 406} ) 407local bufnr =vim . api . nvim_get_current_buf () 408local client_id =assert ( _G . _start_server ( _G . server2 )) 409return client_id , bufnr 410end ) 411 412screen : expect { 413grid =[[ 414^#include <iostream> | 415| 416int {8:main}() | 417{ | 418int {7:x}; | 419| 420]] , 421} 422 423eq ( 4242 , 425exec_lua ( function () 426return # vim . lsp . semantic_tokens . __STHighlighter . active [ bufnr ]. client_state [ client_id ]. current_result . highlights 427end ) 428) 429 430exec_lua ( function () 431_G . response =[[{ 432"data": [ 7, 0, 5, 20, 0, 1, 0, 22, 20, 0, 1, 0, 6, 20, 0 ] 433}]] 434end ) 435 436feed ( 'G' ) 437 438screen : expect { 439grid =[[ 440{6:#else} | 441{6: printf("%d\n", x);} | 442{6:#endif} | 443} | 444^} | 445| 446]] , 447} 448 449eq ( 4505 , 451exec_lua ( function () 452return # vim . lsp . semantic_tokens . __STHighlighter . active [ bufnr ]. client_state [ client_id ]. current_result . highlights 453end ) 454) 455 456exec_lua ( function () 457_G . response =[[{ 458"data": [ 2, 4, 4, 3, 8193, 2, 8, 1, 1, 1025, 1, 7, 11, 19, 8192 ] 459}]] 460end ) 461feed ( 'ggLj0' ) 462 463screen : expect { 464grid =[[ 465| 466int {8:main}() | 467{ | 468int {7:x}; | 469^#ifdef {5:__cplusplus} | 470| 471]] , 472} 473 474eq ( 4756 , 476exec_lua ( function () 477return # vim . lsp . semantic_tokens . __STHighlighter . active [ bufnr ]. client_state [ client_id ]. current_result . highlights 478end ) 479) 480 481eq ( 482{ 483{ 484line =2 , 485end_line =2 , 486start_col =4 , 487end_col =8 , 488marked =true , 489modifiers ={ 490declaration =true , 491globalScope =true , 492} , 493type ='function' , 494} , 495{ 496line =4 , 497end_line =4 , 498start_col =8 , 499end_col =9 , 500marked =true , 501modifiers ={ 502declaration =true , 503functionScope =true , 504} , 505type ='variable' , 506} , 507{ 508line =5 , 509end_line =5 , 510start_col =7 , 511end_col =18 , 512marked =true , 513modifiers ={ 514globalScope =true , 515} , 516type ='macro' , 517} , 518{ 519line =7 , 520end_line =7 , 521start_col =0 , 522end_col =5 , 523marked =true , 524modifiers ={} , 525type ='comment' , 526} , 527{ 528line =8 , 529end_line =8 , 530start_col =0 , 531end_col =22 , 532marked =true , 533modifiers ={} , 534type ='comment' , 535} , 536{ 537line =9 , 538end_line =9 , 539start_col =0 , 540end_col =6 , 541marked =true , 542modifiers ={} , 543type ='comment' , 544} , 545} , 546exec_lua ( function () 547return vim . lsp . semantic_tokens . __STHighlighter . active [ bufnr ]. client_state [ client_id ]. current_result . highlights 548end ) 549) 550end ) 551 552it ( 'use LspTokenUpdate and highlight_token' , function () 553insert ( text ) 554 555exec_lua ( function () 556vim . api . nvim_create_autocmd ( 'LspTokenUpdate' , { 557callback =function ( ev ) 558local token =ev . data . token --- @type STTokenRange 559if token . type == 'function' and token . modifiers . declaration then 560vim . lsp . semantic_tokens . highlight_token ( token , ev . buf , ev . data . client_id , 'Macro' ) 561end 562end , 563} ) 564local bufnr =vim . api . nvim_get_current_buf () 565vim . api . nvim_win_set_buf ( 0 , bufnr ) 566_G . _start_server ( _G . server ) 567end ) 568 569screen : expect { 570grid =[[ 571#include <iostream> | 572| 573int {9:main}() | 574{ | 575int {7:x}; | 576#ifdef {5:__cplusplus} | 577{4:std}::{2:cout} << {2:x} << "\n"; | 578{6:#else} | 579{6: printf("%d\n", x);} | 580{6:#endif} | 581} | 582^} | 583{1:~ }|*3 584| 585]] , 586} 587end ) 588 589it ( 'buffer is unhighlighted when client is detached' , function () 590insert ( text ) 591 592local bufnr =n . api . nvim_get_current_buf () 593local client_id =exec_lua ( function () 594return _G . _start_server ( _G . server ) 595end ) 596 597screen : expect { 598grid =[[ 599#include <iostream> | 600| 601int {8:main}() | 602{ | 603int {7:x}; | 604#ifdef {5:__cplusplus} | 605{4:std}::{2:cout} << {2:x} << "\n"; | 606{6:#else} | 607{6: printf("%d\n", x);} | 608{6:#endif} | 609} | 610^} | 611{1:~ }|*3 612| 613]] , 614} 615 616exec_lua ( function () 617--- @diagnostic disable-next-line:duplicate-set-field 618vim . notify =function () end 619vim . lsp . buf_detach_client ( bufnr , client_id ) 620end ) 621 622screen : expect { 623grid =[[ 624#include <iostream> | 625| 626int main() | 627{ | 628int x; | 629#ifdef __cplusplus | 630std::cout << x << "\n"; | 631#else | 632printf("%d\n", x); | 633#endif | 634} | 635^} | 636{1:~ }|*3 637| 638]] , 639} 640end ) 641 642it ( 643'buffer is highlighted and unhighlighted when semantic token highlighting is enabled and disabled' , 644function () 645insert ( text ) 646 647exec_lua ( function () 648return _G . _start_server ( _G . server ) 649end ) 650 651screen : expect { 652grid =[[ 653#include <iostream> | 654| 655int {8:main}() | 656{ | 657int {7:x}; | 658#ifdef {5:__cplusplus} | 659{4:std}::{2:cout} << {2:x} << "\n"; | 660{6:#else} | 661{6: printf("%d\n", x);} | 662{6:#endif} | 663} | 664^} | 665{1:~ }|*3 666| 667]] , 668} 669 670exec_lua ( function () 671--- @diagnostic disable-next-line:duplicate-set-field 672vim . notify =function () end 673vim . lsp . semantic_tokens . enable ( false ) 674end ) 675 676screen : expect { 677grid =[[ 678#include <iostream> | 679| 680int main() | 681{ | 682int x; | 683#ifdef __cplusplus | 684std::cout << x << "\n"; | 685#else | 686printf("%d\n", x); | 687#endif | 688} | 689^} | 690{1:~ }|*3 691| 692]] , 693} 694 695exec_lua ( function () 696vim . lsp . semantic_tokens . enable ( true ) 697end ) 698 699screen : expect { 700grid =[[ 701#include <iostream> | 702| 703int {8:main}() | 704{ | 705int {7:x}; | 706#ifdef {5:__cplusplus} | 707{4:std}::{2:cout} << {2:x} << "\n"; | 708{6:#else} | 709{6: printf("%d\n", x);} | 710{6:#endif} | 711} | 712^} | 713{1:~ }|*3 714| 715]] , 716} 717end 718) 719 720it ( 'highlights start and stop when using "0" for current buffer' , function () 721insert ( text ) 722 723exec_lua ( function () 724return _G . _start_server ( _G . server ) 725end ) 726 727screen : expect { 728grid =[[ 729#include <iostream> | 730| 731int {8:main}() | 732{ | 733int {7:x}; | 734#ifdef {5:__cplusplus} | 735{4:std}::{2:cout} << {2:x} << "\n"; | 736{6:#else} | 737{6: printf("%d\n", x);} | 738{6:#endif} | 739} | 740^} | 741{1:~ }|*3 742| 743]] , 744} 745 746exec_lua ( function () 747--- @diagnostic disable-next-line:duplicate-set-field 748vim . notify =function () end 749vim . lsp . semantic_tokens . enable ( false , { bufnr =0 } ) 750end ) 751 752screen : expect { 753grid =[[ 754#include <iostream> | 755| 756int main() | 757{ | 758int x; | 759#ifdef __cplusplus | 760std::cout << x << "\n"; | 761#else | 762printf("%d\n", x); | 763#endif | 764} | 765^} | 766{1:~ }|*3 767| 768]] , 769} 770 771exec_lua ( function () 772vim . lsp . semantic_tokens . enable ( true , { bufnr =0 } ) 773end ) 774 775screen : expect { 776grid =[[ 777#include <iostream> | 778| 779int {8:main}() | 780{ | 781int {7:x}; | 782#ifdef {5:__cplusplus} | 783{4:std}::{2:cout} << {2:x} << "\n"; | 784{6:#else} | 785{6: printf("%d\n", x);} | 786{6:#endif} | 787} | 788^} | 789{1:~ }|*3 790| 791]] , 792} 793end ) 794 795it ( 'buffer is re-highlighted when force refreshed' , function () 796insert ( text ) 797 798exec_lua ( function () 799return _G . _start_server ( _G . server ) 800end ) 801 802screen : expect { 803grid =[[ 804#include <iostream> | 805| 806int {8:main}() | 807{ | 808int {7:x}; | 809#ifdef {5:__cplusplus} | 810{4:std}::{2:cout} << {2:x} << "\n"; | 811{6:#else} | 812{6: printf("%d\n", x);} | 813{6:#endif} | 814} | 815^} | 816{1:~ }|*3 817| 818]] , 819} 820 821exec_lua ( function () 822vim . lsp . semantic_tokens . force_refresh () 823end ) 824 825screen : expect { 826grid =[[ 827#include <iostream> | 828| 829int {8:main}() | 830{ | 831int {7:x}; | 832#ifdef {5:__cplusplus} | 833{4:std}::{2:cout} << {2:x} << "\n"; | 834{6:#else} | 835{6: printf("%d\n", x);} | 836{6:#endif} | 837} | 838^} | 839{1:~ }|*3 840| 841]] , 842unchanged =true , 843} 844 845local messages =exec_lua ( 'return server.messages' ) 846local token_request_count =0 847for _ , message in 848ipairs ( messages --[[@as {method:string,params:table}[] ]] ) 849do 850assert ( message . method ~= 'textDocument/semanticTokens/full/delta' , 'delta request received' ) 851if message . method == 'textDocument/semanticTokens/full' then 852token_request_count =token_request_count + 1 853end 854end 855eq ( 2 , token_request_count ) 856end ) 857 858it ( 'destroys the highlighter if the buffer is deleted' , function () 859insert ( text ) 860 861exec_lua ( function () 862return _G . _start_server ( _G . server ) 863end ) 864 865eq ( 866{} , 867exec_lua ( function () 868local bufnr =vim . api . nvim_get_current_buf () 869vim . api . nvim_buf_delete ( bufnr , { force =true } ) 870return vim . lsp . semantic_tokens . __STHighlighter . active 871end ) 872) 873end ) 874 875it ( 'updates highlights with delta request on buffer change' , function () 876insert ( text ) 877 878exec_lua ( function () 879return _G . _start_server ( _G . server ) 880end ) 881 882screen : expect { 883grid =[[ 884#include <iostream> | 885| 886int {8:main}() | 887{ | 888int {7:x}; | 889#ifdef {5:__cplusplus} | 890{4:std}::{2:cout} << {2:x} << "\n"; | 891{6:#else} | 892{6: printf("%d\n", x);} | 893{6:#endif} | 894} | 895^} | 896{1:~ }|*3 897| 898]] , 899} 900feed ( ':%s/int x/int x()/<CR>' ) 901feed ( ':noh<CR>' ) 902screen : expect { 903grid =[[ 904#include <iostream> | 905| 906int {8:main}() | 907{ | 908^int {8:x}(); | 909#ifdef {5:__cplusplus} | 910{4:std}::{2:cout} << {3:x} << "\n"; | 911{6:#else} | 912{6: printf("%d\n", x);} | 913{6:#endif} | 914} |*2 915{1:~ }|*3 916:noh | 917]] , 918} 919end ) 920 921it ( 922'opt-out: does not activate semantic token highlighting if disabled in client attach' , 923function () 924local client_id =exec_lua ( function () 925return vim . lsp . start ( { 926name ='dummy' , 927cmd =_G . server . cmd , 928--- @param client vim.lsp.Client 929on_attach =vim . schedule_wrap ( function ( client , _bufnr ) 930client . server_capabilities . semanticTokensProvider =nil 931end ), 932} ) 933end ) 934eq ( true , exec_lua ( 'return vim.lsp.buf_is_attached(0, ...)' , client_id )) 935 936insert ( text ) 937 938screen : expect { 939grid =[[ 940#include <iostream> | 941| 942int main() | 943{ | 944int x; | 945#ifdef __cplusplus | 946std::cout << x << "\n"; | 947#else | 948printf("%d\n", x); | 949#endif | 950} | 951^} | 952{1:~ }|*3 953| 954]] , 955} 956 957screen : expect { 958grid =[[ 959#include <iostream> | 960| 961int main() | 962{ | 963int x; | 964#ifdef __cplusplus | 965std::cout << x << "\n"; | 966#else | 967printf("%d\n", x); | 968#endif | 969} | 970^} | 971{1:~ }|*3 972| 973]] , 974unchanged =true , 975} 976end 977) 978 979it ( 'ignores null responses from the server' , function () 980local client_id =exec_lua ( function () 981_G . server2 =_G . _create_server ( { 982capabilities ={ 983textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 984semanticTokensProvider ={ 985legend =vim . fn . json_decode ( legend ), 986full ={ delta =false } , 987} , 988} , 989handlers ={ 990[ 'textDocument/semanticTokens/full' ] =function ( _ , _ , callback ) 991callback ( nil , nil ) 992end , 993[ 'textDocument/semanticTokens/full/delta' ] =function ( _ , _ , callback ) 994callback ( nil , nil ) 995end , 996} , 997} ) 998return _G . _start_server ( _G . server2 ) 999end ) 1000eq ( 1001true , 1002exec_lua ( function () 1003return vim . lsp . buf_is_attached ( 0 , client_id ) 1004end ) 1005) 1006 1007insert ( text ) 1008 1009screen : expect { 1010grid =[[ 1011#include <iostream> | 1012| 1013int main() | 1014{ | 1015int x; | 1016#ifdef __cplusplus | 1017std::cout << x << "\n"; | 1018#else | 1019printf("%d\n", x); | 1020#endif | 1021} | 1022^} | 1023{1:~ }|*3 1024| 1025]] , 1026} 1027end ) 1028 1029it ( 'resets active request after receiving error responses from the server' , function () 1030local error ={ code =- 32801 , message ='Content modified' } 1031exec_lua ( function () 1032_G . server2 =_G . _create_server ( { 1033capabilities ={ 1034textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 1035semanticTokensProvider ={ 1036legend =vim . fn . json_decode ( legend ), 1037full ={ delta =false } , 1038} , 1039} , 1040handlers ={ 1041-- There is same logic for handling nil responses and error responses, 1042-- so keep responses not nil. 1043-- 1044-- if an error response was not be handled, this test will hang on here. 1045--- @param callback function 1046[ 'textDocument/semanticTokens/full' ] =function ( _ , _ , callback ) 1047callback ( error , vim . fn . json_decode ( response )) 1048end , 1049--- @param callback function 1050[ 'textDocument/semanticTokens/full/delta' ] =function ( _ , _ , callback ) 1051callback ( error , vim . fn . json_decode ( response )) 1052end , 1053} , 1054} ) 1055return _G . _start_server ( _G . server2 ) 1056end ) 1057screen : expect ( [[ 1058^ | 1059{1:~ }|*14 1060| 1061]] ) 1062end ) 1063 1064it ( 'does not send delta requests if not supported by server' , function () 1065insert ( text ) 1066exec_lua ( function () 1067_G . server2 =_G . _create_server ( { 1068capabilities ={ 1069textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 1070semanticTokensProvider ={ 1071full ={ delta =false } , 1072legend =vim . fn . json_decode ( legend ), 1073} , 1074} , 1075handlers ={ 1076[ 'textDocument/semanticTokens/full' ] =function ( _ , _ , callback ) 1077callback ( nil , vim . fn . json_decode ( response )) 1078end , 1079[ 'textDocument/semanticTokens/full/delta' ] =function ( _ , _ , callback ) 1080callback ( nil , vim . fn . json_decode ( edit_response )) 1081end , 1082} , 1083} ) 1084return _G . _start_server ( _G . server2 ) 1085end ) 1086 1087screen : expect { 1088grid =[[ 1089#include <iostream> | 1090| 1091int {8:main}() | 1092{ | 1093int {7:x}; | 1094#ifdef {5:__cplusplus} | 1095{4:std}::{2:cout} << {2:x} << "\n"; | 1096{6:#else} | 1097{6: printf("%d\n", x);} | 1098{6:#endif} | 1099} | 1100^} | 1101{1:~ }|*3 1102| 1103]] , 1104} 1105feed ( ':%s/int x/int x()/<CR>' ) 1106feed ( ':noh<CR>' ) 1107 1108-- the highlights don't change because our fake server sent the exact 1109-- same result for the same method (the full request). "x" would have 1110-- changed to highlight index 3 had we sent a delta request 1111screen : expect { 1112grid =[[ 1113#include <iostream> | 1114| 1115int {8:main}() | 1116{ | 1117^int {7:x}(); | 1118#ifdef {5:__cplusplus} | 1119{4:std}::{2:cout} << {2:x} << "\n"; | 1120{6:#else} | 1121{6: printf("%d\n", x);} | 1122{6:#endif} | 1123} |*2 1124{1:~ }|*3 1125:noh | 1126]] , 1127} 1128local messages =exec_lua ( 'return server2.messages' ) 1129local token_request_count =0 1130for _ , message in 1131ipairs ( messages --[[@as {method:string,params:table}[] ]] ) 1132do 1133assert ( message . method ~= 'textDocument/semanticTokens/full/delta' , 'delta request received' ) 1134if message . method == 'textDocument/semanticTokens/full' then 1135token_request_count =token_request_count + 1 1136end 1137end 1138eq ( 2 , token_request_count ) 1139end ) 1140end ) 1141 1142describe ( 'token array decoding' , function () 1143for _ , test in ipairs ( { 1144{ 1145it ='clangd-15 on C' , 1146text =[[char* foo = "\n";]] , 1147response =[[{"data": [0, 6, 3, 0, 8193], "resultId": "1"}]] , 1148legend =[[{ 1149"tokenTypes": [ 1150"variable", "variable", "parameter", "function", "method", "function", "property", "variable", "class", "interface", "enum", "enumMember", "type", "type", "unknown", "namespace", "typeParameter", "concept", "type", "macro", "comment" 1151], 1152"tokenModifiers": [ 1153"declaration", "deprecated", "deduced", "readonly", "static", "abstract", "virtual", "dependentName", "defaultLibrary", "usedAsMutableReference", "functionScope", "classScope", "fileScope", "globalScope" 1154] 1155}]] , 1156expected ={ 1157{ 1158line =0 , 1159end_line =0 , 1160modifiers ={ declaration =true , globalScope =true } , 1161start_col =6 , 1162end_col =9 , 1163type ='variable' , 1164marked =true , 1165} , 1166} , 1167expected_screen =function () 1168screen : expect { 1169grid =[[ 1170char* {7:foo} = "\n"^; | 1171{1:~ }|*14 1172| 1173]] , 1174} 1175end , 1176} , 1177{ 1178it ='clangd-15 on C (bad response data)' , 1179text =[[char* foo = "\n";]] , 1180response =[[{"data": [0, 6, 4294967295, 0, 8193], "resultId": "1"}]] , 1181legend =[[{ 1182"tokenTypes": [ 1183"variable", "variable", "parameter", "function", "method", "function", "property", "variable", "class", "interface", "enum", "enumMember", "type", "type", "unknown", "namespace", "typeParameter", "concept", "type", "macro", "comment" 1184], 1185"tokenModifiers": [ 1186"declaration", "deprecated", "deduced", "readonly", "static", "abstract", "virtual", "dependentName", "defaultLibrary", "usedAsMutableReference", "functionScope", "classScope", "fileScope", "globalScope" 1187] 1188}]] , 1189expected ={ 1190{ 1191line =0 , 1192end_line =0 , 1193modifiers ={ declaration =true , globalScope =true } , 1194start_col =6 , 1195end_col =17 , 1196type ='variable' , 1197marked =true , 1198} , 1199} , 1200expected_screen =function () 1201screen : expect { 1202grid =[[ 1203char* {7:foo = "\n"^;} | 1204{1:~ }|*14 1205| 1206]] , 1207} 1208end , 1209} , 1210{ 1211it ='clangd-15 on C++' , 1212text =[[#include <iostream> 1213int main() 1214{ 1215#ifdef __cplusplus 1216const int x = 1; 1217std::cout << x << std::endl; 1218#else 1219comment 1220#endif 1221}]] , 1222response =[[{"data": [1, 4, 4, 3, 8193, 2, 9, 11, 19, 8192, 1, 12, 1, 1, 1033, 1, 2, 3, 15, 8448, 0, 5, 4, 0, 8448, 0, 8, 1, 1, 1032, 0, 5, 3, 15, 8448, 0, 5, 4, 3, 8448, 1, 0, 7, 20, 0, 1, 0, 11, 20, 0, 1, 0, 8, 20, 0], "resultId": "1"}]] , 1223legend =[[{ 1224"tokenTypes": [ 1225"variable", "variable", "parameter", "function", "method", "function", "property", "variable", "class", "interface", "enum", "enumMember", "type", "type", "unknown", "namespace", "typeParameter", "concept", "type", "macro", "comment" 1226], 1227"tokenModifiers": [ 1228"declaration", "deprecated", "deduced", "readonly", "static", "abstract", "virtual", "dependentName", "defaultLibrary", "usedAsMutableReference", "functionScope", "classScope", "fileScope", "globalScope" 1229] 1230}]] , 1231expected ={ 1232{ -- main 1233line =1 , 1234end_line =1 , 1235modifiers ={ declaration =true , globalScope =true } , 1236start_col =4 , 1237end_col =8 , 1238type ='function' , 1239marked =true , 1240} , 1241{ -- __cplusplus 1242line =3 , 1243end_line =3 , 1244modifiers ={ globalScope =true } , 1245start_col =9 , 1246end_col =20 , 1247type ='macro' , 1248marked =true , 1249} , 1250{ -- x 1251line =4 , 1252end_line =4 , 1253modifiers ={ declaration =true , readonly =true , functionScope =true } , 1254start_col =12 , 1255end_col =13 , 1256type ='variable' , 1257marked =true , 1258} , 1259{ -- std 1260line =5 , 1261end_line =5 , 1262modifiers ={ defaultLibrary =true , globalScope =true } , 1263start_col =2 , 1264end_col =5 , 1265type ='namespace' , 1266marked =true , 1267} , 1268{ -- cout 1269line =5 , 1270end_line =5 , 1271modifiers ={ defaultLibrary =true , globalScope =true } , 1272start_col =7 , 1273end_col =11 , 1274type ='variable' , 1275marked =true , 1276} , 1277{ -- x 1278line =5 , 1279end_line =5 , 1280modifiers ={ readonly =true , functionScope =true } , 1281start_col =15 , 1282end_col =16 , 1283type ='variable' , 1284marked =true , 1285} , 1286{ -- std 1287line =5 , 1288end_line =5 , 1289modifiers ={ defaultLibrary =true , globalScope =true } , 1290start_col =20 , 1291end_col =23 , 1292type ='namespace' , 1293marked =true , 1294} , 1295{ -- endl 1296line =5 , 1297end_line =5 , 1298modifiers ={ defaultLibrary =true , globalScope =true } , 1299start_col =25 , 1300end_col =29 , 1301type ='function' , 1302marked =true , 1303} , 1304{ -- #else comment #endif 1305line =6 , 1306end_line =6 , 1307modifiers ={} , 1308start_col =0 , 1309end_col =7 , 1310type ='comment' , 1311marked =true , 1312} , 1313{ 1314line =7 , 1315end_line =7 , 1316modifiers ={} , 1317start_col =0 , 1318end_col =11 , 1319type ='comment' , 1320marked =true , 1321} , 1322{ 1323line =8 , 1324end_line =8 , 1325modifiers ={} , 1326start_col =0 , 1327end_col =8 , 1328type ='comment' , 1329marked =true , 1330} , 1331} , 1332expected_screen =function () 1333screen : expect { 1334grid =[[ 1335#include <iostream> | 1336int {8:main}() | 1337{ | 1338#ifdef {5:__cplusplus} | 1339const int {7:x} = 1; | 1340{4:std}::{2:cout} << {2:x} << {4:std}::{3:endl}; | 1341{6: #else} | 1342{6: comment} | 1343{6: #endif} | 1344^} | 1345{1:~ }|*5 1346| 1347]] , 1348} 1349end , 1350} , 1351{ 1352it ='sumneko_lua' , 1353text =[[-- comment 1354local a = 1 1355b = "as"]] , 1356response =[[{"data": [0, 0, 10, 17, 0, 1, 6, 1, 8, 1, 1, 0, 1, 8, 8]}]] , 1357legend =[[{ 1358"tokenTypes": [ 1359"namespace", "type", "class", "enum", "interface", "struct", "typeParameter", "parameter", "variable", "property", "enumMember", "event", "function", "method", "macro", "keyword", "modifier", "comment", "string", "number", "regexp", "operator" 1360], 1361"tokenModifiers": [ 1362"declaration", "definition", "readonly", "static", "deprecated", "abstract", "async", "modification", "documentation", "defaultLibrary" 1363] 1364}]] , 1365expected ={ 1366{ 1367line =0 , 1368end_line =0 , 1369modifiers ={} , 1370start_col =0 , 1371end_col =10 , 1372type ='comment' , -- comment 1373marked =true , 1374} , 1375{ 1376line =1 , 1377end_line =1 , 1378modifiers ={ declaration =true } , -- a 1379start_col =6 , 1380end_col =7 , 1381type ='variable' , 1382marked =true , 1383} , 1384{ 1385line =2 , 1386end_line =2 , 1387modifiers ={ static =true } , -- b (global) 1388start_col =0 , 1389end_col =1 , 1390type ='variable' , 1391marked =true , 1392} , 1393} , 1394expected_screen =function () 1395screen : expect { 1396grid =[[ 1397{6:-- comment} | 1398local {7:a} = 1 | 1399{2:b} = "as^" | 1400{1:~ }|*12 1401| 1402]] , 1403} 1404end , 1405} , 1406{ 1407it ='rust-analyzer' , 1408text =[[pub fn main() { 1409println!("Hello world!"); 1410break rust; 1411/// what? 1412} 1413]] , 1414response =[[{"data": [0, 0, 3, 1, 0, 0, 4, 2, 1, 0, 0, 3, 4, 14, 524290, 0, 4, 1, 45, 0, 0, 1, 1, 45, 0, 0, 2, 1, 26, 0, 1, 4, 8, 17, 0, 0, 8, 1, 45, 0, 0, 1, 14, 2, 0, 0, 14, 1, 45, 0, 0, 1, 1, 48, 0, 1, 4, 5, 1, 8192, 0, 6, 4, 52, 0, 0, 4, 1, 48, 0, 1, 4, 9, 0, 1, 1, 0, 1, 26, 0 ], "resultId": "1"}]] , 1415 1416legend =[[{ 1417"tokenTypes": [ 1418"comment", "keyword", "string", "number", "regexp", "operator", "namespace", "type", "struct", "class", "interface", "enum", "enumMember", "typeParameter", "function", "method", "property", "macro", "variable", 1419"parameter", "angle", "arithmetic", "attribute", "attributeBracket", "bitwise", "boolean", "brace", "bracket", "builtinAttribute", "builtinType", "character", "colon", "comma", "comparison", "constParameter", "derive", 1420"dot", "escapeSequence", "formatSpecifier", "generic", "label", "lifetime", "logical", "macroBang", "operator", "parenthesis", "punctuation", "selfKeyword", "semicolon", "typeAlias", "toolModule", "union", "unresolvedReference" 1421], 1422"tokenModifiers": [ 1423"documentation", "declaration", "definition", "static", "abstract", "deprecated", "readonly", "defaultLibrary", "async", "attribute", "callable", "constant", "consuming", "controlFlow", "crateRoot", "injected", "intraDocLink", 1424"library", "mutable", "public", "reference", "trait", "unsafe" 1425] 1426}]] , 1427expected ={ 1428{ 1429line =0 , 1430end_line =0 , 1431modifiers ={} , 1432start_col =0 , 1433end_col =3 , -- pub 1434type ='keyword' , 1435marked =true , 1436} , 1437{ 1438line =0 , 1439end_line =0 , 1440modifiers ={} , 1441start_col =4 , 1442end_col =6 , -- fn 1443type ='keyword' , 1444marked =true , 1445} , 1446{ 1447line =0 , 1448end_line =0 , 1449modifiers ={ declaration =true , public =true } , 1450start_col =7 , 1451end_col =11 , -- main 1452type ='function' , 1453marked =true , 1454} , 1455{ 1456line =0 , 1457end_line =0 , 1458modifiers ={} , 1459start_col =11 , 1460end_col =12 , 1461type ='parenthesis' , 1462marked =true , 1463} , 1464{ 1465line =0 , 1466end_line =0 , 1467modifiers ={} , 1468start_col =12 , 1469end_col =13 , 1470type ='parenthesis' , 1471marked =true , 1472} , 1473{ 1474line =0 , 1475end_line =0 , 1476modifiers ={} , 1477start_col =14 , 1478end_col =15 , 1479type ='brace' , 1480marked =true , 1481} , 1482{ 1483line =1 , 1484end_line =1 , 1485modifiers ={} , 1486start_col =4 , 1487end_col =12 , 1488type ='macro' , -- println! 1489marked =true , 1490} , 1491{ 1492line =1 , 1493end_line =1 , 1494modifiers ={} , 1495start_col =12 , 1496end_col =13 , 1497type ='parenthesis' , 1498marked =true , 1499} , 1500{ 1501line =1 , 1502end_line =1 , 1503modifiers ={} , 1504start_col =13 , 1505end_col =27 , 1506type ='string' , -- "Hello world!" 1507marked =true , 1508} , 1509{ 1510line =1 , 1511end_line =1 , 1512modifiers ={} , 1513start_col =27 , 1514end_col =28 , 1515type ='parenthesis' , 1516marked =true , 1517} , 1518{ 1519line =1 , 1520end_line =1 , 1521modifiers ={} , 1522start_col =28 , 1523end_col =29 , 1524type ='semicolon' , 1525marked =true , 1526} , 1527{ 1528line =2 , 1529end_line =2 , 1530modifiers ={ controlFlow =true } , 1531start_col =4 , 1532end_col =9 , -- break 1533type ='keyword' , 1534marked =true , 1535} , 1536{ 1537line =2 , 1538end_line =2 , 1539modifiers ={} , 1540start_col =10 , 1541end_col =14 , -- rust 1542type ='unresolvedReference' , 1543marked =true , 1544} , 1545{ 1546line =2 , 1547end_line =2 , 1548modifiers ={} , 1549start_col =14 , 1550end_col =15 , 1551type ='semicolon' , 1552marked =true , 1553} , 1554{ 1555line =3 , 1556end_line =3 , 1557modifiers ={ documentation =true } , 1558start_col =4 , 1559end_col =13 , 1560type ='comment' , -- /// what? 1561marked =true , 1562} , 1563{ 1564line =4 , 1565end_line =4 , 1566modifiers ={} , 1567start_col =0 , 1568end_col =1 , 1569type ='brace' , 1570marked =true , 1571} , 1572} , 1573expected_screen =function () 1574screen : expect { 1575grid =[[ 1576{10:pub} {10:fn} {8:main}() { | 1577{5:println!}({11:"Hello world!"}); | 1578{10:break} rust; | 1579{6:/// what?} | 1580} | 1581^ | 1582{1:~ }|*9 1583| 1584]] , 1585} 1586end , 1587} , 1588} ) do 1589it ( test . it , function () 1590local client_id =exec_lua ( function ( legend , resp ) 1591_G . server =_G . _create_server ( { 1592capabilities ={ 1593textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 1594semanticTokensProvider ={ 1595full ={ delta =false } , 1596legend =vim . fn . json_decode ( legend ), 1597} , 1598} , 1599handlers ={ 1600[ 'textDocument/semanticTokens/full' ] =function ( _ , _ , callback ) 1601callback ( nil , vim . fn . json_decode ( resp )) 1602end , 1603} , 1604} ) 1605return _G . _start_server ( _G . server ) 1606end , test . legend , test . response ) 1607 1608insert ( test . text ) 1609 1610test . expected_screen () 1611 1612eq ( 1613test . expected , 1614exec_lua ( function () 1615local bufnr =vim . api . nvim_get_current_buf () 1616return vim . lsp . semantic_tokens . __STHighlighter . active [ bufnr ]. client_state [ client_id ]. current_result . highlights 1617end ) 1618) 1619end ) 1620end 1621end ) 1622 1623describe ( 'token decoding with deltas' , function () 1624for _ , test in ipairs ( { 1625{ 1626it ='semantic_tokens_delta: clangd-15 on C' , 1627legend =[[{ 1628"tokenTypes": [ 1629"variable", "variable", "parameter", "function", "method", "function", "property", "variable", "class", "interface", "enum", "enumMember", "type", "type", "unknown", "namespace", "typeParameter", "concept", "type", "macro", "comment" 1630], 1631"tokenModifiers": [ 1632"declaration", "deprecated", "deduced", "readonly", "static", "abstract", "virtual", "dependentName", "defaultLibrary", "usedAsMutableReference", "functionScope", "classScope", "fileScope", "globalScope" 1633] 1634}]] , 1635text1 =[[char* foo = "\n";]] , 1636edit =[[ggO<Esc>]] , 1637response1 =[[{"data": [0, 6, 3, 0, 8193], "resultId": "1"}]] , 1638response2 =[[{"edits": [{ "start": 0, "deleteCount": 1, "data": [1] }], "resultId": "2"}]] , 1639expected1 ={ 1640{ 1641line =0 , 1642modifiers ={ 1643declaration =true , 1644globalScope =true , 1645} , 1646start_col =6 , 1647end_line =0 , 1648end_col =9 , 1649type ='variable' , 1650marked =true , 1651} , 1652} , 1653expected2 ={ 1654{ 1655line =1 , 1656modifiers ={ 1657declaration =true , 1658globalScope =true , 1659} , 1660start_col =6 , 1661end_line =1 , 1662end_col =9 , 1663type ='variable' , 1664marked =true , 1665} , 1666} , 1667expected_screen1 =function () 1668screen : expect { 1669grid =[[ 1670char* {7:foo} = "\n"^; | 1671{1:~ }|*14 1672| 1673]] , 1674} 1675end , 1676expected_screen2 =function () 1677screen : expect { 1678grid =[[ 1679^ | 1680char* {7:foo} = "\n"; | 1681{1:~ }|*13 1682| 1683]] , 1684} 1685end , 1686} , 1687{ 1688it ='response with multiple delta edits' , 1689legend =[[{ 1690"tokenTypes": [ 1691"variable", "variable", "parameter", "function", "method", "function", "property", "variable", "class", "interface", "enum", "enumMember", "type", "type", "unknown", "namespace", "typeParameter", "concept", "type", "macro", "comment" 1692], 1693"tokenModifiers": [ 1694"declaration", "deprecated", "deduced", "readonly", "static", "abstract", "virtual", "dependentName", "defaultLibrary", "usedAsMutableReference", "functionScope", "classScope", "fileScope", "globalScope" 1695] 1696}]] , 1697text1 =dedent ( [[ 1698#include <iostream> 1699 1700int main() 1701{ 1702int x; 1703#ifdef __cplusplus 1704std::cout << x << "\n"; 1705#else 1706printf("%d\n", x); 1707#endif 1708}]] ), 1709text2 =[[#include <iostream> 1710 1711int main() 1712{ 1713int x(); 1714double y; 1715#ifdef __cplusplus 1716std::cout << x << "\n"; 1717#else 1718printf("%d\n", x); 1719#endif 1720}]] , 1721response1 =[[{ 1722"data": [ 2, 4, 4, 3, 8193, 2, 8, 1, 1, 1025, 1, 7, 11, 19, 8192, 1, 4, 3, 15, 8448, 0, 5, 4, 0, 8448, 0, 8, 1, 1, 1024, 1, 0, 5, 20, 0, 1, 0, 22, 20, 0, 1, 0, 6, 20, 0 ], 1723"resultId": 1 1724}]] , 1725response2 =[[{ 1726"edits": [ {"data": [ 2, 8, 1, 3, 8193, 1, 11, 1, 1, 1025 ], "deleteCount": 5, "start": 5}, {"data": [ 0, 8, 1, 3, 8192 ], "deleteCount": 5, "start": 25 } ], 1727"resultId":"2" 1728}]] , 1729expected1 ={ 1730{ 1731line =2 , 1732end_line =2 , 1733start_col =4 , 1734end_col =8 , 1735modifiers ={ declaration =true , globalScope =true } , 1736type ='function' , 1737marked =true , 1738} , 1739{ 1740line =4 , 1741end_line =4 , 1742start_col =8 , 1743end_col =9 , 1744modifiers ={ declaration =true , functionScope =true } , 1745type ='variable' , 1746marked =true , 1747} , 1748{ 1749line =5 , 1750end_line =5 , 1751start_col =7 , 1752end_col =18 , 1753modifiers ={ globalScope =true } , 1754type ='macro' , 1755marked =true , 1756} , 1757{ 1758line =6 , 1759end_line =6 , 1760start_col =4 , 1761end_col =7 , 1762modifiers ={ defaultLibrary =true , globalScope =true } , 1763type ='namespace' , 1764marked =true , 1765} , 1766{ 1767line =6 , 1768end_line =6 , 1769start_col =9 , 1770end_col =13 , 1771modifiers ={ defaultLibrary =true , globalScope =true } , 1772type ='variable' , 1773marked =true , 1774} , 1775{ 1776line =6 , 1777end_line =6 , 1778start_col =17 , 1779end_col =18 , 1780marked =true , 1781modifiers ={ functionScope =true } , 1782type ='variable' , 1783} , 1784{ 1785line =7 , 1786end_line =7 , 1787start_col =0 , 1788end_col =5 , 1789marked =true , 1790modifiers ={} , 1791type ='comment' , 1792} , 1793{ 1794line =8 , 1795end_line =8 , 1796end_col =22 , 1797modifiers ={} , 1798start_col =0 , 1799type ='comment' , 1800marked =true , 1801} , 1802{ 1803line =9 , 1804end_line =9 , 1805start_col =0 , 1806end_col =6 , 1807modifiers ={} , 1808type ='comment' , 1809marked =true , 1810} , 1811} , 1812expected2 ={ 1813{ 1814line =2 , 1815end_line =2 , 1816start_col =4 , 1817end_col =8 , 1818modifiers ={ declaration =true , globalScope =true } , 1819type ='function' , 1820marked =true , 1821} , 1822{ 1823line =4 , 1824end_line =4 , 1825start_col =8 , 1826end_col =9 , 1827modifiers ={ declaration =true , globalScope =true } , 1828type ='function' , 1829marked =true , 1830} , 1831{ 1832line =5 , 1833end_line =5 , 1834end_col =12 , 1835start_col =11 , 1836modifiers ={ declaration =true , functionScope =true } , 1837type ='variable' , 1838marked =true , 1839} , 1840{ 1841line =6 , 1842end_line =6 , 1843start_col =7 , 1844end_col =18 , 1845modifiers ={ globalScope =true } , 1846type ='macro' , 1847marked =true , 1848} , 1849{ 1850line =7 , 1851end_line =7 , 1852start_col =4 , 1853end_col =7 , 1854modifiers ={ defaultLibrary =true , globalScope =true } , 1855type ='namespace' , 1856marked =true , 1857} , 1858{ 1859line =7 , 1860end_line =7 , 1861start_col =9 , 1862end_col =13 , 1863modifiers ={ defaultLibrary =true , globalScope =true } , 1864type ='variable' , 1865marked =true , 1866} , 1867{ 1868line =7 , 1869end_line =7 , 1870start_col =17 , 1871end_col =18 , 1872marked =true , 1873modifiers ={ globalScope =true } , 1874type ='function' , 1875} , 1876{ 1877line =8 , 1878end_line =8 , 1879start_col =0 , 1880end_col =5 , 1881marked =true , 1882modifiers ={} , 1883type ='comment' , 1884} , 1885{ 1886line =9 , 1887end_line =9 , 1888end_col =22 , 1889modifiers ={} , 1890start_col =0 , 1891type ='comment' , 1892marked =true , 1893} , 1894{ 1895line =10 , 1896end_line =10 , 1897start_col =0 , 1898end_col =6 , 1899modifiers ={} , 1900type ='comment' , 1901marked =true , 1902} , 1903} , 1904expected_screen1 =function () 1905screen : expect { 1906grid =[[ 1907#include <iostream> | 1908| 1909int {8:main}() | 1910{ | 1911int {7:x}; | 1912#ifdef {5:__cplusplus} | 1913{4:std}::{2:cout} << {2:x} << "\n"; | 1914{6:#else} | 1915{6: printf("%d\n", x);} | 1916{6:#endif} | 1917^} | 1918{1:~ }|*4 1919| 1920]] , 1921} 1922end , 1923expected_screen2 =function () 1924screen : expect { 1925grid =[[ 1926#include <iostream> | 1927| 1928int {8:main}() | 1929{ | 1930int {8:x}(); | 1931double {7:y}; | 1932#ifdef {5:__cplusplus} | 1933{4:std}::{2:cout} << {3:x} << "\n"; | 1934{6:#else} | 1935{6: printf("%d\n", x);} | 1936{6:^#endif} | 1937} | 1938{1:~ }|*3 1939| 1940]] , 1941} 1942end , 1943} , 1944{ 1945it ='optional token_edit.data on deletion' , 1946legend =[[{ 1947"tokenTypes": [ 1948"comment", "keyword", "operator", "string", "number", "regexp", "type", "class", "interface", "enum", "enumMember", "typeParameter", "function", "method", "property", "variable", "parameter", "module", "intrinsic", "selfParameter", "clsParameter", "magicFunction", "builtinConstant", "parenthesis", "curlybrace", "bracket", "colon", "semicolon", "arrow" 1949], 1950"tokenModifiers": [ 1951"declaration", "static", "abstract", "async", "documentation", "typeHint", "typeHintComment", "readonly", "decorator", "builtin" 1952] 1953}]] , 1954text1 =[[string = "test"]] , 1955text2 =[[]] , 1956response1 =[[{"data": [0, 0, 6, 15, 1], "resultId": "1"}]] , 1957response2 =[[{"edits": [{ "start": 0, "deleteCount": 5 }], "resultId": "2"}]] , 1958expected1 ={ 1959{ 1960line =0 , 1961end_line =0 , 1962modifiers ={ 1963declaration =true , 1964} , 1965start_col =0 , 1966end_col =6 , 1967type ='variable' , 1968marked =true , 1969} , 1970} , 1971expected2 ={} , 1972expected_screen1 =function () 1973screen : expect { 1974grid =[[ 1975{7:string} = "test^" | 1976{1:~ }|*14 1977| 1978]] , 1979} 1980end , 1981expected_screen2 =function () 1982screen : expect { 1983grid =[[ 1984^ | 1985{1:~ }|*14 1986| 1987]] , 1988} 1989end , 1990} , 1991} ) do 1992it ( test . it , function () 1993local bufnr =n . api . nvim_get_current_buf () 1994insert ( test . text1 ) 1995local client_id =exec_lua ( function ( legend , resp1 , resp2 ) 1996_G . server =_G . _create_server ( { 1997capabilities ={ 1998textDocumentSync =vim . lsp . protocol . TextDocumentSyncKind . Full , 1999semanticTokensProvider ={ 2000full ={ delta =true } , 2001legend =vim . fn . json_decode ( legend ), 2002} , 2003} , 2004handlers ={ 2005[ 'textDocument/semanticTokens/full' ] =function ( _ , _ , callback ) 2006callback ( nil , vim . fn . json_decode ( resp1 )) 2007end , 2008[ 'textDocument/semanticTokens/full/delta' ] =function ( _ , _ , callback ) 2009callback ( nil , vim . fn . json_decode ( resp2 )) 2010end , 2011} , 2012} ) 2013return assert ( _G . _start_server ( _G . server )) 2014end , test . legend , test . response1 , test . response2 ) 2015 2016test . expected_screen1 () 2017 2018eq ( 2019test . expected1 , 2020exec_lua ( function () 2021return vim . lsp . semantic_tokens . __STHighlighter . active [ bufnr ]. client_state [ client_id ]. current_result . highlights 2022end ) 2023) 2024 2025if test . edit then 2026feed ( test . edit ) 2027else 2028exec_lua ( function ( text ) 2029vim . api . nvim_buf_set_lines ( bufnr , 0 , - 1 , false , vim . fn . split ( text , '\n' )) 2030end , test . text2 ) 2031end 2032 2033feed ( '<Ignore>' ) 2034test . expected_screen2 () 2035 2036eq ( 2037test . expected2 , 2038exec_lua ( function () 2039return vim . lsp . semantic_tokens . __STHighlighter . active [ bufnr ]. client_state [ client_id ]. current_result . highlights 2040end ) 2041) 2042end ) 2043end 2044end ) 2045end )