robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
50c50ab119
master
1-- TODO(justinmk): move most of this to `vim.text`. 2 3local fmt =string . format 4 5--- @class nvim.util.MDNode 6--- @field [integer] nvim.util.MDNode 7--- @field type string 8--- @field text? string 9 10local INDENTATION =4 11 12local NBSP =string . char ( 160 ) 13 14local M ={} 15 16local function contains ( t , xs ) 17return vim . tbl_contains ( xs , t ) 18end 19 20-- Map of api_level:version, by inspection of: 21-- :lua= vim.mpack.decode(vim.fn.readfile('test/functional/fixtures/api_level_9.mpack','B')).version 22M . version_level ={ 23[ 15 ] ='0.13.0' , 24[ 14 ] ='0.12.0' , 25[ 13 ] ='0.11.0' , 26[ 12 ] ='0.10.0' , 27[ 11 ] ='0.9.0' , 28[ 10 ] ='0.8.0' , 29[ 9 ] ='0.7.0' , 30[ 8 ] ='0.6.0' , 31[ 7 ] ='0.5.0' , 32[ 6 ] ='0.4.0' , 33[ 5 ] ='0.3.2' , 34[ 4 ] ='0.3.0' , 35[ 3 ] ='0.2.1' , 36[ 2 ] ='0.2.0' , 37[ 1 ] ='0.1.0' , 38} 39 40--- @param txt string 41--- @param srow integer 42--- @param scol integer 43--- @param erow? integer 44--- @param ecol? integer 45--- @return string 46local function slice_text ( txt , srow , scol , erow , ecol ) 47local lines =vim . split ( txt , '\n' ) 48 49if srow == erow then 50return lines [ srow + 1 ]: sub ( scol + 1 , ecol ) 51end 52 53if erow then 54-- Trim the end 55for _ =erow + 2 , # lines do 56table . remove ( lines , # lines ) 57end 58end 59 60-- Trim the start 61for _ =1 , srow do 62table . remove ( lines , 1 ) 63end 64 65lines [ 1 ] =lines [ 1 ]: sub ( scol + 1 ) 66lines [ # lines ] =lines [ # lines ]: sub ( 1 , ecol ) 67 68return table . concat ( lines , '\n' ) 69end 70 71--- @param text string 72--- @return nvim.util.MDNode 73local function parse_md_inline ( text ) 74local parser =vim . treesitter . languagetree . new ( text , 'markdown_inline' ) 75local root =parser : parse ( true )[ 1 ]: root () 76 77--- @param node TSNode 78--- @return nvim.util.MDNode? 79local function extract ( node ) 80local ntype =node : type () 81 82if ntype : match ( '^%p$' ) then 83return 84end 85 86--- @type table<any,any> 87local ret ={ type =ntype } 88ret . text =vim . treesitter . get_node_text ( node , text ) 89 90local row , col =0 , 0 91 92for child , child_field in node : iter_children () do 93local e =extract ( child ) 94if e and ntype == 'inline' then 95local srow , scol =child : start () 96if ( srow == row and scol > col ) or srow > row then 97local t =slice_text ( ret . text , row , col , srow , scol ) 98if t and t ~= '' then 99table . insert ( ret , { type ='text' , j =true , text =t } ) 100end 101end 102row , col =child : end_ () 103end 104 105if child_field then 106ret [ child_field ] =e 107else 108table . insert ( ret , e ) 109end 110end 111 112if ntype == 'inline' and ( row > 0 or col > 0 ) then 113local t =slice_text ( ret . text , row , col ) 114if t and t ~= '' then 115table . insert ( ret , { type ='text' , text =t } ) 116end 117end 118 119return ret 120end 121 122return extract ( root ) or {} 123end 124 125--- @param text string 126--- @return nvim.util.MDNode 127local function parse_md ( text ) 128local parser =vim . treesitter . languagetree . new ( text , 'markdown' , { 129injections ={ markdown ='' } , 130} ) 131 132local root =parser : parse ( true )[ 1 ]: root () 133 134local EXCLUDE_TEXT_TYPE ={ 135list =true , 136list_item =true , 137section =true , 138document =true , 139fenced_code_block =true , 140fenced_code_block_delimiter =true , 141} 142 143--- @param node TSNode 144--- @return nvim.util.MDNode? 145local function extract ( node ) 146local ntype =node : type () 147 148if ntype : match ( '^%p$' ) or contains ( ntype , { 'block_continuation' } ) then 149return 150end 151 152--- @type table<any,any> 153local ret ={ type =ntype } 154 155if not EXCLUDE_TEXT_TYPE [ ntype ] then 156ret . text =vim . treesitter . get_node_text ( node , text ) 157end 158 159if ntype == 'inline' then 160ret =parse_md_inline ( ret . text ) 161end 162 163for child , child_field in node : iter_children () do 164local e =extract ( child ) 165if child_field then 166ret [ child_field ] =e 167else 168table . insert ( ret , e ) 169end 170end 171 172return ret 173end 174 175return extract ( root ) or {} 176end 177 178--- Prefixes each line in `text`. 179--- 180--- Does not wrap, not important for "meta" files? (You probably want md_to_vimdoc instead.) 181--- 182--- @param text string 183--- @param prefix_ string 184function M . prefix_lines ( prefix_ , text ) 185local r ='' 186for _ , l in ipairs ( vim . split ( text , '\n' , { plain =true } )) do 187r =r .. vim . trim ( prefix_ .. l ) .. '\n' 188end 189return r 190end 191 192--- @param x string 193--- @param start_indent integer 194--- @param indent integer 195--- @param text_width integer 196--- @return string 197function M . wrap ( x , start_indent , indent , text_width ) 198local words =vim . split ( vim . trim ( x ), '%s+' ) 199local parts ={ string . rep ( ' ' , start_indent ) } --- @type string[] 200local count =indent 201 202for i , w in ipairs ( words ) do 203if count > indent and count + # w > text_width - 1 then 204parts [ # parts + 1 ] ='\n' 205parts [ # parts + 1 ] =string . rep ( ' ' , indent ) 206count =indent 207elseif i ~= 1 then 208parts [ # parts + 1 ] =' ' 209count =count + 1 210end 211count =count + # w 212parts [ # parts + 1 ] =w 213end 214 215return ( table . concat ( parts ): gsub ( '%s+\n' , '\n' ): gsub ( '\n+$' , '' )) 216end 217 218--- @param node nvim.util.MDNode 219--- @param start_indent integer 220--- @param indent integer 221--- @param text_width integer 222--- @param level integer 223--- @return string[] 224local function render_md ( node , start_indent , indent , text_width , level , is_list ) 225local parts ={} --- @type string[] 226 227-- For debugging 228local add_tag =false 229-- local add_tag = true 230 231local ntype =node . type 232 233if add_tag then 234parts [ # parts + 1 ] ='<' .. ntype .. '>' 235end 236 237if ntype == 'text' then 238parts [ # parts + 1 ] =node . text 239elseif ntype == 'atx_heading' then 240parts [ # parts + 1 ] =( '*%s*' ): format ( node . heading_content . text ) 241elseif ntype == 'html_tag' then 242error ( 'html_tag: ' .. node . text ) 243elseif ntype == 'inline_link' then 244-- Markdown links with empty URLs, e.g. [lsp-buftypes](), are converted 245-- to vim help tags, e.g. *lsp-buftypes*. 246vim . list_extend ( parts , { '*' , node [ 1 ]. text , '*' } ) 247elseif ntype == 'shortcut_link' then 248if node [ 1 ]. text : find ( '^<.*>$' ) then 249parts [ # parts + 1 ] =node [ 1 ]. text 250elseif node [ 1 ]. text : find ( '^%d+$' ) then 251vim . list_extend ( parts , { '[' , node [ 1 ]. text , ']' } ) 252else 253vim . list_extend ( parts , { '|' , node [ 1 ]. text , '|' } ) 254end 255elseif ntype == 'backslash_escape' then 256parts [ # parts + 1 ] =node . text 257elseif ntype == 'emphasis' then 258parts [ # parts + 1 ] =node . text : sub ( 2 , - 2 ) 259elseif ntype == 'code_span' then 260vim . list_extend ( parts , { '`' , node . text : sub ( 2 , - 2 ): gsub ( ' ' , NBSP ), '`' } ) 261elseif ntype == 'inline' then 262if # node == 0 then 263local text =assert ( node . text ) 264parts [ # parts + 1 ] =M . wrap ( text , start_indent , indent , text_width ) 265else 266for _ , child in ipairs ( node ) do 267vim . list_extend ( parts , render_md ( child , start_indent , indent , text_width , level + 1 )) 268end 269end 270elseif ntype == 'paragraph' then 271local pparts ={} 272for _ , child in ipairs ( node ) do 273vim . list_extend ( pparts , render_md ( child , start_indent , indent , text_width , level + 1 )) 274end 275parts [ # parts + 1 ] =M . wrap ( table . concat ( pparts ), start_indent , indent , text_width ) 276parts [ # parts + 1 ] ='\n' 277elseif ntype == 'code_fence_content' then 278local lines =vim . split ( node . text : gsub ( '\n%s*$' , '' ), '\n' ) 279 280local cindent =indent + INDENTATION 281if level > 3 then 282-- The tree-sitter markdown parser doesn't parse the code blocks indents 283-- correctly in lists. Fudge it! 284lines [ 1 ] =' ' .. lines [ 1 ] -- ¯\_(ツ)_/¯ 285cindent =indent - level 286local _ , initial_indent =lines [ 1 ]: find ( '^%s*' ) 287initial_indent =initial_indent + cindent 288if initial_indent < indent then 289cindent =indent - INDENTATION 290end 291end 292 293for _ , l in ipairs ( lines ) do 294if # l > 0 then 295parts [ # parts + 1 ] =string . rep ( ' ' , cindent ) 296parts [ # parts + 1 ] =l 297end 298parts [ # parts + 1 ] ='\n' 299end 300elseif ntype == 'fenced_code_block' then 301parts [ # parts + 1 ] ='>' 302for _ , child in ipairs ( node ) do 303if child . type == 'info_string' then 304parts [ # parts + 1 ] =child . text 305break 306end 307end 308parts [ # parts + 1 ] ='\n' 309for _ , child in ipairs ( node ) do 310if child . type ~= 'info_string' then 311vim . list_extend ( parts , render_md ( child , start_indent , indent , text_width , level + 1 )) 312end 313end 314parts [ # parts + 1 ] ='<\n' 315elseif ntype == 'html_block' then 316local text =node . text : gsub ( '^<pre>help%s*' , '' ) 317text =text : gsub ( '</pre>%s*$' , '' ) 318parts [ # parts + 1 ] =text 319elseif ntype == 'list_marker_dot' then 320parts [ # parts + 1 ] =node . text 321elseif contains ( ntype , { 'list_marker_minus' , 'list_marker_star' } ) then 322parts [ # parts + 1 ] ='• ' 323elseif ntype == 'list_item' then 324parts [ # parts + 1 ] =string . rep ( ' ' , indent ) 325local offset =node [ 1 ]. type == 'list_marker_dot' and 3 or 2 326for i , child in ipairs ( node ) do 327local sindent =i <= 2 and 0 or ( indent + offset ) 328vim . list_extend ( 329parts , 330render_md ( child , sindent , indent + offset , text_width , level + 1 , true ) 331) 332end 333else 334if node . text then 335error ( fmt ( 'cannot render:\n%s' , vim . inspect ( node ))) 336end 337for i , child in ipairs ( node ) do 338local start_indent0 =i == 1 and start_indent or indent 339vim . list_extend ( 340parts , 341render_md ( child , start_indent0 , indent , text_width , level + 1 , is_list ) 342) 343if ntype ~= 'list' and i ~= # node then 344if ( node [ i + 1 ] or {} ). type ~= 'list' then 345parts [ # parts + 1 ] ='\n' 346end 347end 348end 349end 350 351if add_tag then 352parts [ # parts + 1 ] ='</' .. ntype .. '>' 353end 354 355return parts 356end 357 358--- @param text_width integer 359function M . align_tags ( text_width ) 360--- @param line string 361--- @return string 362return function ( line ) 363local tag_pat ='%s*(%*.+%*)%s*$' 364local tags ={} 365for m in line : gmatch ( tag_pat ) do 366table . insert ( tags , m ) 367end 368 369if # tags > 0 then 370line =line : gsub ( tag_pat , '' ) 371local tags_str =' ' .. table . concat ( tags , ' ' ) 372--- @type integer 373local conceal_offset =select ( 2 , tags_str : gsub ( '%*' , '' )) - 2 374local pad =string . rep ( ' ' , text_width - # line - # tags_str + conceal_offset ) 375return line .. pad .. tags_str 376end 377 378return line 379end 380end 381 382--- @param text string 383--- @param start_indent integer 384--- @param indent integer 385--- @param is_list? boolean 386--- @return string 387function M . md_to_vimdoc ( text , start_indent , indent , text_width , is_list ) 388-- Add an extra newline so the parser can properly capture ending ``` 389local parsed =parse_md ( text .. '\n' ) 390local ret =render_md ( parsed , start_indent , indent , text_width , 0 , is_list ) 391 392local lines =vim . split ( table . concat ( ret ): gsub ( NBSP , ' ' ), '\n' ) 393 394lines =vim . tbl_map ( M . align_tags ( text_width ), lines ) 395 396local s =table . concat ( lines , '\n' ) 397 398-- Reduce whitespace in code-blocks 399s =s : gsub ( '\n+%s*>([a-z]+)\n' , ' >%1\n' ) 400s =s : gsub ( '\n+%s*>\n?\n' , ' >\n' ) 401 402return s 403end 404 405--- Sorts a list in-place, with underscore-prefixed names last (e.g. "_cmdline_offset"). 406--- If `key` is given, sorts by `item[key]`; otherwise sorts by value. 407--- 408--- @param t any[] 409--- @param key? string 410function M . sort_by_key ( t , key ) 411table . sort ( t , function ( a , b ) 412local a_val =key and a [ key ] or a 413local b_val =key and b [ key ] or b 414return a_val : gsub ( '^_' , '~' ) < b_val : gsub ( '^_' , '~' ) 415end ) 416end 417 418return M