Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 11a77ed

Browse files
authoredSep 3, 2020
Merge pull request #399 from KapitanOczywisty/namedArguments
Fix multiline ternary, add named arguments
2 parents ce982cd + b94d8d2 commit 11a77ed

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed
 

‎grammars/php.cson

+31
Original file line numberDiff line numberDiff line change
@@ -1391,6 +1391,13 @@
13911391
]
13921392
}
13931393
]
1394+
'named-arguments':
1395+
'match': '(?i)(?<=^|\\(|,)\\s*([a-z_\\x{7f}-\\x{7fffffff}][a-z0-9_\\x{7f}-\\x{7fffffff}]*)\\s*(:)(?!:)'
1396+
'captures':
1397+
'1':
1398+
'name': 'entity.name.variable.parameter.php'
1399+
'2':
1400+
'name': 'punctuation.separator.colon.php'
13941401
'function-call':
13951402
'patterns': [
13961403
{
@@ -1421,6 +1428,9 @@
14211428
'name': 'punctuation.definition.arguments.end.bracket.round.php'
14221429
'name': 'meta.function-call.php'
14231430
'patterns': [
1431+
{
1432+
'include': '#named-arguments'
1433+
}
14241434
{
14251435
'include': '$self'
14261436
}
@@ -1454,6 +1464,9 @@
14541464
'name': 'punctuation.definition.arguments.end.bracket.round.php'
14551465
'name': 'meta.function-call.php'
14561466
'patterns': [
1467+
{
1468+
'include': '#named-arguments'
1469+
}
14571470
{
14581471
'include': '$self'
14591472
}
@@ -2137,6 +2150,9 @@
21372150
'name': 'punctuation.definition.arguments.end.bracket.round.php'
21382151
'name': 'meta.method-call.php'
21392152
'patterns': [
2153+
{
2154+
'include': '#named-arguments'
2155+
}
21402156
{
21412157
'include': '$self'
21422158
}
@@ -2462,6 +2478,9 @@
24622478
'name': 'punctuation.definition.arguments.end.bracket.round.php'
24632479
'name': 'meta.method-call.static.php'
24642480
'patterns': [
2481+
{
2482+
'include': '#named-arguments'
2483+
}
24652484
{
24662485
'include': '$self'
24672486
}
@@ -3668,6 +3687,18 @@
36683687
'0':
36693688
'name': 'keyword.operator.ternary.php'
36703689
'patterns': [
3690+
{
3691+
# prevent matching goto label in ternary
3692+
# See https://github.com/atom/language-php/issues/386
3693+
'match': '(?i)^\\s*([a-z_\\x{7f}-\\x{7fffffff}][a-z0-9_\\x{7f}-\\x{7fffffff}]*)\\s*(?=:(?!:))'
3694+
'captures':
3695+
'1':
3696+
'patterns': [
3697+
{
3698+
'include': '$self'
3699+
}
3700+
]
3701+
}
36713702
{
36723703
'include': '$self'
36733704
}

‎spec/php-spec.coffee

+43
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,22 @@ describe 'PHP grammar', ->
317317
expect(tokens[9]).toEqual value: ':', scopes: ["source.php", "keyword.operator.ternary.php"]
318318
expect(tokens[12]).toEqual value: '::', scopes: ["source.php", "keyword.operator.class.php"]
319319

320+
it 'should NOT tokenize a ternary statement as a goto label', ->
321+
# See https://github.com/atom/language-php/issues/386
322+
lines = grammar.tokenizeLines '''
323+
$a ?
324+
null :
325+
$b
326+
'''
327+
328+
expect(lines[0][0]).toEqual value: '$', scopes: ['source.php', 'variable.other.php', 'punctuation.definition.variable.php']
329+
expect(lines[0][1]).toEqual value: 'a', scopes: ['source.php', 'variable.other.php']
330+
expect(lines[0][3]).toEqual value: '?', scopes: ['source.php', 'keyword.operator.ternary.php']
331+
expect(lines[1][1]).toEqual value: 'null', scopes: ['source.php', 'constant.language.php']
332+
expect(lines[1][3]).toEqual value: ':', scopes: ['source.php', 'keyword.operator.ternary.php']
333+
expect(lines[2][1]).toEqual value: '$', scopes: ['source.php', 'variable.other.php', 'punctuation.definition.variable.php']
334+
expect(lines[2][2]).toEqual value: 'b', scopes: ['source.php', 'variable.other.php']
335+
320336
describe 'identifiers', ->
321337
it 'tokenizes identifiers with only letters', ->
322338
{tokens} = grammar.tokenizeLine '$abc'
@@ -1152,6 +1168,33 @@ describe 'PHP grammar', ->
11521168
expect(tokens[8]).toEqual value: "'", scopes: ['source.php', 'meta.function-call.php', 'string.quoted.single.php', 'punctuation.definition.string.end.php']
11531169
expect(tokens[9]).toEqual value: ')', scopes: ['source.php', 'meta.function-call.php', 'punctuation.definition.arguments.end.bracket.round.php']
11541170

1171+
it 'tokenizes function calls with named arguments', ->
1172+
{tokens} = grammar.tokenizeLine 'doSomething($a ? null : true, b: $b);'
1173+
1174+
expect(tokens[0]).toEqual value: 'doSomething', scopes: ['source.php', 'meta.function-call.php', 'entity.name.function.php']
1175+
expect(tokens[14]).toEqual value: 'b', scopes: ['source.php', 'meta.function-call.php', 'entity.name.variable.parameter.php']
1176+
expect(tokens[15]).toEqual value: ':', scopes: ['source.php', 'meta.function-call.php', 'punctuation.separator.colon.php']
1177+
# ternary should be still tokenized
1178+
expect(tokens[7]).toEqual value: 'null', scopes: ['source.php', 'meta.function-call.php', 'constant.language.php']
1179+
expect(tokens[9]).toEqual value: ':', scopes: ['source.php', 'meta.function-call.php', 'keyword.operator.ternary.php']
1180+
1181+
it 'tokenizes multiline function calls with named arguments', ->
1182+
lines = grammar.tokenizeLines '''
1183+
doSomething(
1184+
x: $a ?
1185+
null : true,
1186+
a: $b);
1187+
'''
1188+
1189+
expect(lines[0][0]).toEqual value: 'doSomething', scopes: ['source.php', 'meta.function-call.php', 'entity.name.function.php']
1190+
expect(lines[1][1]).toEqual value: 'x', scopes: ['source.php', 'meta.function-call.php', 'entity.name.variable.parameter.php']
1191+
expect(lines[1][2]).toEqual value: ':', scopes: ['source.php', 'meta.function-call.php', 'punctuation.separator.colon.php']
1192+
expect(lines[3][1]).toEqual value: 'a', scopes: ['source.php', 'meta.function-call.php', 'entity.name.variable.parameter.php']
1193+
expect(lines[3][2]).toEqual value: ':', scopes: ['source.php', 'meta.function-call.php', 'punctuation.separator.colon.php']
1194+
# ternary should be still tokenized
1195+
expect(lines[2][1]).toEqual value: 'null', scopes: ['source.php', 'meta.function-call.php', 'constant.language.php']
1196+
expect(lines[2][3]).toEqual value: ':', scopes: ['source.php', 'meta.function-call.php', 'keyword.operator.ternary.php']
1197+
11551198
it 'tokenizes builtin function calls', ->
11561199
{tokens} = grammar.tokenizeLine "echo('Hi!')"
11571200

0 commit comments

Comments
 (0)