| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library trydart.source_update; | 5 library trydart.source_update; |
| 6 | 6 |
| 7 /// Returns [updates] expanded to full compilation units/source files. | 7 /// Returns [updates] expanded to full compilation units/source files. |
| 8 /// | 8 /// |
| 9 /// [updates] is a convenient way to write updates/patches to a single source | 9 /// [updates] is a convenient way to write updates/patches to a single source |
| 10 /// file without repeating common parts. | 10 /// file without repeating common parts. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 } else { | 33 } else { |
| 34 for (StringBuffer buffer in result) { | 34 for (StringBuffer buffer in result) { |
| 35 buffer.write(chunk); | 35 buffer.write(chunk); |
| 36 } | 36 } |
| 37 } | 37 } |
| 38 } | 38 } |
| 39 | 39 |
| 40 return result.map((e) => '$e').toList(); | 40 return result.map((e) => '$e').toList(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 /// Returns [files] split into mulitple named files. The keys in the returned | 43 /// Returns [files] split into multiple named files. The keys in the returned |
| 44 /// map are filenames, the values are the files' content. | 44 /// map are filenames, the values are the files' content. |
| 45 /// | 45 /// |
| 46 /// Names are indicated by a line on the form "==> filename <==". Spaces are | 46 /// Names are indicated by a line on the form "==> filename <==". Spaces are |
| 47 /// significant. For example, given this input: | 47 /// significant. For example, given this input: |
| 48 /// | 48 /// |
| 49 /// ==> file1.dart <== | 49 /// ==> file1.dart <== |
| 50 /// First line of file 1. | 50 /// First line of file 1. |
| 51 /// Second line of file 1. | 51 /// Second line of file 1. |
| 52 /// Third line of file 1. | 52 /// Third line of file 1. |
| 53 /// ==> empty.dart <== | 53 /// ==> empty.dart <== |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 105 finishFile(); | 105 finishFile(); |
| 106 return result; | 106 return result; |
| 107 } | 107 } |
| 108 | 108 |
| 109 /// Split [text] into lines preserving trailing newlines (unlike | 109 /// Split [text] into lines preserving trailing newlines (unlike |
| 110 /// String.split("\n"). Also, if [text] is empty, return an empty list (unlike | 110 /// String.split("\n"). Also, if [text] is empty, return an empty list (unlike |
| 111 /// String.split("\n")). | 111 /// String.split("\n")). |
| 112 List<String> splitLines(String text) { | 112 List<String> splitLines(String text) { |
| 113 return text.split(new RegExp('^', multiLine: true)); | 113 return text.split(new RegExp('^', multiLine: true)); |
| 114 } | 114 } |
| 115 |
| 116 /// Expand a file with diffs in common merge conflict format into a [List] that |
| 117 /// can be passed to [expandUpdates]. |
| 118 /// |
| 119 /// For example: |
| 120 /// first |
| 121 /// <<<<<<< |
| 122 /// v1 |
| 123 /// ======= |
| 124 /// v2 |
| 125 /// ======= |
| 126 /// v3 |
| 127 /// >>>>>>> |
| 128 /// last |
| 129 /// |
| 130 /// Would be expanded to something equivalent to: |
| 131 /// |
| 132 /// ["first\n", ["v1\n", "v2\n", "v3\n"], "last\n"] |
| 133 List expandDiff(String text) { |
| 134 List result = [new StringBuffer()]; |
| 135 bool inDiff = false; |
| 136 for (String line in splitLines(text)) { |
| 137 if (inDiff) { |
| 138 if (line.startsWith("=======")) { |
| 139 result.last.add(new StringBuffer()); |
| 140 } else if (line.startsWith(">>>>>>>")) { |
| 141 inDiff = false; |
| 142 result.add(new StringBuffer()); |
| 143 } else { |
| 144 result.last.last.write(line); |
| 145 } |
| 146 } else if (line.startsWith("<<<<<<<")) { |
| 147 inDiff = true; |
| 148 result.add(<StringBuffer>[new StringBuffer()]); |
| 149 } else { |
| 150 result.last.write(line); |
| 151 } |
| 152 } |
| 153 return result; |
| 154 } |
| OLD | NEW |