diff options
author | zeertzjq <zeertzjq@outlook.com> | 2023-02-24 15:03:28 +0800 |
---|---|---|
committer | zeertzjq <zeertzjq@outlook.com> | 2023-02-24 15:36:08 +0800 |
commit | 904d0995837a2569ae640f5253da3dd4569fec6f (patch) | |
tree | cf18e60265b26ed416e71c64b0370c3cb436e73e /src | |
parent | 47bc297d81cf7dab5a28608657ddab3c09f794d0 (diff) | |
download | rneovim-904d0995837a2569ae640f5253da3dd4569fec6f.tar.gz rneovim-904d0995837a2569ae640f5253da3dd4569fec6f.tar.bz2 rneovim-904d0995837a2569ae640f5253da3dd4569fec6f.zip |
vim-patch:8.2.2449: Vim9: flatten() always changes the list type
Problem: Vim9: flatten() always changes the list type.
Solution: Disallow using flatten() and add flattennew().
https://github.com/vim/vim/commit/3b690069730805a147d45d92eaca4dc838272d1d
Co-authored-by: Bram Moolenaar <Bram@vim.org>
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/eval.lua | 1 | ||||
-rw-r--r-- | src/nvim/eval/funcs.c | 41 | ||||
-rw-r--r-- | src/nvim/eval/typval.c | 9 | ||||
-rw-r--r-- | src/nvim/testdir/test_flatten.vim | 11 |
4 files changed, 49 insertions, 13 deletions
diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 9a5ab51c71..a1caeea95a 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -128,6 +128,7 @@ return { finddir={args={1, 3}, base=1}, findfile={args={1, 3}, base=1}, flatten={args={1, 2}, base=1}, + flattennew={args={1, 2}, base=1}, float2nr={args=1, base=1}, floor={args=1, base=1, float_func="floor"}, fmod={args=2, base=1}, diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 6f983d3208..3a58b90f96 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -1870,8 +1870,8 @@ static void f_expandcmd(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) rettv->vval.v_string = cmdstr; } -/// "flatten(list[, {maxdepth}])" function -static void f_flatten(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +/// "flatten()" and "flattennew()" functions +static void flatten_common(typval_T *argvars, typval_T *rettv, bool make_copy) { bool error = false; @@ -1895,13 +1895,38 @@ static void f_flatten(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) } list_T *list = argvars[0].vval.v_list; - if (list != NULL - && !value_check_lock(tv_list_locked(list), - N_("flatten() argument"), - TV_TRANSLATE) - && tv_list_flatten(list, maxdepth) == OK) { - tv_copy(&argvars[0], rettv); + rettv->v_type = VAR_LIST; + rettv->vval.v_list = list; + if (list == NULL) { + return; } + + if (make_copy) { + list = tv_list_copy(NULL, list, true, get_copyID()); + rettv->vval.v_list = list; + if (list == NULL) { + return; + } + } else { + if (value_check_lock(tv_list_locked(list), N_("flatten() argument"), TV_TRANSLATE)) { + return; + } + tv_list_ref(list); + } + + tv_list_flatten(list, maxdepth); +} + +/// "flatten(list[, {maxdepth}])" function +static void f_flatten(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +{ + flatten_common(argvars, rettv, false); +} + +/// "flattennew(list[, {maxdepth}])" function +static void f_flattennew(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +{ + flatten_common(argvars, rettv, true); } /// "extend(list, list [, idx])" function diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index 9a3a1c3c0f..ab4245b59a 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -654,14 +654,14 @@ tv_list_copy_error: /// @param[in] maxdepth Maximum depth that will be flattened /// /// @return OK or FAIL -int tv_list_flatten(list_T *list, long maxdepth) - FUNC_ATTR_NONNULL_ARG(1) FUNC_ATTR_WARN_UNUSED_RESULT +void tv_list_flatten(list_T *list, long maxdepth) + FUNC_ATTR_NONNULL_ARG(1) { listitem_T *item; listitem_T *to_free; int n; if (maxdepth == 0) { - return OK; + return; } n = 0; @@ -669,7 +669,7 @@ int tv_list_flatten(list_T *list, long maxdepth) while (item != NULL) { fast_breakcheck(); if (got_int) { - return FAIL; + return; } if (item->li_tv.v_type == VAR_LIST) { listitem_T *next = item->li_next; @@ -695,7 +695,6 @@ int tv_list_flatten(list_T *list, long maxdepth) item = item->li_next; } } - return OK; } /// Extend first list with the second diff --git a/src/nvim/testdir/test_flatten.vim b/src/nvim/testdir/test_flatten.vim index 99086611e1..d8abc1ad68 100644 --- a/src/nvim/testdir/test_flatten.vim +++ b/src/nvim/testdir/test_flatten.vim @@ -79,3 +79,14 @@ func Test_flatten() call assert_equal([1, 2, 1, 2], flatten(l:x, 2)) call assert_equal([2, l:x], l:y) endfunc + +func Test_flattennew() + let l = [1, [2, [3, 4]], 5] + call assert_equal([1, 2, 3, 4, 5], flattennew(l)) + call assert_equal([1, [2, [3, 4]], 5], l) + + call assert_equal([1, 2, [3, 4], 5], flattennew(l, 1)) + call assert_equal([1, [2, [3, 4]], 5], l) +endfunc + +" vim: shiftwidth=2 sts=2 expandtab |