Skip to content

Commit

Permalink
patch 8.1.1705: using ~{} for a literal dict is not nice
Browse files Browse the repository at this point in the history
Problem:    Using ~{} for a literal dict is not nice.
Solution:   Use #{} instead.
  • Loading branch information
brammool committed Jul 16, 2019
1 parent 69a5b86 commit 4c6d904
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 181 deletions.
8 changes: 4 additions & 4 deletions runtime/doc/eval.txt
Expand Up @@ -60,7 +60,7 @@ Dictionary An associative, unordered array: Each entry has a key and a
value. |Dictionary| value. |Dictionary|
Examples: Examples:
{'blue': "#0000ff", 'red': "#ff0000"} {'blue': "#0000ff", 'red': "#ff0000"}
~{blue: "#0000ff", red: "#ff0000"} #{blue: "#0000ff", red: "#ff0000"}


Funcref A reference to a function |Funcref|. Funcref A reference to a function |Funcref|.
Example: function("strlen") Example: function("strlen")
Expand Down Expand Up @@ -482,11 +482,11 @@ entry. Note that the String '04' and the Number 04 are different, since the
Number will be converted to the String '4'. The empty string can also be used Number will be converted to the String '4'. The empty string can also be used
as a key. as a key.
*literal-Dict* *literal-Dict*
To avoid having to put quotes around every key the ~{} form can be used. This To avoid having to put quotes around every key the #{} form can be used. This
does require the key to consist only of ASCII letters, digits, '-' and '_'. does require the key to consist only of ASCII letters, digits, '-' and '_'.
Example: > Example: >
let mydict = ~{zero: 0, one_key: 1, two-key: 2, 333: 3} let mydict = #{zero: 0, one_key: 1, two-key: 2, 333: 3}
Note that 333 here is the string "333". Empty keys are not possible here. Note that 333 here is the string "333". Empty keys are not possible with #{}.


