Skip to content

Commit 0072cee

Browse files
yegappanchrisbra
authored andcommitted
patch 9.1.0994: Vim9: not able to use comment after opening curly brace
Problem: Vim9: not able to use comment after opening curly brace (lifepillar) Solution: allow to use comments after curly braces of an inner-block, modify the logic to search for comment in a line, update Vim9 tests to use specific class type instead of any (Yegappan Lakshmanan) fixes: #16363 closes: #16405 Signed-off-by: Yegappan Lakshmanan <[email protected]> Signed-off-by: Christian Brabandt <[email protected]>
1 parent c97e869 commit 0072cee

File tree

5 files changed

+149
-16
lines changed

5 files changed

+149
-16
lines changed

src/testdir/test_vim9_class.vim

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4345,38 +4345,35 @@ def Test_lockvar_object_variable()
43454345
END
43464346
v9.CheckSourceFailure(lines, 'E1335: Variable "val4" in class "C" is not writable')
43474347

4348-
# TODO: the following tests use type "any" for argument. Need a run time
4349-
# check for access. Probably OK as is for now.
4350-
43514348
# read-only lockvar from object method arg
43524349
lines =<< trim END
43534350
vim9script
43544351

43554352
class C
43564353
var val5: number
4357-
def Lock(o_any: any)
4358-
lockvar o_any.val5
4354+
def Lock(c: C)
4355+
lockvar c.val5
43594356
enddef
43604357
endclass
43614358
var o = C.new(3)
43624359
o.Lock(C.new(5))
43634360
END
4364-
v9.CheckSourceFailure(lines, 'E1391: Cannot (un)lock variable "o_any.val5" in class "C"')
4361+
v9.CheckSourceFailure(lines, 'E1391: Cannot (un)lock variable "c.val5" in class "C"')
43654362

43664363
# read-only lockvar from class method arg
43674364
lines =<< trim END
43684365
vim9script
43694366

43704367
class C
43714368
var val6: number
4372-
static def Lock(o_any: any)
4373-
lockvar o_any.val6
4369+
static def Lock(c: C)
4370+
lockvar c.val6
43744371
enddef
43754372
endclass
43764373
var o = C.new(3)
43774374
C.Lock(o)
43784375
END
4379-
v9.CheckSourceFailure(lines, 'E1391: Cannot (un)lock variable "o_any.val6" in class "C"')
4376+
v9.CheckSourceFailure(lines, 'E1391: Cannot (un)lock variable "c.val6" in class "C"')
43804377

43814378
#
43824379
# lockvar of public object variable
@@ -4444,29 +4441,29 @@ def Test_lockvar_object_variable()
44444441

44454442
class C
44464443
public var val5: number
4447-
def Lock(o_any: any)
4448-
lockvar o_any.val5
4444+
def Lock(c: C)
4445+
lockvar c.val5
44494446
enddef
44504447
endclass
44514448
var o = C.new(3)
44524449
o.Lock(C.new(5))
44534450
END
4454-
v9.CheckSourceFailure(lines, 'E1391: Cannot (un)lock variable "o_any.val5" in class "C"', 1)
4451+
v9.CheckSourceFailure(lines, 'E1391: Cannot (un)lock variable "c.val5" in class "C"', 1)
44554452

44564453
# lockvar from class method arg
44574454
lines =<< trim END
44584455
vim9script
44594456

44604457
class C
44614458
public var val6: number
4462-
static def Lock(o_any: any)
4463-
lockvar o_any.val6
4459+
static def Lock(c: C)
4460+
lockvar c.val6
44644461
enddef
44654462
endclass
44664463
var o = C.new(3)
44674464
C.Lock(o)
44684465
END
4469-
v9.CheckSourceFailure(lines, 'E1391: Cannot (un)lock variable "o_any.val6" in class "C"', 1)
4466+
v9.CheckSourceFailure(lines, 'E1391: Cannot (un)lock variable "c.val6" in class "C"', 1)
44704467
enddef
44714468

44724469
" Test trying to lock a class variable from various places

src/testdir/test_vim9_func.vim

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4697,6 +4697,23 @@ def Test_test_override_defcompile()
46974697
test_override('defcompile', 0)
46984698
enddef
46994699

4700+
" Test for using a comment after the opening curly brace of an inner block.
4701+
def Test_comment_after_inner_block()
4702+
var lines =<< trim END
4703+
vim9script
4704+
4705+
def F(G: func)
4706+
enddef
4707+
4708+
F(() => { # comment1
4709+
F(() => { # comment2
4710+
echo 'ok' # comment3
4711+
}) # comment4
4712+
}) # comment5
4713+
END
4714+
v9.CheckScriptSuccess(lines)
4715+
enddef
4716+
47004717
" The following messes up syntax highlight, keep near the end.
47014718
if has('python3')
47024719
def Test_python3_command()

