| 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 AnalysisRequest, CompletionContributor, CompletionRequest; |
| 12 import 'package:analysis_server/src/provisional/completion/dart/completion_targe
t.dart'; | 12 import 'package:analysis_server/src/provisional/completion/dart/completion_targe
t.dart'; |
| 13 import 'package:analysis_server/src/services/completion/completion_core.dart'; | 13 import 'package:analysis_server/src/services/completion/completion_core.dart'; |
| 14 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; | 14 import 'package:analysis_server/src/services/completion/completion_manager.dart'
; |
| 15 import 'package:analysis_server/src/services/completion/dart/common_usage_sorter
.dart'; | 15 import 'package:analysis_server/src/services/completion/dart/common_usage_sorter
.dart'; |
| 16 import 'package:analysis_server/src/services/completion/dart/contribution_sorter
.dart'; | 16 import 'package:analysis_server/src/services/completion/dart/contribution_sorter
.dart'; |
| 17 import 'package:analysis_server/src/services/completion/dart_completion_cache.da
rt'; | |
| 18 import 'package:analysis_server/src/services/completion/optype.dart'; | 17 import 'package:analysis_server/src/services/completion/optype.dart'; |
| 19 import 'package:analysis_server/src/services/search/search_engine.dart'; | 18 import 'package:analysis_server/src/services/search/search_engine.dart'; |
| 20 import 'package:analyzer/file_system/file_system.dart'; | 19 import 'package:analyzer/file_system/file_system.dart'; |
| 21 import 'package:analyzer/src/generated/ast.dart'; | 20 import 'package:analyzer/src/generated/ast.dart'; |
| 22 import 'package:analyzer/src/generated/engine.dart'; | 21 import 'package:analyzer/src/generated/engine.dart'; |
| 23 import 'package:analyzer/src/generated/scanner.dart'; | 22 import 'package:analyzer/src/generated/scanner.dart'; |
| 24 import 'package:analyzer/src/generated/source.dart'; | 23 import 'package:analyzer/src/generated/source.dart'; |
| 25 | 24 |
| 26 export 'package:analysis_server/src/provisional/completion/dart/completion_dart.
dart' | 25 export 'package:analysis_server/src/provisional/completion/dart/completion_dart.
dart' |
| 27 show | 26 show |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 */ | 69 */ |
| 71 class DartCompletionManager extends CompletionManager { | 70 class DartCompletionManager extends CompletionManager { |
| 72 /** | 71 /** |
| 73 * The [defaultContributionSorter] is a long-lived object that isn't allowed | 72 * The [defaultContributionSorter] is a long-lived object that isn't allowed |
| 74 * to maintain state between calls to [ContributionSorter#sort(...)]. | 73 * to maintain state between calls to [ContributionSorter#sort(...)]. |
| 75 */ | 74 */ |
| 76 static DartContributionSorter defaultContributionSorter = | 75 static DartContributionSorter defaultContributionSorter = |
| 77 new CommonUsageSorter(); | 76 new CommonUsageSorter(); |
| 78 | 77 |
| 79 final SearchEngine searchEngine; | 78 final SearchEngine searchEngine; |
| 80 final DartCompletionCache cache; | |
| 81 List<DartCompletionContributor> contributors; | 79 List<DartCompletionContributor> contributors; |
| 82 Iterable<CompletionContributor> newContributors; | 80 Iterable<CompletionContributor> newContributors; |
| 83 DartContributionSorter contributionSorter; | 81 DartContributionSorter contributionSorter; |
| 84 | 82 |
| 85 DartCompletionManager( | 83 DartCompletionManager( |
| 86 AnalysisContext context, this.searchEngine, Source source, this.cache, | 84 AnalysisContext context, this.searchEngine, Source source, |
| 87 [this.contributors, this.newContributors, this.contributionSorter]) | 85 [this.contributors, this.newContributors, this.contributionSorter]) |
| 88 : super(context, source) { | 86 : super(context, source) { |
| 89 if (contributors == null) { | 87 if (contributors == null) { |
| 90 contributors = [ | 88 contributors = [ |
| 91 // LocalReferenceContributor before ImportedReferenceContributor | 89 // LocalReferenceContributor before ImportedReferenceContributor |
| 92 // because local suggestions take precedence | 90 // because local suggestions take precedence |
| 93 // and can hide other suggestions with the same name | 91 // and can hide other suggestions with the same name |
| 94 //new LocalReferenceContributor(), | 92 //new LocalReferenceContributor(), |
| 95 //new ImportedReferenceContributor(), | 93 //new ImportedReferenceContributor(), |
| 96 //new KeywordContributor(), | 94 //new KeywordContributor(), |
| (...skipping 15 matching lines...) Expand all Loading... |
| 112 } | 110 } |
| 113 | 111 |
| 114 /** | 112 /** |
| 115 * Create a new initialized Dart source completion manager | 113 * Create a new initialized Dart source completion manager |
| 116 */ | 114 */ |
| 117 factory DartCompletionManager.create( | 115 factory DartCompletionManager.create( |
| 118 AnalysisContext context, | 116 AnalysisContext context, |
| 119 SearchEngine searchEngine, | 117 SearchEngine searchEngine, |
| 120 Source source, | 118 Source source, |
| 121 Iterable<CompletionContributor> newContributors) { | 119 Iterable<CompletionContributor> newContributors) { |
| 122 return new DartCompletionManager(context, searchEngine, source, | 120 return new DartCompletionManager( |
| 123 new DartCompletionCache(context, source), null, newContributors); | 121 context, searchEngine, source, null, newContributors); |
| 124 } | |
| 125 | |
| 126 @override | |
| 127 Future<bool> computeCache() { | |
| 128 return waitForAnalysis().then((CompilationUnit unit) { | |
| 129 if (unit != null && !cache.isImportInfoCached(unit)) { | |
| 130 return cache.computeImportInfo(unit, searchEngine, true); | |
| 131 } else { | |
| 132 return new Future.value(false); | |
| 133 } | |
| 134 }); | |
| 135 } | 122 } |
| 136 | 123 |
| 137 /** | 124 /** |
| 138 * Compute suggestions based upon cached information only | 125 * Compute suggestions based upon cached information only |
| 139 * then send an initial response to the client. | 126 * then send an initial response to the client. |
| 140 * Return a list of contributors for which [computeFull] should be called | 127 * Return a list of contributors for which [computeFull] should be called |
| 141 */ | 128 */ |
| 142 List<DartCompletionContributor> computeFast( | 129 List<DartCompletionContributor> computeFast( |
| 143 DartCompletionRequest request, CompletionPerformance performance) { | 130 DartCompletionRequest request, CompletionPerformance performance) { |
| 144 return performance.logElapseTime('computeFast', () { | 131 return performance.logElapseTime('computeFast', () { |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 }); | 229 }); |
| 243 }); | 230 }); |
| 244 }); | 231 }); |
| 245 }); | 232 }); |
| 246 }); | 233 }); |
| 247 } | 234 } |
| 248 | 235 |
| 249 @override | 236 @override |
| 250 void computeSuggestions(CompletionRequest completionRequest) { | 237 void computeSuggestions(CompletionRequest completionRequest) { |
| 251 DartCompletionRequest request = | 238 DartCompletionRequest request = |
| 252 new DartCompletionRequest.from(completionRequest, cache); | 239 new DartCompletionRequest.from(completionRequest); |
| 253 CompletionPerformance performance = new CompletionPerformance(); | 240 CompletionPerformance performance = new CompletionPerformance(); |
| 254 performance.logElapseTime('compute', () { | 241 performance.logElapseTime('compute', () { |
| 255 List<DartCompletionContributor> todo = computeFast(request, performance); | 242 List<DartCompletionContributor> todo = computeFast(request, performance); |
| 256 computeFull(request, performance, todo); | 243 computeFull(request, performance, todo); |
| 257 }); | 244 }); |
| 258 } | 245 } |
| 259 | 246 |
| 260 /** | 247 /** |
| 261 * Send the current list of suggestions to the client. | 248 * Send the current list of suggestions to the client. |
| 262 */ | 249 */ |
| (...skipping 29 matching lines...) Expand all Loading... |
| 292 return null; | 279 return null; |
| 293 }, test: (e) => e is AnalysisNotScheduledError); | 280 }, test: (e) => e is AnalysisNotScheduledError); |
| 294 } | 281 } |
| 295 } | 282 } |
| 296 | 283 |
| 297 /** | 284 /** |
| 298 * The context in which the completion is requested. | 285 * The context in which the completion is requested. |
| 299 */ | 286 */ |
| 300 class DartCompletionRequest extends CompletionRequestImpl { | 287 class DartCompletionRequest extends CompletionRequestImpl { |
| 301 /** | 288 /** |
| 302 * Cached information from a prior code completion operation. | |
| 303 */ | |
| 304 final DartCompletionCache cache; | |
| 305 | |
| 306 /** | |
| 307 * The compilation unit in which the completion was requested. This unit | 289 * The compilation unit in which the completion was requested. This unit |
| 308 * may or may not be resolved when [DartCompletionContributor.computeFast] | 290 * may or may not be resolved when [DartCompletionContributor.computeFast] |
| 309 * is called but is resolved when [DartCompletionContributor.computeFull]. | 291 * is called but is resolved when [DartCompletionContributor.computeFull]. |
| 310 */ | 292 */ |
| 311 CompilationUnit unit; | 293 CompilationUnit unit; |
| 312 | 294 |
| 313 /** | 295 /** |
| 314 * The completion target. This determines what part of the parse tree | 296 * The completion target. This determines what part of the parse tree |
| 315 * will receive the newly inserted text. | 297 * will receive the newly inserted text. |
| 316 */ | 298 */ |
| (...skipping 28 matching lines...) Expand all Loading... |
| 345 /** | 327 /** |
| 346 * The set of completions used to prevent duplicates | 328 * The set of completions used to prevent duplicates |
| 347 */ | 329 */ |
| 348 final Set<String> _completions = new Set<String>(); | 330 final Set<String> _completions = new Set<String>(); |
| 349 | 331 |
| 350 DartCompletionRequest( | 332 DartCompletionRequest( |
| 351 AnalysisContext context, | 333 AnalysisContext context, |
| 352 ResourceProvider resourceProvider, | 334 ResourceProvider resourceProvider, |
| 353 SearchEngine searchEngine, | 335 SearchEngine searchEngine, |
| 354 Source source, | 336 Source source, |
| 355 int offset, | 337 int offset) |
| 356 this.cache) | |
| 357 : super(context, resourceProvider, searchEngine, source, offset); | 338 : super(context, resourceProvider, searchEngine, source, offset); |
| 358 | 339 |
| 359 factory DartCompletionRequest.from( | 340 factory DartCompletionRequest.from(CompletionRequestImpl request) => |
| 360 CompletionRequestImpl request, DartCompletionCache cache) => | |
| 361 new DartCompletionRequest(request.context, request.resourceProvider, | 341 new DartCompletionRequest(request.context, request.resourceProvider, |
| 362 request.searchEngine, request.source, request.offset, cache); | 342 request.searchEngine, request.source, request.offset); |
| 363 | 343 |
| 364 /** | 344 /** |
| 365 * Return the original text from the [replacementOffset] to the [offset] | 345 * Return the original text from the [replacementOffset] to the [offset] |
| 366 * that can be used to filter the suggestions on the server side. | 346 * that can be used to filter the suggestions on the server side. |
| 367 */ | 347 */ |
| 368 String get filterText { | 348 String get filterText { |
| 369 return context | 349 return context |
| 370 .getContents(source) | 350 .getContents(source) |
| 371 .data | 351 .data |
| 372 .substring(replacementOffset, offset); | 352 .substring(replacementOffset, offset); |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 // Replacement range for import URI | 458 // Replacement range for import URI |
| 479 return new ReplacementRange(start, end - start); | 459 return new ReplacementRange(start, end - start); |
| 480 } | 460 } |
| 481 } | 461 } |
| 482 } | 462 } |
| 483 } | 463 } |
| 484 } | 464 } |
| 485 return new ReplacementRange(requestOffset, 0); | 465 return new ReplacementRange(requestOffset, 0); |
| 486 } | 466 } |
| 487 } | 467 } |
| OLD | NEW |