vim-patch:9.1.0329: String interpolation fails for Dict type (#28335)

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: vim/vim#14529
closes: vim/vim#14541

f01493c550

Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
This commit is contained in:
zeertzjq
2024-04-15 07:11:39 +08:00
committed by GitHub
parent 7180ef6901
commit 43f8d7e3ef
11 changed files with 62 additions and 48 deletions

View File

@@ -52,43 +52,34 @@ describe('insert-mode', function()
end)
it('double quote is removed after hit-enter prompt #22609', function()
local screen = Screen.new(60, 6)
screen:set_default_attr_ids({
[0] = { bold = true, foreground = Screen.colors.Blue }, -- NonText
[1] = { foreground = Screen.colors.Blue }, -- SpecialKey
[2] = { foreground = Screen.colors.SlateBlue },
[3] = { bold = true }, -- ModeMsg
[4] = { reverse = true, bold = true }, -- MsgSeparator
[5] = { background = Screen.colors.Red, foreground = Screen.colors.White }, -- ErrorMsg
[6] = { foreground = Screen.colors.SeaGreen, bold = true }, -- MoreMsg
})
local screen = Screen.new(50, 6)
screen:attach()
feed('i<C-R>')
screen:expect([[
{1:^"} |
{0:~ }|*4
{3:-- INSERT --} |
{18:^"} |
{1:~ }|*4
{5:-- INSERT --} |
]])
feed('={}')
feed("=function('add')")
screen:expect([[
{1:"} |
{0:~ }|*4
={2:{}}^ |
{18:"} |
{1:~ }|*4
={25:function}{16:(}{26:'add'}{16:)}^ |
]])
feed('<CR>')
screen:expect([[
{1:"} |
{0:~ }|
{4: }|
={2:{}} |
{5:E731: Using a Dictionary as a String} |
{6:Press ENTER or type command to continue}^ |
{18:"} |
{1:~ }|
{3: }|
={25:function}{16:(}{26:'add'}{16:)} |
{9:E729: Using a Funcref as a String} |
{6:Press ENTER or type command to continue}^ |
]])
feed('<CR>')
screen:expect([[
^ |
{0:~ }|*4
{3:-- INSERT --} |
^ |
{1:~ }|*4
{5:-- INSERT --} |
]])
end)
end)

View File

@@ -56,20 +56,20 @@ describe('edit', function()
{1:~ }|*4
{5:-- INSERT --} |
]])
feed('={}')
feed('=0z')
screen:expect([[
{18:"} |
{1:~ }|*4
={16:{}}^ |
={26:0}{9:z}^ |
]])
-- trying to insert a dictionary produces an error
-- trying to insert a blob produces an error
feed('<CR>')
screen:expect([[
{18:"} |
{1:~ }|
{3: }|
={16:{}} |
{9:E731: Using a Dictionary as a String} |
={26:0}{9:z} |
{9:E976: Using a Blob as a String} |
{6:Press ENTER or type command to continue}^ |
]])

View File

@@ -1983,8 +1983,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>")

View File

@@ -904,6 +904,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.

View File

@@ -687,6 +687,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 +

View File

@@ -323,4 +323,13 @@ func Test_put_visual_replace_fold_marker()
call assert_equal(lines, getline(1, '$'))
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