Skip to content

Commit 2c9d3c8

Browse files
authored
Track offsets instead of locations in InterpolationMap (#2674)
This is semantically identical, but a little more efficient and easier to construct in situations without actual `SourceLocation`s.
1 parent d8d7f9c commit 2c9d3c8

File tree

3 files changed

+21
-21
lines changed

3 files changed

+21
-21
lines changed

lib/src/interpolation_map.dart

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,27 @@ final class InterpolationMap {
1616
/// The interpolation from which this map was generated.
1717
final Interpolation _interpolation;
1818

19-
/// Locations in the generated string.
19+
/// Location offsets in the generated string.
2020
///
2121
/// Each of these indicates the location in the generated string that
2222
/// corresponds to the end of the component at the same index of
2323
/// [_interpolation.contents]. Its length is always one less than
2424
/// [_interpolation.contents] because the last element always ends the string.
25-
final List<SourceLocation> _targetLocations;
25+
final List<int> _targetOffsets;
2626

27-
/// Creates a new interpolation map that maps the given [targetLocations] in
28-
/// the generated string to the contents of the interpolation.
27+
/// Creates a new interpolation map that maps the given [targetOffsets] in the
28+
/// generated string to the contents of the interpolation.
2929
///
30-
/// Each target location at index `i` corresponds to the character in the
30+
/// Each target offset at index `i` corresponds to the character in the
3131
/// generated string after `interpolation.contents[i]`.
3232
InterpolationMap(
3333
this._interpolation,
34-
Iterable<SourceLocation> targetLocations,
35-
) : _targetLocations = List.unmodifiable(targetLocations) {
34+
Iterable<int> targetOffsets,
35+
) : _targetOffsets = List.unmodifiable(targetOffsets) {
3636
var expectedLocations = math.max(0, _interpolation.contents.length - 1);
37-
if (_targetLocations.length != expectedLocations) {
37+
if (_targetOffsets.length != expectedLocations) {
3838
throw ArgumentError(
39-
"InterpolationMap must have $expectedLocations targetLocations if the "
39+
"InterpolationMap must have $expectedLocations targetOffsets if the "
4040
"interpolation has ${_interpolation.contents.length} components.",
4141
);
4242
}
@@ -139,7 +139,7 @@ final class InterpolationMap {
139139
),
140140
);
141141
var offsetInString =
142-
target.offset - (index == 0 ? 0 : _targetLocations[index - 1].offset);
142+
target.offset - (index == 0 ? 0 : _targetOffsets[index - 1]);
143143

144144
// This produces slightly incorrect mappings if there are _unnecessary_
145145
// escapes in the source file, but that's unlikely enough that it's probably
@@ -151,8 +151,8 @@ final class InterpolationMap {
151151

152152
/// Return the index in [_interpolation.contents] at which [target] points.
153153
int _indexInContents(SourceLocation target) {
154-
for (var i = 0; i < _targetLocations.length; i++) {
155-
if (target.offset < _targetLocations[i].offset) return i;
154+
for (var i = 0; i < _targetOffsets.length; i++) {
155+
if (target.offset < _targetOffsets[i]) return i;
156156
}
157157

158158
return _interpolation.contents.length - 1;

lib/src/visitor/async_evaluate.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4233,13 +4233,13 @@ final class _EvaluateVisitor
42334233
required bool sourceMap,
42344234
bool warnForColor = false,
42354235
}) async {
4236-
var targetLocations = sourceMap ? <SourceLocation>[] : null;
4236+
var targetOffsets = sourceMap ? <int>[] : null;
42374237
var oldInSupportsDeclaration = _inSupportsDeclaration;
42384238
_inSupportsDeclaration = false;
42394239
var buffer = StringBuffer();
42404240
var first = true;
42414241
for (var value in interpolation.contents) {
4242-
if (!first) targetLocations?.add(SourceLocation(buffer.length));
4242+
if (!first) targetOffsets?.add(buffer.length);
42434243
first = false;
42444244

42454245
if (value is String) {
@@ -4277,8 +4277,8 @@ final class _EvaluateVisitor
42774277

42784278
return (
42794279
buffer.toString(),
4280-
targetLocations.andThen(
4281-
(targetLocations) => InterpolationMap(interpolation, targetLocations),
4280+
targetOffsets.andThen(
4281+
(targetOffsets) => InterpolationMap(interpolation, targetOffsets),
42824282
),
42834283
);
42844284
}

lib/src/visitor/evaluate.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// DO NOT EDIT. This file was generated from async_evaluate.dart.
66
// See tool/grind/synchronize.dart for details.
77
//
8-
// Checksum: 02a6149a29eca1d21306e9582a6b7df86f4cd2f2
8+
// Checksum: deb9d00931542dae02ed23607cb17f1273849432
99
//
1010
// ignore_for_file: unused_import
1111

@@ -4234,13 +4234,13 @@ final class _EvaluateVisitor
42344234
required bool sourceMap,
42354235
bool warnForColor = false,
42364236
}) {
4237-
var targetLocations = sourceMap ? <SourceLocation>[] : null;
4237+
var targetOffsets = sourceMap ? <int>[] : null;
42384238
var oldInSupportsDeclaration = _inSupportsDeclaration;
42394239
_inSupportsDeclaration = false;
42404240
var buffer = StringBuffer();
42414241
var first = true;
42424242
for (var value in interpolation.contents) {
4243-
if (!first) targetLocations?.add(SourceLocation(buffer.length));
4243+
if (!first) targetOffsets?.add(buffer.length);
42444244
first = false;
42454245

42464246
if (value is String) {
@@ -4278,8 +4278,8 @@ final class _EvaluateVisitor
42784278

42794279
return (
42804280
buffer.toString(),
4281-
targetLocations.andThen(
4282-
(targetLocations) => InterpolationMap(interpolation, targetLocations),
4281+
targetOffsets.andThen(
4282+
(targetOffsets) => InterpolationMap(interpolation, targetOffsets),
42834283
),
42844284
);
42854285
}

0 commit comments

Comments
 (0)