mirror of
https://github.com/neovim/neovim.git
synced 2025-10-15 06:16:08 +00:00
vim-patch:9.1.1329: cannot get information about command line completion
Problem: cannot get information about command line completion
Solution: add CmdlineLeavePre autocommand and cmdcomplete_info() Vim
script function (Girish Palya)
This commit introduces two features to improve introspection and control
over command-line completion in Vim:
- Add CmdlineLeavePre autocmd event:
A new event triggered just before leaving the command line and before
CmdlineLeave. It allows capturing completion-related state that is
otherwise cleared by the time CmdlineLeave fires.
- Add cmdcomplete_info() Vim script function:
Returns a Dictionary with details about the current command-line
completion state.
These are similar in spirit to InsertLeavePre and complete_info(),
but focused on command-line mode.
**Use case:**
In [[PR vim/vim#16759](https://github.com/vim/vim/pull/16759)], two examples
demonstrate command-line completion: one for live grep, and another for
fuzzy file finding. However, both examples share two key limitations:
1. **Broken history recall (`<Up>`)**
When selecting a completion item via `<Tab>` or `<C-n>`, the original
pattern used for searching (e.g., a regex or fuzzy string) is
overwritten in the command-line history. This makes it impossible to
recall the original query later.
This is especially problematic for interactive grep workflows, where
it’s useful to recall a previous search and simply select a different
match from the menu.
2. **Lack of default selection on `<CR>`**
Often, it’s helpful to allow `<CR>` (Enter) to accept the first match
in the completion list, even when no item is explicitly selected. This
behavior is particularly useful in fuzzy file finding.
----
Below are the updated examples incorporating these improvements:
**Live grep, fuzzy find file, fuzzy find buffer:**
```vim
command! -nargs=+ -complete=customlist,GrepComplete Grep VisitFile()
def GrepComplete(arglead: string, cmdline: string, cursorpos: number):
list<any>
return arglead->len() > 1 ? systemlist($'grep -REIHns "{arglead}"' ..
' --exclude-dir=.git --exclude=".*" --exclude="tags" --exclude="*.swp"') : []
enddef
def VisitFile()
if (selected_match != null_string)
var qfitem = getqflist({lines: [selected_match]}).items[0]
if qfitem->has_key('bufnr') && qfitem.lnum > 0
var pos = qfitem.vcol > 0 ? 'setcharpos' : 'setpos'
exec $':b +call\ {pos}(".",\ [0,\ {qfitem.lnum},\ {qfitem.col},\ 0]) {qfitem.bufnr}'
setbufvar(qfitem.bufnr, '&buflisted', 1)
endif
endif
enddef
nnoremap <leader>g :Grep<space>
nnoremap <leader>G :Grep <c-r>=expand("<cword>")<cr>
command! -nargs=* -complete=customlist,FuzzyFind Find
execute(selected_match != '' ? $'edit {selected_match}' : '')
var allfiles: list<string>
autocmd CmdlineEnter : allfiles = null_list
def FuzzyFind(arglead: string, _: string, _: number): list<string>
if allfiles == null_list
allfiles = systemlist($'find {get(g:, "fzfind_root", ".")} \! \(
-path "*/.git" -prune -o -name "*.swp" \) -type f -follow')
endif
return arglead == '' ? allfiles : allfiles->matchfuzzy(arglead)
enddef
nnoremap <leader><space> :<c-r>=execute('let
fzfind_root="."')\|''<cr>Find<space><c-@>
nnoremap <leader>fv :<c-r>=execute('let
fzfind_root="$HOME/.vim"')\|''<cr>Find<space><c-@>
nnoremap <leader>fV :<c-r>=execute('let
fzfind_root="$VIMRUNTIME"')\|''<cr>Find<space><c-@>
command! -nargs=* -complete=customlist,FuzzyBuffer Buffer execute('b '
.. selected_match->matchstr('\d\+'))
def FuzzyBuffer(arglead: string, _: string, _: number): list<string>
var bufs = execute('buffers', 'silent!')->split("\n")
var altbuf = bufs->indexof((_, v) => v =~ '^\s*\d\+\s\+#')
if altbuf != -1
[bufs[0], bufs[altbuf]] = [bufs[altbuf], bufs[0]]
endif
return arglead == '' ? bufs : bufs->matchfuzzy(arglead)
enddef
nnoremap <leader><bs> :Buffer <c-@>
var selected_match = null_string
autocmd CmdlineLeavePre : SelectItem()
def SelectItem()
selected_match = ''
if getcmdline() =~ '^\s*\%(Grep\|Find\|Buffer\)\s'
var info = cmdcomplete_info()
if info != {} && info.pum_visible && !info.matches->empty()
selected_match = info.selected != -1 ? info.matches[info.selected] : info.matches[0]
setcmdline(info.cmdline_orig). # Preserve search pattern in history
endif
endif
enddef
```
**Auto-completion snippet:**
```vim
set wim=noselect:lastused,full wop=pum wcm=<C-@> wmnu
autocmd CmdlineChanged : CmdComplete()
def CmdComplete()
var [cmdline, curpos] = [getcmdline(), getcmdpos()]
if getchar(1, {number: true}) == 0 # Typehead is empty (no more pasted input)
&& !pumvisible() && curpos == cmdline->len() + 1
&& cmdline =~ '\%(\w\|[*/:.-]\)$' && cmdline !~ '^\d\+$' # Reduce noise
feedkeys("\<C-@>", "ti")
SkipCmdlineChanged() # Suppress redundant completion attempts
# Remove <C-@> that get inserted when no items are available
timer_start(0, (_) => getcmdline()->substitute('\%x00', '', 'g')->setcmdline())
endif
enddef
cnoremap <expr> <up> SkipCmdlineChanged("\<up>")
cnoremap <expr> <down> SkipCmdlineChanged("\<down>")
autocmd CmdlineEnter : set bo+=error
autocmd CmdlineLeave : set bo-=error
def SkipCmdlineChanged(key = ''): string
set ei+=CmdlineChanged
timer_start(0, (_) => execute('set ei-=CmdlineChanged'))
return key != '' ? ((pumvisible() ? "\<c-e>" : '') .. key) : ''
enddef
```
These customizable snippets can serve as *lightweight* and *native*
alternatives to picker plugins like **FZF** or **Telescope** for common,
everyday workflows. Also, live grep snippet can replace **cscope**
without the overhead of building its database.
closes: vim/vim#17115
92f68e26ec
Co-authored-by: Girish Palya <girishji@gmail.com>
This commit is contained in:
@@ -91,6 +91,8 @@ static int compl_match_arraysize;
|
||||
/// First column in cmdline of the matched item for completion.
|
||||
static int compl_startcol;
|
||||
static int compl_selected;
|
||||
/// cmdline before expansion
|
||||
static char *cmdline_orig = NULL;
|
||||
|
||||
#define SHOW_MATCH(m) (showtail ? showmatches_gettail(matches[m], false) : matches[m])
|
||||
|
||||
@@ -401,6 +403,7 @@ void cmdline_pum_remove(void)
|
||||
{
|
||||
pum_undisplay(true);
|
||||
XFREE_CLEAR(compl_match_array);
|
||||
compl_match_arraysize = 0;
|
||||
}
|
||||
|
||||
void cmdline_pum_cleanup(CmdlineInfo *cclp)
|
||||
@@ -967,6 +970,7 @@ void ExpandInit(expand_T *xp)
|
||||
xp->xp_backslash = XP_BS_NONE;
|
||||
xp->xp_prefix = XP_PREFIX_NONE;
|
||||
xp->xp_numfiles = -1;
|
||||
XFREE_CLEAR(cmdline_orig);
|
||||
}
|
||||
|
||||
/// Cleanup an expand structure after use.
|
||||
@@ -1059,6 +1063,11 @@ int showmatches(expand_T *xp, bool wildmenu)
|
||||
int columns;
|
||||
bool showtail;
|
||||
|
||||
// Save cmdline before expansion
|
||||
if (ccline->cmdbuff != NULL) {
|
||||
cmdline_orig = xstrnsave(ccline->cmdbuff, (size_t)ccline->cmdlen);
|
||||
}
|
||||
|
||||
if (xp->xp_numfiles == -1) {
|
||||
set_expand_context(xp);
|
||||
if (xp->xp_context == EXPAND_LUA) {
|
||||
@@ -3653,3 +3662,30 @@ theend:
|
||||
xfree(pat);
|
||||
ExpandCleanup(&xpc);
|
||||
}
|
||||
|
||||
/// "cmdcomplete_info()" function
|
||||
void f_cmdcomplete_info(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
|
||||
{
|
||||
CmdlineInfo *ccline = get_cmdline_info();
|
||||
|
||||
tv_dict_alloc_ret(rettv);
|
||||
if (ccline == NULL || ccline->xpc == NULL || ccline->xpc->xp_files == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
dict_T *retdict = rettv->vval.v_dict;
|
||||
int ret = tv_dict_add_str(retdict, S_LEN("cmdline_orig"), cmdline_orig);
|
||||
if (ret == OK) {
|
||||
ret = tv_dict_add_nr(retdict, S_LEN("pum_visible"), pum_visible());
|
||||
}
|
||||
if (ret == OK) {
|
||||
ret = tv_dict_add_nr(retdict, S_LEN("selected"), ccline->xpc->xp_selected);
|
||||
}
|
||||
if (ret == OK) {
|
||||
list_T *li = tv_list_alloc(ccline->xpc->xp_numfiles);
|
||||
ret = tv_dict_add_list(retdict, S_LEN("matches"), li);
|
||||
for (int idx = 0; ret == OK && idx < ccline->xpc->xp_numfiles; idx++) {
|
||||
tv_list_append_string(li, ccline->xpc->xp_files[idx], -1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user