Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 /// Contains a builder object useful for creating source maps programatically. | 5 /// Contains a builder object useful for creating source maps programatically. |
| 6 library source_maps.builder; | 6 library source_maps.builder; |
| 7 | 7 |
| 8 // TODO(sigmund): add a builder for multi-section mappings. | 8 // TODO(sigmund): add a builder for multi-section mappings. |
| 9 | 9 |
| 10 import 'dart:collection'; | 10 import 'dart:collection'; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 44 | 44 |
| 45 void addLocation(Location source, Location target, String identifier) { | 45 void addLocation(Location source, Location target, String identifier) { |
| 46 _entries.add(new Entry(source, target, identifier)); | 46 _entries.add(new Entry(source, target, identifier)); |
| 47 } | 47 } |
| 48 | 48 |
| 49 /// Encodes all mappings added to this builder as a json map. | 49 /// Encodes all mappings added to this builder as a json map. |
| 50 Map build(String fileUrl) { | 50 Map build(String fileUrl) { |
| 51 var buff = new StringBuffer(); | 51 var buff = new StringBuffer(); |
| 52 var line = 0; | 52 var line = 0; |
| 53 var column = 0; | 53 var column = 0; |
| 54 var srcLine = 0; | 54 var srcLine = null; |
| 55 var srcColumn = 0; | 55 var srcColumn = null; |
| 56 var srcUrlId = 0; | 56 var srcUrlId = null; |
| 57 var srcNameId = 0; | 57 var srcNameId = null; |
| 58 var first = true; | 58 var first = true; |
| 59 | 59 |
| 60 // The encoding needs to be sorted by the target offsets. | 60 // The encoding needs to be sorted by the target offsets. |
| 61 _entries.sort(); | 61 _entries.sort(); |
| 62 for (var entry in _entries) { | 62 for (var entry in _entries) { |
| 63 int nextLine = entry.target.line; | 63 int nextLine = entry.target.line; |
| 64 if (nextLine > line) { | 64 if (nextLine > line) { |
| 65 for (int i = line; i < nextLine; ++i) { | 65 for (int i = line; i < nextLine; ++i) { |
| 66 buff.write(';'); | 66 buff.write(';'); |
| 67 } | 67 } |
| 68 line = nextLine; | 68 line = nextLine; |
| 69 column = 0; | 69 column = 0; |
| 70 first = true; | 70 first = true; |
| 71 } | 71 } |
| 72 | 72 |
| 73 if (!first) buff.write(','); | 73 if (!first) buff.write(','); |
| 74 first = false; | 74 first = false; |
| 75 column = _append(buff, column, entry.target.column); | 75 column = _append(buff, column, entry.target.column); |
| 76 | 76 |
| 77 // Encoding can be just the column offset if there is no source | 77 // Encoding can be just the column offset if there is no source |
| 78 // information, or if two consecutive mappings share exactly the same | 78 // information. |
| 79 // source information. | |
| 80 var source = entry.source; | 79 var source = entry.source; |
| 81 if (source == null) continue; | 80 if (source == null) continue; |
| 82 var newUrlId = _indexOf(_urls, source.sourceUrl); | 81 var newUrlId = _indexOf(_urls, source.sourceUrl); |
| 83 if (newUrlId == srcUrlId && source.line == srcLine | |
| 84 && source.column == srcColumn && entry.identifierName == null) { | |
| 85 continue; | |
| 86 } | |
| 87 | 82 |
| 88 srcUrlId = _append(buff, srcUrlId, newUrlId); | 83 srcUrlId = _append(buff, srcUrlId, newUrlId); |
| 89 srcLine = _append(buff, srcLine, source.line); | 84 srcLine = _append(buff, srcLine, source.line); |
| 90 srcColumn = _append(buff, srcColumn, source.column); | 85 srcColumn = _append(buff, srcColumn, source.column); |
| 91 | 86 |
| 92 if (entry.identifierName == null) continue; | 87 if (entry.identifierName == null) continue; |
| 93 srcNameId = _append(buff, srcNameId, | 88 srcNameId = _append(buff, srcNameId, |
| 94 _indexOf(_names, entry.identifierName)); | 89 _indexOf(_names, entry.identifierName)); |
| 95 } | 90 } |
| 96 | 91 |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 115 return map.putIfAbsent(value, () { | 110 return map.putIfAbsent(value, () { |
| 116 int index = map.length; | 111 int index = map.length; |
| 117 map[value] = index; | 112 map[value] = index; |
| 118 return index; | 113 return index; |
| 119 }); | 114 }); |
| 120 } | 115 } |
| 121 | 116 |
| 122 /// Appends to [buff] a VLQ encoding of [newValue] using the difference | 117 /// Appends to [buff] a VLQ encoding of [newValue] using the difference |
| 123 /// between [oldValue] and [newValue] | 118 /// between [oldValue] and [newValue] |
| 124 static int _append(StringBuffer buff, int oldValue, int newValue) { | 119 static int _append(StringBuffer buff, int oldValue, int newValue) { |
| 120 if (oldValue == null) { | |
|
Siggi Cherem (dart-lang)
2014/04/18 02:00:31
I might be missing something, could we simply reve
zarah
2014/04/23 07:52:12
I don't think you are :-), Done.
| |
| 121 oldValue = 0; | |
| 122 } | |
| 125 buff.writeAll(encodeVlq(newValue - oldValue)); | 123 buff.writeAll(encodeVlq(newValue - oldValue)); |
| 126 return newValue; | 124 return newValue; |
| 127 } | 125 } |
| 128 } | 126 } |
| 129 | 127 |
| 130 /// An entry in the source map builder. | 128 /// An entry in the source map builder. |
| 131 class Entry implements Comparable { | 129 class Entry implements Comparable { |
| 132 /// Span denoting the original location in the input source file | 130 /// Span denoting the original location in the input source file |
| 133 final Location source; | 131 final Location source; |
| 134 | 132 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 145 /// because source map files are encoded by printing each mapping in order as | 143 /// because source map files are encoded by printing each mapping in order as |
| 146 /// they appear in the target file. | 144 /// they appear in the target file. |
| 147 int compareTo(Entry other) { | 145 int compareTo(Entry other) { |
| 148 int res = target.compareTo(other.target); | 146 int res = target.compareTo(other.target); |
| 149 if (res != 0) return res; | 147 if (res != 0) return res; |
| 150 res = source.sourceUrl.compareTo(other.source.sourceUrl); | 148 res = source.sourceUrl.compareTo(other.source.sourceUrl); |
| 151 if (res != 0) return res; | 149 if (res != 0) return res; |
| 152 return source.compareTo(other.source); | 150 return source.compareTo(other.source); |
| 153 } | 151 } |
| 154 } | 152 } |
| OLD | NEW |