| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library test.services.completion.dart.util; | 5 library test.services.completion.dart.util; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/plugin/protocol/protocol.dart' as protocol | 9 import 'package:analysis_server/plugin/protocol/protocol.dart' as protocol |
| 10 show Element, ElementKind; | 10 show Element, ElementKind; |
| 11 import 'package:analysis_server/plugin/protocol/protocol.dart' | 11 import 'package:analysis_server/plugin/protocol/protocol.dart' |
| 12 hide Element, ElementKind; | 12 hide Element, ElementKind; |
| 13 import 'package:analysis_server/plugin/protocol/protocol.dart'; | 13 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
| 14 import 'package:analysis_server/src/ide_options.dart'; |
| 14 import 'package:analysis_server/src/provisional/completion/dart/completion_dart.
dart'; | 15 import 'package:analysis_server/src/provisional/completion/dart/completion_dart.
dart'; |
| 15 import 'package:analysis_server/src/services/completion/completion_core.dart'; | 16 import 'package:analysis_server/src/services/completion/completion_core.dart'; |
| 16 import 'package:analysis_server/src/services/completion/completion_performance.d
art'; | 17 import 'package:analysis_server/src/services/completion/completion_performance.d
art'; |
| 17 import 'package:analysis_server/src/services/completion/dart/completion_manager.
dart' | 18 import 'package:analysis_server/src/services/completion/dart/completion_manager.
dart' |
| 18 show DartCompletionRequestImpl, ReplacementRange; | 19 show DartCompletionRequestImpl, ReplacementRange; |
| 19 import 'package:analysis_server/src/services/index/index.dart'; | 20 import 'package:analysis_server/src/services/index/index.dart'; |
| 20 import 'package:analysis_server/src/services/search/search_engine_internal.dart'
; | 21 import 'package:analysis_server/src/services/search/search_engine_internal.dart'
; |
| 22 import 'package:analyzer/file_system/file_system.dart'; |
| 23 import 'package:analyzer/source/package_map_resolver.dart'; |
| 21 import 'package:analyzer/src/dart/analysis/ast_provider_context.dart'; | 24 import 'package:analyzer/src/dart/analysis/ast_provider_context.dart'; |
| 22 import 'package:analyzer/src/dart/analysis/driver.dart'; | 25 import 'package:analyzer/src/dart/analysis/driver.dart'; |
| 23 import 'package:analyzer/src/generated/source.dart'; | 26 import 'package:analyzer/src/generated/source.dart'; |
| 24 import 'package:analyzer/task/dart.dart'; | 27 import 'package:analyzer/task/dart.dart'; |
| 25 import 'package:test/test.dart'; | 28 import 'package:test/test.dart'; |
| 26 | 29 |
| 27 import '../../../abstract_context.dart'; | 30 import '../../../abstract_context.dart'; |
| 31 import '../../correction/flutter_util.dart'; |
| 28 | 32 |
| 29 int suggestionComparator(CompletionSuggestion s1, CompletionSuggestion s2) { | 33 int suggestionComparator(CompletionSuggestion s1, CompletionSuggestion s2) { |
| 30 String c1 = s1.completion.toLowerCase(); | 34 String c1 = s1.completion.toLowerCase(); |
| 31 String c2 = s2.completion.toLowerCase(); | 35 String c2 = s2.completion.toLowerCase(); |
| 32 return c1.compareTo(c2); | 36 return c1.compareTo(c2); |
| 33 } | 37 } |
| 34 | 38 |
| 35 abstract class DartCompletionContributorTest extends AbstractContextTest { | 39 abstract class DartCompletionContributorTest extends AbstractContextTest { |
| 36 Index index; | 40 Index index; |
| 37 SearchEngineImpl searchEngine; | 41 SearchEngineImpl searchEngine; |
| (...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 469 } | 473 } |
| 470 context.performAnalysisTask(); | 474 context.performAnalysisTask(); |
| 471 // We use a delayed future to allow microtask events to finish. The | 475 // We use a delayed future to allow microtask events to finish. The |
| 472 // Future.value or Future() constructors use scheduleMicrotask themselves an
d | 476 // Future.value or Future() constructors use scheduleMicrotask themselves an
d |
| 473 // would therefore not wait for microtask callbacks that are scheduled after | 477 // would therefore not wait for microtask callbacks that are scheduled after |
| 474 // invoking this method. | 478 // invoking this method. |
| 475 return new Future.delayed( | 479 return new Future.delayed( |
| 476 Duration.ZERO, () => computeLibrariesContaining(times - 1)); | 480 Duration.ZERO, () => computeLibrariesContaining(times - 1)); |
| 477 } | 481 } |
| 478 | 482 |
| 479 Future computeSuggestions([int times = 200]) async { | 483 Future computeSuggestions({int times = 200, IdeOptions options}) async { |
| 480 AnalysisResult analysisResult = null; | 484 AnalysisResult analysisResult = null; |
| 481 if (enableNewAnalysisDriver) { | 485 if (enableNewAnalysisDriver) { |
| 482 analysisResult = await driver.getResult(testFile); | 486 analysisResult = await driver.getResult(testFile); |
| 483 testSource = analysisResult.unit.element.source; | 487 testSource = analysisResult.unit.element.source; |
| 484 } else { | 488 } else { |
| 485 context.analysisPriorityOrder = [testSource]; | 489 context.analysisPriorityOrder = [testSource]; |
| 486 } | 490 } |
| 487 CompletionRequestImpl baseRequest = new CompletionRequestImpl( | 491 CompletionRequestImpl baseRequest = new CompletionRequestImpl( |
| 488 analysisResult, | 492 analysisResult, |
| 489 enableNewAnalysisDriver ? null : context, | 493 enableNewAnalysisDriver ? null : context, |
| 490 provider, | 494 provider, |
| 491 searchEngine, | 495 searchEngine, |
| 492 testSource, | 496 testSource, |
| 493 completionOffset, | 497 completionOffset, |
| 494 new CompletionPerformance()); | 498 new CompletionPerformance(), |
| 499 options); |
| 495 | 500 |
| 496 // Build the request | 501 // Build the request |
| 497 Completer<DartCompletionRequest> requestCompleter = | 502 Completer<DartCompletionRequest> requestCompleter = |
| 498 new Completer<DartCompletionRequest>(); | 503 new Completer<DartCompletionRequest>(); |
| 499 DartCompletionRequestImpl | 504 DartCompletionRequestImpl |
| 500 .from(baseRequest) | 505 .from(baseRequest) |
| 501 .then((DartCompletionRequest request) { | 506 .then((DartCompletionRequest request) { |
| 502 requestCompleter.complete(request); | 507 requestCompleter.complete(request); |
| 503 }); | 508 }); |
| 504 request = await performAnalysis(times, requestCompleter); | 509 request = await performAnalysis(times, requestCompleter); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 515 .then((List<CompletionSuggestion> computedSuggestions) { | 520 .then((List<CompletionSuggestion> computedSuggestions) { |
| 516 suggestionCompleter.complete(computedSuggestions); | 521 suggestionCompleter.complete(computedSuggestions); |
| 517 }); | 522 }); |
| 518 | 523 |
| 519 // Perform analysis until the suggestions have been computed | 524 // Perform analysis until the suggestions have been computed |
| 520 // or the max analysis cycles ([times]) has been reached | 525 // or the max analysis cycles ([times]) has been reached |
| 521 suggestions = await performAnalysis(times, suggestionCompleter); | 526 suggestions = await performAnalysis(times, suggestionCompleter); |
| 522 expect(suggestions, isNotNull, reason: 'expected suggestions'); | 527 expect(suggestions, isNotNull, reason: 'expected suggestions'); |
| 523 } | 528 } |
| 524 | 529 |
| 530 /** |
| 531 * Configures the [SourceFactory] to have the `flutter` package in |
| 532 * `/packages/flutter/lib` folder. |
| 533 */ |
| 534 void configureFlutterPkg(Map<String, String> pathToCode) { |
| 535 pathToCode.forEach((path, code) { |
| 536 provider.newFile('$flutterPkgLibPath/$path', code); |
| 537 }); |
| 538 // configure SourceFactory |
| 539 Folder myPkgFolder = provider.getResource(flutterPkgLibPath); |
| 540 UriResolver pkgResolver = new PackageMapUriResolver(provider, { |
| 541 'flutter': [myPkgFolder] |
| 542 }); |
| 543 SourceFactory sourceFactory = new SourceFactory( |
| 544 [new DartUriResolver(sdk), pkgResolver, resourceResolver]); |
| 545 if (enableNewAnalysisDriver) { |
| 546 driver.configure(sourceFactory: sourceFactory); |
| 547 } else { |
| 548 context.sourceFactory = sourceFactory; |
| 549 } |
| 550 // force 'flutter' resolution |
| 551 addSource( |
| 552 '/tmp/other.dart', |
| 553 pathToCode.keys |
| 554 .map((path) => "import 'package:flutter/$path';") |
| 555 .join('\n')); |
| 556 } |
| 557 |
| 525 DartCompletionContributor createContributor(); | 558 DartCompletionContributor createContributor(); |
| 526 | 559 |
| 527 void failedCompletion(String message, | 560 void failedCompletion(String message, |
| 528 [Iterable<CompletionSuggestion> completions]) { | 561 [Iterable<CompletionSuggestion> completions]) { |
| 529 StringBuffer sb = new StringBuffer(message); | 562 StringBuffer sb = new StringBuffer(message); |
| 530 if (completions != null) { | 563 if (completions != null) { |
| 531 sb.write('\n found'); | 564 sb.write('\n found'); |
| 532 completions.toList() | 565 completions.toList() |
| 533 ..sort(suggestionComparator) | 566 ..sort(suggestionComparator) |
| 534 ..forEach((CompletionSuggestion suggestion) { | 567 ..forEach((CompletionSuggestion suggestion) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 599 | 632 |
| 600 @override | 633 @override |
| 601 void setUp() { | 634 void setUp() { |
| 602 super.setUp(); | 635 super.setUp(); |
| 603 index = createMemoryIndex(); | 636 index = createMemoryIndex(); |
| 604 searchEngine = | 637 searchEngine = |
| 605 new SearchEngineImpl(index, (_) => new AstProviderForContext(context)); | 638 new SearchEngineImpl(index, (_) => new AstProviderForContext(context)); |
| 606 contributor = createContributor(); | 639 contributor = createContributor(); |
| 607 } | 640 } |
| 608 } | 641 } |
| OLD | NEW |