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