src/testdir/test_vim9_script.vim

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,27 @@ def Test_autocommand_block()
595595
unlet g:otherVar
596596
enddef
597597

598+
def Test_block_in_a_string()
599+
var lines =<< trim END
600+
vim9script
601+
602+
def Foo(): string
603+
var x = ' => { # abc'
604+
return x
605+
enddef
606+
607+
assert_equal(' => { # abc', Foo())
608+
609+
def Bar(): string
610+
var x = " => { # abc"
611+
return x
612+
enddef
613+
614+
assert_equal(" => { # abc", Bar())
615+
END
616+
v9.CheckSourceSuccess(lines)
617+
enddef
618+
598619
func g:NoSuchFunc()
599620
echo 'none'
600621
endfunc

src/userfunc.c

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,92 @@ function_using_block_scopes(ufunc_T *fp, cstack_T *cstack)
859859
cstack->cs_flags[i] |= CSF_FUNC_DEF;
860860
}
861861

862+
/*
863+
* Skip over all the characters in a single quoted string starting at "p" and
864+
* return a pointer to the character following the ending single quote.
865+
* If the ending single quote is missing, then return a pointer to the NUL
866+
* character.
867+
*/
868+
static char_u *
869+
skip_single_quote_string(char_u *p)
870+
{
871+
p++; // skip the beginning single quote
872+
while (*p != NUL)
873+
{
874+
// Within the string, a single quote can be escaped by using
875+
// two single quotes.
876+
if (*p == '\'' && *(p + 1) == '\'')
877+
p += 2;
878+
else if (*p == '\'')
879+
{
880+
p++; // skip the ending single quote
881+
break;
882+
}
883+
else
884+
MB_PTR_ADV(p);
885+
}
886+
887+
return p;
888+
}
889+
890+
/*
891+
* Skip over all the characters in a double quoted string starting at "p" and
892+
* return a pointer to the character following the ending double quote.
893+
* If the ending double quote is missing, then return a pointer to the NUL
894+
* character.
895+
*/
896+
static char_u *
897+
skip_double_quote_string(char_u *p)
898+
{
899+
p++; // skip the beginning double quote
900+
while (*p != NUL)
901+
{
902+
// Within the string, a double quote can be escaped by
903+
// preceding it with a backslash.
904+
if (*p == '\\' && *(p + 1) == '"')
905+
p += 2;
906+
else if (*p == '"')
907+
{
908+
p++; // skip the ending double quote
909+
break;
910+
}
911+
else
912+
MB_PTR_ADV(p);
913+
}
914+
915+
return p;
916+
}
917+
918+
/*
919+
* Return the start of a Vim9 comment (#) in the line starting at "line".
920+
* If a comment is not found, then returns a pointer to the end of the
921+
* string (NUL).
922+
*/
923+
static char_u *
924+
find_start_of_vim9_comment(char_u *line)
925+
{
926+
char_u *p = line;
927+
928+
while (*p != NUL)
929+
{
930+
if (*p == '\'')
931+
// Skip a single quoted string.
932+
p = skip_single_quote_string(p);
933+
else if (*p == '"')
934+
// Skip a double quoted string.
935+
p = skip_double_quote_string(p);
936+
else
937+
{
938+
if (*p == '#')
939+
// Found the start of a Vim9 comment
940+
break;
941+
MB_PTR_ADV(p);
942+
}
943+
}
944+
945+
return p;
946+
}
947+
862948
/*
863949
* Read the body of a function, put every line in "newlines".
864950
* This stops at "}", "endfunction" or "enddef".
@@ -1123,7 +1209,17 @@ get_function_body(
11231209
if (nesting_def[nesting] ? *p != '#' : *p != '"')
11241210
{
11251211
// Not a comment line: check for nested inline function.
1126-
end = p + STRLEN(p) - 1;
1212+
1213+
if (nesting_inline[nesting])
1214+
{
1215+
// A comment (#) can follow the opening curly brace of a
1216+
// block statement. Need to ignore the comment and look
1217+
// for the opening curly brace before the comment.
1218+
end = find_start_of_vim9_comment(p) - 1;
1219+
}
1220+
else
1221+
end = p + STRLEN(p) - 1;
1222+
11271223
while (end > p && VIM_ISWHITE(*end))
11281224
--end;
11291225
if (end > p + 1 && *end == '{' && VIM_ISWHITE(end[-1]))

src/version.c

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

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
994,
707709
/**/
708710
993,
709711
/**/

0 commit comments

Comments
 (0)