Hi Bram!
On Sa, 23 Jul 2016, Bram Moolenaar wrote:
>
> Christian Brabandt wrote:
>
> > Bram,
> > I am writing a test for the popupmenu and the different keys, that can
> > be used there (:h popupmenu-keys)
>
> Thanks, that is useful.
>
> > And I have some questions.
> > 1)
> >
> > ,----
> > | In the first state these keys have a special meaning:
> > | <BS> and CTRL-H Delete one character, find the matches for the word
> > before
> > | the cursor. This reduces the list of matches, often to one
> > | entry, and switches to the second state.
> > `----
> >
> > I fail to understand what this does, especially in difference to the
> > second state:
>
> For example, if you type "compl<Ctrl-N>" then you get a list of matches,
> and the first one is used for the text. That could be "completion".
> Still the matches for "compl" are shown.
> Then typing BS changes it to "completio" and only matches for that are
> shown.
Hm, that works when using <Ctrl-N>, but not when using, e.g.
calling the completion menu like this:
inoremap <f5> <c-r>=ListMonths()<cr>
Then <BS> will always end the completion menu and remove the last
letter.
I did not think, that there is a difference depending on how the
completion menu is called. This might be a bug.
> > ,----
> > | In the second and third state these keys have a special meaning:
> > | <BS> and CTRL-H Delete one character, find the matches for the shorter
> > word
> > | before the cursor. This may find more matches.
> > `----
> >
> > And whatever I do, using <bs> will always end the completion menu and
> > remove one character. Either from the last typed char or from the
> > inserted char. I can't seem to figure out, how to use <bs> to adjust the
> > number of entries in the completion menu.
>
> The main difference is that in the second state the highlighted item
> differs from what the current text is.
Oh yes. Emphasis on "Cursor Key". Basically, that happens only then. Now
I understand.
>
> > 2) The behaviour of the enter key
> >
> > ,----
> > | The behavior of the <Enter> key depends on the state you are in:
> > | first state: Use the text as it is and insert a line break.
> > | second state: Insert the currently selected match.
> > | third state: Use the text as it is and insert a line break.
> > `----
> >
> > For me the <Enter> Key will always insert a line break.
>
> Not for me. Probably depends on 'completeopt'. My value is
> menu,preview
Yeah, once i figured, that I need to use the cursor keys to select the
match, it works as expected.
> It's possible that the help is not correct for all possible ways
> completion works. The code is very complicated and there are many
> corner cases.
Well, here is an updated patch for the popupmenu.
I think I also found a bug, and this makes the test currently fail.
Take this example from the test:
(The popupmenu is the one from the help for all months)
The initial state of the buffer is this:
,----
| D
| December2015
`----
(cursor at D, press <f5><c-e><c-e><c-e><c-e><enter>)
Now if you want to complete the letter D this should only complete to
December, since all other values do not match it and therefore it should
be completed immediately to December and no menu shown. Therefore,
pressing <C-e> afterwards should select the letter from below the
cursors line, so it should complete to "December2015" if you press <c-e>
4 times, then there should be a new line because of the enter and then
the "December2015" should be shown. So in the end, the buffer should
look like this:
,----
| December2015
|
| December2015
`----
For whatever reason, doing it interactively, this results in:
,----
| Dece
|
| December2015
`----
(because after inserting the match <c-e> is still in popupmenu mode and
"ends completion and goes back to what was there before selecting a
match")
However, a total mystery is to me, when the test is run, it will
complete to
,----
| December2015
| December2015
| December2015
`----
I have left this documented in the test. I am pretty sure, this is a
bug, and therefore, the test fails currently.
On a related note, since the "noinsert/noselect" property only seem to
change, what is inserted when the completion menu is entered initially,
It is hard to correctly test for that, therefore, I basically tested,
whether enter will insert a line break or not (difference in the states
as you explained above) and by that we know, if there was something
selected.
Mit freundlichen Grüßen
Christian
--
Die ersten vierzig Jahre unseres Lebens liefern den Text, die
folgenden dreißig den Kommentar dazu.
-- Arthur Schopenhauer
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
diff --git a/src/testdir/test_popup.vim b/src/testdir/test_popup.vim
index ea489f9..b3bf73a 100644
--- a/src/testdir/test_popup.vim
+++ b/src/testdir/test_popup.vim
@@ -3,14 +3,226 @@
let g:months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
let g:setting = ''
-func ListMonths()
+func! ListMonths()
if g:setting != ''
- exe ":set" g:setting
+ exe ":set" g:setting
endif
- call complete(col('.'), g:months)
+ let mth=copy(g:months)
+ let entered = strcharpart(getline('.'),0,col('.'))
+ if !empty(entered)
+ let mth=filter(mth, 'v:val=~"^".entered')
+ endif
+ call complete(1, mth)
return ''
endfunc
+func! Test_popup_complete()
+ new
+ inoremap <f5> <c-r>=ListMonths()<cr>
+
+ " <C-E> - select original typed text before the completion started
+ call feedkeys("aJu\<f5>\<down>\<c-e>\<esc>", 'tx')
+ call assert_equal(["Ju"], getline(1,2))
+ %d
+
+ " <C-Y> - accept current match
+ call feedkeys("a\<f5>". repeat("\<down>",7). "\<c-y>\<esc>", 'tx')
+ call assert_equal(["August"], getline(1,2))
+ %d
+
+ " <BS> - Delete one character from the inserted text (state: 1)
+ " TODO: This should not end the completion, but it does.
+ " This should according to the documentation:
+ " January
+ " but instead, this does
+ " Januar
+ " (idea is, C-L inserts the match from the popup menu
+ " but if the menu is closed, it will insert the character <c-l>
+ call feedkeys("aJ\<f5>\<bs>\<c-l>\<esc>", 'tx')
+ call assert_equal(["Januar"], getline(1,2))
+ %d
+
+ " any-non special character: Stop completion without changing the match
+ " and insert the typed character
+ call feedkeys("a\<f5>20", 'tx')
+ call assert_equal(["January20"], getline(1,2))
+ %d
+
+ " any-non printable, non-white character: Add this character and
+ " reduce number of matches
+ call feedkeys("aJu\<f5>\<c-p>l\<c-y>", 'tx')
+ call assert_equal(["Jul"], getline(1,2))
+ %d
+
+ " any-non printable, non-white character: Add this character and
+ " reduce number of matches
+ call feedkeys("aJu\<f5>\<c-p>l\<c-n>\<c-y>", 'tx')
+ call assert_equal(["July"], getline(1,2))
+ %d
+
+ " any-non printable, non-white character: Add this character and
+ " reduce number of matches
+ call feedkeys("aJu\<f5>\<c-p>l\<c-e>", 'tx')
+ call assert_equal(["Jul"], getline(1,2))
+ %d
+
+ " <BS> - Delete one character from the inserted text (state: 2)
+ call feedkeys("a\<f5>\<c-n>\<bs>", 'tx')
+ call assert_equal(["Februar"], getline(1,2))
+ %d
+
+ " <c-l> - Insert one character from the current match
+ call feedkeys("aJ\<f5>".repeat("\<c-n>",3)."\<c-l>\<esc>", 'tx')
+ call assert_equal(["J"], getline(1,2))
+ %d
+
+ " <c-l> - Insert one character from the current match
+ call feedkeys("aJ\<f5>".repeat("\<c-n>",4)."\<c-l>\<esc>", 'tx')
+ call assert_equal(["January"], getline(1,2))
+ %d
+
+ " <c-y> - Accept current selected match
+ call feedkeys("aJ\<f5>\<c-y>\<esc>", 'tx')
+ call assert_equal(["January"], getline(1,2))
+ %d
+
+ " <c-e> - End completion, go back to what was there before selecting a match
+ call feedkeys("aJu\<f5>\<c-e>\<esc>", 'tx')
+ call assert_equal(["Ju"], getline(1,2))
+ %d
+
+ " <PageUp> - Select a match several entries back
+ call feedkeys("a\<f5>\<PageUp>\<c-y>\<esc>", 'tx')
+ call assert_equal([""], getline(1,2))
+ %d
+
+ " <PageUp><PageUp> - Select a match several entries back
+ call feedkeys("a\<f5>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx')
+ call assert_equal(["December"], getline(1,2))
+ %d
+
+ " <PageUp><PageUp><PageUp> - Select a match several entries back
+ call feedkeys("a\<f5>\<PageUp>\<PageUp>\<PageUp>\<c-y>\<esc>", 'tx')
+ call assert_equal(["February"], getline(1,2))
+ %d
+
+ " <PageDown> - Select a match several entries further
+ call feedkeys("a\<f5>\<PageDown>\<c-y>\<esc>", 'tx')
+ call assert_equal(["November"], getline(1,2))
+ %d
+
+ " <PageDown><PageDown> - Select a match several entries further
+ call feedkeys("a\<f5>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx')
+ call assert_equal(["December"], getline(1,2))
+ %d
+
+ " <PageDown><PageDown><PageDown> - Select a match several entries further
+ call feedkeys("a\<f5>\<PageDown>\<PageDown>\<PageDown>\<c-y>\<esc>", 'tx')
+ call assert_equal([""], getline(1,2))
+ %d
+
+ " <PageDown><PageDown><PageDown><PageDown> - Select a match several entries further
+ call feedkeys("a\<f5>".repeat("\<PageDown>",4)."\<c-y>\<esc>", 'tx')
+ call assert_equal(["October"], getline(1,2))
+ %d
+
+ " <Up> - Select a match don't insert yet
+ call feedkeys("a\<f5>\<Up>\<c-y>\<esc>", 'tx')
+ call assert_equal([""], getline(1,2))
+ %d
+
+ " <Up><Up> - Select a match don't insert yet
+ call feedkeys("a\<f5>\<Up>\<Up>\<c-y>\<esc>", 'tx')
+ call assert_equal(["December"], getline(1,2))
+ %d
+
+ " <Up><Up><Up> - Select a match don't insert yet
+ call feedkeys("a\<f5>\<Up>\<Up>\<Up>\<c-y>\<esc>", 'tx')
+ call assert_equal(["November"], getline(1,2))
+ %d
+
+ " <Tab> - Stop completion and insert the match
+ call feedkeys("a\<f5>\<Tab>\<c-y>\<esc>", 'tx')
+ call assert_equal(["January "], getline(1,2))
+ %d
+
+ " <Space> - Stop completion and insert the match
+ call feedkeys("a\<f5>".repeat("\<c-p>",5)." \<esc>", 'tx')
+ call assert_equal(["September "], getline(1,2))
+ %d
+
+ " <Enter> - Use the text and insert line break (state: 1)
+ call feedkeys("a\<f5>\<enter>\<esc>", 'tx')
+ call assert_equal(["January", ''], getline(1,2))
+ %d
+
+ " <Enter> - Insert the current selected text (state: 2)
+ call feedkeys("a\<f5>".repeat("\<Up>",5)."\<enter>\<esc>", 'tx')
+ call assert_equal(["September"], getline(1,2))
+ %d
+
+ " Insert match immediately, if there is only one match
+ " <c-y> selects a character from the line above
+ call append(0, ["December2015"])
+ call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx')
+ call assert_equal(["December2015", "December2015", ""], getline(1,3))
+
+ " Insert match immediately, if there is only one match
+ " <c-e> Should select a character from the line below
+ " TODO: when run by hand, I get this result
+ " ,----
+ " |Dece
+ " |
+ " |December2015
+ " `----
+ " But, when running the test, I get this result:
+ " ,----
+ " |December2015
+ " |December2015
+ " |December2015
+ " `----
+ " While I would expect this result:
+ " ,----
+ " |December2015
+ " |
+ " |December2015
+ " `----
+ " Note: Because of that this test currently fails
+ call append(1, ["December2015"])
+ call feedkeys("aD\<f5>\<C-E>\<C-E>\<C-E>\<C-E>\<enter>\<esc>", 'tx')
+ call assert_equal(["December2015", "", "December2015"], getline(1,3))
+ %d
+
+ " use menuone for 'completeopt'
+ " Since for the first <c-y> the menu is still shown, will only select
+ " three letters from the line above
+ set completeopt&vim
+ set completeopt+=menuone
+ call append(0, ["December2015"])
+ call feedkeys("aD\<f5>\<C-Y>\<C-Y>\<C-Y>\<C-Y>\<enter>\<esc>", 'tx')
+ call assert_equal(["December2015", "December201", ""], getline(1,3))
+ %d
+
+ " use longest for 'completeopt'
+ set completeopt&vim
+ call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx')
+ set completeopt+=longest
+ call feedkeys("aM\<f5>\<C-N>\<C-P>\<c-e>\<enter>\<esc>", 'tx')
+ call assert_equal(["M", "Ma", ""], getline(1,3))
+ %d
+
+ " use noselect/noinsert for 'completeopt'
+ set completeopt&vim
+ call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
+ set completeopt+=noselect
+ call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
+ set completeopt-=noselect completeopt+=noinsert
+ call feedkeys("aM\<f5>\<enter>\<esc>", 'tx')
+ call assert_equal(["March", "M", "March"], getline(1,4))
+ %d
+endfu
+
+
func! Test_popup_completion_insertmode()
new
inoremap <F5> <C-R>=ListMonths()<CR>
@@ -18,18 +230,22 @@ func! Test_popup_completion_insertmode()
call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx')
call assert_equal('February', getline(1))
%d
+ " Set noinsertmode
let g:setting = 'noinsertmode'
call feedkeys("a\<f5>\<down>\<enter>\<esc>", 'tx')
call assert_equal('February', getline(1))
call assert_false(pumvisible())
%d
+ " Go through all matches, until none is selected
let g:setting = ''
call feedkeys("a\<f5>". repeat("\<c-n>",12)."\<enter>\<esc>", 'tx')
call assert_equal('', getline(1))
%d
+ " select previous entry
call feedkeys("a\<f5>\<c-p>\<enter>\<esc>", 'tx')
call assert_equal('', getline(1))
%d
+ " select last entry
call feedkeys("a\<f5>\<c-p>\<c-p>\<enter>\<esc>", 'tx')
call assert_equal('December', getline(1))
@@ -66,3 +282,5 @@ function! Test() abort
call complete(1, ['source', 'soundfold'])
return ''
endfunction
+
+" vim: tabstop=2 shiftwidth=0 sts=-1 expandtab