robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
aa1d8a2702
master
1local ts =vim . treesitter 2local api =vim . api 3 4--- Treesitter-based navigation functions for headings 5local M ={} 6 7-- TODO(clason): use runtimepath queries (for other languages) 8local heading_queries ={ 9vimdoc =[[ 10(h1 (heading) @h1) 11(h2 (heading) @h2) 12(h3 (heading) @h3) 13(column_heading (heading) @h4) 14]] , 15markdown =[[ 16(setext_heading 17heading_content: (_) @h1 18(setext_h1_underline)) 19(setext_heading 20heading_content: (_) @h2 21(setext_h2_underline)) 22(atx_heading 23(atx_h1_marker) 24heading_content: (_) @h1) 25(atx_heading 26(atx_h2_marker) 27heading_content: (_) @h2) 28(atx_heading 29(atx_h3_marker) 30heading_content: (_) @h3) 31(atx_heading 32(atx_h4_marker) 33heading_content: (_) @h4) 34(atx_heading 35(atx_h5_marker) 36heading_content: (_) @h5) 37(atx_heading 38(atx_h6_marker) 39heading_content: (_) @h6) 40]] , 41} 42 43---@class TS.Heading 44---@field bufnr integer 45---@field lnum integer 46---@field text string 47---@field level integer 48 49--- Extract headings from buffer 50--- @param bufnr integer buffer to extract headings from 51--- @return TS.Heading[] 52local function get_headings ( bufnr ) 53local lang =ts . language . get_lang ( vim . bo [ bufnr ]. filetype ) 54if not lang then 55return {} 56end 57local parser =assert ( ts . get_parser ( bufnr , lang )) 58local query =ts . query . parse ( lang , assert ( heading_queries [ lang ])) 59local root =parser : parse ()[ 1 ]: root () 60local headings ={} --- @type TS.Heading[] 61for id , node , _ , _ in query : iter_captures ( root , bufnr ) do 62local text =ts . get_node_text ( node , bufnr ) 63local row , col =node : start () 64--- why can't you just be normal?! 65local skip ---@type boolean|integer 66if lang == 'vimdoc' then 67-- only column_headings at col 1 are headings, otherwise it's code examples 68skip =( id == 4 and col > 0 ) 69-- ignore tabular material 70or ( id == 4 and ( text : find ( '\t' ) or text : find ( ' ' ))) 71-- ignore tag-only headings 72or ( node : child_count () == 1 and assert ( node : child ( 0 )): type () == 'tag' ) 73end 74if not skip then 75headings [ # headings + 1 ] ={ 76bufnr =bufnr , 77lnum =row + 1 , 78text =text , 79level =id , 80} 81end 82end 83return headings 84end 85 86--- @param qf_height? integer height of loclist window 87--- Shows an Outline (table of contents) of the current buffer, in the loclist. 88function M . show_toc ( qf_height ) 89local bufnr =api . nvim_get_current_buf () 90local bufname =api . nvim_buf_get_name ( bufnr ) 91local headings =get_headings ( bufnr ) 92if # headings == 0 then 93return 94end 95-- add indentation for nicer list formatting 96for _ , heading in pairs ( headings ) do 97-- Quickfix trims whitespace, so use non-breaking space instead 98heading . text =( '\194\160' ): rep (( heading . level - 1 ) * 2 ) .. heading . text 99end 100vim . fn . setloclist ( 0 , headings --[[@as vim.quickfix.entry[] ]] , ' ' ) 101vim . fn . setloclist ( 0 , {} , 'a' , { title ='Table of contents' } ) 102vim . cmd . lopen ( qf_height ) 103vim . w . qf_toc =bufname 104-- reload syntax file after setting qf_toc variable 105vim . bo . filetype ='qf' 106end 107 108--- Jump to section 109--- @param opts table jump options 110--- - count integer direction to jump (>0 forward, <0 backward) 111--- - level integer only consider headings up to level 112--- todo(clason): support count 113function M . jump ( opts ) 114local bufnr =api . nvim_get_current_buf () 115local headings =get_headings ( bufnr ) 116if # headings == 0 then 117return 118end 119 120local winid =api . nvim_get_current_win () 121local curpos =vim . fn . getcurpos ( winid )[ 2 ] --[[@as integer]] 122local maxlevel =opts . level or 6 123 124if opts . count > 0 then 125for _ , heading in ipairs ( headings ) do 126if heading . lnum > curpos and heading . level <= maxlevel then 127api . nvim_win_set_cursor ( winid , { heading . lnum , 0 } ) 128return 129end 130end 131elseif opts . count < 0 then 132for i =# headings , 1 , - 1 do 133if headings [ i ]. lnum < curpos and headings [ i ]. level <= maxlevel then 134api . nvim_win_set_cursor ( winid , { headings [ i ]. lnum , 0 } ) 135return 136end 137end 138end 139end 140 141return M