vim-patch:9.1.1797: completion: autocompletion can be improved

Problem:  completion: autocompletion can be improved
Solution: Add support for "longest" and "preinsert" in 'autocomplete';
          add preinserted() (Girish Palya)

* Add support for "longest" in 'completeopt' when 'autocomplete'
  is enabled. (Note: the cursor position does not change automatically
  when 'autocomplete' is enabled.)
* Add support for "preinsert" when 'autocomplete' is enabled. Ensure
  "preinsert" works the same with and without 'autocomplete'
* introduce the preinserted() Vim script function, useful for defining
  custom key mappings.

fixes: vim/vim#18314
closes: vim/vim#18387

c05335082a

Co-authored-by: Girish Palya <girishji@gmail.com>
This commit is contained in:
zeertzjq
2025-09-27 09:09:03 +08:00
parent 4896178863
commit 7b9c063d11
11 changed files with 381 additions and 130 deletions

View File

@@ -1638,11 +1638,22 @@ A jump table for the options with a short description can be found at |Q_op|.
to gather more alternatives for your candidate list,
see 'completefuzzycollect'.
longest Only insert the longest common text of the matches. If
the menu is displayed you can use CTRL-L to add more
characters. Whether case is ignored depends on the kind
of completion. For buffer text the 'ignorecase' option is
used.
longest
When 'autocomplete' is not active, only the longest
common prefix of the matches is inserted. If the popup
menu is displayed, you can use CTRL-L to add more
characters. Whether case is ignored depends on the type
of completion. For buffer text the 'ignorecase' option
applies.
When 'autocomplete' is active and no completion item is
selected, the longest common prefix of the matches is
inserted after the cursor. The prefix is taken either
from all displayed items or only from items in the current
buffer. The inserted text is highlighted with
|hl-PreInsert|, and the cursor position does not change
(similar to `"preinsert"`). Press CTRL-Y to accept.
See also |preinserted()|.
menu Use a popup menu to show the possible completions. The
menu is only shown when there is more than one match and
@@ -1675,22 +1686,21 @@ A jump table for the options with a short description can be found at |Q_op|.
with "menu" or "menuone". Overrides "preview".
preinsert
When 'autocomplete' is not active, inserts the part of the
first candidate word beyond the current completion leader,
highlighted with |hl-PreInsert|. The cursor doesn't move.
Requires "fuzzy" unset and "menuone" in 'completeopt'.
When 'autocomplete' is active, inserts the longest common
prefix of matches (from all shown items or from the
current buffer items). This occurs only when no menu item
is selected. Press CTRL-Y to accept.
Inserts the text of the first completion candidate
beyond the current leader, highlighted with |hl-PreInsert|.
The cursor does not move.
Requires "fuzzy" to be unset, and either "menuone" in
'completeopt' or 'autocomplete' enabled. When
'autocomplete' is enabled, this does not work if
'ignorecase' is set without 'infercase'.
See also |preinserted()|.
preview Show extra information about the currently selected
completion in the preview window. Only works in
combination with "menu" or "menuone".
Only "fuzzy", "popup", "preinsert" and "preview" have an effect when
'autocomplete' is enabled.
Only "fuzzy", "longest", "popup", "preinsert" and "preview" have an
effect when 'autocomplete' is enabled.
This option does not apply to |cmdline-completion|. See 'wildoptions'
for that.

View File

@@ -940,6 +940,7 @@ Insert mode completion: *completion-functions*
complete_info() get current completion information
complete_match() get insert completion start match col and
trigger text
preinserted() check if text is inserted after cursor
pumvisible() check if the popup menu is displayed
pum_getpos() position and size of popup menu if visible

View File

@@ -7232,6 +7232,15 @@ pow({x}, {y}) *pow()*
Return: ~
(`number`)
preinserted() *preinserted()*
Returns non-zero if text has been inserted after the cursor
because "preinsert" is present in 'completeopt', or if
"longest" is present in 'completeopt' while 'autocomplete'
is enabled. Otherwise returns zero.
Return: ~
(`number`)
prevnonblank({lnum}) *prevnonblank()*
Return the line number of the first line at or above {lnum}
that is not blank. Example: >vim

View File

@@ -1183,11 +1183,22 @@ vim.go.cia = vim.go.completeitemalign
--- to gather more alternatives for your candidate list,
--- see 'completefuzzycollect'.
---
--- longest Only insert the longest common text of the matches. If
--- the menu is displayed you can use CTRL-L to add more
--- characters. Whether case is ignored depends on the kind
--- of completion. For buffer text the 'ignorecase' option is
--- used.
--- longest
--- When 'autocomplete' is not active, only the longest
--- common prefix of the matches is inserted. If the popup
--- menu is displayed, you can use CTRL-L to add more
--- characters. Whether case is ignored depends on the type
--- of completion. For buffer text the 'ignorecase' option
--- applies.
---
--- When 'autocomplete' is active and no completion item is
--- selected, the longest common prefix of the matches is
--- inserted after the cursor. The prefix is taken either
--- from all displayed items or only from items in the current
--- buffer. The inserted text is highlighted with
--- `hl-PreInsert`, and the cursor position does not change
--- (similar to `"preinsert"`). Press CTRL-Y to accept.
--- See also `preinserted()`.
---
--- menu Use a popup menu to show the possible completions. The
--- menu is only shown when there is more than one match and
@@ -1220,22 +1231,21 @@ vim.go.cia = vim.go.completeitemalign
--- with "menu" or "menuone". Overrides "preview".
---
--- preinsert
--- When 'autocomplete' is not active, inserts the part of the
--- first candidate word beyond the current completion leader,
--- highlighted with `hl-PreInsert`. The cursor doesn't move.
--- Requires "fuzzy" unset and "menuone" in 'completeopt'.
---
--- When 'autocomplete' is active, inserts the longest common
--- prefix of matches (from all shown items or from the
--- current buffer items). This occurs only when no menu item
--- is selected. Press CTRL-Y to accept.
--- Inserts the text of the first completion candidate
--- beyond the current leader, highlighted with `hl-PreInsert`.
--- The cursor does not move.
--- Requires "fuzzy" to be unset, and either "menuone" in
--- 'completeopt' or 'autocomplete' enabled. When
--- 'autocomplete' is enabled, this does not work if
--- 'ignorecase' is set without 'infercase'.
--- See also `preinserted()`.
---
--- preview Show extra information about the currently selected
--- completion in the preview window. Only works in
--- combination with "menu" or "menuone".
---
--- Only "fuzzy", "popup", "preinsert" and "preview" have an effect when
--- 'autocomplete' is enabled.
--- Only "fuzzy", "longest", "popup", "preinsert" and "preview" have an
--- effect when 'autocomplete' is enabled.
---
--- This option does not apply to `cmdline-completion`. See 'wildoptions'
--- for that.

View File

@@ -6554,6 +6554,14 @@ function vim.fn.perleval(expr) end
--- @return number
function vim.fn.pow(x, y) end
--- Returns non-zero if text has been inserted after the cursor
--- because "preinsert" is present in 'completeopt', or if
--- "longest" is present in 'completeopt' while 'autocomplete'
--- is enabled. Otherwise returns zero.
---
--- @return number
function vim.fn.preinserted() end
--- Return the line number of the first line at or above {lnum}
--- that is not blank. Example: >vim
--- let ind = indent(prevnonblank(v:lnum - 1))