| 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 services.completion.dart; | 5 library services.completion.dart; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/plugin/protocol/protocol.dart'; | 9 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
| 10 import 'package:analysis_server/src/provisional/completion/completion_core.dart' | 10 import 'package:analysis_server/src/provisional/completion/completion_core.dart' |
| 11 show AnalysisRequest, CompletionContributor, CompletionRequest; | 11 show |
| 12 AnalysisRequest, |
| 13 CompletionContributor, |
| 14 CompletionRequest, |
| 15 CompletionResult; |
| 12 import 'package:analysis_server/src/services/completion/completion_core.dart'; | 16 import 'package:analysis_server/src/services/completion/completion_core.dart'; |
| 13 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; | 17 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; |
| 14 import 'package:analysis_server/src/services/search/search_engine.dart'; | 18 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 15 import 'package:analyzer/src/generated/ast.dart'; | |
| 16 import 'package:analyzer/src/generated/engine.dart'; | 19 import 'package:analyzer/src/generated/engine.dart'; |
| 17 import 'package:analyzer/src/generated/source.dart'; | 20 import 'package:analyzer/src/generated/source.dart'; |
| 18 | 21 |
| 19 /** | 22 /** |
| 20 * Manages code completion for a given Dart file completion request. | 23 * Manages code completion for a given Dart file completion request. |
| 21 */ | 24 */ |
| 22 class DartCompletionManager extends CompletionManager { | 25 class DartCompletionManager extends CompletionManager { |
| 23 final SearchEngine searchEngine; | 26 final SearchEngine searchEngine; |
| 24 Iterable<CompletionContributor> newContributors; | 27 Iterable<CompletionContributor> newContributors; |
| 25 | 28 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 37 */ | 40 */ |
| 38 factory DartCompletionManager.create( | 41 factory DartCompletionManager.create( |
| 39 AnalysisContext context, | 42 AnalysisContext context, |
| 40 SearchEngine searchEngine, | 43 SearchEngine searchEngine, |
| 41 Source source, | 44 Source source, |
| 42 Iterable<CompletionContributor> newContributors) { | 45 Iterable<CompletionContributor> newContributors) { |
| 43 return new DartCompletionManager( | 46 return new DartCompletionManager( |
| 44 context, searchEngine, source, newContributors); | 47 context, searchEngine, source, newContributors); |
| 45 } | 48 } |
| 46 | 49 |
| 47 /** | 50 @override |
| 48 * If there is remaining work to be done, then wait for the unit to be | 51 Future<CompletionResult> computeSuggestions( |
| 49 * resolved and request that each remaining contributor finish their work. | 52 CompletionRequestImpl request) async { |
| 50 * Return a [Future] that completes when the last notification has been sent. | 53 CompletionPerformance performance = new CompletionPerformance(); |
| 51 */ | |
| 52 Future computeFull( | |
| 53 CompletionRequestImpl request, CompletionPerformance performance) async { | |
| 54 List<CompletionSuggestion> suggestions = <CompletionSuggestion>[]; | 54 List<CompletionSuggestion> suggestions = <CompletionSuggestion>[]; |
| 55 | 55 |
| 56 performance.logStartTime('computeSuggestions'); | 56 const COMPUTE_SUGGESTIONS_TAG = 'computeSuggestions'; |
| 57 performance.logStartTime(COMPUTE_SUGGESTIONS_TAG); |
| 57 for (CompletionContributor contributor in newContributors) { | 58 for (CompletionContributor contributor in newContributors) { |
| 58 String contributorTag = 'computeSuggestions - ${contributor.runtimeType}'; | 59 String contributorTag = 'computeSuggestions - ${contributor.runtimeType}'; |
| 59 performance.logStartTime(contributorTag); | 60 performance.logStartTime(contributorTag); |
| 60 suggestions.addAll(await contributor.computeSuggestions(request)); | 61 suggestions.addAll(await contributor.computeSuggestions(request)); |
| 61 performance.logElapseTime(contributorTag); | 62 performance.logElapseTime(contributorTag); |
| 62 } | 63 } |
| 63 performance.logElapseTime('computeSuggestions'); | 64 performance.logElapseTime(COMPUTE_SUGGESTIONS_TAG); |
| 64 | 65 |
| 65 // TODO (danrubel) if request is obsolete | 66 // TODO (danrubel) if request is obsolete |
| 66 // (processAnalysisRequest returns false) | 67 // (processAnalysisRequest returns false) |
| 67 // then send empty results | 68 // then send empty results |
| 68 | 69 |
| 69 if (controller != null && !controller.isClosed) { | 70 return new CompletionResultImpl( |
| 70 controller.add(new CompletionResultImpl(request.replacementOffset, | 71 request.replacementOffset, request.replacementLength, suggestions); |
| 71 request.replacementLength, suggestions, true)); | |
| 72 controller.close(); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 @override | |
| 77 void computeSuggestions(CompletionRequest request) { | |
| 78 CompletionPerformance performance = new CompletionPerformance(); | |
| 79 performance.logElapseTime('compute', () { | |
| 80 computeFull(request, performance); | |
| 81 }); | |
| 82 } | |
| 83 | |
| 84 /** | |
| 85 * Return a future that either (a) completes with the resolved compilation | |
| 86 * unit when analysis is complete, or (b) completes with null if the | |
| 87 * compilation unit is never going to be resolved. | |
| 88 */ | |
| 89 Future<CompilationUnit> waitForAnalysis() { | |
| 90 List<Source> libraries = context.getLibrariesContaining(source); | |
| 91 assert(libraries != null); | |
| 92 if (libraries.length == 0) { | |
| 93 return new Future.value(null); | |
| 94 } | |
| 95 Source libSource = libraries[0]; | |
| 96 assert(libSource != null); | |
| 97 return context | |
| 98 .computeResolvedCompilationUnitAsync(source, libSource) | |
| 99 .catchError((_) { | |
| 100 // This source file is not scheduled for analysis, so a resolved | |
| 101 // compilation unit is never going to get computed. | |
| 102 return null; | |
| 103 }, test: (e) => e is AnalysisNotScheduledError); | |
| 104 } | 72 } |
| 105 } | 73 } |
| OLD | NEW |