haredoc.vim (1996B)
1 " Haredoc plugin -- Hare documentation in vim 2 " Maintainer: Byron Torres <b@torresjrjr.com> 3 " Last Updated: 2022-06-15 4 " License: Vim License 5 6 command -nargs=? Haredoc :call Haredoc(<q-args>) 7 8 function Haredoc(symbol) 9 let symbol = a:symbol 10 let popup = symbol == '.' 11 12 if (symbol == '.' || symbol == ',') 13 let oldiskeyword = &iskeyword 14 setlocal iskeyword+=: 15 let symbol = expand('<cword>') 16 let &iskeyword = oldiskeyword 17 18 " resolve possibly partial symbols (chrono::moment) 19 if match(symbol, "::") != -1 && &buftype == '' 20 let [parent, base] = split(symbol, "::")[-2:-1] 21 22 " try joining symbol with a use statement. 23 " example: 'use time::chrono' -> 'time::chrono::moment' 24 let resolvecmd =<< trim eval CMD 25 awk '/use [a-zA-Z:]*{parent};/ {{ 26 gsub("(use *|;.*)", ""); 27 print $1 "::" "{base}" 28 }}' {expand("%")} 2>&- 29 CMD 30 let joined_symbol = system(join(resolvecmd)) 31 if len(joined_symbol) != 0 32 let symbol = joined_symbol 33 endif 34 endif 35 endif 36 37 if popup && has('popupwin') && has('terminal') 38 let buf = term_start( 39 \ [ 40 \ 'sh', '-c', 41 \ "haredoc -Ftty '"..symbol.."' | less -RKX", 42 \ ], 43 \ #{ 44 \ term_name: '[haredoc] '..symbol, 45 \ hidden: 1, 46 \ term_finish: 'close', 47 \ }, 48 \ ) 49 call setbufvar(buf, "&bufhidden", 'wipe') 50 let minheight = 1 + system( 51 \ "haredoc -Ftty '"..symbol.."' 2>&1 | wc -l" 52 \ ) 53 let winid = popup_atcursor( 54 \ buf, 55 \ #{ 56 \ title: "[haredoc] "..symbol, 57 \ col: 'cursor-'..virtcol('.'), 58 \ minwidth: 84, 59 \ minheight: minheight, 60 \ fixed: v:false, 61 \ wrap: v:false, 62 \ border: [1, 1, 1, 1], 63 \ }, 64 \ ) 65 elseif has('terminal') 66 call term_start( 67 \ [ 68 \ 'sh', '-c', 69 \ "haredoc -Ftty '"..symbol.."' | less -RKX", 70 \ ], 71 \ #{ 72 \ term_name: '[haredoc] '..symbol, 73 \ }, 74 \ ) 75 set nonumber filetype=hare bufhidden=wipe 76 nnoremap <buffer> q :close<CR> 77 nnoremap <buffer> <nowait> u <C-U> 78 nnoremap <buffer> <nowait> d <C-D> 79 else 80 execute '!haredoc '..symbol 81 endif 82 endfunction