OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library services.completion.contributor.dart.importuri; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:core' hide Resource; | |
9 | |
10 import 'package:analysis_server/src/services/completion/dart_completion_manager.
dart'; | |
11 import 'package:analyzer/file_system/file_system.dart'; | |
12 import 'package:analyzer/src/generated/ast.dart'; | |
13 import 'package:analyzer/src/generated/sdk.dart'; | |
14 import 'package:analyzer/src/generated/source.dart'; | |
15 import 'package:path/path.dart' show posix; | |
16 import 'package:path/src/context.dart'; | |
17 | |
18 import '../../protocol_server.dart' | |
19 show CompletionSuggestion, CompletionSuggestionKind; | |
20 | |
21 /** | |
22 * A contributor for calculating uri suggestions | |
23 * for import and part directives. | |
24 */ | |
25 class UriContributor extends DartCompletionContributor { | |
26 _UriSuggestionBuilder builder; | |
27 | |
28 @override | |
29 bool computeFast(DartCompletionRequest request) { | |
30 builder = new _UriSuggestionBuilder(request); | |
31 return builder.computeFast(request.target.containingNode); | |
32 } | |
33 | |
34 @override | |
35 Future<bool> computeFull(DartCompletionRequest request) { | |
36 return new Future.value(false); | |
37 } | |
38 } | |
39 | |
40 class _UriSuggestionBuilder extends SimpleAstVisitor { | |
41 final DartCompletionRequest request; | |
42 | |
43 _UriSuggestionBuilder(this.request); | |
44 | |
45 bool computeFast(AstNode node) { | |
46 node.accept(this); | |
47 return true; | |
48 } | |
49 | |
50 @override | |
51 visitExportDirective(ExportDirective node) { | |
52 visitNamespaceDirective(node); | |
53 } | |
54 | |
55 @override | |
56 visitImportDirective(ImportDirective node) { | |
57 visitNamespaceDirective(node); | |
58 } | |
59 | |
60 visitNamespaceDirective(NamespaceDirective node) { | |
61 StringLiteral uri = node.uri; | |
62 if (uri is SimpleStringLiteral) { | |
63 int offset = request.offset; | |
64 if (uri.offset < offset && | |
65 (offset < uri.end || offset == uri.offset + 1)) { | |
66 // Handle degenerate case where import or export is only line in file | |
67 // and there is no semicolon | |
68 visitSimpleStringLiteral(uri); | |
69 } | |
70 } | |
71 } | |
72 | |
73 @override | |
74 visitSimpleStringLiteral(SimpleStringLiteral node) { | |
75 AstNode parent = node.parent; | |
76 if (parent is NamespaceDirective && parent.uri == node) { | |
77 String partialUri = _extractPartialUri(node); | |
78 if (partialUri != null) { | |
79 _addDartSuggestions(); | |
80 _addPackageSuggestions(partialUri); | |
81 _addFileSuggestions(partialUri); | |
82 } | |
83 } else if (parent is PartDirective && parent.uri == node) { | |
84 String partialUri = _extractPartialUri(node); | |
85 if (partialUri != null) { | |
86 _addFileSuggestions(partialUri); | |
87 } | |
88 } | |
89 } | |
90 | |
91 void _addDartSuggestions() { | |
92 _addSuggestion('dart:'); | |
93 SourceFactory factory = request.context.sourceFactory; | |
94 for (SdkLibrary lib in factory.dartSdk.sdkLibraries) { | |
95 if (!lib.isInternal && !lib.isImplementation) { | |
96 if (!lib.shortName.startsWith('dart:_')) { | |
97 _addSuggestion(lib.shortName, | |
98 relevance: lib.shortName == 'dart:core' | |
99 ? DART_RELEVANCE_LOW | |
100 : DART_RELEVANCE_DEFAULT); | |
101 } | |
102 } | |
103 } | |
104 } | |
105 | |
106 void _addFileSuggestions(String partialUri) { | |
107 ResourceProvider resProvider = request.resourceProvider; | |
108 Context resContext = resProvider.pathContext; | |
109 Source source = request.source; | |
110 | |
111 String parentUri; | |
112 if ((partialUri.endsWith('/'))) { | |
113 parentUri = partialUri; | |
114 } else { | |
115 parentUri = posix.dirname(partialUri); | |
116 if (parentUri != '.' && !parentUri.endsWith('/')) { | |
117 parentUri = '$parentUri/'; | |
118 } | |
119 } | |
120 String uriPrefix = parentUri == '.' ? '' : parentUri; | |
121 | |
122 String dirPath = resContext.normalize(parentUri); | |
123 if (resContext.isRelative(dirPath)) { | |
124 String sourceDirPath = resContext.dirname(source.fullName); | |
125 if (resContext.isAbsolute(sourceDirPath)) { | |
126 dirPath = resContext.join(sourceDirPath, dirPath); | |
127 } else { | |
128 return; | |
129 } | |
130 } | |
131 | |
132 Resource dir = resProvider.getResource(dirPath); | |
133 if (dir is Folder) { | |
134 for (Resource child in dir.getChildren()) { | |
135 String completion; | |
136 if (child is Folder) { | |
137 completion = '$uriPrefix${child.shortName}/'; | |
138 } else { | |
139 completion = '$uriPrefix${child.shortName}'; | |
140 } | |
141 if (completion != source.shortName) { | |
142 _addSuggestion(completion); | |
143 } | |
144 } | |
145 } | |
146 } | |
147 | |
148 void _addPackageFolderSuggestions( | |
149 String partial, String prefix, Folder folder) { | |
150 for (Resource child in folder.getChildren()) { | |
151 if (child is Folder) { | |
152 String childPrefix = '$prefix${child.shortName}/'; | |
153 _addSuggestion(childPrefix); | |
154 if (partial.startsWith(childPrefix)) { | |
155 _addPackageFolderSuggestions(partial, childPrefix, child); | |
156 } | |
157 } else { | |
158 _addSuggestion('$prefix${child.shortName}'); | |
159 } | |
160 } | |
161 } | |
162 | |
163 void _addPackageSuggestions(String partial) { | |
164 SourceFactory factory = request.context.sourceFactory; | |
165 Map<String, List<Folder>> packageMap = factory.packageMap; | |
166 if (packageMap != null) { | |
167 _addSuggestion('package:'); | |
168 packageMap.forEach((String pkgName, List<Folder> folders) { | |
169 String prefix = 'package:$pkgName/'; | |
170 _addSuggestion(prefix); | |
171 for (Folder folder in folders) { | |
172 if (folder.exists) { | |
173 _addPackageFolderSuggestions(partial, prefix, folder); | |
174 } | |
175 } | |
176 }); | |
177 } | |
178 } | |
179 | |
180 void _addSuggestion(String completion, | |
181 {int relevance: DART_RELEVANCE_DEFAULT}) { | |
182 request.addSuggestion(new CompletionSuggestion( | |
183 CompletionSuggestionKind.IMPORT, | |
184 relevance, | |
185 completion, | |
186 completion.length, | |
187 0, | |
188 false, | |
189 false)); | |
190 } | |
191 | |
192 String _extractPartialUri(SimpleStringLiteral node) { | |
193 if (request.offset < node.contentsOffset) { | |
194 return null; | |
195 } | |
196 String partial = node.literal.lexeme.substring( | |
197 node.contentsOffset - node.offset, request.offset - node.offset); | |
198 request.replacementOffset = node.contentsOffset; | |
199 request.replacementLength = node.contentsEnd - node.contentsOffset; | |
200 return partial; | |
201 } | |
202 } | |
OLD | NEW |