From e7fb8f326e7c3e184e305286199640fdaac35d5b Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 10 May 2017 00:24:43 +0200 Subject: autoload/gitcha.vim: Set completeopt+=menuone Work in progress Adds 'menuone' to the 'completeopt' option. This makes the popup menu display even if there's only a single result. We want this because it allows us to see the commit message subject for the completed SHA, providing a way to confirm that the completed SHA is the right one. Here, the idea was to try to save the user's 'completeopt' setting and restore it after completion, but we can't do that in `gitcha#GitSHAComplete` because if we do, it restores 'completeopt' before the completion menu is shown. Thus, it cancels out our custom `+=menuone` setting. After checking online to see how other plugins handle this (https://github.com/search?utf8=%E2%9C%93&q=completeopt+language%3AVimL+path%3A%2Fautoload&type=Code&ref=advsearch&l=&l=), I found that they use the `complete()` built-in function instead of overriding the user-designated 'completefunc' setting: https://github.com/heavenshell/vim-flood/blob/2bd8580642c56c2a090a8e44cc768e98019310be/autoload/flood/complete.vim This allows us to add `+=menuone` before calling `complete()` and remove the setting afterwards. Using `complete()` seems like the way to go here, so it looks like I'm going to have to refactor the code to do that instead in order to get "menuone" to work. The way I originally wrote the program was intuitive at the time. I'm glad I did it that way, because it provided me with the knowledge of how to remap the built-in completion mappings in Auditory.vim. Will have to try that out at some point. --- autoload/gitcha.vim | 3 +++ 1 file changed, 3 insertions(+) diff --git a/autoload/gitcha.vim b/autoload/gitcha.vim index 7ed45c7..e5543f7 100644 --- a/autoload/gitcha.vim +++ b/autoload/gitcha.vim @@ -1,6 +1,7 @@ " 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) @@ -16,6 +17,7 @@ function! gitcha#GitSHAComplete(findstart, base) " Restore user completion function let &completefunc = s:old_completefunc + " let &completeopt = s:old_completeopt " Match Git SHAs in the current repository let matches = [] @@ -57,5 +59,6 @@ endfunction " Allow mappings to initiate completion function! gitcha#StartGitSHACompletion() set completefunc=gitcha#GitSHAComplete + set completeopt=menu,menuone,preview return "\\" endfunction -- cgit v1.2.3 From 60bc8ea8b3b7be511c4240ca1d7dc1cc1149b113 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 10 May 2017 05:10:41 +0200 Subject: gitcha#GitSHAComplete(): Use `complete()` to open popup menu (WIP) Work in progress Rough code for a working implementation using `complete()` instead of 'completefunc'. Rejigger `start` and make a new `base` variable since we can no longer get it as an argument. Correctly reset 'completeopt' to its original user value after opening the popup menu. Return an empty string from the function as recommended by :h complete() --- autoload/gitcha.vim | 36 +++++++++++++++++++++--------------- ftplugin/gitcommit/gitcha.vim | 11 +++++++---- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/autoload/gitcha.vim b/autoload/gitcha.vim index e5543f7..c2e30b1 100644 --- a/autoload/gitcha.vim +++ b/autoload/gitcha.vim @@ -4,32 +4,36 @@ 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 +function! gitcha#GitSHAComplete() + " 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 = col('.') + while start > 0 && line[start - 2] =~ '[0-9a-f]' let start -= 1 endwhile - return start - endif - - " Restore user completion function - let &completefunc = s:old_completefunc - " let &completeopt = s:old_completeopt + " return start + " endif " Match Git SHAs in the current repository let matches = [] let revs = system('git rev-list --all --pretty=oneline --no-abbrev-commit') + let base = line[start - 1 : col('.') - 1] for m in s:BuildMatchDictionary(revs) - if m['word'] =~ '^' . a:base + if m['word'] =~ '^' . base call add(matches, m) endif endfor - return matches + " echom start + " echom string(matches) + " let &completeopt = 'menu,pattern,menuone' + set completeopt=menu,menuone,preview + call complete(start, matches) + " call complete(col('.'), matches) + let &completeopt = s:old_completeopt + return '' endfunction " Takes rev-list output from: @@ -58,7 +62,9 @@ endfunction " Allow mappings to initiate completion function! gitcha#StartGitSHACompletion() - set completefunc=gitcha#GitSHAComplete - set completeopt=menu,menuone,preview - return "\\" + " call gitcha#GitSHAComplete() + " return '' + " set completefunc=gitcha#GitSHAComplete + " set completeopt=menu,menuone,preview + " return "\\" endfunction diff --git a/ftplugin/gitcommit/gitcha.vim b/ftplugin/gitcommit/gitcha.vim index 19f4a0b..9be271d 100644 --- a/ftplugin/gitcommit/gitcha.vim +++ b/ftplugin/gitcommit/gitcha.vim @@ -8,10 +8,13 @@ if exists('g:no_plugin_maps') || exists('g:no_gitcha_maps') finish endif -if !hasmapto('GitchaCompleteSHA') - imap GitchaCompleteSHA -endif +" if !hasmapto('GitchaCompleteSHA') +" imap GitchaCompleteSHA +" endif -inoremap GitchaCompleteSHA gitcha#StartGitSHACompletion() +" inoremap GitchaCompleteSHA =gitcha#StartGitSHACompletion() +" inoremap GitchaCompleteSHA =gitcha#GitSHAComplete() +" inoremap =gitcha#GitSHAComplete() +inoremap =gitcha#GitSHAComplete() let b:undo_ftplugin = 'iunmap ' -- cgit v1.2.3 From 66b4ed72d3bc3a6c03049aa5364f567aab5ae980 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 10 May 2017 20:55:24 +0200 Subject: autoload/gitcha.vim: Remove cruft after switching to `complete()` Remove all the cruft from 60bc8ea8b3b7be511c4240ca1d7dc1cc1149b113: * Don't store user-defined 'completefunc' any more since we no longer change that setting. * Remove the commented `a:findstart` code, which is no longer relevant since this function is no longer a 'completefunc'. * Remove commented test & debugging code * Remove the `gitcha#StartGitSHACompletion()` function, which was there only to facilitate mapping to the user completion function defined in 'completefunc' using the Vim-assigned mapping ``. Our map now directly invokes `gitcha#GitSHAComplete()`. --- autoload/gitcha.vim | 33 +++++++++------------------------ 1 file changed, 9 insertions(+), 24 deletions(-) diff --git a/autoload/gitcha.vim b/autoload/gitcha.vim index c2e30b1..13d61bf 100644 --- a/autoload/gitcha.vim +++ b/autoload/gitcha.vim @@ -1,19 +1,14 @@ -" Save user-defined completefunc so it can be restored after running this +" Save user-defined 'completeopt's so they 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() - " if a:findstart - " locate the start of the word - let line = getline('.') - let start = col('.') - while start > 0 && line[start - 2] =~ '[0-9a-f]' - let start -= 1 - endwhile - " return start - " endif + let line = getline('.') + let start = col('.') + while start > 0 && line[start - 2] =~ '[0-9a-f]' + let start -= 1 + endwhile " Match Git SHAs in the current repository let matches = [] @@ -26,13 +21,12 @@ function! gitcha#GitSHAComplete() endif endfor - " echom start - " echom string(matches) - " let &completeopt = 'menu,pattern,menuone' set completeopt=menu,menuone,preview + call complete(start, matches) - " call complete(col('.'), matches) + let &completeopt = s:old_completeopt + return '' endfunction @@ -59,12 +53,3 @@ function! s:BuildMatchDictionary(rev_list) return matches endfunction - -" Allow mappings to initiate completion -function! gitcha#StartGitSHACompletion() - " call gitcha#GitSHAComplete() - " return '' - " set completefunc=gitcha#GitSHAComplete - " set completeopt=menu,menuone,preview - " return "\\" -endfunction -- cgit v1.2.3 From 69a382bf1d6cd4645dfb4408f757368b069694fc Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 10 May 2017 21:00:54 +0200 Subject: ftplugin/gitcommit: Restore mapping Restore this after 60bc8ea8b3b7be511c4240ca1d7dc1cc1149b113 where I made a mess experimenting trying to get `complete()` to work. Take the mapping that we used successfully from that commit and combine it with our `` mappings from before to call our new completion function. --- ftplugin/gitcommit/gitcha.vim | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/ftplugin/gitcommit/gitcha.vim b/ftplugin/gitcommit/gitcha.vim index 9be271d..c81cf26 100644 --- a/ftplugin/gitcommit/gitcha.vim +++ b/ftplugin/gitcommit/gitcha.vim @@ -8,13 +8,10 @@ if exists('g:no_plugin_maps') || exists('g:no_gitcha_maps') finish endif -" if !hasmapto('GitchaCompleteSHA') -" imap GitchaCompleteSHA -" endif +if !hasmapto('GitchaCompleteSHA') + imap GitchaCompleteSHA +endif -" inoremap GitchaCompleteSHA =gitcha#StartGitSHACompletion() -" inoremap GitchaCompleteSHA =gitcha#GitSHAComplete() -" inoremap =gitcha#GitSHAComplete() -inoremap =gitcha#GitSHAComplete() +inoremap GitchaCompleteSHA =gitcha#GitSHAComplete() let b:undo_ftplugin = 'iunmap ' -- cgit v1.2.3