0
0
mirror of https://github.com/vim/vim.git synced 2025-09-25 03:54:15 -04:00

patch 8.2.3908: cannot use a script-local function for 'foldtext'

Problem:    Cannot use a script-local function for 'foldtext'.
Solution:   Expand "s:" and "<SID>". (Yegappan Lakshmanan, closes #9411)
This commit is contained in:
Yegappan Lakshmanan
2021-12-26 21:54:43 +00:00
committed by Bram Moolenaar
parent c553a21e18
commit 27708e6c7b
9 changed files with 60 additions and 4 deletions

View File

@@ -122,7 +122,7 @@ Try to avoid the "=", "a" and "s" return values, since Vim often has to search
backwards for a line for which the fold level is defined. This can be slow. backwards for a line for which the fold level is defined. This can be slow.
If the 'foldexpr' expression starts with s: or |<SID>|, then it is replaced If the 'foldexpr' expression starts with s: or |<SID>|, then it is replaced
with the script ID (|local-function|). Example: > with the script ID (|local-function|). Examples: >
set foldexpr=s:MyFoldExpr() set foldexpr=s:MyFoldExpr()
set foldexpr=<SID>SomeFoldExpr() set foldexpr=<SID>SomeFoldExpr()
< <
@@ -529,6 +529,11 @@ The resulting line is truncated to fit in the window, it never wraps.
When there is room after the text, it is filled with the character specified When there is room after the text, it is filled with the character specified
by 'fillchars'. by 'fillchars'.
If the 'foldtext' expression starts with s: or |<SID>|, then it is replaced
with the script ID (|local-function|). Examples: >
set foldtext=s:MyFoldText()
set foldtext=<SID>SomeFoldText()
<
Note that backslashes need to be used for characters that the ":set" command Note that backslashes need to be used for characters that the ":set" command
handles differently: Space, backslash and double-quote. |option-backslash| handles differently: Space, backslash and double-quote. |option-backslash|

View File

@@ -2310,6 +2310,7 @@ ambw_end:
# endif # endif
# ifdef FEAT_FOLDING # ifdef FEAT_FOLDING
varp == &curwin->w_p_fde || varp == &curwin->w_p_fde ||
varp == &curwin->w_p_fdt ||
# endif # endif
gvarp == &p_fex || gvarp == &p_fex ||
# ifdef FEAT_FIND_ID # ifdef FEAT_FIND_ID
@@ -2341,8 +2342,10 @@ ambw_end:
p_opt = &p_dex; p_opt = &p_dex;
# endif # endif
# ifdef FEAT_FOLDING # ifdef FEAT_FOLDING
if(varp == &curwin->w_p_fde) // 'foldexpr' if (varp == &curwin->w_p_fde) // 'foldexpr'
p_opt = &curwin->w_p_fde; p_opt = &curwin->w_p_fde;
if (varp == &curwin->w_p_fdt) // 'foldtext'
p_opt = &curwin->w_p_fdt;
# endif # endif
if (gvarp == &p_fex) // 'formatexpr' if (gvarp == &p_fex) // 'formatexpr'
p_opt = &curbuf->b_p_fex; p_opt = &curbuf->b_p_fex;

View File

@@ -905,8 +905,6 @@ string_filter_map(
set_vim_var_nr(VV_KEY, idx); set_vim_var_nr(VV_KEY, idx);
if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL if (filter_map_one(&tv, expr, filtermap, &newtv, &rem) == FAIL
|| did_emsg) || did_emsg)
break;
if (did_emsg)
{ {
clear_tv(&newtv); clear_tv(&newtv);
clear_tv(&tv); clear_tv(&tv);

View File

@@ -37,6 +37,7 @@ func Test_blob_create()
call assert_fails('VAR b = 0z001122.') call assert_fails('VAR b = 0z001122.')
call assert_fails('call get("", 1)', 'E896:') call assert_fails('call get("", 1)', 'E896:')
call assert_equal(0, len(test_null_blob())) call assert_equal(0, len(test_null_blob()))
call assert_equal(0z, copy(test_null_blob()))
END END
call CheckLegacyAndVim9Success(lines) call CheckLegacyAndVim9Success(lines)
endfunc endfunc
@@ -369,6 +370,14 @@ func Test_blob_add()
add(test_null_blob(), 0x22) add(test_null_blob(), 0x22)
END END
call CheckDefExecAndScriptFailure(lines, 'E1131:') call CheckDefExecAndScriptFailure(lines, 'E1131:')
let lines =<< trim END
let b = 0zDEADBEEF
lockvar b
call add(b, 0)
unlockvar b
END
call CheckScriptFailure(lines, 'E741:')
endfunc endfunc
func Test_blob_empty() func Test_blob_empty()
@@ -445,6 +454,9 @@ func Test_blob_func_remove()
remove(b, 0) remove(b, 0)
END END
call CheckScriptFailure(lines, 'E741:') call CheckScriptFailure(lines, 'E741:')
call assert_fails('echo remove(0z1020, [])', 'E745:')
call assert_fails('echo remove(0z1020, 0, [])', 'E745:')
endfunc endfunc
func Test_blob_read_write() func Test_blob_read_write()
@@ -474,6 +486,7 @@ func Test_blob_filter()
call assert_equal(0zADEF, filter(0zDEADBEEF, 'v:key % 2')) call assert_equal(0zADEF, filter(0zDEADBEEF, 'v:key % 2'))
END END
call CheckLegacyAndVim9Success(lines) call CheckLegacyAndVim9Success(lines)
call assert_fails('echo filter(0z10, "a10")', 'E121:')
endfunc endfunc
" map() item in blob " map() item in blob
@@ -489,6 +502,7 @@ func Test_blob_map()
call map(0z00, '[9]') call map(0z00, '[9]')
END END
call CheckLegacyAndVim9Failure(lines, 'E978:') call CheckLegacyAndVim9Failure(lines, 'E978:')
call assert_fails('echo map(0z10, "a10")', 'E121:')
endfunc endfunc
func Test_blob_index() func Test_blob_index()

View File

@@ -405,6 +405,7 @@ func Test_printf_misc()
call assert_equal('[00000あiう]', printf('[%010.7S]', 'あiう')) call assert_equal('[00000あiう]', printf('[%010.7S]', 'あiう'))
call assert_equal('1%', printf('%d%%', 1)) call assert_equal('1%', printf('%d%%', 1))
call assert_notequal('', printf('%p', "abc"))
END END
call CheckLegacyAndVim9Success(lines) call CheckLegacyAndVim9Success(lines)

View File

@@ -183,6 +183,7 @@ func Test_filter_map_string()
call assert_equal('', map('', "v:val == 'a'")) call assert_equal('', map('', "v:val == 'a'"))
call assert_equal('', map(test_null_string(), "v:val == 'a'")) call assert_equal('', map(test_null_string(), "v:val == 'a'"))
call assert_fails('echo map("abc", "10")', 'E928:') call assert_fails('echo map("abc", "10")', 'E928:')
call assert_fails('echo map("abc", "a10")', 'E121:')
END END
call CheckLegacyAndVim9Success(lines) call CheckLegacyAndVim9Success(lines)

View File

@@ -1408,4 +1408,35 @@ func Test_foldexpr_scriptlocal_func()
bw! bw!
endfunc endfunc
" Test for using a script-local function for 'foldtext'
func Test_foldtext_scriptlocal_func()
func! s:FoldText()
let g:FoldTextArgs = [v:foldstart, v:foldend]
return foldtext()
endfunc
new | only
call setline(1, range(50))
let g:FoldTextArgs = []
set foldmethod=manual
set foldtext=s:FoldText()
norm! 4Gzf4j
redraw!
call assert_equal(expand('<SID>') .. 'FoldText()', &foldtext)
call assert_equal([4, 8], g:FoldTextArgs)
set foldtext&
bw!
new | only
call setline(1, range(50))
let g:FoldTextArgs = []
set foldmethod=manual
set foldtext=<SID>FoldText()
norm! 8Gzf4j
redraw!
call assert_equal(expand('<SID>') .. 'FoldText()', &foldtext)
call assert_equal([8, 12], g:FoldTextArgs)
set foldtext&
bw!
delfunc s:FoldText
endfunc
" vim: shiftwidth=2 sts=2 expandtab " vim: shiftwidth=2 sts=2 expandtab

View File

@@ -1163,6 +1163,7 @@ func Test_listdict_extend()
let d = {'a': 'A', 'b': 'B'} let d = {'a': 'A', 'b': 'B'}
call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'error')", 'E737:') call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'error')", 'E737:')
call assert_fails("call extend(d, {'b': 0}, [])", 'E730:')
call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'xxx')", 'E475:') call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 'xxx')", 'E475:')
if has('float') if has('float')
call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 1.2)", 'E475:') call assert_fails("call extend(d, {'b': 0, 'c':'C'}, 1.2)", 'E475:')

View File

@@ -749,6 +749,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
3908,
/**/ /**/
3907, 3907,
/**/ /**/