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; |
| 11 | 11 |
| 12 import 'dart:io' show | 12 import 'dart:io' show |
| 13 File, | |
| 13 HttpClient, | 14 HttpClient, |
| 14 HttpClientRequest, | 15 HttpClientRequest, |
| 15 HttpClientResponse, | 16 HttpClientResponse, |
| 16 Platform, | 17 Platform, |
| 17 stdout; | 18 stdout; |
| 18 | 19 |
| 19 import 'dart:io' as io; | 20 import 'dart:io' as io; |
| 20 | 21 |
| 21 import 'dart:convert' show | 22 import 'dart:convert' show |
| 22 LineSplitter, | 23 LineSplitter, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 62 PartialClassElement, | 63 PartialClassElement, |
| 63 PartialElement, | 64 PartialElement, |
| 64 Token; | 65 Token; |
| 65 | 66 |
| 66 /// Controls if this program should be querying Dart Mind. Used by tests. | 67 /// Controls if this program should be querying Dart Mind. Used by tests. |
| 67 bool enableDartMind = true; | 68 bool enableDartMind = true; |
| 68 | 69 |
| 69 /// Iterator over lines from standard input (or the argument array). | 70 /// Iterator over lines from standard input (or the argument array). |
| 70 Iterator<String> stdin; | 71 Iterator<String> stdin; |
| 71 | 72 |
| 73 bool isSimulateMutationEnabled = false; | |
| 74 | |
| 75 int poiCount; | |
|
Johnni Winther
2014/09/09 09:05:52
What is this for? (Add documentation)
| |
| 76 | |
| 77 int globalCounter = 0; | |
| 78 | |
| 72 /// Iterator for reading lines from [io.stdin]. | 79 /// Iterator for reading lines from [io.stdin]. |
| 73 class StdinIterator implements Iterator<String> { | 80 class StdinIterator implements Iterator<String> { |
| 74 String current; | 81 String current; |
| 75 | 82 |
| 76 bool moveNext() { | 83 bool moveNext() { |
| 77 current = io.stdin.readLineSync(); | 84 current = io.stdin.readLineSync(); |
| 78 return true; | 85 return true; |
| 79 } | 86 } |
| 80 } | 87 } |
| 81 | 88 |
| 82 main(List<String> arguments) { | 89 main(List<String> arguments) { |
| 90 poiCount = 0; | |
| 91 List<String> nonOptionArguments = []; | |
| 92 for (String argument in arguments) { | |
| 93 if (argument.startsWith('-')) { | |
| 94 switch (argument) { | |
| 95 case '--simulate-mutation': | |
| 96 isSimulateMutationEnabled = true; | |
| 97 break; | |
| 98 default: | |
| 99 throw 'Unknown option: $argument.'; | |
| 100 } | |
| 101 } else { | |
| 102 nonOptionArguments.add(argument); | |
| 103 } | |
| 104 } | |
| 105 if (nonOptionArguments.isEmpty) { | |
| 106 stdin = new StdinIterator(); | |
| 107 } else { | |
| 108 stdin = nonOptionArguments.iterator; | |
| 109 } | |
| 110 | |
| 83 FormattingDiagnosticHandler handler = new FormattingDiagnosticHandler(); | 111 FormattingDiagnosticHandler handler = new FormattingDiagnosticHandler(); |
| 84 handler | 112 handler |
| 85 ..verbose = false | 113 ..verbose = false |
| 86 ..enableColors = true; | 114 ..enableColors = true; |
| 87 api.CompilerInputProvider inputProvider = handler.provider; | 115 api.CompilerInputProvider inputProvider = handler.provider; |
| 88 | 116 |
| 89 if (arguments.length == 0) { | |
| 90 stdin = new StdinIterator(); | |
| 91 } else { | |
| 92 stdin = arguments.where((String line) { | |
| 93 print(line); // Simulates user input in terminal. | |
| 94 return true; | |
| 95 }).iterator; | |
| 96 } | |
| 97 | |
| 98 return prompt('Dart file: ').then((String fileName) { | 117 return prompt('Dart file: ').then((String fileName) { |
| 118 if (isSimulateMutationEnabled) { | |
| 119 inputProvider = simulateMutation(fileName, inputProvider); | |
| 120 } | |
| 99 return prompt('Position: ').then((String position) { | 121 return prompt('Position: ').then((String position) { |
| 100 return parseUserInput(fileName, position, inputProvider, handler); | 122 return parseUserInput(fileName, position, inputProvider, handler); |
| 101 }); | 123 }); |
| 102 }); | 124 }); |
| 103 } | 125 } |
| 104 | 126 |
| 127 api.CompilerInputProvider simulateMutation( | |
| 128 String fileName, | |
| 129 SourceFileProvider inputProvider) { | |
| 130 Uri script = Uri.base.resolveUri(new Uri.file(fileName)); | |
| 131 int count = poiCount; | |
| 132 Future cache; | |
| 133 String cachedFileName = script.toFilePath(); | |
| 134 int counter = ++globalCounter; | |
| 135 return (Uri uri) { | |
| 136 if (counter != globalCounter) throw 'Using old provider'; | |
| 137 print('fake inputProvider#$counter($uri): $poiCount $count'); | |
| 138 if (uri == script) { | |
| 139 if (poiCount == count) { | |
| 140 cachedFileName = uri.toFilePath(); | |
| 141 if (count != 0) { | |
| 142 cachedFileName = '$cachedFileName.$count.dart'; | |
| 143 } | |
| 144 print('Not using cached version of $cachedFileName'); | |
| 145 cache = new File(cachedFileName).readAsBytes().then((data) { | |
| 146 print('Read file $cachedFileName: ${UTF8.decode(data)}'); | |
| 147 return data; | |
| 148 }); | |
| 149 count++; | |
| 150 } else { | |
| 151 print('Using cached version of $cachedFileName'); | |
| 152 } | |
| 153 return cache; | |
| 154 } else { | |
| 155 print('Using realProvider for $uri'); | |
| 156 return inputProvider(uri); | |
| 157 } | |
| 158 }; | |
| 159 } | |
| 160 | |
| 105 Future<String> prompt(message) { | 161 Future<String> prompt(message) { |
| 106 stdout.write(message); | 162 if (stdin is StdinIterator) { |
| 163 stdout.write(message); | |
| 164 } | |
| 107 return stdout.flush().then((_) { | 165 return stdout.flush().then((_) { |
| 108 stdin.moveNext(); | 166 stdin.moveNext(); |
| 109 return stdin.current; | 167 return stdin.current; |
| 110 }); | 168 }); |
| 111 } | 169 } |
| 112 | 170 |
| 113 Future queryDartMind(String prefix, String info) { | 171 Future queryDartMind(String prefix, String info) { |
| 114 // TODO(lukechurch): Use [info] for something. | 172 // TODO(lukechurch): Use [info] for something. |
| 115 if (!enableDartMind) return new Future.value("[]"); | 173 if (!enableDartMind) return new Future.value("[]"); |
| 116 String encodedArg0 = Uri.encodeComponent('"$prefix"'); | 174 String encodedArg0 = Uri.encodeComponent('"$prefix"'); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 inputProvider(script); | 210 inputProvider(script); |
| 153 handler( | 211 handler( |
| 154 script, position, position + 1, | 212 script, position, position + 1, |
| 155 'Point of interest. Cursor is immediately before highlighted character.', | 213 'Point of interest. Cursor is immediately before highlighted character.', |
| 156 api.Diagnostic.HINT); | 214 api.Diagnostic.HINT); |
| 157 | 215 |
| 158 Stopwatch sw = new Stopwatch()..start(); | 216 Stopwatch sw = new Stopwatch()..start(); |
| 159 | 217 |
| 160 Future future = runPoi(script, position, inputProvider, handler); | 218 Future future = runPoi(script, position, inputProvider, handler); |
| 161 return future.then((Element element) { | 219 return future.then((Element element) { |
| 220 poiCount++; | |
| 162 print('Resolving took ${sw.elapsedMicroseconds}us.'); | 221 print('Resolving took ${sw.elapsedMicroseconds}us.'); |
| 163 sw.reset(); | 222 sw.reset(); |
| 164 String info = scopeInformation(element, position); | 223 String info = scopeInformation(element, position); |
| 165 sw.stop(); | 224 sw.stop(); |
| 166 print(info); | 225 print(info); |
| 167 print('Scope information took ${sw.elapsedMicroseconds}us.'); | 226 print('Scope information took ${sw.elapsedMicroseconds}us.'); |
| 168 sw..reset()..start(); | 227 sw..reset()..start(); |
| 169 Token token = findToken(element, position); | 228 Token token = findToken(element, position); |
| 170 String prefix; | 229 String prefix; |
| 171 if (token != null) { | 230 if (token != null) { |
| (...skipping 392 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 564 buffer.write('\n'); | 623 buffer.write('\n'); |
| 565 indented.write('}'); | 624 indented.write('}'); |
| 566 } | 625 } |
| 567 } | 626 } |
| 568 | 627 |
| 569 modelx.ScopeX localScope(modelx.LibraryElementX element) => element.localScope; | 628 modelx.ScopeX localScope(modelx.LibraryElementX element) => element.localScope; |
| 570 | 629 |
| 571 modelx.ImportScope importScope(modelx.LibraryElementX element) { | 630 modelx.ImportScope importScope(modelx.LibraryElementX element) { |
| 572 return element.importScope; | 631 return element.importScope; |
| 573 } | 632 } |
| OLD | NEW |