OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 analysis_server.src.services.completion.completion_dart; | 5 library analysis_server.src.services.completion.completion_dart; |
6 | 6 |
7 import 'package:analysis_server/src/provisional/completion/completion_core.dart'
; | 7 import 'dart:async'; |
| 8 |
| 9 import 'package:analysis_server/src/provisional/completion/completion_core.dart' |
| 10 show CompletionRequest; |
8 import 'package:analysis_server/src/provisional/completion/completion_dart.dart'
; | 11 import 'package:analysis_server/src/provisional/completion/completion_dart.dart'
; |
9 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'; |
10 import 'package:analysis_server/src/services/completion/completion_core.dart'; | 13 import 'package:analysis_server/src/services/completion/completion_core.dart'; |
| 14 import 'package:analyzer/file_system/file_system.dart'; |
| 15 import 'package:analyzer/src/context/context.dart' |
| 16 show AnalysisFutureHelper, AnalysisContextImpl; |
11 import 'package:analyzer/src/generated/ast.dart'; | 17 import 'package:analyzer/src/generated/ast.dart'; |
| 18 import 'package:analyzer/src/generated/engine.dart' hide AnalysisContextImpl; |
| 19 import 'package:analyzer/src/generated/source.dart'; |
| 20 import 'package:analyzer/src/task/dart.dart'; |
| 21 import 'package:analyzer/task/dart.dart'; |
12 | 22 |
13 /** | 23 /** |
14 * The information about a requested list of completions within a Dart file. | 24 * The information about a requested list of completions within a Dart file. |
15 */ | 25 */ |
16 class DartCompletionRequestImpl extends CompletionRequestImpl | 26 class DartCompletionRequestImpl extends CompletionRequestImpl |
17 implements DartCompletionRequest { | 27 implements DartCompletionRequest { |
18 /** | 28 /** |
19 * The compilation unit in which the completion was requested. | 29 * The cached completion target or `null` if not computed yet. |
20 */ | 30 */ |
21 final CompilationUnit unit; | 31 CompletionTarget _target; |
22 | 32 |
23 /** | 33 /** |
24 * A flag indicating whether the compilation [unit] is resolved. | 34 * `true` if [resolveDeclarationsInScope] has partially resolved the unit |
| 35 * referenced by [target], else `false`. |
25 */ | 36 */ |
26 final bool isResolved; | 37 bool _haveResolveDeclarationsInScope = false; |
27 | 38 |
28 /** | 39 /** |
29 * The completion target. This determines what part of the parse tree | 40 * Initialize a newly created completion request based on the given request. |
30 * will receive the newly inserted text. | |
31 */ | 41 */ |
32 final CompletionTarget target; | 42 factory DartCompletionRequestImpl.forRequest(CompletionRequest request) { |
| 43 return new DartCompletionRequestImpl._(request.context, |
| 44 request.resourceProvider, request.source, request.offset); |
| 45 } |
33 | 46 |
34 /** | 47 DartCompletionRequestImpl._(AnalysisContext context, |
35 * Initialize a newly created completion request based on the given arguments. | 48 ResourceProvider resourceProvider, Source source, int offset) |
36 */ | 49 : super(context, resourceProvider, source, offset); |
37 DartCompletionRequestImpl( | 50 |
38 CompletionRequest request, this.unit, this.isResolved, this.target) | 51 @override |
39 : super(request.context, request.resourceProvider, request.source, | 52 Future<CompilationUnit> resolveDeclarationsInScope() async { |
40 request.offset); | 53 CompilationUnit unit = target.unit; |
| 54 if (_haveResolveDeclarationsInScope) { |
| 55 return unit; |
| 56 } |
| 57 |
| 58 // Determine the library source |
| 59 Source librarySource; |
| 60 if (unit.directives.any((d) => d is PartOfDirective)) { |
| 61 List<Source> libraries = context.getLibrariesContaining(source); |
| 62 if (libraries.isEmpty) { |
| 63 return null; |
| 64 } |
| 65 librarySource = libraries[0]; |
| 66 } else { |
| 67 librarySource = source; |
| 68 } |
| 69 |
| 70 // Resolve declarations in the target unit |
| 71 CompilationUnit resolvedUnit = |
| 72 await new AnalysisFutureHelper<CompilationUnit>( |
| 73 context, |
| 74 new LibrarySpecificUnit(librarySource, source), |
| 75 RESOLVED_UNIT3).computeAsync(); |
| 76 |
| 77 // TODO(danrubel) determine if the underlying source has been modified |
| 78 // in a way that invalidates the completion request |
| 79 // and return null |
| 80 |
| 81 // Gracefully degrade if unit cannot be resolved |
| 82 if (resolvedUnit == null) { |
| 83 return null; |
| 84 } |
| 85 |
| 86 // Recompute the target for the newly resolved unit |
| 87 _target = new CompletionTarget.forOffset(resolvedUnit, offset); |
| 88 _haveResolveDeclarationsInScope = true; |
| 89 return resolvedUnit; |
| 90 } |
| 91 |
| 92 @override |
| 93 CompletionTarget get target { |
| 94 if (_target == null) { |
| 95 CompilationUnit unit = context.computeResult(source, PARSED_UNIT); |
| 96 _target = new CompletionTarget.forOffset(unit, offset); |
| 97 } |
| 98 return _target; |
| 99 } |
41 } | 100 } |
OLD | NEW |