Skip to content

Commit 42bdd60

Browse files
bwilkersonCommit Queue
authored andcommitted
Add support for a zero-length marker in test code
Change-Id: I44642ecbb09d8fc2c3b86400f5d388c74d6fb3d4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/416521 Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]>
1 parent c3f6e4f commit 42bdd60

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

pkg/analysis_server/test/services/completion/dart/completion_test.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4845,7 +4845,7 @@ suggestions
48454845
Future<void> test_commentSnippets019_1() async {
48464846
allowedIdentifiers = {'toString'};
48474847
await computeSuggestions('''
4848-
class A{m(){Object x;x.^/**/clear()
4848+
class A{m(){Object x;x.^/* */clear()
48494849
''');
48504850
assertResponse(r'''
48514851
suggestions
@@ -4857,7 +4857,7 @@ suggestions
48574857
Future<void> test_commentSnippets020_1() async {
48584858
allowedIdentifiers = {'newt', 'newf', 'newz', 'Map'};
48594859
await computeSuggestions('''
4860-
classMap{}class tst {var newt;void newf(){}test() {var newz;new^/**/;}}
4860+
classMap{}class tst {var newt;void newf(){}test() {var newz;new^/* */;}}
48614861
''');
48624862
assertResponse(r'''
48634863
replacement
@@ -4875,7 +4875,7 @@ suggestions
48754875
Future<void> test_commentSnippets021_1() async {
48764876
allowedIdentifiers = {'Map', 'newt'};
48774877
await computeSuggestions('''
4878-
class Map{}class tst {var newt;void newf(){}test() {var newz;new ^/**/;}}
4878+
class Map{}class tst {var newt;void newf(){}test() {var newz;new ^/* */;}}
48794879
''');
48804880
assertResponse(r'''
48814881
suggestions
@@ -5037,7 +5037,7 @@ suggestions
50375037
Future<void> test_commentSnippets025_1() async {
50385038
allowedIdentifiers = {'q'};
50395039
await computeSuggestions('''
5040-
class C {num m() {var q; num x=^ q + /**/;}}
5040+
class C {num m() {var q; num x=^ q + /* */;}}
50415041
''');
50425042
assertResponse(r'''
50435043
suggestions
@@ -5063,7 +5063,7 @@ suggestions
50635063
Future<void> test_commentSnippets025_2() async {
50645064
allowedIdentifiers = {'q'};
50655065
await computeSuggestions('''
5066-
class C {num m() {var q; num x= q + ^/**/;}}
5066+
class C {num m() {var q; num x= q + ^/* */;}}
50675067
''');
50685068
assertResponse(r'''
50695069
suggestions
@@ -5089,7 +5089,7 @@ suggestions
50895089
Future<void> test_commentSnippets025_3() async {
50905090
allowedIdentifiers = {'q'};
50915091
await computeSuggestions('''
5092-
class C {num m() {var q; num x= q^ + /**/;}}
5092+
class C {num m() {var q; num x= q^ + /* */;}}
50935093
''');
50945094
assertResponse(r'''
50955095
replacement
@@ -11146,7 +11146,6 @@ suggestions
1114611146
''');
1114711147
}
1114811148

11149-
1115011149
Future<void> test_partial_code_before_nested_function_2() async {
1115111150
// Variant of issue https://github.com/dart-lang/sdk/issues/49477,
1115211151
// requested in https://dart-review.googlesource.com/c/sdk/+/393340.

pkg/analysis_server/test/services/completion/statement/statement_completion_test.dart

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -599,12 +599,12 @@ class _ForCompletionTest extends StatementCompletionTest {
599599
Future<void> test_emptyCondition() async {
600600
await _prepareCompletion('0;', '''
601601
void f() {
602-
for (int i = 0;) /**/ ////
602+
for (int i = 0;) /* */ ////
603603
}
604604
''', atEnd: true);
605605
_assertHasChange('Complete for-statement', '''
606606
void f() {
607-
for (int i = 0; ; ) /**/ {
607+
for (int i = 0; ; ) /* */ {
608608
////
609609
}
610610
}
@@ -659,14 +659,14 @@ void f() {
659659
}
660660

661661
Future<void> test_emptyInitializersEmptyCondition() async {
662-
await _prepareCompletion('/**/', '''
662+
await _prepareCompletion('/* */', '''
663663
void f() {
664-
for (;/**/)
664+
for (;/* */)
665665
}
666666
''', atEnd: true);
667667
_assertHasChange('Complete for-statement', '''
668668
void f() {
669-
for (; /**/; ) {
669+
for (; /* */; ) {
670670
////
671671
}
672672
}
@@ -689,30 +689,30 @@ void f() {
689689
}
690690

691691
Future<void> test_emptyUpdaters() async {
692-
await _prepareCompletion('/**/', '''
692+
await _prepareCompletion('/* */', '''
693693
void f() {
694-
for (int i = 0; i < 10 /**/)
694+
for (int i = 0; i < 10 /* */)
695695
}
696696
''', atEnd: true);
697697
_assertHasChange('Complete for-statement', '''
698698
void f() {
699-
for (int i = 0; i < 10 /**/; ) {
699+
for (int i = 0; i < 10 /* */; ) {
700700
////
701701
}
702702
}
703703
''', (s) => _after(s, ' '));
704704
}
705705

706706
Future<void> test_emptyUpdatersWithBody() async {
707-
await _prepareCompletion('/**/', '''
707+
await _prepareCompletion('/* */', '''
708708
void f() {
709-
for (int i = 0; i < 10 /**/) {
709+
for (int i = 0; i < 10 /* */) {
710710
}
711711
}
712712
''', atEnd: true);
713713
_assertHasChange('Complete for-statement', '''
714714
void f() {
715-
for (int i = 0; i < 10 /**/; ) {
715+
for (int i = 0; i < 10 /* */; ) {
716716
}
717717
}
718718
''', (s) => _after(s, '*/; '));

pkg/analyzer/lib/src/test_utilities/test_code_format.dart

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,41 @@ import 'package:collection/collection.dart';
1212
/// Positions and ranges are marked with brackets inside block comments:
1313
///
1414
/// ```
15-
/// position ::= '/*' integer '*/
16-
/// rangeStart ::= '/*[' integer '*/
17-
/// rangeEnd ::= '/*' integer ']*/
15+
/// position ::= '/*' integer '*/'
16+
/// rangeStart ::= '/*[' integer '*/'
17+
/// rangeEnd ::= '/*' integer ']*/'
1818
/// ```
1919
///
2020
/// Numbers start at 0 and positions and range starts must be consecutive.
2121
/// The same numbers can be used to represent both positions and ranges.
2222
///
2323
/// For convenience, a single position can also be marked with a `^` (which
24-
/// behaves the same as `/*0*/).
24+
/// behaves the same as `/*0*/`). A single range can be marked with `[!` and
25+
/// `!]`, which behave the same as `/*[0*/` and `/*0]*/`.
26+
///
27+
/// In addition, the pattern `/**/` will be removed from the test code. This is
28+
/// be used for two purposes.
29+
///
30+
/// First, it can prevent certain code from being interpreted as markup. For
31+
/// example, if the test code includes `[!` (such as in a list literal whose
32+
/// first element begins with a unary prefix operator), you can use `[/**/!` to
33+
/// prevent the `[!` from being interpreted as markup. Similarly, you can use
34+
/// `!/**/]` in places where `!]` should appear in the unmarked code.
35+
///
36+
/// Second, it can be used at the end of a line of code that contains trailing
37+
/// whitespace. Without some form of marker the formatter will remove the
38+
/// trailing whitespace.
2539
class TestCode {
2640
static final _positionShorthand = '^';
41+
static final _positionPattern = RegExp(r'/\*(\d+)\*/');
42+
2743
static final _rangeStartShorthand = '[!';
2844
static final _rangeEndShorthand = '!]';
29-
static final _positionPattern = RegExp(r'/\*(\d+)\*/');
3045
static final _rangeStartPattern = RegExp(r'/\*\[(\d+)\*/');
3146
static final _rangeEndPattern = RegExp(r'/\*(\d+)\]\*/');
3247

48+
static final _zeroWidthMarker = '/**/';
49+
3350
/// An empty code block with a single position at offset 0.
3451
static final empty = TestCode.parse('^');
3552

@@ -60,6 +77,7 @@ class TestCode {
6077
String markedCode, {
6178
bool positionShorthand = true,
6279
bool rangeShorthand = true,
80+
bool zeroWidthMarker = true,
6381
}) {
6482
var scanner = _StringScanner(markedCode);
6583
var codeBuffer = StringBuffer();
@@ -118,6 +136,9 @@ class TestCode {
118136
recordRangeStart(scannedNumber());
119137
} else if (scanner.scan(_rangeEndPattern)) {
120138
recordRangeEnd(scannedNumber());
139+
} else if (zeroWidthMarker && scanner.scan(_zeroWidthMarker)) {
140+
// Don't record any information about zero-width markers, simply remove
141+
// the marker from the unmarked code.
121142
} else {
122143
codeBuffer.writeCharCode(scanner.readChar());
123144
}

0 commit comments

Comments
 (0)