Skip to content

Commit 0c6ceaf

Browse files
committed
patch 8.2.0298: Vim9 script: cannot start command with a string constant
Problem: Vim9 script: cannot start command with a string constant. Solution: Recognize expression starting with '('.
1 parent 8b430b4 commit 0c6ceaf

5 files changed

Lines changed: 17 additions & 17 deletions

File tree

runtime/doc/vim9.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,21 +131,23 @@ Functions can be called without `:call`: >
131131
Using `:call` is still possible, but this is discouraged.
132132

133133
A method call without `eval` is possible, so long as the start is an
134-
identifier or can't be an Ex command. It does not work for string constants: >
134+
identifier or can't be an Ex command. It does NOT work for string constants: >
135135
myList->add(123) " works
136136
g:myList->add(123) " works
137137
[1, 2, 3]->Process() " works
138138
#{a: 1, b: 2}->Process() " works
139139
{'a': 1, 'b': 2}->Process() " works
140140
"foobar"->Process() " does NOT work
141-
eval "foobar"->Process() " works
141+
("foobar")->Process() " works
142+
'foobar'->Process() " does NOT work
143+
('foobar')->Process() " works
142144
143145
In case there is ambiguity between a function name and an Ex command, use ":"
144146
to make clear you want to use the Ex command. For example, there is both the
145147
`:substitute` command and the `substitute()` function. When the line starts
146148
with `substitute(` this will use the function, prepend a colon to use the
147149
command instead: >
148-
:substitute(pattern(replacement(
150+
:substitute(pattern (replacement (
149151
150152
151153
No curly braces expansion ~

src/ex_docmd.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3146,8 +3146,9 @@ find_ex_command(
31463146
* Recognize a Vim9 script function/method call and assignment:
31473147
* "lvar = value", "lvar(arg)", "[1, 2 3]->Func()"
31483148
*/
3149-
if (lookup != NULL && (p = to_name_const_end(eap->cmd)) > eap->cmd
3150-
&& *p != NUL)
3149+
p = eap->cmd;
3150+
if (lookup != NULL && (*p == '('
3151+
|| ((p = to_name_const_end(eap->cmd)) > eap->cmd && *p != NUL)))
31513152
{
31523153
int oplen;
31533154
int heredoc;
@@ -3156,6 +3157,7 @@ find_ex_command(
31563157
// "varname[]" is an expression.
31573158
// "g:varname" is an expression.
31583159
// "varname->expr" is an expression.
3160+
// "(..." is an expression.
31593161
if (*p == '('
31603162
|| *p == '['
31613163
|| p[1] == ':'

src/testdir/test_vim9_script.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,11 @@ def Test_vim9script_call()
370370
assert_equal(#{a: 1, b: 2}, dictvar)
371371
#{a: 3, b: 4}->DictFunc()
372372
assert_equal(#{a: 3, b: 4}, dictvar)
373+
374+
('text')->MyFunc()
375+
assert_equal('text', var)
376+
("some")->MyFunc()
377+
assert_equal('some', var)
373378
END
374379
writefile(lines, 'Xcall.vim')
375380
source Xcall.vim

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,8 @@ static char *(features[]) =
738738

739739
static int included_patches[] =
740740
{ /* Add new patch number below this line */
741+
/**/
742+
298,
741743
/**/
742744
297,
743745
/**/

src/vim9compile.c

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4821,22 +4821,11 @@ compile_def_function(ufunc_T *ufunc, int set_return_type)
48214821
p = (*ea.cmd == '&' || *ea.cmd == '$' || *ea.cmd == '@')
48224822
? ea.cmd + 1 : ea.cmd;
48234823
p = to_name_end(p);
4824-
if (p > ea.cmd && *p != NUL)
4824+
if ((p > ea.cmd && *p != NUL) || *p == '(')
48254825
{
48264826
int oplen;
48274827
int heredoc;
48284828

4829-
// "funcname(" is always a function call.
4830-
// "varname[]" is an expression.
4831-
// "varname->expr" is an expression.
4832-
if (*p == '('
4833-
|| *p == '['
4834-
|| ((p - ea.cmd) > 2 && ea.cmd[1] == ':')
4835-
|| (*p == '-' && p[1] == '>'))
4836-
{
4837-
// TODO
4838-
}
4839-
48404829
oplen = assignment_len(skipwhite(p), &heredoc);
48414830
if (oplen > 0)
48424831
{

0 commit comments

Comments
 (0)