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 const $CR = 13; |
| 14 const $LF = 10; |
| 15 const $TAB = 9; |
| 16 const $SPACE = 32; |
| 17 |
| 18 /** |
| 19 * Editable text transaction. Applies a series of edits using original location |
| 20 * information, and composes them into the edited string. |
| 21 */ |
| 22 class TextEditTransaction { |
| 23 final String original; |
| 24 final _edits = <_TextEdit>[]; |
| 25 |
| 26 TextEditTransaction(this.original); |
| 27 |
| 28 bool get hasEdits => _edits.length > 0; |
| 29 |
| 30 /** |
| 31 * Edit the original text, replacing text on the range [begin] and [end] |
| 32 * with the [replace] text. |
| 33 */ |
| 34 void edit(int begin, int end, String replace) { |
| 35 _edits.add(new _TextEdit(begin, end, replace)); |
| 36 } |
| 37 |
| 38 /** |
| 39 * Applies all pending [edit]s and returns the rewritten string. |
| 40 * If no edits were made, returns the [original] string. |
| 41 * Throws [UnsupportedError] if the edits were overlapping. |
| 42 */ |
| 43 String commit() { |
| 44 if (_edits.length == 0) return original; |
| 45 |
| 46 // Sort edits by start location. |
| 47 _edits.sort((x, y) => x.begin - y.begin); |
| 48 |
| 49 var result = new StringBuffer(); |
| 50 int consumed = 0; |
| 51 for (var edit in _edits) { |
| 52 if (consumed > edit.begin) { |
| 53 throw new UnsupportedError('overlapping edits: insert at offset ' |
| 54 '${edit.begin} but have consumed $consumed input characters.'); |
| 55 } |
| 56 |
| 57 // Add characters from the original string between this edit and the last |
| 58 // one, if any. |
| 59 var betweenEdits = original.substring(consumed, edit.begin); |
| 60 result..add(betweenEdits)..add(edit.replace); |
| 61 consumed = edit.end; |
| 62 } |
| 63 |
| 64 // Add any text from the end of the original string that was not replaced. |
| 65 result.add(original.substring(consumed)); |
| 66 return result.toString(); |
| 67 } |
| 68 } |
| 69 |
| 70 class _TextEdit { |
| 71 final int begin; |
| 72 final int end; |
| 73 final String replace; |
| 74 |
| 75 _TextEdit(this.begin, this.end, this.replace); |
| 76 |
| 77 int get length => end - begin; |
| 78 } |
| 79 |
| 80 /** |
| 81 * Finds and returns all whitespace characters at the start of the current line. |
| 82 */ |
| 83 String guessIndent(String code, int charOffset) { |
| 84 // Find the beginning of the line |
| 85 int lineStart = 0; |
| 86 for (int i = charOffset - 1; i >= 0; i--) { |
| 87 var c = code.charCodeAt(i); |
| 88 if (c == $LF || c == $CR) { |
| 89 lineStart = i + 1; |
| 90 break; |
| 91 } |
| 92 } |
| 93 |
| 94 // Grab all the whitespace |
| 95 int whitespaceEnd = code.length; |
| 96 for (int i = lineStart; i < code.length; i++) { |
| 97 var c = code.charCodeAt(i); |
| 98 if (c != $SPACE && c != $TAB) { |
| 99 whitespaceEnd = i; |
| 100 break; |
| 101 } |
| 102 } |
| 103 |
| 104 return code.substring(lineStart, whitespaceEnd); |
| 105 } |
OLD | NEW |