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
|
" Save user-defined completefunc so it can be restored after running this
" custom completion function
let s:old_completefunc = &completefunc
let s:old_completeopt = &completeopt
" 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
" let &completeopt = s:old_completeopt
" 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
set completeopt=menu,menuone,preview
return "\<C-x>\<C-u>"
endfunction
|