From f01493c55062c01b1cdf9b1e946577f4d1bdddf3 Mon Sep 17 00:00:00 2001 From: Yegappan Lakshmanan Date: Sun, 14 Apr 2024 23:21:02 +0200 Subject: patch 9.1.0329: String interpolation fails for Dict type Problem: String interpolation fails for Dict type Solution: Support Dict data type properly, also support :put =Dict (without having to convert it to string() first) (Yegappan Lakshmanan) fixes: #14529 closes: #14541 Signed-off-by: Yegappan Lakshmanan Signed-off-by: Christian Brabandt --- src/eval.c | 5 ++++- src/testdir/test_edit.vim | 4 ++-- src/testdir/test_expr.vim | 4 ++++ src/testdir/test_let.vim | 7 +++++++ src/testdir/test_put.vim | 9 +++++++++ src/testdir/test_vim9_assign.vim | 10 ++++++++++ src/version.c | 2 ++ src/vim9execute.c | 1 + src/vim9instr.c | 2 +- 9 files changed, 40 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/eval.c b/src/eval.c index e4c8baefb3..d81ef17526 100644 --- a/src/eval.c +++ b/src/eval.c @@ -575,7 +575,8 @@ skip_expr_concatenate( /* * Convert "tv" to a string. - * When "convert" is TRUE convert a List into a sequence of lines. + * When "convert" is TRUE convert a List into a sequence of lines and a Dict + * into a textual representation of the Dict. * Returns an allocated string (NULL when out of memory). */ char_u * @@ -596,6 +597,8 @@ typval2string(typval_T *tv, int convert) ga_append(&ga, NUL); retval = (char_u *)ga.ga_data; } + else if (convert && tv->v_type == VAR_DICT) + retval = dict2string(tv, get_copyID(), FALSE); else retval = vim_strsave(tv_get_string(tv)); return retval; diff --git a/src/testdir/test_edit.vim b/src/testdir/test_edit.vim index cdf20976e1..789e44cd25 100644 --- a/src/testdir/test_edit.vim +++ b/src/testdir/test_edit.vim @@ -1962,8 +1962,8 @@ func Test_edit_ctrl_r_failed() let buf = RunVimInTerminal('', #{rows: 6, cols: 60}) - " trying to insert a dictionary produces an error - call term_sendkeys(buf, "i\={}\") + " trying to insert a blob produces an error + call term_sendkeys(buf, "i\=0z\") " ending Insert mode should put the cursor back on the ':' call term_sendkeys(buf, ":\") diff --git a/src/testdir/test_expr.vim b/src/testdir/test_expr.vim index c1869c1d1f..1fa460d09a 100644 --- a/src/testdir/test_expr.vim +++ b/src/testdir/test_expr.vim @@ -950,6 +950,10 @@ func Test_string_interp() endif call assert_equal(0, tmp) + #" Dict interpolation + VAR d = {'a': 10, 'b': [1, 2]} + call assert_equal("{'a': 10, 'b': [1, 2]}", $'{d}') + #" Stray closing brace. call assert_fails('echo $"moo}"', 'E1278:') #" Undefined variable in expansion. diff --git a/src/testdir/test_let.vim b/src/testdir/test_let.vim index 17f78ea347..fec02731c4 100644 --- a/src/testdir/test_let.vim +++ b/src/testdir/test_let.vim @@ -689,6 +689,13 @@ END END call assert_equal(['let a = {abc}', 'let b = X', 'let c = {'], code) + " Evaluate a dictionary + let d1 = #{a: 10, b: 'ss', c: {}} + let code =<< eval trim END + let d2 = {d1} + END + call assert_equal(["let d2 = {'a': 10, 'b': 'ss', 'c': {}}"], code) + let code = 'xxx' let code =<< eval trim END let n = {5 + diff --git a/src/testdir/test_put.vim b/src/testdir/test_put.vim index 5b5c354c45..6f1e992e36 100644 --- a/src/testdir/test_put.vim +++ b/src/testdir/test_put.vim @@ -319,4 +319,13 @@ func Test_put_visual_replace_fold_marker() bwipe! endfunc +func Test_put_dict() + new + let d = #{a: #{b: 'abc'}, c: [1, 2], d: 0z10} + put! =d + call assert_equal(["{'a': {'b': 'abc'}, 'c': [1, 2], 'd': 0z10}", ''], + \ getline(1, '$')) + bw! +endfunc + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/testdir/test_vim9_assign.vim b/src/testdir/test_vim9_assign.vim index 0beaa440a8..4da368c663 100644 --- a/src/testdir/test_vim9_assign.vim +++ b/src/testdir/test_vim9_assign.vim @@ -2984,6 +2984,16 @@ def Test_heredoc_expr() CODE v9.CheckDefAndScriptSuccess(lines) + # Evaluate a dictionary + lines =<< trim CODE + var d1 = {'a': 10, 'b': [1, 2]} + var code =<< trim eval END + var d2 = {d1} + END + assert_equal(["var d2 = {'a': 10, 'b': [1, 2]}"], code) + CODE + v9.CheckDefAndScriptSuccess(lines) + lines =<< trim CODE var code =<< eval trim END var s = "{$SOME_ENV_VAR}" diff --git a/src/version.c b/src/version.c index 957475dfe8..91a3d612e7 100644 --- a/src/version.c +++ b/src/version.c @@ -704,6 +704,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ +/**/ + 329, /**/ 328, /**/ diff --git a/src/vim9execute.c b/src/vim9execute.c index 2d128e05b4..b3e89396e8 100644 --- a/src/vim9execute.c +++ b/src/vim9execute.c @@ -1653,6 +1653,7 @@ do_2string(typval_T *tv, int is_2string_any, int tolerant) case VAR_BOOL: case VAR_NUMBER: case VAR_FLOAT: + case VAR_DICT: case VAR_BLOB: break; case VAR_LIST: diff --git a/src/vim9instr.c b/src/vim9instr.c index a2179f3ecc..48ebf1ae4b 100644 --- a/src/vim9instr.c +++ b/src/vim9instr.c @@ -222,6 +222,7 @@ may_generate_2STRING(int offset, int tolerant, cctx_T *cctx) // conversion possible when tolerant case VAR_LIST: + case VAR_DICT: if (tolerant) { isntype = ISN_2STRING_ANY; @@ -234,7 +235,6 @@ may_generate_2STRING(int offset, int tolerant, cctx_T *cctx) case VAR_BLOB: case VAR_FUNC: case VAR_PARTIAL: - case VAR_DICT: case VAR_JOB: case VAR_CHANNEL: case VAR_INSTR: -- cgit v1.2.3