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