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' ) 4 5local describe , it , before_each =t . describe , t . it , t . before_each 6local clear =n . clear 7local exec =n . exec 8local exec_lua =n . exec_lua 9local eq =t . eq 10local fn =n . fn 11local api =n . api 12local insert =n . insert 13 14local function html_syntax_match () 15local styles = 16vim . split ( api . nvim_exec2 ( [[/<style>/+,/<\/style>/-p]] , { output =true } ). output , '\n' ) 17local attrnames ={ 18[ 'font%-weight: bold' ] ='bold' , 19[ 'text%-decoration%-line: [^;]*underline' ] ='underline' , 20[ 'font%-style: italic' ] ='italic' , 21[ 'text%-decoration%-line: [^;]*line%-through' ] ='strikethrough' , 22} 23local hls ={} 24for _ , style in ipairs ( styles ) do 25local attr ={} 26for match , attrname in pairs ( attrnames ) do 27if style : find ( match ) then 28---@type boolean 29attr [ attrname ] =true 30end 31end 32if style : find ( 'text%-decoration%-style: wavy' ) and attr . underline then 33---@type boolean 34attr . underline =nil 35attr . undercurl =true 36end 37attr . sp =style : match ( 'text%-decoration%-color: #(%x+)' ) 38if attr . sp then 39attr . sp =tonumber ( attr . sp , 16 ) 40end 41attr . bg =style : match ( 'background%-color: #(%x+)' ) 42if attr . bg then 43attr . bg =tonumber ( attr . bg , 16 ) 44end 45attr . fg =style : match ( '[^%-]color: #(%x+)' ) 46if attr . fg then 47attr . fg =tonumber ( attr . fg , 16 ) 48end 49if style : match ( '^%.(%w+)' ) then 50---@type table 51hls [ style : match ( '^%.(%w+)' )] =attr 52end 53end 54local whitelist ={ 55'fg' , 56'bg' , 57'sp' , 58--'blend', 59'bold' , 60--'standout', 61'underline' , 62'undercurl' , 63--'underdouble', 64--'underdotted', 65--'underdashed', 66'strikethrough' , 67'italic' , 68--'reverse', 69--'nocombine', 70} 71for name , attrs_old in 72pairs ( api . nvim_get_hl ( 0 , { link =true } ) --[[@as table<string,table>]] ) 73do 74---@type table 75local other =hls [ name : gsub ( '%.' , '-' ): gsub ( '@' , '-' )] 76if other then 77local attrs ={} 78for _ , attrname in ipairs ( whitelist ) do 79---@type table 80attrs [ attrname ] =attrs_old [ attrname ] 81end 82eq ( attrs , other ) 83end 84end 85return hls 86end 87 88local function html_to_extmarks () 89local buf =api . nvim_get_current_buf () 90local ns =api . nvim_create_namespace 'test-namespace' 91api . nvim_buf_clear_namespace ( buf , ns , 0 , - 1 ) 92exec 'silent! norm! ggd/^<pre>$\rddG3dk' 93local stack ={} 94exec [[set filetype=]] 95exec [[silent! %s/</¤/g]] 96exec [[silent! %s/"/"/g]] 97exec [[silent! %s/&/\&/g]] 98exec [[silent! %s/>/>/g]] 99exec [[silent! %s/</</g]] 100for _ , match in 101ipairs ( 102fn . matchbufline ( buf , [[¤span class="\([^"]\+\)">\|¤/span>]] , 1 , '$' , { submatches =true } ) --[[@as (table[])]] 103) 104do 105if match . text == '¤/span>' then 106local val =table . remove ( stack ) 107api . nvim_buf_set_extmark ( buf , ns , val . lnum - 1 , val . byteidx , { 108hl_group =val . submatches [ 1 ], 109end_row =match . lnum - 1 , 110end_col =match . byteidx , 111} ) 112else 113table . insert ( stack , match ) 114end 115end 116exec [[silent! %s/¤\/span>//g]] 117exec [[silent! %s/¤span[^>]*>//g]] 118end 119 120---@param screen test.functional.ui.screen 121---@param func function? 122local function run_tohtml_and_assert ( screen , func ) 123exec ( 'norm! ggO--;' ) 124screen : expect ( { any =vim . pesc ( '--^;' ) } ) 125exec ( 'norm! :\rh' ) 126screen : expect ( { any =vim . pesc ( '-^-;' ) } ) 127local expected =screen : get_snapshot () 128do 129( func or exec )( 'TOhtml' ) 130end 131exec ( 'only' ) 132html_syntax_match () 133html_to_extmarks () 134exec ( 'norm! gg0f;' ) 135screen : expect ( { any =vim . pesc ( '--^;' ) } ) 136exec ( 'norm! :\rh' ) 137screen : expect ( { grid =expected . grid , attr_ids =expected . attr_ids } ) 138end 139 140---@param guifont boolean 141local function test_generates_html ( guifont , expect_font ) 142insert ( [[line]] ) 143exec ( 'set termguicolors' ) 144local bg =fn . synIDattr ( fn . hlID ( 'Normal' ), 'bg#' , 'gui' ) 145local fg =fn . synIDattr ( fn . hlID ( 'Normal' ), 'fg#' , 'gui' ) 146local tmpfile =t . tmpname () 147 148exec_lua ( 149[[ 150local guifont, outfile = ... 151local html = (guifont 152and require('tohtml').tohtml(0,{title="title"}) 153or require('tohtml').tohtml(0,{title="title",font={ "dumyfont","anotherfont" }})) 154vim.fn.writefile(html, outfile) 155vim.cmd.split(outfile) 156]] , 157guifont , 158tmpfile 159) 160 161local out_file =api . nvim_buf_get_name ( api . nvim_get_current_buf ()) 162eq ( { 163'<!-- vim: set nomodeline: -->' , 164'<!DOCTYPE html>' , 165'<html>' , 166'<head>' , 167'<meta charset="UTF-8">' , 168'<title>title</title>' , 169( '<meta name="colorscheme" content="%s"></meta>' ): format ( api . nvim_get_var ( 'colors_name' )), 170'<style>' , 171( '* {font-family: %s,monospace}' ): format ( expect_font ), 172( 'body {background-color: %s; color: %s}' ): format ( bg , fg ), 173'</style>' , 174'</head>' , 175'<body style="display: flex">' , 176'<pre>' , 177'line' , 178'' , 179'</pre>' , 180'</body>' , 181'</html>' , 182} , fn . readfile ( out_file )) 183end 184 185describe ( ':TOhtml' , function () 186--- @type test.functional.ui.screen 187local screen 188before_each ( function () 189clear ( { args ={ '--clean' } } ) 190n . command ( 'packadd nvim.tohtml' ) 191screen =Screen . new ( 80 , 80 , { term_name ='xterm' } ) 192exec ( 'colorscheme default' ) 193end ) 194 195it ( 'generates html with given font' , function () 196test_generates_html ( false , '"dumyfont","anotherfont"' ) 197end ) 198 199it ( "generates html, respects 'guifont'" , function () 200exec_lua [[vim.o.guifont='Font,Escape\\,comma, Ignore space after comma']] 201test_generates_html ( true , '"Font","Escape,comma","Ignore space after comma"' ) 202end ) 203 204it ( 'generates html from range' , function () 205insert ( [[ 206line1 207line2 208line3 209]] ) 210local ns =api . nvim_create_namespace '' 211api . nvim_buf_set_extmark ( 0 , ns , 0 , 0 , { end_col =1 , end_row =1 , hl_group ='Visual' } ) 212exec ( 'set termguicolors' ) 213local bg =fn . synIDattr ( fn . hlID ( 'Normal' ), 'bg#' , 'gui' ) 214local fg =fn . synIDattr ( fn . hlID ( 'Normal' ), 'fg#' , 'gui' ) 215exec_lua [[vim.o.guifont='Courier New' ]] 216n . command ( '2,2TOhtml' ) 217local out_file =api . nvim_buf_get_name ( api . nvim_get_current_buf ()) 218eq ( { 219'<!-- vim: set nomodeline: -->' , 220'<!DOCTYPE html>' , 221'<html>' , 222'<head>' , 223'<meta charset="UTF-8">' , 224'<title></title>' , 225( '<meta name="colorscheme" content="%s"></meta>' ): format ( api . nvim_get_var ( 'colors_name' )), 226'<style>' , 227( '* {font-family: "%s",monospace}' ): format ( n . eval ( '&guifont' )), 228( 'body {background-color: %s; color: %s}' ): format ( bg , fg ), 229'.Visual {background-color: #9b9ea4}' , 230'</style>' , 231'</head>' , 232'<body style="display: flex">' , 233'<pre><span class="Visual">' , 234'l</span>ine2' , 235'' , 236'</pre>' , 237'</body>' , 238'</html>' , 239} , fn . readfile ( out_file )) 240end ) 241 242it ( 'generates highlight attributes' , function () 243--Make sure to uncomment the attribute in `html_syntax_match()` 244exec ( 'hi LINE guisp=#00ff00 gui=' .. table . concat ( { 245'bold' , 246'underline' , 247'italic' , 248'strikethrough' , 249} , ',' )) 250exec ( 'hi UNDERCURL gui=undercurl' ) 251exec ( 'syn keyword LINE line' ) 252exec ( 'syn keyword UNDERCURL undercurl' ) 253insert ( 'line\nundercurl' ) 254run_tohtml_and_assert ( screen ) 255end ) 256 257it ( 'syntax' , function () 258insert [[ 259function main() 260print("hello world") 261end 262]] 263exec ( 'set termguicolors' ) 264exec ( 'syntax enable' ) 265exec ( 'setf lua' ) 266exec_lua ( 'vim.treesitter.stop()' ) -- Ensure that legacy syntax (not just TS) is tested. 267run_tohtml_and_assert ( screen ) 268end ) 269 270it ( 'diff' , function () 271exec ( 'set diffopt=' ) 272insert [[ 273diffadd 274nochage 275diffchange1 276]] 277exec ( 'new' ) 278insert [[ 279nochage 280diffchange2 281diffremove 282]] 283exec ( 'set diff' ) 284exec ( 'close' ) 285exec ( 'set diff' ) 286run_tohtml_and_assert ( screen ) 287end ) 288 289it ( 'treesitter' , function () 290insert [[ 291function main() 292print("hello world") 293end 294]] 295exec ( 'setf lua' ) 296exec_lua ( 'vim.treesitter.start()' ) 297run_tohtml_and_assert ( screen ) 298end ) 299 300it ( 'matchadd' , function () 301insert [[ 302line 303]] 304fn . matchadd ( 'Visual' , 'line' ) 305run_tohtml_and_assert ( screen ) 306end ) 307 308describe ( 'conceallevel' , function () 309local function run ( level ) 310insert ( [[ 311line0 312line1 313line2 314line3 315]] ) 316local ns =api . nvim_create_namespace '' 317fn . matchadd ( 'Conceal' , 'line1' , 3 , 5 , { conceal ='a' } ) 318api . nvim_buf_set_extmark ( 0 , ns , 2 , 0 , { conceal ='a' , end_col =5 } ) 319exec ( ':syntax match Conceal "line3" conceal cchar=a' ) 320exec ( 'set conceallevel=' .. level ) 321run_tohtml_and_assert ( screen ) 322end 323it ( 'conceallevel=0' , function () 324run ( 0 ) 325end ) 326it ( 'conceallevel=1' , function () 327run ( 1 ) 328end ) 329it ( 'conceallevel=2' , function () 330run ( 2 ) 331end ) 332it ( 'conceallevel=3' , function () 333run ( 3 ) 334end ) 335end ) 336 337describe ( 'extmarks' , function () 338it ( 'virt_text' , function () 339insert [[ 340line1 341line2 342line3 343line4 344]] 345local ns =api . nvim_create_namespace '' 346api . nvim_buf_set_extmark ( 0 , ns , 0 , 0 , { virt_text ={ { 'foo' } } } ) 347api . nvim_buf_set_extmark ( 3480 , 349ns , 3501 , 3510 , 352{ virt_text ={ { 'foo' } } , virt_text_pos ='overlay' } 353) 354api . nvim_buf_set_extmark ( 3550 , 356ns , 3572 , 3580 , 359{ virt_text ={ { 'fo┊o' , { 'Conceal' , 'Comment' } } } , virt_text_pos ='inline' } 360) 361--api.nvim_buf_set_extmark(0,ns,3,0,{virt_text={{'foo'}},virt_text_pos='right_align'}) 362run_tohtml_and_assert ( screen ) 363end ) 364it ( 'highlight' , function () 365insert [[ 366line1 367]] 368local ns =api . nvim_create_namespace '' 369api . nvim_buf_set_extmark ( 0 , ns , 0 , 0 , { end_col =2 , hl_group ='Visual' } ) 370run_tohtml_and_assert ( screen ) 371end ) 372it ( 'virt_line' , function () 373insert [[ 374line1 375line2 376]] 377local ns =api . nvim_create_namespace '' 378api . nvim_buf_set_extmark ( 0 , ns , 1 , 0 , { end_col =2 , virt_lines ={ { { 'foo' } } } } ) 379run_tohtml_and_assert ( screen ) 380end ) 381end ) 382 383it ( 'listchars' , function () 384exec ( 'setlocal list' ) 385exec ( 386'setlocal listchars=eol:$,tab:<->,space:-,multispace:++,lead:_,leadmultispace:##,trail:&,nbsp:%' 387) 388fn . setline ( 1 , '\tfoo\t' ) 389fn . setline ( 2 , ' foo foo ' ) 390fn . setline ( 3 , ' foo foo ' ) 391fn . setline ( 4 , 'foo\194\160 \226\128\175foo' ) 392run_tohtml_and_assert ( screen ) 393exec ( 'new|only' ) 394fn . setline ( 1 , '\tfoo\t' ) 395exec ( 'setlocal list' ) 396exec ( 'setlocal listchars=tab:a-' ) 397run_tohtml_and_assert ( screen ) 398end ) 399 400it ( 'folds' , function () 401insert ( [[ 402line1 403line2 404]] ) 405exec ( 'set foldtext=foldtext()' ) 406exec ( '%fo' ) 407run_tohtml_and_assert ( screen ) 408end ) 409 410it ( 'warns about highlighted foldtext from a Lua callback' , function () 411insert ( 'line1\nline2' ) 412exec_lua ( function () 413_G . warnings ={} 414vim . notify =function ( msg ) 415table . insert ( _G . warnings , msg ) 416end 417vim . o . foldtext =function () 418return { { 'folded' , 'Comment' } } 419end 420end ) 421exec ( '%fold' ) 422exec_lua ( [[require('tohtml').tohtml(0)]] ) 423eq ( { 424'TOhtml: foldtext returning a table with highlights is not supported, HTML may be incorrect' , 425} , exec_lua ( 'return _G.warnings' )) 426end ) 427 428it ( 'statuscol' , function () 429local function run () 430local buf =api . nvim_get_current_buf () 431run_tohtml_and_assert ( screen , function () 432exec_lua ( function () 433local outfile =vim . fn . tempname () .. '.html' 434local html =require ( 'tohtml' ). tohtml ( 0 , { number_lines =true } ) 435vim . fn . writefile ( html , outfile ) 436vim . cmd . split ( outfile ) 437end ) 438end ) 439api . nvim_set_current_buf ( buf ) 440end 441insert ( [[ 442line1 443line2 444]] ) 445exec ( 'setlocal relativenumber' ) 446run () 447exec ( 'setlocal norelativenumber' ) 448exec ( 'setlocal number' ) 449run () 450exec ( 'setlocal relativenumber' ) 451run () 452exec ( 'setlocal signcolumn=yes:2' ) 453run () 454exec ( 'setlocal foldcolumn=2' ) 455run () 456exec ( 'setlocal norelativenumber' ) 457run () 458exec ( 'setlocal signcolumn=no' ) 459run () 460end ) 461end )