OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 /** | |
6 * Tools to help implement refactoring like transformations to Dart code. | |
7 * | |
8 * [TextEditTransaction] supports making a series of changes to a text buffer. | |
9 * [guessIndent] helps to guess the appropriate indentiation for the new code. | |
10 */ | |
11 library refactor; | |
12 | |
13 import 'code_printer.dart'; | |
14 import 'package:source_maps/span.dart'; | |
15 | |
16 const $CR = 13; | |
17 const $LF = 10; | |
18 const $TAB = 9; | |
19 const $SPACE = 32; | |
20 | |
21 /** | |
22 * Editable text transaction. Applies a series of edits using original location | |
23 * information, and composes them into the edited string. | |
24 */ | |
25 class TextEditTransaction { | |
26 final SourceFile file; | |
27 final String original; | |
28 final _edits = <_TextEdit>[]; | |
29 | |
30 TextEditTransaction(this.original, this.file); | |
31 | |
32 bool get hasEdits => _edits.length > 0; | |
33 | |
34 /** | |
35 * Edit the original text, replacing text on the range [begin] and [end] | |
36 * with the [replacement]. [replacement] can be either a string or a | |
37 * [CodePrinter]. | |
38 */ | |
39 void edit(int begin, int end, replacement) { | |
40 _edits.add(new _TextEdit(begin, end, replacement)); | |
41 } | |
42 | |
43 /** Create a source map [Location] for [offset]. */ | |
44 Location _loc(int offset) => | |
45 file != null ? file.location(offset) : null; | |
46 | |
47 /** | |
48 * Applies all pending [edit]s and returns the rewritten string. | |
49 * If no edits were made, returns the [original] string. | |
50 * Throws [UnsupportedError] if the edits were overlapping. | |
51 */ | |
52 CodePrinter commit() { | |
53 var printer = new CodePrinter(0); | |
54 if (_edits.length == 0) { | |
55 printer.add(original, location: _loc(0), isOriginal: true); | |
56 return printer; | |
57 } | |
58 | |
59 // Sort edits by start location. | |
60 _edits.sort(); | |
61 | |
62 int consumed = 0; | |
63 for (var edit in _edits) { | |
64 if (consumed > edit.begin) { | |
65 var sb = new StringBuffer(); | |
66 sb..write(file.location(edit.begin).formatString) | |
67 ..write(': overlapping edits. Insert at offset ') | |
68 ..write(edit.begin) | |
69 ..write(' but have consumed ') | |
70 ..write(consumed) | |
71 ..write(' input characters. List of edits:'); | |
72 for (var e in _edits) sb..write('\n ')..write(e); | |
73 throw new UnsupportedError(sb.toString()); | |
74 } | |
75 | |
76 // Add characters from the original string between this edit and the last | |
77 // one, if any. | |
78 var betweenEdits = original.substring(consumed, edit.begin); | |
79 printer..add(betweenEdits, location: _loc(consumed), isOriginal: true) | |
80 ..add(edit.replace, location: _loc(edit.begin)); | |
81 consumed = edit.end; | |
82 } | |
83 | |
84 // Add any text from the end of the original string that was not replaced. | |
85 printer.add(original.substring(consumed), | |
86 location: _loc(consumed), isOriginal: true); | |
87 return printer; | |
88 } | |
89 } | |
90 | |
91 class _TextEdit implements Comparable<_TextEdit> { | |
92 final int begin; | |
93 final int end; | |
94 | |
95 /** The replacement used by the edit, can be a string or a [CodePrinter]. */ | |
96 final replace; | |
97 | |
98 _TextEdit(this.begin, this.end, this.replace); | |
99 | |
100 int get length => end - begin; | |
101 | |
102 String toString() => '(Edit @ $begin,$end: "$replace")'; | |
103 | |
104 int compareTo(_TextEdit other) { | |
105 int diff = begin - other.begin; | |
106 if (diff != 0) return diff; | |
107 return end - other.end; | |
108 } | |
109 } | |
110 | |
111 /** | |
112 * Finds and returns all whitespace characters at the start of the current line. | |
113 */ | |
114 String guessIndent(String code, int charOffset) { | |
115 // Find the beginning of the line | |
116 int lineStart = 0; | |
117 for (int i = charOffset - 1; i >= 0; i--) { | |
118 var c = code.codeUnitAt(i); | |
119 if (c == $LF || c == $CR) { | |
120 lineStart = i + 1; | |
121 break; | |
122 } | |
123 } | |
124 | |
125 // Grab all the whitespace | |
126 int whitespaceEnd = code.length; | |
127 for (int i = lineStart; i < code.length; i++) { | |
128 var c = code.codeUnitAt(i); | |
129 if (c != $SPACE && c != $TAB) { | |
130 whitespaceEnd = i; | |
131 break; | |
132 } | |
133 } | |
134 | |
135 return code.substring(lineStart, whitespaceEnd); | |
136 } | |
OLD | NEW |