@@ -156,6 +156,120 @@ var a = ^;
156156 _assertHasClass (text: 'String' );
157157 }
158158
159+ Future <void > test_filterSort_byPattern_excludeNotMatching () async {
160+ await _compute2 (r'''
161+ var a = F^;
162+ ''' );
163+
164+ _assertHasClass (text: 'Future' );
165+ _assertNoClass (text: 'String' );
166+ }
167+
168+ Future <void > test_filterSort_byPattern_location_beforeMethod () async {
169+ await _compute2 (r'''
170+ class A {
171+ F^
172+ void foo() {}
173+ }
174+ ''' );
175+
176+ _assertHasClass (text: 'Future' );
177+ _assertNoClass (text: 'String' );
178+ }
179+
180+ Future <void > test_filterSort_byPattern_location_functionReturnType () async {
181+ await _compute2 (r'''
182+ F^ foo() {}
183+ ''' );
184+
185+ _assertHasClass (text: 'Future' );
186+ _assertNoClass (text: 'String' );
187+ }
188+
189+ Future <void > test_filterSort_byPattern_location_methodReturnType () async {
190+ await _compute2 (r'''
191+ class A {
192+ F^ foo() {}
193+ }
194+ ''' );
195+
196+ _assertHasClass (text: 'Future' );
197+ _assertNoClass (text: 'String' );
198+ }
199+
200+ Future <void > test_filterSort_byPattern_location_parameterType () async {
201+ await _compute2 (r'''
202+ void foo(F^ a) {}
203+ ''' );
204+
205+ _assertHasClass (text: 'Future' );
206+ _assertNoClass (text: 'String' );
207+ }
208+
209+ Future <void > test_filterSort_byPattern_location_parameterType2 () async {
210+ await _compute2 (r'''
211+ void foo(^a) {}
212+ ''' );
213+
214+ _assertHasClass (text: 'Future' );
215+ _assertHasClass (text: 'String' );
216+ }
217+
218+ Future <void > test_filterSort_byPattern_location_statement () async {
219+ await _compute2 (r'''
220+ main() {
221+ F^
222+ 0;
223+ }
224+ ''' );
225+
226+ _assertHasClass (text: 'Future' );
227+ _assertNoClass (text: 'String' );
228+ }
229+
230+ Future <void > test_filterSort_byPattern_preferPrefix () async {
231+ await _compute2 (r'''
232+ class Foobar {}
233+ class Falcon {}
234+ var a = Fo^;
235+ ''' );
236+
237+ _assertOrder ([
238+ _assertHasClass (text: 'Foobar' ),
239+ _assertHasClass (text: 'Falcon' ),
240+ ]);
241+ }
242+
243+ Future <void > test_filterSort_preferLocal () async {
244+ await _compute2 (r'''
245+ var a = 0;
246+ main() {
247+ var b = 0;
248+ var v = ^;
249+ }
250+ ''' );
251+
252+ _assertOrder ([
253+ _assertHasLocalVariable (text: 'b' ),
254+ _assertHasTopLevelVariable (text: 'a' ),
255+ ]);
256+ }
257+
258+ Future <void > test_filterSort_sortByName () async {
259+ await _compute2 (r'''
260+ main() {
261+ var a = 0;
262+ var b = 0;
263+ var v = ^;
264+ }
265+ ''' );
266+
267+ _assertOrder ([
268+ _assertHasLocalVariable (text: 'a' ),
269+ _assertHasLocalVariable (text: 'b' ),
270+ ]);
271+ }
272+
159273 void _assertComputedImportedLibraries (List <String > expected) {
160274 expected = expected.map (convertPath).toList ();
161275 expect (
@@ -164,19 +278,46 @@ var a = ^;
164278 );
165279 }
166280
167- void _assertHasClass ({@required String text}) {
281+ CompletionSuggestion _assertHasClass ({@required String text}) {
168282 var matching = _matchingCompletions (
169283 text: text,
170284 elementKind: ElementKind .CLASS ,
171285 );
172286 expect (matching, hasLength (1 ), reason: 'Expected exactly one completion' );
287+ return matching.single;
173288 }
174289
175290 void _assertHasCompletion ({@required String text}) {
176291 var matching = _matchingCompletions (text: text);
177292 expect (matching, hasLength (1 ), reason: 'Expected exactly one completion' );
178293 }
179294
295+ CompletionSuggestion _assertHasLocalVariable ({@required String text}) {
296+ var matching = _matchingCompletions (
297+ text: text,
298+ elementKind: ElementKind .LOCAL_VARIABLE ,
299+ );
300+ expect (
301+ matching,
302+ hasLength (1 ),
303+ reason: 'Expected exactly one completion in $_suggestions ' ,
304+ );
305+ return matching.single;
306+ }
307+
308+ CompletionSuggestion _assertHasTopLevelVariable ({@required String text}) {
309+ var matching = _matchingCompletions (
310+ text: text,
311+ elementKind: ElementKind .TOP_LEVEL_VARIABLE ,
312+ );
313+ expect (
314+ matching,
315+ hasLength (1 ),
316+ reason: 'Expected exactly one completion in $_suggestions ' ,
317+ );
318+ return matching.single;
319+ }
320+
180321 void _assertNoClass ({@required String text}) {
181322 var matching = _matchingCompletions (
182323 text: text,
@@ -185,6 +326,16 @@ var a = ^;
185326 expect (matching, isEmpty, reason: 'Expected zero completions' );
186327 }
187328
329+ void _assertOrder (List <CompletionSuggestion > suggestions) {
330+ var lastIndex = - 2 ;
331+ for (var suggestion in suggestions) {
332+ var index = _suggestions.indexOf (suggestion);
333+ expect (index, isNonNegative, reason: '$suggestion ' );
334+ expect (index, greaterThan (lastIndex), reason: '$suggestion ' );
335+ lastIndex = index;
336+ }
337+ }
338+
188339 Future _compute2 (String content) async {
189340 var context = _updateFile (content);
190341
@@ -227,15 +378,16 @@ var a = ^;
227378 }
228379
229380 _CompletionContext _updateFile (String content) {
230- newFile (testPath, content: content);
231-
232381 var offset = content.indexOf ('^' );
233382 expect (offset, isPositive, reason: 'Expected to find ^' );
234383 expect (content.indexOf ('^' , offset + 1 ), - 1 , reason: 'Expected only one ^' );
235384
236385 var lineInfo = LineInfo .fromContent (content);
237386 var location = lineInfo.getLocation (offset);
238387
388+ content = content.substring (0 , offset) + content.substring (offset + 1 );
389+ newFile (testPath, content: content);
390+
239391 return _CompletionContext (
240392 content,
241393 offset,
0 commit comments