robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1-- use treesitter over syntax (for highlighted code blocks) 2vim . treesitter . start () 3 4--- Apply current colorscheme to lists of default highlight groups 5--- 6--- Note: {patterns} is assumed to be sorted by occurrence in the file. 7--- @param patterns {start:string,stop:string,match:string}[] 8local function colorize_hl_groups ( patterns ) 9local ns =vim . api . nvim_create_namespace ( 'nvim.vimhelp' ) 10vim . api . nvim_buf_clear_namespace ( 0 , ns , 0 , - 1 ) 11 12local save_cursor =vim . fn . getcurpos () 13 14for _ , pat in pairs ( patterns ) do 15local start_lnum =vim . fn . search ( pat . start , 'c' ) 16local end_lnum =vim . fn . search ( pat . stop ) 17if start_lnum == 0 or end_lnum == 0 then 18break 19end 20 21for lnum =start_lnum , end_lnum do 22local line =assert ( vim . api . nvim_buf_get_lines ( 0 , lnum - 1 , lnum , true )[ 1 ]) 23local word =line : match ( pat . match ) 24if vim . fn . hlexists ( word ) ~= 0 then 25vim . api . nvim_buf_set_extmark ( 0 , ns , lnum - 1 , 0 , { end_col =# word , hl_group =word } ) 26end 27end 28end 29 30vim . fn . setpos ( '.' , save_cursor ) 31end 32 33--- @param bufname string 34--- @param path string 35local function help_bufname_match ( bufname , path ) 36return vim . endswith ( bufname , path .. '.txt' ) or bufname : find ( path .. '%.%a%ax$' ) 37end 38 39-- Add custom highlights for list in `:h highlight-groups`. 40local bufname =vim . fs . normalize ( vim . api . nvim_buf_get_name ( 0 )) 41if help_bufname_match ( bufname , '/doc/syntax' ) then 42colorize_hl_groups ( { 43{ start =[[\*group-name\*]] , stop ='^======' , match ='^(%w+)\t' } , 44{ start =[[\*highlight-groups\*]] , stop ='^======' , match ='^(%w+)\t' } , 45} ) 46elseif help_bufname_match ( bufname , '/doc/treesitter' ) then 47colorize_hl_groups ( { 48{ 49start =[[\*treesitter-highlight-groups\*]] , 50stop =[[\*treesitter-highlight-spell\*]] , 51match ='^@[%w%p]+' , 52} , 53} ) 54elseif help_bufname_match ( bufname , '/doc/diagnostic' ) then 55colorize_hl_groups ( { 56{ start =[[\*diagnostic-highlights\*]] , stop ='^======' , match ='^(%w+)' } , 57} ) 58elseif help_bufname_match ( bufname , '/doc/lsp' ) then 59colorize_hl_groups ( { 60{ start =[[\*lsp-highlight\*]] , stop ='^------' , match ='^(%w+)' } , 61{ start =[[\*lsp-semantic-highlight\*]] , stop ='^======' , match ='^@[%w%p]+' } , 62} ) 63end 64 65vim . keymap . set ( 'n' , 'gO' , function () 66require ( 'vim.treesitter._headings' ). show_toc () 67end , { buf =0 , silent =true , desc ='Show an Outline of the current buffer' } ) 68 69vim . keymap . set ( 'n' , ']]' , function () 70require ( 'vim.treesitter._headings' ). jump ( { count =1 } ) 71end , { buf =0 , silent =false , desc ='Jump to next section' } ) 72vim . keymap . set ( 'n' , '[[' , function () 73require ( 'vim.treesitter._headings' ). jump ( { count =- 1 } ) 74end , { buf =0 , silent =false , desc ='Jump to previous section' } ) 75 76local parser =assert ( vim . treesitter . get_parser ( 0 , 'vimdoc' )) 77 78local function runnables () 79---@type table<integer, { lang: string, code: string }> 80local code_blocks ={} 81local query =vim . treesitter . query . parse ( 82'vimdoc' , 83[[ 84(codeblock 85(language) @_lang 86. 87(code) @code 88(#any-of? @_lang "lua\n" "vim\n") 89(#set! @code lang @_lang)) 90]] 91) 92 93local root =parser : parse ()[ 1 ]: root () 94for _ , match , metadata in query : iter_matches ( root , 0 , 0 , - 1 ) do 95for id , nodes in pairs ( match ) do 96local name =query . captures [ id ] 97local node =assert ( nodes [ 1 ]) 98 99if name == 'code' then 100local start , _ , end_ =assert ( node : parent ()): range () 101local code =vim . treesitter . get_node_text ( node , 0 ) 102local lang_id =assert ( metadata [ id ]). lang --[[@as integer]] 103local lang_node =assert ( match [ lang_id ][ 1 ]) 104local lang =vim . treesitter . get_node_text ( lang_node , 0 ) 105for i =start + 1 , end_ do 106code_blocks [ i ] ={ lang =lang , code =code } 107end 108end 109end 110end 111 112vim . keymap . set ( 'n' , 'g==' , function () 113local pos =vim . api . nvim_win_get_cursor ( 0 )[ 1 ] 114local code_block =code_blocks [ pos ] 115if not code_block then 116vim . ( 'No code block found' ) 117elseif code_block . lang == 'lua\n' then 118vim . cmd . lua ( code_block . code ) 119elseif code_block . lang == 'vim\n' then 120vim . cmd ( code_block . code ) 121end 122end , { buf =0 } ) 123end 124 125-- Retry once if the buffer has changed during the iteration of the code 126-- blocks. See #36574. 127if not pcall ( runnables ) then 128runnables () 129end 130 131local url_ns =vim . api . nvim_create_namespace ( 'nvim.help.urls' ) 132local function urls () 133local filepath =vim . fs . normalize ( vim . api . nvim_buf_get_name ( 0 )) 134 135if vim . fs . relpath ( assert ( vim . env . VIMRUNTIME ), filepath ) ~= nil then 136local base ='https://neovim.io/doc/user/helptag/?tag=' 137local query =vim . treesitter . query . parse ( 138'vimdoc' , 139[[ 140((optionlink) @helplink) 141(taglink 142text: (_) @helplink) 143(tag 144text: (_) @helplink) 145]] 146) 147 148vim . api . nvim_buf_clear_namespace ( 0 , url_ns , 0 , - 1 ) 149local root =parser : parse ()[ 1 ]: root () 150for _ , match , _ in query : iter_matches ( root , 0 , 0 , - 1 ) do 151for id , nodes in pairs ( match ) do 152if query . captures [ id ] == 'helplink' then 153for _ , node in ipairs ( nodes ) do 154local start_line , start_col , end_line , end_col =node : range () 155local tag =vim . treesitter . get_node_text ( node , 0 ) 156vim . api . nvim_buf_set_extmark ( 0 , url_ns , start_line , start_col , { 157end_line =end_line , 158end_col =end_col , 159url =base .. vim . uri_encode ( tag ), 160} ) 161end 162end 163end 164end 165end 166end 167 168-- Retry once if the buffer has changed during the iteration of the code 169-- blocks. See #36574. 170if not pcall ( urls ) then 171urls () 172end 173 174vim . b . undo_ftplugin =( vim . b . undo_ftplugin or '' ) 175.. '\n sil! exe "nunmap <buffer> gO" | sil! exe "nunmap <buffer> g=="' 176.. '\n sil! exe "nunmap <buffer> ]]" | sil! exe "nunmap <buffer> [["' 177.. ( '\n call v:lua.vim.api.nvim_buf_clear_namespace(0, %d, 0, -1)' ): format ( url_ns ) 178vim . b . undo_ftplugin =vim . b . undo_ftplugin .. ' | call v:lua.vim.treesitter.stop()'