Chromium Code Reviews| 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 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 /// information about position 11, then position 22 in test.dart.1.dart, then | 81 /// information about position 11, then position 22 in test.dart.1.dart, then |
| 82 /// position 33 in test.dart.2.dart, and finally position 44 in | 82 /// position 33 in test.dart.2.dart, and finally position 44 in |
| 83 /// test.dart.3.dart. | 83 /// test.dart.3.dart. |
| 84 bool isSimulateMutationEnabled = false; | 84 bool isSimulateMutationEnabled = false; |
| 85 | 85 |
| 86 /// Counts the number of times [runPoi] has been invoked. | 86 /// Counts the number of times [runPoi] has been invoked. |
| 87 int poiCount; | 87 int poiCount; |
| 88 | 88 |
| 89 int globalCounter = 0; | 89 int globalCounter = 0; |
| 90 | 90 |
| 91 /// Enabled by the option --verbose (or -v). Prints more information than you | |
| 92 /// really need. | |
|
Johnni Winther
2014/09/09 13:49:13
Nice comment :)
ahe
2014/09/09 14:35:13
Acknowledged.
| |
| 93 bool isVerbose = false; | |
| 94 | |
| 95 /// When true (the default value) print serialized scope information at the | |
| 96 /// provided position. | |
| 97 const bool PRINT_SCOPE_INFO = | |
| 98 const bool.fromEnvironment('PRINT_SCOPE_INFO', defaultValue: true); | |
| 99 | |
| 91 /// Iterator for reading lines from [io.stdin]. | 100 /// Iterator for reading lines from [io.stdin]. |
| 92 class StdinIterator implements Iterator<String> { | 101 class StdinIterator implements Iterator<String> { |
| 93 String current; | 102 String current; |
| 94 | 103 |
| 95 bool moveNext() { | 104 bool moveNext() { |
| 96 current = io.stdin.readLineSync(); | 105 current = io.stdin.readLineSync(); |
| 97 return true; | 106 return true; |
| 98 } | 107 } |
| 99 } | 108 } |
| 100 | 109 |
| 110 printVerbose(message) { | |
| 111 if (!isVerbose) return; | |
| 112 print(message); | |
| 113 } | |
| 114 | |
| 101 main(List<String> arguments) { | 115 main(List<String> arguments) { |
| 102 poiCount = 0; | 116 poiCount = 0; |
| 103 List<String> nonOptionArguments = []; | 117 List<String> nonOptionArguments = []; |
| 104 for (String argument in arguments) { | 118 for (String argument in arguments) { |
| 105 if (argument.startsWith('-')) { | 119 if (argument.startsWith('-')) { |
| 106 switch (argument) { | 120 switch (argument) { |
| 107 case '--simulate-mutation': | 121 case '--simulate-mutation': |
| 108 isSimulateMutationEnabled = true; | 122 isSimulateMutationEnabled = true; |
| 109 break; | 123 break; |
| 124 case '-v': | |
| 125 case '--verbose': | |
| 126 isVerbose = true; | |
| 127 break; | |
| 110 default: | 128 default: |
| 111 throw 'Unknown option: $argument.'; | 129 throw 'Unknown option: $argument.'; |
| 112 } | 130 } |
| 113 } else { | 131 } else { |
| 114 nonOptionArguments.add(argument); | 132 nonOptionArguments.add(argument); |
| 115 } | 133 } |
| 116 } | 134 } |
| 117 if (nonOptionArguments.isEmpty) { | 135 if (nonOptionArguments.isEmpty) { |
| 118 stdin = new StdinIterator(); | 136 stdin = new StdinIterator(); |
| 119 } else { | 137 } else { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 141 api.CompilerInputProvider simulateMutation( | 159 api.CompilerInputProvider simulateMutation( |
| 142 String fileName, | 160 String fileName, |
| 143 SourceFileProvider inputProvider) { | 161 SourceFileProvider inputProvider) { |
| 144 Uri script = Uri.base.resolveUri(new Uri.file(fileName)); | 162 Uri script = Uri.base.resolveUri(new Uri.file(fileName)); |
| 145 int count = poiCount; | 163 int count = poiCount; |
| 146 Future cache; | 164 Future cache; |
| 147 String cachedFileName = script.toFilePath(); | 165 String cachedFileName = script.toFilePath(); |
| 148 int counter = ++globalCounter; | 166 int counter = ++globalCounter; |
| 149 return (Uri uri) { | 167 return (Uri uri) { |
| 150 if (counter != globalCounter) throw 'Using old provider'; | 168 if (counter != globalCounter) throw 'Using old provider'; |
| 151 print('fake inputProvider#$counter($uri): $poiCount $count'); | 169 printVerbose('fake inputProvider#$counter($uri): $poiCount $count'); |
| 152 if (uri == script) { | 170 if (uri == script) { |
| 153 if (poiCount == count) { | 171 if (poiCount == count) { |
| 154 cachedFileName = uri.toFilePath(); | 172 cachedFileName = uri.toFilePath(); |
| 155 if (count != 0) { | 173 if (count != 0) { |
| 156 cachedFileName = '$cachedFileName.$count.dart'; | 174 cachedFileName = '$cachedFileName.$count.dart'; |
| 157 } | 175 } |
| 158 print('Not using cached version of $cachedFileName'); | 176 printVerbose('Not using cached version of $cachedFileName'); |
| 159 cache = new File(cachedFileName).readAsBytes().then((data) { | 177 cache = new File(cachedFileName).readAsBytes().then((data) { |
| 160 print('Read file $cachedFileName: ${UTF8.decode(data)}'); | 178 printVerbose('Read file $cachedFileName: ${UTF8.decode(data)}'); |
| 161 return data; | 179 return data; |
| 162 }); | 180 }); |
| 163 count++; | 181 count++; |
| 164 } else { | 182 } else { |
| 165 print('Using cached version of $cachedFileName'); | 183 printVerbose('Using cached version of $cachedFileName'); |
| 166 } | 184 } |
| 167 return cache; | 185 return cache; |
| 168 } else { | 186 } else { |
| 169 print('Using realProvider for $uri'); | 187 printVerbose('Using original provider for $uri'); |
| 170 return inputProvider(uri); | 188 return inputProvider(uri); |
| 171 } | 189 } |
| 172 }; | 190 }; |
| 173 } | 191 } |
| 174 | 192 |
| 175 Future<String> prompt(message) { | 193 Future<String> prompt(message) { |
| 176 if (stdin is StdinIterator) { | 194 if (stdin is StdinIterator) { |
| 177 stdout.write(message); | 195 stdout.write(message); |
| 178 } | 196 } |
| 179 return stdout.flush().then((_) { | 197 return stdout.flush().then((_) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 215 }); | 233 }); |
| 216 } | 234 } |
| 217 | 235 |
| 218 Uri script = Uri.base.resolveUri(new Uri.file(fileName)); | 236 Uri script = Uri.base.resolveUri(new Uri.file(fileName)); |
| 219 if (positionString == null) return null; | 237 if (positionString == null) return null; |
| 220 int position = int.parse( | 238 int position = int.parse( |
| 221 positionString, onError: (_) => print('Please enter an integer.')); | 239 positionString, onError: (_) => print('Please enter an integer.')); |
| 222 if (position == null) return repeat(); | 240 if (position == null) return repeat(); |
| 223 | 241 |
| 224 inputProvider(script); | 242 inputProvider(script); |
| 225 handler( | 243 if (isVerbose) { |
| 226 script, position, position + 1, | 244 handler( |
| 227 'Point of interest. Cursor is immediately before highlighted character.', | 245 script, position, position + 1, |
| 228 api.Diagnostic.HINT); | 246 'Point of interest. ' |
| 247 'Cursor is immediately before highlighted character.', | |
| 248 api.Diagnostic.HINT); | |
| 249 } | |
| 229 | 250 |
| 230 Stopwatch sw = new Stopwatch()..start(); | 251 Stopwatch sw = new Stopwatch()..start(); |
| 231 | 252 |
| 232 Future future = runPoi(script, position, inputProvider, handler); | 253 Future future = runPoi(script, position, inputProvider, handler); |
| 233 return future.then((Element element) { | 254 return future.then((Element element) { |
| 234 poiCount++; | 255 poiCount++; |
| 235 print('Resolving took ${sw.elapsedMicroseconds}us.'); | 256 print('Resolving took ${sw.elapsedMicroseconds}us.'); |
| 236 sw.reset(); | 257 sw.reset(); |
| 237 String info = scopeInformation(element, position); | 258 String info = scopeInformation(element, position); |
| 238 sw.stop(); | 259 sw.stop(); |
| 239 print(info); | 260 if (PRINT_SCOPE_INFO) { |
| 240 print('Scope information took ${sw.elapsedMicroseconds}us.'); | 261 print(info); |
| 262 } | |
| 263 printVerbose('Scope information took ${sw.elapsedMicroseconds}us.'); | |
| 241 sw..reset()..start(); | 264 sw..reset()..start(); |
| 242 Token token = findToken(element, position); | 265 Token token = findToken(element, position); |
| 243 String prefix; | 266 String prefix; |
| 244 if (token != null) { | 267 if (token != null) { |
| 245 if (token.charOffset + token.charCount <= position) { | 268 if (token.charOffset + token.charCount <= position) { |
| 246 // After the token; in whitespace, or in the beginning of another token. | 269 // After the token; in whitespace, or in the beginning of another token. |
| 247 prefix = ""; | 270 prefix = ""; |
| 248 } else if (token.kind == IDENTIFIER_TOKEN || | 271 } else if (token.kind == IDENTIFIER_TOKEN || |
| 249 token.kind == KEYWORD_TOKEN) { | 272 token.kind == KEYWORD_TOKEN) { |
| 250 prefix = token.value.substring(0, position - token.charOffset); | 273 prefix = token.value.substring(0, position - token.charOffset); |
| 251 } | 274 } |
| 252 } | 275 } |
| 253 print('Find token took ${sw.elapsedMicroseconds}us.'); | 276 printVerbose('Find token took ${sw.elapsedMicroseconds}us.'); |
| 254 sw.reset(); | 277 sw.reset(); |
| 255 if (prefix != null) { | 278 if (prefix != null) { |
| 256 return queryDartMind(prefix, info).then((String dartMindSuggestion) { | 279 return queryDartMind(prefix, info).then((String dartMindSuggestion) { |
| 257 sw.stop(); | 280 sw.stop(); |
| 258 print('Dart Mind ($prefix): $dartMindSuggestion.'); | 281 print('Dart Mind ($prefix): $dartMindSuggestion.'); |
| 259 print('Dart Mind took ${sw.elapsedMicroseconds}us.'); | 282 printVerbose('Dart Mind took ${sw.elapsedMicroseconds}us.'); |
| 260 return repeat(); | 283 return repeat(); |
| 261 }); | 284 }); |
| 262 } else { | 285 } else { |
| 263 print("Didn't talk to Dart Mind, no identifier at POI ($token)."); | 286 print("Didn't talk to Dart Mind, no identifier at POI ($token)."); |
| 264 return repeat(); | 287 return repeat(); |
| 265 } | 288 } |
| 266 }); | 289 }); |
| 267 } | 290 } |
| 268 | 291 |
| 269 /// Find the token corresponding to [position] in [element]. The method only | 292 /// Find the token corresponding to [position] in [element]. The method only |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 637 buffer.write('\n'); | 660 buffer.write('\n'); |
| 638 indented.write('}'); | 661 indented.write('}'); |
| 639 } | 662 } |
| 640 } | 663 } |
| 641 | 664 |
| 642 modelx.ScopeX localScope(modelx.LibraryElementX element) => element.localScope; | 665 modelx.ScopeX localScope(modelx.LibraryElementX element) => element.localScope; |
| 643 | 666 |
| 644 modelx.ImportScope importScope(modelx.LibraryElementX element) { | 667 modelx.ImportScope importScope(modelx.LibraryElementX element) { |
| 645 return element.importScope; | 668 return element.importScope; |
| 646 } | 669 } |
| OLD | NEW |