OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 /// Tools to help implement refactoring like transformations to Dart code. | 5 /// Tools to help implement refactoring like transformations to Dart code. |
6 /// | 6 /// |
7 /// [TextEditTransaction] supports making a series of changes to a text buffer. | 7 /// [TextEditTransaction] supports making a series of changes to a text buffer. |
8 /// [guessIndent] helps to guess the appropriate indentiation for the new code. | 8 /// [guessIndent] helps to guess the appropriate indentiation for the new code. |
9 library source_maps.refactor; | 9 library source_maps.refactor; |
10 | 10 |
11 import 'span.dart'; | 11 import 'package:source_span/source_span.dart'; |
| 12 |
12 import 'printer.dart'; | 13 import 'printer.dart'; |
13 import 'src/span_wrapper.dart'; | |
14 | 14 |
15 /// Editable text transaction. | 15 /// Editable text transaction. |
16 /// | 16 /// |
17 /// Applies a series of edits using original location | 17 /// Applies a series of edits using original location |
18 /// information, and composes them into the edited string. | 18 /// information, and composes them into the edited string. |
19 class TextEditTransaction { | 19 class TextEditTransaction { |
20 final SourceFile file; | 20 final SourceFile file; |
21 final String original; | 21 final String original; |
22 final _edits = <_TextEdit>[]; | 22 final _edits = <_TextEdit>[]; |
23 | 23 |
24 /// Creates a new transaction. | 24 /// Creates a new transaction. |
25 /// | 25 TextEditTransaction(this.original, this.file); |
26 /// [file] can be either a `source_map` [SourceFile] or a `source_span` | |
27 /// `SourceFile`. Using a `source_map` [SourceFile] is deprecated and will be | |
28 /// unsupported in version 0.10.0. | |
29 TextEditTransaction(this.original, file) | |
30 : file = SourceFileWrapper.wrap(file); | |
31 | 26 |
32 bool get hasEdits => _edits.length > 0; | 27 bool get hasEdits => _edits.length > 0; |
33 | 28 |
34 /// Edit the original text, replacing text on the range [begin] and [end] | 29 /// Edit the original text, replacing text on the range [begin] and [end] |
35 /// with the [replacement]. [replacement] can be either a string or a | 30 /// with the [replacement]. [replacement] can be either a string or a |
36 /// [NestedPrinter]. | 31 /// [NestedPrinter]. |
37 void edit(int begin, int end, replacement) { | 32 void edit(int begin, int end, replacement) { |
38 _edits.add(new _TextEdit(begin, end, replacement)); | 33 _edits.add(new _TextEdit(begin, end, replacement)); |
39 } | 34 } |
40 | 35 |
41 /// Create a source map [Location] for [offset]. | 36 /// Create a source map [SourceLocation] for [offset]. |
42 Location _loc(int offset) => | 37 SourceLocation _loc(int offset) => |
43 file != null ? file.location(offset) : null; | 38 file != null ? file.location(offset) : null; |
44 | 39 |
45 /// Applies all pending [edit]s and returns a [NestedPrinter] containing the | 40 /// Applies all pending [edit]s and returns a [NestedPrinter] containing the |
46 /// rewritten string and source map information. [filename] is given to the | 41 /// rewritten string and source map information. [filename] is given to the |
47 /// underlying printer to indicate the name of the generated file that will | 42 /// underlying printer to indicate the name of the generated file that will |
48 /// contains the source map information. | 43 /// contains the source map information. |
49 /// | 44 /// |
50 /// Throws [UnsupportedError] if the edits were overlapping. If no edits were | 45 /// Throws [UnsupportedError] if the edits were overlapping. If no edits were |
51 /// made, the printer simply contains the original string. | 46 /// made, the printer simply contains the original string. |
52 NestedPrinter commit() { | 47 NestedPrinter commit() { |
53 var printer = new NestedPrinter(); | 48 var printer = new NestedPrinter(); |
54 if (_edits.length == 0) { | 49 if (_edits.length == 0) { |
55 return printer..add(original, location: _loc(0), isOriginal: true); | 50 return printer..add(original, location: _loc(0), isOriginal: true); |
56 } | 51 } |
57 | 52 |
58 // Sort edits by start location. | 53 // Sort edits by start location. |
59 _edits.sort(); | 54 _edits.sort(); |
60 | 55 |
61 int consumed = 0; | 56 int consumed = 0; |
62 for (var edit in _edits) { | 57 for (var edit in _edits) { |
63 if (consumed > edit.begin) { | 58 if (consumed > edit.begin) { |
64 var sb = new StringBuffer(); | 59 var sb = new StringBuffer(); |
65 sb..write(file.location(edit.begin).formatString) | 60 sb..write(file.location(edit.begin).toolString) |
66 ..write(': overlapping edits. Insert at offset ') | 61 ..write(': overlapping edits. Insert at offset ') |
67 ..write(edit.begin) | 62 ..write(edit.begin) |
68 ..write(' but have consumed ') | 63 ..write(' but have consumed ') |
69 ..write(consumed) | 64 ..write(consumed) |
70 ..write(' input characters. List of edits:'); | 65 ..write(' input characters. List of edits:'); |
71 for (var e in _edits) sb..write('\n ')..write(e); | 66 for (var e in _edits) sb..write('\n ')..write(e); |
72 throw new UnsupportedError(sb.toString()); | 67 throw new UnsupportedError(sb.toString()); |
73 } | 68 } |
74 | 69 |
75 // Add characters from the original string between this edit and the last | 70 // Add characters from the original string between this edit and the last |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 } | 124 } |
130 } | 125 } |
131 | 126 |
132 return code.substring(lineStart, whitespaceEnd); | 127 return code.substring(lineStart, whitespaceEnd); |
133 } | 128 } |
134 | 129 |
135 const int _CR = 13; | 130 const int _CR = 13; |
136 const int _LF = 10; | 131 const int _LF = 10; |
137 const int _TAB = 9; | 132 const int _TAB = 9; |
138 const int _SPACE = 32; | 133 const int _SPACE = 32; |
OLD | NEW |