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 trydart.poi; | 5 library trydart.poi; |
6 | 6 |
7 import 'dart:async' show | 7 import 'dart:async' show |
8 Completer, | 8 Completer, |
9 Future, | 9 Future, |
10 Stream; | 10 Stream; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 DartType; | 57 DartType; |
58 | 58 |
59 import 'package:compiler/implementation/scanner/scannerlib.dart' show | 59 import 'package:compiler/implementation/scanner/scannerlib.dart' show |
60 EOF_TOKEN, | 60 EOF_TOKEN, |
61 IDENTIFIER_TOKEN, | 61 IDENTIFIER_TOKEN, |
62 KEYWORD_TOKEN, | 62 KEYWORD_TOKEN, |
63 PartialClassElement, | 63 PartialClassElement, |
64 PartialElement, | 64 PartialElement, |
65 Token; | 65 Token; |
66 | 66 |
67 /// Controls if this program should be querying Dart Mind. Used by tests. | 67 /// Enabled by the option --enable-dart-mind. Controls if this program should |
68 bool enableDartMind = true; | 68 /// be querying Dart Mind. |
| 69 bool isDartMindEnabled = false; |
69 | 70 |
70 /// Iterator over lines from standard input (or the argument array). | 71 /// Iterator over lines from standard input (or the argument array). |
71 Iterator<String> stdin; | 72 Iterator<String> stdin; |
72 | 73 |
73 /// Enabled by the option --simulate-mutation. When true, this program will | 74 /// Enabled by the option --simulate-mutation. When true, this program will |
74 /// only prompt for one file name, and subsequent runs will read | 75 /// only prompt for one file name, and subsequent runs will read |
75 /// FILENAME.N.dart, where N starts at 1, and is increased on each iteration. | 76 /// FILENAME.N.dart, where N starts at 1, and is increased on each iteration. |
76 /// For example, if the program is invoked as: | 77 /// For example, if the program is invoked as: |
77 /// | 78 /// |
78 /// dart poi.dart --simulate-mutation test.dart 11 22 33 44 | 79 /// dart poi.dart --simulate-mutation test.dart 11 22 33 44 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 | 115 |
115 main(List<String> arguments) { | 116 main(List<String> arguments) { |
116 poiCount = 0; | 117 poiCount = 0; |
117 List<String> nonOptionArguments = []; | 118 List<String> nonOptionArguments = []; |
118 for (String argument in arguments) { | 119 for (String argument in arguments) { |
119 if (argument.startsWith('-')) { | 120 if (argument.startsWith('-')) { |
120 switch (argument) { | 121 switch (argument) { |
121 case '--simulate-mutation': | 122 case '--simulate-mutation': |
122 isSimulateMutationEnabled = true; | 123 isSimulateMutationEnabled = true; |
123 break; | 124 break; |
| 125 case '--enable-dart-mind': |
| 126 isDartMindEnabled = true; |
| 127 break; |
124 case '-v': | 128 case '-v': |
125 case '--verbose': | 129 case '--verbose': |
126 isVerbose = true; | 130 isVerbose = true; |
127 break; | 131 break; |
128 default: | 132 default: |
129 throw 'Unknown option: $argument.'; | 133 throw 'Unknown option: $argument.'; |
130 } | 134 } |
131 } else { | 135 } else { |
132 nonOptionArguments.add(argument); | 136 nonOptionArguments.add(argument); |
133 } | 137 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 stdout.write(message); | 199 stdout.write(message); |
196 } | 200 } |
197 return stdout.flush().then((_) { | 201 return stdout.flush().then((_) { |
198 stdin.moveNext(); | 202 stdin.moveNext(); |
199 return stdin.current; | 203 return stdin.current; |
200 }); | 204 }); |
201 } | 205 } |
202 | 206 |
203 Future queryDartMind(String prefix, String info) { | 207 Future queryDartMind(String prefix, String info) { |
204 // TODO(lukechurch): Use [info] for something. | 208 // TODO(lukechurch): Use [info] for something. |
205 if (!enableDartMind) return new Future.value("[]"); | |
206 String encodedArg0 = Uri.encodeComponent('"$prefix"'); | 209 String encodedArg0 = Uri.encodeComponent('"$prefix"'); |
207 String mindQuery = | 210 String mindQuery = |
208 'http://dart-mind.appspot.com/rpc' | 211 'http://dart-mind.appspot.com/rpc' |
209 '?action=GetExportingPubCompletions' | 212 '?action=GetExportingPubCompletions' |
210 '&arg0=$encodedArg0'; | 213 '&arg0=$encodedArg0'; |
211 Uri uri = Uri.parse(mindQuery); | 214 Uri uri = Uri.parse(mindQuery); |
212 | 215 |
213 HttpClient client = new HttpClient(); | 216 HttpClient client = new HttpClient(); |
214 return client.getUrl(uri).then((HttpClientRequest request) { | 217 return client.getUrl(uri).then((HttpClientRequest request) { |
215 return request.close(); | 218 return request.close(); |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 String prefix; | 269 String prefix; |
267 if (token != null) { | 270 if (token != null) { |
268 if (token.charOffset + token.charCount <= position) { | 271 if (token.charOffset + token.charCount <= position) { |
269 // After the token; in whitespace, or in the beginning of another token. | 272 // After the token; in whitespace, or in the beginning of another token. |
270 prefix = ""; | 273 prefix = ""; |
271 } else if (token.kind == IDENTIFIER_TOKEN || | 274 } else if (token.kind == IDENTIFIER_TOKEN || |
272 token.kind == KEYWORD_TOKEN) { | 275 token.kind == KEYWORD_TOKEN) { |
273 prefix = token.value.substring(0, position - token.charOffset); | 276 prefix = token.value.substring(0, position - token.charOffset); |
274 } | 277 } |
275 } | 278 } |
| 279 sw.stop(); |
276 printVerbose('Find token took ${sw.elapsedMicroseconds}us.'); | 280 printVerbose('Find token took ${sw.elapsedMicroseconds}us.'); |
277 sw.reset(); | 281 if (isDartMindEnabled && prefix != null) { |
278 if (prefix != null) { | 282 sw..reset()..start(); |
279 return queryDartMind(prefix, info).then((String dartMindSuggestion) { | 283 return queryDartMind(prefix, info).then((String dartMindSuggestion) { |
280 sw.stop(); | 284 sw.stop(); |
281 print('Dart Mind ($prefix): $dartMindSuggestion.'); | 285 print('Dart Mind ($prefix): $dartMindSuggestion.'); |
282 printVerbose('Dart Mind took ${sw.elapsedMicroseconds}us.'); | 286 printVerbose('Dart Mind took ${sw.elapsedMicroseconds}us.'); |
283 return repeat(); | 287 return repeat(); |
284 }); | 288 }); |
285 } else { | 289 } else { |
286 print("Didn't talk to Dart Mind, no identifier at POI ($token)."); | 290 if (isDartMindEnabled) { |
| 291 print("Didn't talk to Dart Mind, no identifier at POI ($token)."); |
| 292 } |
287 return repeat(); | 293 return repeat(); |
288 } | 294 } |
289 }); | 295 }); |
290 } | 296 } |
291 | 297 |
292 /// Find the token corresponding to [position] in [element]. The method only | 298 /// Find the token corresponding to [position] in [element]. The method only |
293 /// works for instances of [PartialElement] or [LibraryElement]. Support for | 299 /// works for instances of [PartialElement] or [LibraryElement]. Support for |
294 /// [LibraryElement] is currently limited, and works only for named libraries. | 300 /// [LibraryElement] is currently limited, and works only for named libraries. |
295 Token findToken(Element element, int position) { | 301 Token findToken(Element element, int position) { |
296 Token beginToken; | 302 Token beginToken; |
(...skipping 363 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
660 buffer.write('\n'); | 666 buffer.write('\n'); |
661 indented.write('}'); | 667 indented.write('}'); |
662 } | 668 } |
663 } | 669 } |
664 | 670 |
665 modelx.ScopeX localScope(modelx.LibraryElementX element) => element.localScope; | 671 modelx.ScopeX localScope(modelx.LibraryElementX element) => element.localScope; |
666 | 672 |
667 modelx.ImportScope importScope(modelx.LibraryElementX element) { | 673 modelx.ImportScope importScope(modelx.LibraryElementX element) { |
668 return element.importScope; | 674 return element.importScope; |
669 } | 675 } |
OLD | NEW |