A value can be any expression. Using a Dictionary for a value creates a A value can be any expression. Using a Dictionary for a value creates a
nested Dictionary: > nested Dictionary: >
Expand Down
20 changes: 10 additions & 10 deletions runtime/doc/popup.txt
Expand Up @@ -178,7 +178,7 @@ DETAILS *popup-function-details*
popup_atcursor({what}, {options}) *popup_atcursor()* popup_atcursor({what}, {options}) *popup_atcursor()*
Show the {what} above the cursor, and close it when the cursor Show the {what} above the cursor, and close it when the cursor
moves. This works like: > moves. This works like: >
call popup_create({what}, ~{ call popup_create({what}, #{
\ pos: 'botleft', \ pos: 'botleft',
\ line: 'cursor-1', \ line: 'cursor-1',
\ col: 'cursor', \ col: 'cursor',
Expand All @@ -191,7 +191,7 @@ popup_beval({what}, {options}) *popup_beval()*
Show the {what} above the position from 'ballooneval' and Show the {what} above the position from 'ballooneval' and
close it when the mouse moves. This works like: > close it when the mouse moves. This works like: >
let pos = screenpos(v:beval_winnr, v:beval_lnum, v:beval_col) let pos = screenpos(v:beval_winnr, v:beval_lnum, v:beval_col)
call popup_create({what}, ~{ call popup_create({what}, #{
\ pos: 'botleft', \ pos: 'botleft',
\ line: pos.row - 1, \ line: pos.row - 1,
\ col: pos.col, \ col: pos.col,
Expand Down Expand Up @@ -240,7 +240,7 @@ popup_create({what}, {options}) *popup_create()*


popup_dialog({what}, {options}) *popup_dialog()* popup_dialog({what}, {options}) *popup_dialog()*
Just like |popup_create()| but with these default options: > Just like |popup_create()| but with these default options: >
call popup_create({what}, ~{ call popup_create({what}, #{
\ pos: 'center', \ pos: 'center',
\ zindex: 200, \ zindex: 200,
\ drag: 1, \ drag: 1,
Expand All @@ -249,7 +249,7 @@ popup_dialog({what}, {options}) *popup_dialog()*
\}) \})
< Use {options} to change the properties. E.g. add a 'filter' < Use {options} to change the properties. E.g. add a 'filter'
option with value 'popup_filter_yesno'. Example: > option with value 'popup_filter_yesno'. Example: >
call popup_create('do you want to quit (Yes/no)?', ~{ call popup_create('do you want to quit (Yes/no)?', #{
\ filter: 'popup_filter_yesno', \ filter: 'popup_filter_yesno',
\ callback: 'QuitCallback', \ callback: 'QuitCallback',
\ }) \ })
Expand Down Expand Up @@ -356,7 +356,7 @@ popup_menu({what}, {options}) *popup_menu()*
items with cursorkeys, and close it an item is selected with items with cursorkeys, and close it an item is selected with
Space or Enter. {what} should have multiple lines to make this Space or Enter. {what} should have multiple lines to make this
useful. This works like: > useful. This works like: >
call popup_create({what}, ~{ call popup_create({what}, #{
\ pos: 'center', \ pos: 'center',
\ zindex: 200, \ zindex: 200,
\ drag: 1, \ drag: 1,
Expand Down Expand Up @@ -391,7 +391,7 @@ popup_move({id}, {options}) *popup_move()*
popup_notification({what}, {options}) *popup_notification()* popup_notification({what}, {options}) *popup_notification()*
Show the {what} for 3 seconds at the top of the Vim window. Show the {what} for 3 seconds at the top of the Vim window.
This works like: > This works like: >
call popup_create({what}, ~{ call popup_create({what}, #{
\ line: 1, \ line: 1,
\ col: 10, \ col: 10,
\ minwidth: 20, \ minwidth: 20,
Expand Down Expand Up @@ -732,15 +732,15 @@ Prompt the user to press y/Y or n/N: >
endif endif
endfunc endfunc
call popup_dialog('Continue? y/n', ~{ call popup_dialog('Continue? y/n', #{
\ filter: 'popup_filter_yesno', \ filter: 'popup_filter_yesno',
\ callback: 'MyDialogHandler', \ callback: 'MyDialogHandler',
\ }) \ })
< <
*popup_menu-shortcut-example* *popup_menu-shortcut-example*
Extend popup_filter_menu() with shortcut keys: > Extend popup_filter_menu() with shortcut keys: >
call popup_menu(['Save', 'Cancel', 'Discard'], ~{ call popup_menu(['Save', 'Cancel', 'Discard'], #{
\ filter: 'MyMenuFilter', \ filter: 'MyMenuFilter',
\ callback: 'MyMenuHandler', \ callback: 'MyMenuHandler',
\ }) \ })
Expand Down Expand Up @@ -781,7 +781,7 @@ Example for using a popup window for 'ballooneval': >
endif endif
call popup_close(s:winid) call popup_close(s:winid)
endif endif
let s:winid = popup_beval(v:beval_text, ~{mousemoved: 'word'}) let s:winid = popup_beval(v:beval_text, #{mousemoved: 'word'})
let s:last_text = v:beval_text let s:last_text = v:beval_text
return '' return ''
endfunc endfunc
Expand Down Expand Up @@ -812,7 +812,7 @@ this example simulated with a timer callback: >
endfunc endfunc
func ShowPopup(id) func ShowPopup(id)
let s:winid = popup_beval(s:balloonText, ~{mousemoved: 'word'}) let s:winid = popup_beval(s:balloonText, #{mousemoved: 'word'})
endfunc endfunc
< <


Expand Down
9 changes: 4 additions & 5 deletions src/eval.c
Expand Up @@ -4392,7 +4392,7 @@ eval6(
* (expression) nested expression * (expression) nested expression
* [expr, expr] List * [expr, expr] List
* {key: val, key: val} Dictionary * {key: val, key: val} Dictionary
* ~{key: val, key: val} Dictionary with literal keys * #{key: val, key: val} Dictionary with literal keys
* *
* Also handle: * Also handle:
* ! in front logical NOT * ! in front logical NOT
Expand Down Expand Up @@ -4577,9 +4577,9 @@ eval7(
break; break;


/* /*
* Dictionary: ~{key: val, key: val} * Dictionary: #{key: val, key: val}
*/ */
case '~': if ((*arg)[1] == '{') case '#': if ((*arg)[1] == '{')
{ {
++*arg; ++*arg;
ret = dict_get_tv(arg, rettv, evaluate, TRUE); ret = dict_get_tv(arg, rettv, evaluate, TRUE);
Expand Down Expand Up @@ -7963,8 +7963,7 @@ find_var_ht(char_u *name, char_u **varname)
*varname = name + 2; *varname = name + 2;
if (*name == 'g') /* global variable */ if (*name == 'g') /* global variable */
return &globvarht; return &globvarht;
/* There must be no ':' or '#' in the rest of the name, unless g: is used // There must be no ':' or '#' in the rest of the name, unless g: is used
*/
if (vim_strchr(name + 2, ':') != NULL if (vim_strchr(name + 2, ':') != NULL
|| vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL) || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
return NULL; return NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/testdir/test_listdict.vim
Expand Up @@ -281,7 +281,7 @@ func Test_dict_func_remove_in_use()
endfunc endfunc


func Test_dict_literal_keys() func Test_dict_literal_keys()
call assert_equal({'one': 1, 'two2': 2, '3three': 3, '44': 4}, ~{one: 1, two2: 2, 3three: 3, 44: 4},) call assert_equal({'one': 1, 'two2': 2, '3three': 3, '44': 4}, #{one: 1, two2: 2, 3three: 3, 44: 4},)


" why *{} cannot be used " why *{} cannot be used
let blue = 'blue' let blue = 'blue'
Expand Down

0 comments on commit 4c6d904

Please sign in to comment.