robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1-- Test suite for vim.range 2local t =require ( 'test.testutil' ) 3local n =require ( 'test.functional.testnvim' )() 4local describe , it , before_each =t . describe , t . it , t . before_each 5local eq =t . eq 6 7local clear =n . clear 8local exec_lua =n . exec_lua 9local insert =n . insert 10 11describe ( 'vim.range' , function () 12before_each ( clear ) 13 14it ( 'creates a range' , function () 15local range , buf =exec_lua ( function () 16local buf =vim . api . nvim_create_buf ( false , true ) 17return vim . range ( buf , 3 , 5 , 4 , 6 ), buf 18end ) 19eq ( 3 , range [ 1 ]) 20eq ( 5 , range [ 2 ]) 21eq ( 4 , range [ 3 ]) 22eq ( 6 , range [ 4 ]) 23eq ( buf , range [ 5 ]) 24end ) 25 26it ( 'creates a range with buf=0' , function () 27local range , buf =exec_lua ( function () 28return vim . range ( 0 , 3 , 5 , 4 , 6 ), vim . api . nvim_get_current_buf () 29end ) 30eq ( 3 , range [ 1 ]) 31eq ( 5 , range [ 2 ]) 32eq ( 4 , range [ 3 ]) 33eq ( 6 , range [ 4 ]) 34eq ( buf , range [ 5 ]) 35end ) 36 37it ( 'creates a range from two positions' , function () 38local range , buf1 =exec_lua ( function () 39local buf =vim . api . nvim_create_buf ( false , true ) 40return vim . range ( vim . pos ( buf , 3 , 5 ), vim . pos ( buf , 4 , 6 )), buf 41end ) 42eq ( 3 , range [ 1 ]) 43eq ( 5 , range [ 2 ]) 44eq ( 4 , range [ 3 ]) 45eq ( 6 , range [ 4 ]) 46eq ( buf1 , range [ 5 ]) 47 48local buf2 =exec_lua ( function () 49return vim . api . nvim_create_buf ( false , true ) 50end ) 51local success =exec_lua ( function () 52return pcall ( function () 53return vim . range ( vim . pos ( buf1 , 3 , 5 ), vim . pos ( buf2 , 4 , 6 )) 54end ) 55end ) 56eq ( false , success ) 57end ) 58 59it ( 'is modifiable' , function () 60local range , buf =exec_lua ( function () 61local range =vim . range ( - 1 , 5 , 5 , 5 , 5 ) 62range . buf =0 63range . start_row =4 64range . start_col =2 65range . end_row =6 66range . end_col =8 67return range , vim . api . nvim_get_current_buf () 68end ) 69eq ( { 4 , 2 , 6 , 8 , buf } , range ) 70end ) 71 72it ( 'converts between vim.Range and lsp.Range' , function () 73local buf =exec_lua ( function () 74return vim . api . nvim_get_current_buf () 75end ) 76insert ( 'Neovim 是 Vim 的分支,专注于扩展性和可用性。' ) 77local lsp_range =exec_lua ( function () 78local range =vim . range ( buf , 0 , 10 , 0 , 36 ) 79return range : to_lsp ( 'utf-16' ) 80end ) 81eq ( { 82[ 'start' ] ={ line =0 , character =8 } , 83[ 'end' ] ={ line =0 , character =20 } , 84} , lsp_range ) 85local range =exec_lua ( function () 86return vim . range . lsp ( buf , lsp_range , 'utf-16' ) 87end ) 88eq ( { 890 , 9010 , 910 , 9236 , 93buf , 94} , range ) 95end ) 96 97it ( 'converts between inclusive mark ranges ending on multibyte characters' , function () 98insert ( '🙂' ) 99 100local range , mark_range =exec_lua ( function () 101vim . o . selection ='inclusive' 102local range =vim . range . mark ( 0 , 1 , 0 , 1 , 0 ) 103return { range [ 1 ], range [ 2 ], range [ 3 ], range [ 4 ] } , { range : to_mark () } 104end ) 105eq ( { 0 , 0 , 0 , 4 } , range ) 106eq ( { 1 , 0 , 1 , 0 } , mark_range ) 107end ) 108 109it ( 'uses the preceding line for inclusive marks ending at column zero' , function () 110insert ( 'abcdef\nx' ) 111eq ( 112{ 1 , 0 , 1 , 5 } , 113exec_lua ( function () 114vim . o . selection ='inclusive' 115return { vim . range ( 0 , 0 , 0 , 1 , 0 ): to_mark () } 116end ) 117) 118end ) 119 120it ( "converts between vim.Range and extmark on buffer's last line" , function () 121local buf =exec_lua ( function () 122return vim . api . nvim_get_current_buf () 123end ) 124insert ( 'Some text' ) 125local extmark_range ={ 126exec_lua ( function () 127local range =vim . range ( buf , 0 , 0 , 1 , 0 ) 128return range : to_extmark () 129end ), 130} 131eq ( { 0 , 0 , 0 , 9 } , extmark_range ) 132local range =exec_lua ( function () 133return vim . range . extmark ( 134buf , 135extmark_range [ 1 ], 136extmark_range [ 2 ], 137extmark_range [ 3 ], 138extmark_range [ 4 ] 139) 140end ) 141eq ( { 0 , 0 , 0 , 9 , buf } , range ) 142 143local extmark_range2 ={ 144exec_lua ( function () 145local range2 =vim . range ( buf , 0 , 0 , 0 , 9 ) 146return range2 : to_extmark () 147end ), 148} 149eq ( { 0 , 0 , 0 , 9 } , extmark_range2 ) 150local range2 =exec_lua ( function () 151return vim . range . extmark ( 152buf , 153extmark_range2 [ 1 ], 154extmark_range2 [ 2 ], 155extmark_range2 [ 3 ], 156extmark_range2 [ 4 ] 157) 158end ) 159eq ( { 0 , 0 , 0 , 9 , buf } , range2 ) 160end ) 161 162it ( 'checks whether a range contains a position' , function () 163eq ( 164true , 165exec_lua ( function () 166local buf =vim . api . nvim_create_buf ( false , true ) 167return vim . range ( buf , 0 , 0 , 1 , 5 ): has ( vim . pos ( buf , 0 , 1 )) 168end ) 169) 170end ) 171 172it ( 'a range does not contain an empty range just outside it' , function () 173eq ( 174false , 175exec_lua ( function () 176return vim . range ( 0 , 0 , 0 , 0 , 4 ): has ( vim . range ( 0 , 0 , 0 , 0 , 0 )) 177end ) 178) 179 180eq ( 181false , 182exec_lua ( function () 183return vim . range ( 0 , 0 , 0 , 0 , 4 ): has ( vim . range ( 0 , 0 , 4 , 0 , 4 )) 184end ) 185) 186end ) 187 188it ( 'an empty range contains no other range' , function () 189eq ( 190false , 191exec_lua ( function () 192return vim . range ( 0 , 1 , 0 , 1 , 0 ): has ( vim . range ( 0 , 1 , 0 , 1 , 0 )) 193end ) 194) 195eq ( 196false , 197exec_lua ( function () 198return vim . range ( 0 , 1 , 0 , 1 , 0 ): has ( vim . range ( 0 , 1 , 0 , 2 , 0 )) 199end ) 200) 201eq ( 202false , 203exec_lua ( function () 204return vim . range ( 0 , 1 , 0 , 1 , 0 ): has ( vim . range ( 0 , 0 , 0 , 1 , 0 )) 205end ) 206) 207end ) 208 209it ( 'preserves single-character intersections' , function () 210insert ( 'abc' ) 211eq ( 212{ 0 , 1 , 0 , 2 } , 213exec_lua ( function () 214local overlap =vim . range ( 0 , 0 , 0 , 0 , 2 ): intersect ( vim . range ( 0 , 0 , 1 , 0 , 3 )) 215return overlap and { overlap : to_extmark () } or nil 216end ) 217) 218end ) 219 220it ( 'an empty range intersercts with no other range' , function () 221eq ( 222nil , 223exec_lua ( function () 224return vim . range ( 0 , 1 , 0 , 1 , 0 ): intersect ( vim . range ( 0 , 1 , 0 , 1 , 0 )) 225end ) 226) 227eq ( 228nil , 229exec_lua ( function () 230return vim . range ( 0 , 1 , 0 , 1 , 0 ): intersect ( vim . range ( 0 , 1 , 0 , 2 , 0 )) 231end ) 232) 233eq ( 234nil , 235exec_lua ( function () 236return vim . range ( 0 , 1 , 0 , 1 , 0 ): intersect ( vim . range ( 0 , 0 , 0 , 1 , 0 )) 237end ) 238) 239end ) 240 241it ( 'empty range comparison semantics' , function () 242eq ( 243true , 244exec_lua ( function () 245return vim . range ( 0 , 0 , 0 , 0 , 0 ) < vim . range ( 0 , 0 , 0 , 0 , 1 ) 246end ) 247) 248 249eq ( 250true , 251exec_lua ( function () 252return vim . range ( 0 , 1 , 0 , 1 , 0 ) < vim . range ( 0 , 1 , 0 , 1 , 1 ) 253end ) 254) 255 256eq ( 257true , 258exec_lua ( function () 259return vim . range ( 0 , 1 , 1 , 1 , 1 ) > vim . range ( 0 , 1 , 0 , 1 , 1 ) 260end ) 261) 262end ) 263 264it ( '1 byte wide range is not empty' , function () 265eq ( 266false , 267exec_lua ( function () 268return vim . range ( 0 , 1 , 0 , 1 , 1 ): is_empty () 269end ) 270) 271end ) 272end )