]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.0329: String interpolation fails for Dict type v9.1.0329
authorYegappan Lakshmanan <yegappan@yahoo.com>
Sun, 14 Apr 2024 21:21:02 +0000 (23:21 +0200)
committerChristian Brabandt <cb@256bit.org>
Sun, 14 Apr 2024 21:21:02 +0000 (23:21 +0200)
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 <yegappan@yahoo.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
runtime/doc/change.txt
src/eval.c
src/testdir/test_edit.vim
src/testdir/test_expr.vim
src/testdir/test_let.vim
src/testdir/test_put.vim
src/testdir/test_vim9_assign.vim
src/version.c
src/vim9execute.c
src/vim9instr.c

index 7063cad304b827ec4674f28fa0a5679e54b1aac4..cd3d6add4a98163ad5347470980ecbed3933ae6f 100644 (file)
@@ -1,4 +1,4 @@
-*change.txt*    For Vim version 9.1.  Last change: 2023 Dec 19
+*change.txt*    For Vim version 9.1.  Last change: 2024 Apr 14
 
 
                  VIM REFERENCE MANUAL    by Bram Moolenaar
@@ -1345,8 +1345,8 @@ expression (like with the "/" command).
 The expression must evaluate to a String.  A Number is always automatically
 converted to a String.  For the "p" and ":put" command, if the result is a
 Float it's converted into a String.  If the result is a List each element is
-turned into a String and used as a line.  A Dictionary or FuncRef results in
-an error message (use string() to convert).
+turned into a String and used as a line.  A Dictionary is converted into a
+String.  A FuncRef results in an error message (use string() to convert).
 
 If the "= register is used for the "p" command, the String is split up at <NL>
 characters.  If the String ends in a <NL>, it is regarded as a linewise
index e4c8baefb3922d40c2f0cc715e67d82272be706e..d81ef17526ab5c4e6689a4b41064a9066c7d8b79 100644 (file)
@@ -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;
index cdf20976e119472cf0e7e652cc74382ac330f12f..789e44cd2596fca1237012b0587019a5617d1eea 100644 (file)
@@ -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\<C-R>={}\<CR>")
+  " trying to insert a blob produces an error
+  call term_sendkeys(buf, "i\<C-R>=0z\<CR>")
 
   " ending Insert mode should put the cursor back on the ':'
   call term_sendkeys(buf, ":\<Esc>")
index c1869c1d1fa6dc6973bed3935a4f0e7ac2d21382..1fa460d09adee1ba583d415f3779304f6f9badc0 100644 (file)
@@ -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.
index 17f78ea347d3eba92940e02fe93dc3e9a5e34b2c..fec02731c4f00043a8d7cadd61b397533ef1787d 100644 (file)
@@ -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 +
index 5b5c354c45579f5860044626b2b0e46f03cd980b..6f1e992e36c675348ac0859ade7890018beac891 100644 (file)
@@ -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
index 0beaa440a8db714bd8f78bab1779c5f68a9e97cc..4da368c663b0f1e74fd3ef1897f95c7b887fa4f7 100644 (file)
@@ -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}"
index 957475dfe88a5bf07dc4c8a65861111cc2070155..91a3d612e7bfffa02ad253a3e7df24ef18af391b 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    329,
 /**/
     328,
 /**/
index 2d128e05b4f12493d1a78246f4630a61a1ece0a1..b3e89396e82afe338b8e9d2c8ffc1a20bb40fcea 100644 (file)
@@ -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:
index a2179f3ecc9421435d73d4be4c046d330fa10ac8..48ebf1ae4b7cb7c1f0f14833e5a109886ae4d999 100644 (file)
@@ -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: