4
4
5
5
import 'package:analysis_server/lsp_protocol/protocol.dart' ;
6
6
import 'package:analysis_server/src/lsp/constants.dart' ;
7
+ import 'package:analyzer/src/test_utilities/test_code_format.dart' ;
7
8
import 'package:test/test.dart' ;
8
9
import 'package:test_reflective_loader/test_reflective_loader.dart' ;
9
10
10
11
import '../tool/lsp_spec/matchers.dart' ;
12
+ import '../utils/test_code_extensions.dart' ;
11
13
import 'server_abstract.dart' ;
12
14
13
15
void main () {
@@ -27,16 +29,16 @@ class FormatTest extends AbstractLspAnalysisServerTest {
27
29
}
28
30
29
31
Future <List <TextEdit >> expectRangeFormattedContents (
30
- Uri uri, String original, String expected) async {
31
- final formatEdits = (await formatRange (uri, rangeFromMarkers (original)))! ;
32
- final formattedContents =
33
- applyTextEdits (withoutMarkers (original), formatEdits);
32
+ Uri uri, TestCode code, String expected) async {
33
+ final formatEdits = (await formatRange (uri, code.range.range))! ;
34
+ final formattedContents = applyTextEdits (code.code, formatEdits);
34
35
expect (formattedContents, equals (expected));
35
36
return formatEdits;
36
37
}
37
38
38
39
Future <void > test_alreadyFormatted () async {
39
- const contents = '''void f() {
40
+ const contents = '''
41
+ void f() {
40
42
print('test');
41
43
}
42
44
''' ;
@@ -159,23 +161,23 @@ ErrorOr<Pair<A, List<B>>> c(
159
161
160
162
Future <void > test_formatOnType_simple () async {
161
163
const contents = '''
162
- void f ()
163
- {
164
+ void f ()
165
+ {
164
166
165
- print('test');
166
- ^}
167
+ print('test');
168
+ ^}
167
169
''' ;
168
170
final expected = '''void f() {
169
171
print('test');
170
172
}
171
173
''' ;
174
+ final code = TestCode .parse (contents);
172
175
await initialize ();
173
- await openFile (mainFileUri, withoutMarkers (contents) );
176
+ await openFile (mainFileUri, code.code );
174
177
175
178
final formatEdits =
176
- (await formatOnType (mainFileUri, positionFromMarker (contents), '}' ))! ;
177
- final formattedContents =
178
- applyTextEdits (withoutMarkers (contents), formatEdits);
179
+ (await formatOnType (mainFileUri, code.position.position, '}' ))! ;
180
+ final formattedContents = applyTextEdits (code.code, formatEdits);
179
181
expect (formattedContents, equals (expected));
180
182
}
181
183
@@ -185,9 +187,9 @@ ErrorOr<Pair<A, List<B>>> c(
185
187
const contents = '''
186
188
void f()
187
189
{
188
- [[ print('test');
190
+ [! print('test');
189
191
print('test');
190
- ] ] print('test');
192
+ ! ] print('test');
191
193
}
192
194
''' ;
193
195
final expected = '''
@@ -198,19 +200,20 @@ void f()
198
200
print('test');
199
201
}
200
202
''' ;
203
+ final code = TestCode .parse (contents);
201
204
await initialize ();
202
- await openFile (mainFileUri, withoutMarkers (contents) );
203
- await expectRangeFormattedContents (mainFileUri, contents , expected);
205
+ await openFile (mainFileUri, code.code );
206
+ await expectRangeFormattedContents (mainFileUri, code , expected);
204
207
}
205
208
206
209
Future <void > test_formatRange_expandsLeadingWhitespaceToNearestLine () async {
207
210
const contents = '''
208
211
void f()
209
212
{
210
213
211
- [[ print('test'); // line 2
214
+ [! print('test'); // line 2
212
215
print('test'); // line 3
213
- print('test'); // line 4] ]
216
+ print('test'); // line 4! ]
214
217
}
215
218
''' ;
216
219
const expected = '''
@@ -222,9 +225,10 @@ void f()
222
225
print('test'); // line 4
223
226
}
224
227
''' ;
228
+ final code = TestCode .parse (contents);
225
229
await initialize ();
226
- await openFile (mainFileUri, withoutMarkers (contents) );
227
- await expectRangeFormattedContents (mainFileUri, contents , expected);
230
+ await openFile (mainFileUri, code.code );
231
+ await expectRangeFormattedContents (mainFileUri, code , expected);
228
232
}
229
233
230
234
Future <void > test_formatRange_invalidRange () async {
@@ -234,8 +238,9 @@ void f()
234
238
print('test');
235
239
}
236
240
''' ;
241
+ final code = TestCode .parse (contents);
237
242
await initialize ();
238
- await openFile (mainFileUri, withoutMarkers (contents) );
243
+ await openFile (mainFileUri, code.code );
239
244
final formatRangeRequest = formatRange (
240
245
mainFileUri,
241
246
Range (
@@ -254,11 +259,11 @@ main ()
254
259
print('test');
255
260
}
256
261
257
- [[ main2 ()
262
+ [! main2 ()
258
263
{
259
264
260
265
print('test');
261
- }] ]
266
+ }! ]
262
267
263
268
main3 ()
264
269
{
@@ -283,33 +288,36 @@ main3 ()
283
288
print('test');
284
289
}
285
290
''' ;
291
+ final code = TestCode .parse (contents);
286
292
await initialize ();
287
- await openFile (mainFileUri, withoutMarkers (contents) );
288
- await expectRangeFormattedContents (mainFileUri, contents , expected);
293
+ await openFile (mainFileUri, code.code );
294
+ await expectRangeFormattedContents (mainFileUri, code , expected);
289
295
}
290
296
291
297
Future <void > test_formatRange_trailingNewline_47702 () async {
292
298
// Check we complete when a formatted block ends with a newline.
293
299
// https://github.com/dart-lang/sdk/issues/47702
294
300
const contents = '''
295
301
int a;
296
- [[
302
+ [!
297
303
int b;
298
- ] ]
304
+ ! ]
299
305
''' ;
300
306
final expected = '''
301
307
int a;
302
308
303
309
int b;
304
310
305
311
''' ;
312
+ final code = TestCode .parse (contents);
306
313
await initialize ();
307
- await openFile (mainFileUri, withoutMarkers (contents) );
308
- await expectRangeFormattedContents (mainFileUri, contents , expected);
314
+ await openFile (mainFileUri, code.code );
315
+ await expectRangeFormattedContents (mainFileUri, code , expected);
309
316
}
310
317
311
318
Future <void > test_invalidSyntax () async {
312
- const contents = '''void f(((( {
319
+ const contents = '''
320
+ void f(((( {
313
321
print('test');
314
322
}
315
323
''' ;
@@ -322,15 +330,18 @@ int b;
322
330
323
331
Future <void > test_lineLength () async {
324
332
const contents = '''
325
- void f() =>
326
- print(
327
- '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789'
328
- );
333
+ void f() =>
334
+ print(
335
+ '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789'
336
+ );
329
337
''' ;
330
- final expectedDefault = '''void f() => print(
331
- '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789');\n ''' ;
332
- final expectedLongLines =
333
- '''void f() => print('123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789');\n ''' ;
338
+ final expectedDefault = '''
339
+ void f() => print(
340
+ '123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789');
341
+ ''' ;
342
+ final expectedLongLines = '''
343
+ void f() => print('123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789');
344
+ ''' ;
334
345
335
346
// Initialize with config support, supplying an empty config when requested.
336
347
await provideConfig (
@@ -659,7 +670,8 @@ void f() {
659
670
print('test');
660
671
}
661
672
''' ;
662
- final expected = '''void f() {
673
+ final expected = '''
674
+ void f() {
663
675
print('test');
664
676
}
665
677
''' ;
@@ -676,7 +688,8 @@ void f() {
676
688
print('test');
677
689
}
678
690
''' ;
679
- final expected = '''void f() {
691
+ final expected = '''
692
+ void f() {
680
693
print('test');
681
694
}
682
695
''' ;
@@ -686,13 +699,15 @@ void f() {
686
699
}
687
700
688
701
Future <void > test_validSyntax_withErrors () async {
689
- // We should still be able to format syntactically valid code even if it has analysis
690
- // errors.
691
- const contents = '''void f() {
702
+ // We should still be able to format syntactically valid code even if it has
703
+ // analysis errors.
704
+ const contents = '''
705
+ void f() {
692
706
print(a);
693
707
}
694
708
''' ;
695
- const expected = '''void f() {
709
+ const expected = '''
710
+ void f() {
696
711
print(a);
697
712
}
698
713
''' ;
0 commit comments