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