Extract generic ingo#plugin#cmdcomplete#MakeCompleteFunc()

This removes a bit of duplication in the existing two functions in this module, and the functionality is useful on its own to some clients.
This commit is contained in:
Ingo Karkat
2018-05-18 21:41:35 +02:00
parent a270c56fa4
commit 25fa3abe99
3 changed files with 50 additions and 25 deletions
+1
View File
@@ -93,6 +93,7 @@ HISTORY
- Add ingo/lists/find.vim module.
- Add ingo/folds/containment.vim module.
- Add ingo/ftplugin/setting.vim module.
- Extract generic ingo#plugin#cmdcomplete#MakeCompleteFunc().
##### 1.034 13-Feb-2018
- Add ingo/regexp/split.vim module.
+48 -25
View File
@@ -2,7 +2,7 @@
"
" DEPENDENCIES:
"
" Copyright: (C) 2013-2017 Ingo Karkat
" Copyright: (C) 2013-2018 Ingo Karkat
" The VIM LICENSE applies to this script; see ':help copyright'.
"
" Maintainer: Ingo Karkat <ingo@karkat.de>
@@ -10,25 +10,27 @@ let s:save_cpo = &cpo
set cpo&vim
let s:completeFuncCnt = 0
function! ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc( argumentList, ... )
function! ingo#plugin#cmdcomplete#MakeCompleteFunc( implementation, ... )
"******************************************************************************
"* PURPOSE:
" Define a complete function for :command -complete=customlist that completes
" from a static list of possible arguments.
" Generically define a complete function for :command -complete=customlist
" with a:implementation as the function body.
"* USAGE:
" call ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc(
" \ ['foo', 'fox', 'bar'], 'FooCompleteFunc')
" call ingo#plugin#cmdcomplete#MakeCompleteFunc(
" \ 'return a:ArgLead ==? "f" ? "Foo" : "Bar"', 'FooCompleteFunc')
" command! -complete=customlist,FooCompleteFunc Foo ...
" or alternatively
" execute 'command! -complete=customlist,' .
" ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc(
" \ ['foo', 'fox', 'bar']) . 'Foo ...'
" ingo#plugin#cmdcomplete#MakeCompleteFunc(
" \ 'return a:ArgLead ==? "f" ? "Foo" : "Bar"') 'Foo ...'
"* ASSUMPTIONS / PRECONDITIONS:
" None.
"* EFFECTS / POSTCONDITIONS:
" Defines function.
"* INPUTS:
" a:argumentList List of possible arguments.
" a:implementation String representing the function body of the completion
" function. It can refer to the completion arguments
" a:ArgLead, a:CmdLine, a:CursorPos.
" a:funcName Optional name for the complete function; when not specified,
" a unique name is generated.
"* RETURN VALUES:
@@ -43,12 +45,42 @@ function! ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc( argumentList, ... )
execute
\ printf('function! %s( ArgLead, CmdLine, CursorPos )', l:funcName) . "\n" .
\ printf(' return filter(%s, ''v:val =~ "\\V\\^" . escape(a:ArgLead, "\\")'')', string(a:argumentList)) . "\n" .
\ 'endfunction'
\ a:implementation . "\n" .
\ 'endfunction'
return l:funcName
endfunction
function! ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc( argumentList, ... )
"******************************************************************************
"* PURPOSE:
" Define a complete function for :command -complete=customlist that completes
" from a static list of possible arguments.
"* USAGE:
" call ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc(
" \ ['foo', 'fox', 'bar'], 'FooCompleteFunc')
" command! -complete=customlist,FooCompleteFunc Foo ...
" or alternatively
" execute 'command! -complete=customlist,' .
" ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc(
" \ ['foo', 'fox', 'bar']) 'Foo ...'
"* ASSUMPTIONS / PRECONDITIONS:
" None.
"* EFFECTS / POSTCONDITIONS:
" Defines function.
"* INPUTS:
" a:argumentList List of possible arguments.
" a:funcName Optional name for the complete function; when not specified,
" a unique name is generated.
"* RETURN VALUES:
" Name of the defined complete function.
"******************************************************************************
return call('ingo#plugin#cmdcomplete#MakeCompleteFunc',
\ [printf('return filter(%s, ''v:val =~ "\\V\\^" . escape(a:ArgLead, "\\")'')', string(a:argumentList))] +
\ a:000
\)
endfunction
function! ingo#plugin#cmdcomplete#MakeListExprCompleteFunc( argumentExpr, ... )
"******************************************************************************
"* PURPOSE:
@@ -61,7 +93,7 @@ function! ingo#plugin#cmdcomplete#MakeListExprCompleteFunc( argumentExpr, ... )
" or alternatively
" execute 'command! -complete=customlist,' .
" ingo#plugin#cmdcomplete#MakeFixedListCompleteFunc(
" \ 'map(copy(g:values), "v:val[0:3]")') ...
" \ 'map(copy(g:values), "v:val[0:3]")') 'Foo ...'
"* ASSUMPTIONS / PRECONDITIONS:
" None.
"* EFFECTS / POSTCONDITIONS:
@@ -74,19 +106,10 @@ function! ingo#plugin#cmdcomplete#MakeListExprCompleteFunc( argumentExpr, ... )
"* RETURN VALUES:
" Name of the defined complete function.
"******************************************************************************
if a:0
let l:funcName = a:1
else
let s:completeFuncCnt += 1
let l:funcName = printf('CompleteFunc%d', s:completeFuncCnt)
endif
execute
\ printf('function! %s( ArgLead, CmdLine, CursorPos )', l:funcName) . "\n" .
\ printf(' return filter(%s, ''v:val =~ "\\V\\^" . escape(a:ArgLead, "\\")'')', a:argumentExpr) . "\n" .
\ 'endfunction'
return l:funcName
return call('ingo#plugin#cmdcomplete#MakeCompleteFunc',
\ [printf('return filter(%s, ''v:val =~ "\\V\\^" . escape(a:ArgLead, "\\")'')', a:argumentExpr)] +
\ a:000
\)
endfunction
let &cpo = s:save_cpo
+1
View File
@@ -107,6 +107,7 @@ HISTORY *ingo-library-history*
- Add ingo/lists/find.vim module.
- Add ingo/folds/containment.vim module.
- Add ingo/ftplugin/setting.vim module.
- Extract generic ingo#plugin#cmdcomplete#MakeCompleteFunc().
1.034 13-Feb-2018
- Add ingo/regexp/split.vim module.