From 6ef5d90a5ddc0f8744149840b9b0d2260d16c35c Mon Sep 17 00:00:00 2001
From: wsdjeg <wsdjeg@163.com>
Date: Tue, 30 May 2017 13:38:57 +0800
Subject: [PATCH] Improve gitcommit completion

---
 autoload/SpaceVim/plugins/gitcommit.vim | 30 ++++++++++++++++++-------
 config/plugins/deoplete.vim             |  5 +++++
 2 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/autoload/SpaceVim/plugins/gitcommit.vim b/autoload/SpaceVim/plugins/gitcommit.vim
index 34c3a01c1..bc286c23e 100644
--- a/autoload/SpaceVim/plugins/gitcommit.vim
+++ b/autoload/SpaceVim/plugins/gitcommit.vim
@@ -1,17 +1,24 @@
 let s:pr_kind = g:spacevim_gitcommit_pr_icon
 let s:issue_kind = g:spacevim_gitcommit_issue_icon
+let s:cache = {}
 
 function! SpaceVim#plugins#gitcommit#complete(findstart, base) abort
     if a:findstart
+        let s:complete_ol = 0
         let line = getline('.')
         let start = col('.') - 1
-        while start > 0 && line[start - 1] != ' '
+        while start > 0 && line[start - 1] != ' ' && line[start - 1] != '#'
             let start -= 1
         endwhile
-        return start
+        if line[start - 1] == '#'
+            let s:complete_ol = 1
+            return start + 1
+        else
+            return start
+        endif
     else
-        if a:base =~ '^#\d*'
-            return s:complete_pr()
+        if s:complete_ol == 1
+            return s:complete_pr(a:base)
         endif
         let res = []
         for m in s:cache_commits()
@@ -28,18 +35,25 @@ function! s:cache_commits() abort
     return rst
 endfunction
 
-function! s:complete_pr() abort
+function! s:complete_pr(base) abort
     let [user,repo] = s:current_repo()
-    let prs = github#api#issues#List_All_for_Repo(user, repo)
+    if !has_key(s:cache, user . '_' . repo)
+        let prs = github#api#issues#List_All_for_Repo(user, repo)
+        let s:cache[user . '_' . repo] = prs
+    else
+        let prs = s:cache[user . '_' . repo]
+    endif
     let rst = []
     for pr in prs
         let item = {
-                    \ 'word' : '#' . pr.number,
+                    \ 'word' : pr.number . '',
                     \ 'abbr' : '#' . pr.number,
                     \ 'menu' : pr.title,
                     \ 'kind' : (has_key(pr, 'pull_request') ? s:pr_kind : s:issue_kind),
                     \ }
-        call add(rst, item)
+        if pr.number . pr.title =~ a:base
+            call add(rst, item)
+        endif
     endfor
     return rst
 endfunction
diff --git a/config/plugins/deoplete.vim b/config/plugins/deoplete.vim
index e5e2965bd..349a2c4c6 100644
--- a/config/plugins/deoplete.vim
+++ b/config/plugins/deoplete.vim
@@ -51,6 +51,11 @@ let g:deoplete#ignore_sources.php = get(g:deoplete#ignore_sources, 'php', ['phpc
 "call deoplete#custom#set('phpcd', 'mark', '')
 "call deoplete#custom#set('phpcd', 'input_pattern', '\w*|[^. \t]->\w*|\w*::\w*')
 
+" gitcommit
+let g:deoplete#omni#input_patterns.gitcommit = get(g:deoplete#omni#input_patterns, 'gitcommit', [
+      \'[ ]#.*',
+      \])
+
 " lua
 let g:deoplete#omni_patterns.lua = get(g:deoplete#omni_patterns, 'lua', '.')