| 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 import 'dart:async'; | 5 import 'dart:async'; |
| 6 import 'dart:core'; | 6 import 'dart:core'; |
| 7 | 7 |
| 8 import 'package:analysis_server/src/protocol_server.dart' | 8 import 'package:analysis_server/src/protocol_server.dart' |
| 9 show CompletionSuggestion, CompletionSuggestionKind; | 9 show CompletionSuggestion, CompletionSuggestionKind; |
| 10 import 'package:analysis_server/src/provisional/completion/dart/completion_dart.
dart'; | 10 import 'package:analysis_server/src/provisional/completion/dart/completion_dart.
dart'; |
| 11 import 'package:analyzer/dart/ast/ast.dart'; | 11 import 'package:analyzer/dart/ast/ast.dart'; |
| 12 import 'package:analyzer/dart/ast/visitor.dart'; | 12 import 'package:analyzer/dart/ast/visitor.dart'; |
| 13 import 'package:analyzer/file_system/file_system.dart'; | 13 import 'package:analyzer/file_system/file_system.dart'; |
| 14 import 'package:analyzer/src/generated/sdk.dart'; | 14 import 'package:analyzer/src/generated/sdk.dart'; |
| 15 import 'package:analyzer/src/generated/source.dart'; | 15 import 'package:analyzer/src/generated/source.dart'; |
| 16 import 'package:path/path.dart' show posix; | 16 import 'package:path/path.dart' show posix; |
| 17 import 'package:path/src/context.dart'; | 17 import 'package:path/src/context.dart'; |
| 18 | 18 |
| 19 /** | 19 /** |
| 20 * A contributor for calculating uri suggestions | 20 * A contributor for calculating uri suggestions |
| 21 * for import and part directives. | 21 * for import and part directives. |
| 22 */ | 22 */ |
| 23 class UriContributor extends DartCompletionContributor { | 23 class UriContributor extends DartCompletionContributor { |
| 24 _UriSuggestionBuilder builder; | 24 _UriSuggestionBuilder builder; |
| 25 | 25 |
| 26 /** |
| 27 * A flag indicating whether file: and package: URI suggestions should |
| 28 * be included in the list of completion suggestions. |
| 29 */ |
| 30 // TODO(danrubel): remove this flag and related functionality |
| 31 // once the UriContributor limits file: and package: URI suggestions |
| 32 // to only those paths within context roots. |
| 33 static bool suggestFilePaths = true; |
| 34 |
| 26 @override | 35 @override |
| 27 Future<List<CompletionSuggestion>> computeSuggestions( | 36 Future<List<CompletionSuggestion>> computeSuggestions( |
| 28 DartCompletionRequest request) async { | 37 DartCompletionRequest request) async { |
| 29 builder = new _UriSuggestionBuilder(request); | 38 builder = new _UriSuggestionBuilder(request); |
| 30 request.target.containingNode.accept(builder); | 39 request.target.containingNode.accept(builder); |
| 31 return builder.suggestions; | 40 return builder.suggestions; |
| 32 } | 41 } |
| 33 } | 42 } |
| 34 | 43 |
| 35 class _UriSuggestionBuilder extends SimpleAstVisitor { | 44 class _UriSuggestionBuilder extends SimpleAstVisitor { |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 } | 97 } |
| 89 } | 98 } |
| 90 | 99 |
| 91 @override | 100 @override |
| 92 visitSimpleStringLiteral(SimpleStringLiteral node) { | 101 visitSimpleStringLiteral(SimpleStringLiteral node) { |
| 93 AstNode parent = node.parent; | 102 AstNode parent = node.parent; |
| 94 if (parent is NamespaceDirective && parent.uri == node) { | 103 if (parent is NamespaceDirective && parent.uri == node) { |
| 95 String partialUri = _extractPartialUri(node); | 104 String partialUri = _extractPartialUri(node); |
| 96 if (partialUri != null) { | 105 if (partialUri != null) { |
| 97 _addDartSuggestions(); | 106 _addDartSuggestions(); |
| 98 _addPackageSuggestions(partialUri); | 107 if (UriContributor.suggestFilePaths) { |
| 99 _addFileSuggestions(partialUri); | 108 _addPackageSuggestions(partialUri); |
| 109 _addFileSuggestions(partialUri); |
| 110 } |
| 100 } | 111 } |
| 101 } else if (parent is PartDirective && parent.uri == node) { | 112 } else if (parent is PartDirective && parent.uri == node) { |
| 102 String partialUri = _extractPartialUri(node); | 113 String partialUri = _extractPartialUri(node); |
| 103 if (partialUri != null) { | 114 if (partialUri != null) { |
| 104 _addFileSuggestions(partialUri); | 115 if (UriContributor.suggestFilePaths) { |
| 116 _addFileSuggestions(partialUri); |
| 117 } |
| 105 } | 118 } |
| 106 } | 119 } |
| 107 } | 120 } |
| 108 | 121 |
| 109 void _addDartSuggestions() { | 122 void _addDartSuggestions() { |
| 110 _addSuggestion('dart:'); | 123 _addSuggestion('dart:'); |
| 111 SourceFactory factory = request.sourceFactory; | 124 SourceFactory factory = request.sourceFactory; |
| 112 for (SdkLibrary lib in factory.dartSdk.sdkLibraries) { | 125 for (SdkLibrary lib in factory.dartSdk.sdkLibraries) { |
| 113 if (!lib.isInternal && !lib.isImplementation) { | 126 if (!lib.isInternal && !lib.isImplementation) { |
| 114 if (!lib.shortName.startsWith('dart:_')) { | 127 if (!lib.shortName.startsWith('dart:_')) { |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 } | 233 } |
| 221 | 234 |
| 222 String _extractPartialUri(SimpleStringLiteral node) { | 235 String _extractPartialUri(SimpleStringLiteral node) { |
| 223 if (request.offset < node.contentsOffset) { | 236 if (request.offset < node.contentsOffset) { |
| 224 return null; | 237 return null; |
| 225 } | 238 } |
| 226 return node.literal.lexeme.substring( | 239 return node.literal.lexeme.substring( |
| 227 node.contentsOffset - node.offset, request.offset - node.offset); | 240 node.contentsOffset - node.offset, request.offset - node.offset); |
| 228 } | 241 } |
| 229 } | 242 } |
| OLD | NEW |