OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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.md file. | 3 // BSD-style license that can be found in the LICENSE.md file. |
4 import 'dart:async'; | 4 import 'dart:async'; |
5 import 'dart:convert'; | 5 import 'dart:convert'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 | 7 |
8 import 'package:front_end/src/base/instrumentation.dart'; | 8 import 'package:front_end/src/base/instrumentation.dart'; |
9 import 'package:front_end/src/fasta/messages.dart'; | 9 import 'package:front_end/src/fasta/messages.dart'; |
10 import 'package:front_end/src/fasta/scanner.dart'; | 10 import 'package:front_end/src/fasta/scanner.dart'; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 'got nothing', | 70 'got nothing', |
71 new _Fix( | 71 new _Fix( |
72 expectation.commentOffset, expectation.commentLength, '')); | 72 expectation.commentOffset, expectation.commentLength, '')); |
73 } | 73 } |
74 }); | 74 }); |
75 }); | 75 }); |
76 } | 76 } |
77 | 77 |
78 /// Updates the source file at [uri] based on the actual property/value | 78 /// Updates the source file at [uri] based on the actual property/value |
79 /// pairs that were observed. | 79 /// pairs that were observed. |
80 Future<Null> fixSource(Uri uri) async { | 80 Future<Null> fixSource(Uri uri, bool offsetsCountCharacters) async { |
81 var fixes = _fixes[uri]; | 81 var fixes = _fixes[uri]; |
82 if (fixes == null) return; | 82 if (fixes == null) return; |
83 File file = new File.fromUri(uri); | 83 File file = new File.fromUri(uri); |
84 var bytes = (await file.readAsBytes()).toList(); | 84 var bytes = (await file.readAsBytes()).toList(); |
| 85 int convertOffset(int offset) { |
| 86 if (offsetsCountCharacters) { |
| 87 return UTF8.encode(UTF8.decode(bytes).substring(0, offset)).length; |
| 88 } else { |
| 89 return offset; |
| 90 } |
| 91 } |
| 92 |
85 // Apply the fixes in reverse order so that offsets don't need to be | 93 // Apply the fixes in reverse order so that offsets don't need to be |
86 // adjusted after each fix. | 94 // adjusted after each fix. |
87 fixes.sort((a, b) => b.offset.compareTo(a.offset)); | 95 fixes.sort((a, b) => b.offset.compareTo(a.offset)); |
88 for (var fix in fixes) { | 96 for (var fix in fixes) { |
89 bytes.replaceRange( | 97 bytes.replaceRange(convertOffset(fix.offset), |
90 fix.offset, fix.offset + fix.length, UTF8.encode(fix.replacement)); | 98 convertOffset(fix.offset + fix.length), UTF8.encode(fix.replacement)); |
91 } | 99 } |
92 await file.writeAsBytes(bytes); | 100 await file.writeAsBytes(bytes); |
93 } | 101 } |
94 | 102 |
95 /// Loads expectations from the source file located at [uri]. | 103 /// Loads expectations from the source file located at [uri]. |
96 /// | 104 /// |
97 /// Should be called before [finish]. | 105 /// Should be called before [finish]. |
98 Future<Null> loadExpectations(Uri uri) async { | 106 Future<Null> loadExpectations(Uri uri) async { |
99 var bytes = await readBytesFromFile(uri); | 107 var bytes = await readBytesFromFile(uri); |
100 var expectations = _unsatisfiedExpectations.putIfAbsent(uri, () => {}); | 108 var expectations = _unsatisfiedExpectations.putIfAbsent(uri, () => {}); |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 this.property, this.value, this.commentOffset, this.commentLength); | 235 this.property, this.value, this.commentOffset, this.commentLength); |
228 } | 236 } |
229 | 237 |
230 class _Fix { | 238 class _Fix { |
231 final int offset; | 239 final int offset; |
232 final int length; | 240 final int length; |
233 final String replacement; | 241 final String replacement; |
234 | 242 |
235 _Fix(this.offset, this.length, this.replacement); | 243 _Fix(this.offset, this.length, this.replacement); |
236 } | 244 } |
OLD | NEW |