aboutsummaryrefslogtreecommitdiff
path: root/plugin/haredoc.vim
blob: 3502184d797e53c613a45e5316807c31aa445b37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
" Haredoc plugin -- Hare documentation in vim
" Maintainer: Byron Torres <b@torresjrjr.com>
" Last Updated: 2022-06-15
" License: Vim License

command -nargs=? Haredoc  :call Haredoc(<q-args>)

function Haredoc(symbol)
	let symbol = a:symbol
	let popup = symbol == '.'

	if (symbol == '.' || symbol == ',')
		let oldiskeyword = &iskeyword
		setlocal iskeyword+=:
		let symbol = expand('<cword>')
		let &iskeyword = oldiskeyword

		" resolve possibly partial symbols (chrono::moment)
		if match(symbol, "::") != -1 && &buftype == ''
			let [parent, base] = split(symbol, "::")[-2:-1]

			" try joining symbol with a use statement.
			" example: 'use time::chrono' -> 'time::chrono::moment'
			let resolvecmd =<< trim eval CMD
			awk '/use [a-zA-Z:]*{parent};/ {{
				gsub("(use *|;.*)", "");
				print $1 "::" "{base}"
			}}' {expand("%")} 2>&-
			CMD
			let joined_symbol = system(join(resolvecmd))[:-2]
			if len(joined_symbol) != 0
				let symbol = joined_symbol
			endif
		endif
	endif

	if popup && has('popupwin') && has('terminal')
		let buf = term_start(
		\	[
		\		'sh', '-c',
		\		"haredoc -Ftty '"..symbol.."' | LESS= less -RKX",
		\	],
		\	#{
		\		term_name: '[haredoc] '..symbol,
		\		hidden: 1,
		\		term_finish: 'close',
		\	},
		\ )
		call setbufvar(buf, "&bufhidden", 'wipe')
		let minheight = 1 + system(
		\	"haredoc -Ftty '"..symbol.."' 2>&1 | wc -l"
		\ )
		let winid = popup_atcursor(
		\	buf,
		\	#{
		\		title: "[haredoc] "..symbol,
		\		col: 'cursor-'..virtcol('.'),
		\		minwidth: 84,
		\		minheight: minheight,
		\		fixed: v:false,
		\		wrap: v:false,
		\		border: [1, 1, 1, 1],
		\	},
		\ )
	elseif has('terminal')
		call term_start(
		\	[
		\		'sh', '-c',
		\		"haredoc -Ftty '"..symbol.."' | less -RKX",
		\	],
		\	#{
		\		term_name: '[haredoc] '..symbol,
		\	},
		\ )
		set nonumber filetype=hare bufhidden=wipe
		nnoremap <buffer> q  :close<CR>
		nnoremap <buffer> <nowait> u  <C-U>
		nnoremap <buffer> <nowait> d  <C-D>
	else
		execute '!haredoc '..symbol
	endif
endfunction