aboutsummaryrefslogtreecommitdiffstats
path: root/autoload/gitcha.vim
blob: 7ed45c79cf379a37146d8b48a0b02f0e1072b8c1 (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
" Save user-defined completefunc so it can be restored after running this
" custom completion function
let s:old_completefunc = &completefunc

" Completion for Git SHAs in the current repository
function! gitcha#GitSHAComplete(findstart, base)
	if a:findstart
		" locate the start of the word
		let line = getline('.')
		let start = col('.') - 1
		while start > 0 && line[start - 1] =~ '[0-9a-f]'
			let start -= 1
		endwhile
		return start
	endif

	" Restore user completion function
	let &completefunc = s:old_completefunc

	" Match Git SHAs in the current repository
	let matches = []
	let revs = system('git rev-list --all --pretty=oneline --no-abbrev-commit')

	for m in s:BuildMatchDictionary(revs)
		if m['word'] =~ '^' . a:base
			call add(matches, m)
		endif
	endfor

	return matches
endfunction

" Takes rev-list output from:
"   $ git rev-list --all --pretty=oneline --no-abbrev-commit
"
" Creates a dictionary to be used for matching that uses commit SHAs for the
" completion word and the commit subject as extra text.
function! s:BuildMatchDictionary(rev_list)
	let matches = []
	let commits = split(a:rev_list, '\n')

	for commit in commits
		let separator = stridx(commit, ' ')
		let sha = strpart(commit, 0, separator)
		let subject = strpart(commit, separator + 1)

		call add(matches, {
			\ 'word': sha,
			\ 'abbr': strpart(sha, 0, 10),
			\ 'menu': subject
		\ })
	endfor

	return matches
endfunction

" Allow mappings to initiate completion
function! gitcha#StartGitSHACompletion()
	set completefunc=gitcha#GitSHAComplete
	return "\<C-x>\<C-u>"
endfunction