| 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.contributor.dart.importuri; | 5 library services.completion.contributor.dart.importuri; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | |
| 9 import 'dart:core' hide Resource; | 8 import 'dart:core' hide Resource; |
| 10 | 9 |
| 11 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | 10 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; |
| 12 import 'package:analyzer/file_system/file_system.dart'; | 11 import 'package:analyzer/file_system/file_system.dart'; |
| 13 import 'package:analyzer/src/generated/ast.dart'; | 12 import 'package:analyzer/src/generated/ast.dart'; |
| 14 import 'package:analyzer/src/generated/sdk.dart'; | 13 import 'package:analyzer/src/generated/sdk.dart'; |
| 15 import 'package:analyzer/src/generated/source.dart'; | 14 import 'package:analyzer/src/generated/source.dart'; |
| 16 import 'package:path/path.dart'; | 15 import 'package:path/path.dart'; |
| 17 | 16 |
| 18 import '../../protocol_server.dart' | 17 import '../../protocol_server.dart' |
| (...skipping 13 matching lines...) Expand all Loading... |
| 32 } | 31 } |
| 33 | 32 |
| 34 @override | 33 @override |
| 35 Future<bool> computeFull(DartCompletionRequest request) { | 34 Future<bool> computeFull(DartCompletionRequest request) { |
| 36 return new Future.value(false); | 35 return new Future.value(false); |
| 37 } | 36 } |
| 38 } | 37 } |
| 39 | 38 |
| 40 class _ImportUriSuggestionBuilder extends SimpleAstVisitor { | 39 class _ImportUriSuggestionBuilder extends SimpleAstVisitor { |
| 41 final DartCompletionRequest request; | 40 final DartCompletionRequest request; |
| 42 HashSet<String> _importedUris; | |
| 43 | 41 |
| 44 _ImportUriSuggestionBuilder(this.request); | 42 _ImportUriSuggestionBuilder(this.request); |
| 45 | 43 |
| 46 bool computeFast(AstNode node) { | 44 bool computeFast(AstNode node) { |
| 47 node.accept(this); | 45 node.accept(this); |
| 48 return true; | 46 return true; |
| 49 } | 47 } |
| 50 | 48 |
| 51 @override | 49 @override |
| 52 visitImportDirective(ImportDirective node) { | 50 visitImportDirective(ImportDirective node) { |
| 53 StringLiteral uri = node.uri; | 51 StringLiteral uri = node.uri; |
| 54 if (uri is SimpleStringLiteral) { | 52 if (uri is SimpleStringLiteral) { |
| 55 int offset = request.offset; | 53 int offset = request.offset; |
| 56 if (uri.offset < offset && | 54 if (uri.offset < offset && |
| 57 (offset < uri.end || offset == uri.offset + 1)) { | 55 (offset < uri.end || offset == uri.offset + 1)) { |
| 58 // Handle degenerate case where import is only line in file | 56 // Handle degenerate case where import is only line in file |
| 59 // and there is no semicolon | 57 // and there is no semicolon |
| 60 visitSimpleStringLiteral(uri); | 58 visitSimpleStringLiteral(uri); |
| 61 } | 59 } |
| 62 } | 60 } |
| 63 } | 61 } |
| 64 | 62 |
| 65 @override | 63 @override |
| 66 visitSimpleStringLiteral(SimpleStringLiteral node) { | 64 visitSimpleStringLiteral(SimpleStringLiteral node) { |
| 67 AstNode parent = node.parent; | 65 AstNode parent = node.parent; |
| 68 if (parent is ImportDirective && parent.uri == node) { | 66 if (parent is ImportDirective && parent.uri == node) { |
| 69 String partial = node.literal.lexeme.substring( | 67 String partial = node.literal.lexeme.substring( |
| 70 node.contentsOffset - node.offset, request.offset - node.offset); | 68 node.contentsOffset - node.offset, request.offset - node.offset); |
| 71 _computeImportedUris(); | |
| 72 request.replacementOffset = node.contentsOffset; | 69 request.replacementOffset = node.contentsOffset; |
| 73 request.replacementLength = node.contentsEnd - node.contentsOffset; | 70 request.replacementLength = node.contentsEnd - node.contentsOffset; |
| 74 _addDartSuggestions(); | 71 _addDartSuggestions(); |
| 75 _addPackageSuggestions(partial); | 72 _addPackageSuggestions(partial); |
| 76 _addFileSuggestions(partial); | 73 _addFileSuggestions(partial); |
| 77 } else if (parent is PartDirective && parent.uri == node) { | 74 } else if (parent is PartDirective && parent.uri == node) { |
| 78 String partial = node.literal.lexeme.substring( | 75 String partial = node.literal.lexeme.substring( |
| 79 node.contentsOffset - node.offset, request.offset - node.offset); | 76 node.contentsOffset - node.offset, request.offset - node.offset); |
| 80 _computeImportedUris(); | |
| 81 request.replacementOffset = node.contentsOffset; | 77 request.replacementOffset = node.contentsOffset; |
| 82 request.replacementLength = node.contentsEnd - node.contentsOffset; | 78 request.replacementLength = node.contentsEnd - node.contentsOffset; |
| 83 _addFileSuggestions(partial); | 79 _addFileSuggestions(partial); |
| 84 } | 80 } |
| 85 } | 81 } |
| 86 | 82 |
| 87 void _addDartSuggestions() { | 83 void _addDartSuggestions() { |
| 88 _addSuggestion('dart:'); | 84 _addSuggestion('dart:'); |
| 89 SourceFactory factory = request.context.sourceFactory; | 85 SourceFactory factory = request.context.sourceFactory; |
| 90 for (SdkLibrary lib in factory.dartSdk.sdkLibraries) { | 86 for (SdkLibrary lib in factory.dartSdk.sdkLibraries) { |
| 91 if (!lib.isInternal && !lib.isImplementation) { | 87 if (!lib.isInternal && !lib.isImplementation) { |
| 92 if (!lib.shortName.startsWith('dart:_')) { | 88 if (!lib.shortName.startsWith('dart:_')) { |
| 93 _addSuggestion(lib.shortName); | 89 _addSuggestion(lib.shortName, |
| 90 relevance: lib.shortName == 'dart:core' |
| 91 ? DART_RELEVANCE_LOW |
| 92 : DART_RELEVANCE_DEFAULT); |
| 94 } | 93 } |
| 95 } | 94 } |
| 96 } | 95 } |
| 97 } | 96 } |
| 98 | 97 |
| 99 void _addFileSuggestions(String partial) { | 98 void _addFileSuggestions(String partial) { |
| 100 Source source = request.source; | 99 Source source = request.source; |
| 101 String sourceFullName = source.fullName; | 100 String sourceFullName = source.fullName; |
| 102 String sourceShortName = source.shortName; | 101 String sourceShortName = source.shortName; |
| 103 String dirPath = (partial.endsWith('/') || partial.endsWith(separator)) | 102 String dirPath = (partial.endsWith('/') || partial.endsWith(separator)) |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 _addSuggestion(prefix); | 152 _addSuggestion(prefix); |
| 154 for (Folder folder in folders) { | 153 for (Folder folder in folders) { |
| 155 if (folder.exists) { | 154 if (folder.exists) { |
| 156 _addPackageFolderSuggestions(partial, prefix, folder); | 155 _addPackageFolderSuggestions(partial, prefix, folder); |
| 157 } | 156 } |
| 158 } | 157 } |
| 159 }); | 158 }); |
| 160 } | 159 } |
| 161 } | 160 } |
| 162 | 161 |
| 163 void _addSuggestion(String completion) { | 162 void _addSuggestion(String completion, |
| 164 if (!_importedUris.contains(completion)) { | 163 {int relevance: DART_RELEVANCE_DEFAULT}) { |
| 165 request.addSuggestion(new CompletionSuggestion( | 164 request.addSuggestion(new CompletionSuggestion( |
| 166 CompletionSuggestionKind.IMPORT, DART_RELEVANCE_DEFAULT, completion, | 165 CompletionSuggestionKind.IMPORT, |
| 167 completion.length, 0, false, false)); | 166 relevance, |
| 168 } | 167 completion, |
| 169 } | 168 completion.length, |
| 170 | 169 0, |
| 171 void _computeImportedUris() { | 170 false, |
| 172 _importedUris = new HashSet<String>(); | 171 false)); |
| 173 _importedUris.add('dart:core'); | |
| 174 for (Directive directive in request.unit.directives) { | |
| 175 if (directive is ImportDirective) { | |
| 176 String uri = directive.uriContent; | |
| 177 if (uri != null && uri.length > 0) { | |
| 178 _importedUris.add(uri); | |
| 179 } | |
| 180 } | |
| 181 } | |
| 182 } | 172 } |
| 183 } | 173 } |
| OLD | NEW |