patch 9.1.0896: completion list wrong after v9.1.0891

Problem:  completion list wrong after v9.1.0891
Solution: update compl_mach_array after leader change
          (glepnir)

compl_shown_match update not correct after refactoring in v9.1.0891
Unfortunately, this regressed what item is selected after leader change.

So generate compl_match_array before updating compl_shown_match range,
and split generate compl_match_array into range match_head

fixes: #16128
closes: #16129

Signed-off-by: glepnir <glephunter@gmail.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/testdir/dumps/Test_pum_keep_select_01.dump b/src/testdir/dumps/Test_pum_keep_select_01.dump
new file mode 100644
index 0000000..42877d0
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_keep_select_01.dump
@@ -0,0 +1,20 @@
+|F+0&#ffffff0|a|b| @71
+|F|i|v|e| @70
+|f|i|n|d| @70
+|f|i|l|m| @70
+> @74
+|F+0#0000001#ffd7ff255|a|b| @11| +0#4040ff13#ffffff0@59
+|F+0#0000001#ffd7ff255|i|v|e| @10| +0#4040ff13#ffffff0@59
+|f+0#0000001#ffd7ff255|i|n|d| @10| +0#4040ff13#ffffff0@59
+|f+0#0000001#e0e0e08|i|l|m| @10| +0#4040ff13#ffffff0@59
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |K|e|y|w|o|r|d| |L|o|c|a|l| |c|o|m|p|l|e|t|i|o|n| |(|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |1| |o|f| |4| +0#0000000&@27
diff --git a/src/testdir/dumps/Test_pum_keep_select_02.dump b/src/testdir/dumps/Test_pum_keep_select_02.dump
new file mode 100644
index 0000000..c4dbb27
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_keep_select_02.dump
@@ -0,0 +1,20 @@
+|F+0&#ffffff0|a|b| @71
+|F|i|v|e| @70
+|f|i|n|d| @70
+|f|i|l|m| @70
+|F> @73
+|F+0#0000001#ffd7ff255|a|b| @11| +0#4040ff13#ffffff0@59
+|F+0#0000001#e0e0e08|i|v|e| @10| +0#4040ff13#ffffff0@59
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |K|e|y|w|o|r|d| |L|o|c|a|l| |c|o|m|p|l|e|t|i|o|n| |(|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |1| |o|f| |4| +0#0000000&@27
diff --git a/src/testdir/test_popup.vim b/src/testdir/test_popup.vim
index 6b807c8..d5e6e31 100644
--- a/src/testdir/test_popup.vim
+++ b/src/testdir/test_popup.vim
@@ -1673,4 +1673,29 @@
   call StopVimInTerminal(buf)
 endfunc
 
+func Test_pum_keep_select()
+  CheckScreendump
+  let lines =<< trim END
+    set completeopt=menu,menuone,noinsert
+  END
+  call writefile(lines, 'Xscript', 'D')
+  let buf = RunVimInTerminal('-S Xscript', {})
+  call TermWait(buf)
+
+  call term_sendkeys(buf, "ggSFab\<CR>Five\<CR>find\<CR>film\<CR>\<C-X>\<C-P>")
+  call TermWait(buf, 50)
+  call VerifyScreenDump(buf, 'Test_pum_keep_select_01', {})
+  call term_sendkeys(buf, "\<C-E>\<Esc>")
+  call TermWait(buf, 50)
+
+  call term_sendkeys(buf, "S\<C-X>\<C-P>")
+  call TermWait(buf, 50)
+  call term_sendkeys(buf, "F")
+  call VerifyScreenDump(buf, 'Test_pum_keep_select_02', {})
+  call term_sendkeys(buf, "\<C-E>\<Esc>")
+
+  call TermWait(buf, 50)
+  call StopVimInTerminal(buf)
+endfunc
+
 " vim: shiftwidth=2 sts=2 expandtab