robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f907614088
master
1local n =require ( 'test.functional.testnvim' )() 2local t =require ( 'test.testutil' ) 3local Screen =require ( 'test.functional.ui.screen' ) 4local describe , it , before_each =t . describe , t . it , t . before_each 5local exec_lua =n . exec_lua 6 7describe ( 'decor perf' , function () 8before_each ( n . clear ) 9 10it ( 'can handle long lines' , function () 11Screen . new ( 100 , 101 ) 12 13local result =exec_lua ( function () 14local ephemeral_pattern ={ 15{ 0 , 4 , 'Comment' , 11 } , 16{ 0 , 3 , 'Keyword' , 12 } , 17{ 1 , 2 , 'Label' , 12 } , 18{ 0 , 1 , 'String' , 21 } , 19{ 1 , 3 , 'Function' , 21 } , 20{ 2 , 10 , 'Label' , 8 } , 21} 22 23local regular_pattern ={ 24{ 4 , 5 , 'String' , 12 } , 25{ 1 , 4 , 'Function' , 2 } , 26} 27 28for _ , list in ipairs ( { ephemeral_pattern , regular_pattern } ) do 29for _ , p in ipairs ( list ) do 30p [ 3 ] =vim . api . nvim_get_hl_id_by_name ( p [ 3 ]) 31end 32end 33 34local text =( 'abcdefghijklmnopqrstuvwxyz0123' ): rep ( 333 ) 35local line_len =# text 36vim . api . nvim_buf_set_lines ( 0 , 0 , 0 , false , { text } ) 37 38local ns =vim . api . nvim_create_namespace ( 'decor_spec.lua' ) 39vim . api . nvim_buf_clear_namespace ( 0 , ns , 0 , - 1 ) 40vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 41 42local ps , pe 43local function add_pattern ( pattern , ephemeral ) 44ps =vim . uv . hrtime () 45local i =0 46while i < line_len - 10 do 47for _ , p in ipairs ( pattern ) do 48vim . api . nvim_buf_set_extmark ( 0 , ns , 0 , i + p [ 1 ], { 49end_row =0 , 50end_col =i + p [ 2 ], 51hl_group =p [ 3 ], 52priority =p [ 4 ], 53ephemeral =ephemeral , 54} ) 55end 56i =i + 5 57end 58pe =vim . uv . hrtime () 59end 60 61vim . api . nvim_set_decoration_provider ( ns , { 62on_win =function () 63return true 64end , 65on_line =function () 66add_pattern ( ephemeral_pattern , true ) 67end , 68} ) 69 70add_pattern ( regular_pattern , false ) 71 72local total ={} 73local provider ={} 74for _ =1 , 100 do 75local tic =vim . uv . hrtime () 76vim . cmd 'redraw!' 77local toc =vim . uv . hrtime () 78table . insert ( total , toc - tic ) 79table . insert ( provider , pe - ps ) 80end 81 82return { total , provider } 83end ) 84 85local total , provider =unpack ( result ) 86table . sort ( total ) 87table . sort ( provider ) 88 89local ms =1 / 1000000 90local function fmt ( stats ) 91return string . format ( 92'min, 25%%, median, 75%%, max:\n\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms' , 93stats [ 1 ] * ms , 94stats [ 1 + math . floor ( # stats * 0.25 )] * ms , 95stats [ 1 + math . floor ( # stats * 0.5 )] * ms , 96stats [ 1 + math . floor ( # stats * 0.75 )] * ms , 97stats [ # stats ] * ms 98) 99end 100 101( '\nTotal ' .. fmt ( total ) .. '\nDecoration provider: ' .. fmt ( provider )) 102end ) 103 104it ( 'can handle full screen of highlighting' , function () 105Screen . new ( 100 , 51 ) 106 107local result =exec_lua ( function () 108local long_line ='local a={' .. ( 'a=5,' ): rep ( 22 ) .. '}' 109local lines ={} 110for _ =1 , 50 do 111table . insert ( lines , long_line ) 112end 113vim . api . nvim_buf_set_lines ( 0 , 0 , 0 , false , lines ) 114vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 115vim . treesitter . start ( 0 , 'lua' ) 116 117local total ={} 118for _ =1 , 100 do 119local tic =vim . uv . hrtime () 120vim . cmd 'redraw!' 121local toc =vim . uv . hrtime () 122table . insert ( total , toc - tic ) 123end 124 125return { total } 126end ) 127 128local total =unpack ( result ) 129table . sort ( total ) 130 131local ms =1 / 1000000 132local res =string . format ( 133'min, 25%%, median, 75%%, max:\n\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms' , 134total [ 1 ] * ms , 135total [ 1 + math . floor ( # total * 0.25 )] * ms , 136total [ 1 + math . floor ( # total * 0.5 )] * ms , 137total [ 1 + math . floor ( # total * 0.75 )] * ms , 138total [ # total ] * ms 139) 140( '\nTotal ' .. res ) 141end ) 142 143it ( 'can handle long lines with treesitter highlighting' , function () 144Screen . new ( 100 , 51 ) 145 146local result =exec_lua ( function () 147local long_line ='local a = { ' .. ( 'a = 5, ' ): rep ( 2000 ) .. '}' 148vim . api . nvim_buf_set_lines ( 0 , 0 , 0 , false , { long_line } ) 149vim . api . nvim_win_set_cursor ( 0 , { 1 , 0 } ) 150vim . treesitter . start ( 0 , 'lua' ) 151 152local total ={} 153for _ =1 , 50 do 154local tic =vim . uv . hrtime () 155vim . cmd 'redraw!' 156local toc =vim . uv . hrtime () 157table . insert ( total , toc - tic ) 158end 159 160return { total } 161end ) 162 163local total =unpack ( result ) 164table . sort ( total ) 165 166local ms =1 / 1000000 167local res =string . format ( 168'min, 25%%, median, 75%%, max:\n\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms,\t%0.1fms' , 169total [ 1 ] * ms , 170total [ 1 + math . floor ( # total * 0.25 )] * ms , 171total [ 1 + math . floor ( # total * 0.5 )] * ms , 172total [ 1 + math . floor ( # total * 0.75 )] * ms , 173total [ # total ] * ms 174) 175( '\nTotal ' .. res ) 176end ) 177end )