ingo#subst#Indexed(): ENH: Support (most) special replacements in a:replacement

This is important to be a drop-in replacement (and because of the "g" short-circuiting) for substitute(). As we already have an emulation for this, we can actually support a lot.
This commit is contained in:
Ingo Karkat
2018-08-09 16:29:10 +02:00
parent e386076025
commit 9811e4345c
2 changed files with 11 additions and 3 deletions
+5 -2
View File
@@ -1,6 +1,8 @@
" ingo/subst.vim: Functions for substitutions.
"
" DEPENDENCIES:
" - ingo/format.vim autoload script
" - ingo/subst/replacement.vim autoload script
"
" Copyright: (C) 2013-2018 Ingo Karkat
" The VIM LICENSE applies to this script; see ':help copyright'.
@@ -142,7 +144,8 @@ function! ingo#subst#Indexed( expr, pattern, replacement, indices, ... )
"* INPUTS:
" a:expr Text to be transformed.
" a:pattern Regular expression.
" a:replacement Replacement. |sub-replace-expression| is not supported.
" a:replacement Replacement. Handles |sub-replace-expression|, & and \0,
" \1 .. \9, and \r\n\t\b (but not \u, \U, etc.)
" a:indices List of 0-based indices whose corresponding matches are
" replaced. A String value of "g" replaces globally, just like
" substitute(..., 'g').
@@ -167,7 +170,7 @@ endfunction
function! s:IndexReplacer( context )
let a:context.matchCnt += 1
execute 'let l:isSelected = index(a:context.indices, a:context.matchCnt - 1)' (a:context.isInvert ? '==' : '!=') '-1'
return (l:isSelected ? a:context.replacement : submatch(0))
return ingo#subst#replacement#DefaultReplacementOnPredicate(l:isSelected, a:context)
endfunction
let &cpo = s:save_cpo
+6 -1
View File
@@ -1,7 +1,7 @@
" Test substitution of indexed matches.
call vimtest#StartTap()
call vimtap#Plan(9)
call vimtap#Plan(13)
call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', 'X', 'g'), 'fXXbar is lXXsie XrigXn', 'global substitution')
call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', 'X', [3]), 'foobar is loXsie origon', 'substitute single index')
@@ -14,4 +14,9 @@ call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', 'X', [-1]), 'f
call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', 'X', [2, 3, 4], 0), 'foobar is lXXsie Xrigon', 'substitute without inversion')
call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', 'X', [2, 3, 4], 1), 'fXXbar is loosie origXn', 'substitute with inversion')
call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', '&&', [0, 1, 2, 3, 5]), 'foooobar is loooosie origoon', 'substitution with &')
call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', '\t', [1, 3]), "fo\tbar is lo\tsie origon", 'substitution with \t')
call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', '\(.\)\(oo\)\(.\)', '\3\2\1', [1]), 'foobar is soolie origon', 'substitution with capture groups')
call vimtap#Is(ingo#subst#Indexed('foobar is loosie origon', 'o', '\=toupper(repeat(submatch(0), 3))', [1, 3]), 'foOOObar is loOOOsie origon', 'substitution with sub-replace-expression')
call vimtest#Quit()