Skip to content

Commit 5d199d6

Browse files
stereotype441Commit Queue
authored andcommitted
[messages] Move SourceRange class to _fe_analyzer_shared.
This will allow `SourceRange` to be referred to by classes in `pkg/_fe_analyzer_shared` such as `LocatableDiagnostic` and `SyntacticEntity`. Change-Id: I6a6a69641f9daa351fbea3a0cc61f760f7a966d3 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/467683 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Paul Berry <paulberry@google.com>
1 parent f677d94 commit 5d199d6

File tree

5 files changed

+111
-98
lines changed

5 files changed

+111
-98
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (c) 2025, 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+
import 'dart:math' as math;
6+
7+
import 'package:_fe_analyzer_shared/src/base/analyzer_public_api.dart';
8+
9+
/// A source range defines a range of characters within source code.
10+
@AnalyzerPublicApi(
11+
message: 'exported by package:analyzer/source/source_range.dart',
12+
)
13+
class SourceRange {
14+
/// An empty source range (a range with offset `0` and length `0`).
15+
static const SourceRange EMPTY = const SourceRange(0, 0);
16+
17+
/// The 0-based index of the first character of the source range.
18+
final int offset;
19+
20+
/// The number of characters in the source range.
21+
final int length;
22+
23+
/// Initialize a newly created source range using the given [offset] and
24+
/// [length].
25+
const SourceRange(this.offset, this.length);
26+
27+
/// Return the 0-based index of the character immediately after this source
28+
/// range.
29+
int get end => offset + length;
30+
31+
@override
32+
int get hashCode => 31 * offset + length;
33+
34+
@override
35+
bool operator ==(Object other) {
36+
return other is SourceRange &&
37+
other.offset == offset &&
38+
other.length == length;
39+
}
40+
41+
/// Return `true` if [x] is in the interval `[offset, offset + length]`.
42+
bool contains(int x) => offset <= x && x <= offset + length;
43+
44+
/// Return `true` if [x] is in the interval `(offset, offset + length)`.
45+
bool containsExclusive(int x) => offset < x && x < offset + length;
46+
47+
/// Return `true` if the [otherRange] covers this source range.
48+
bool coveredBy(SourceRange otherRange) => otherRange.covers(this);
49+
50+
/// Return `true` if this source range covers the [otherRange].
51+
bool covers(SourceRange otherRange) =>
52+
offset <= otherRange.offset && otherRange.end <= end;
53+
54+
/// Return `true` if this source range ends inside the [otherRange].
55+
bool endsIn(SourceRange otherRange) {
56+
int thisEnd = end;
57+
return otherRange.contains(thisEnd);
58+
}
59+
60+
/// Return a source range covering [delta] characters before the start of this
61+
/// source range and [delta] characters after the end of this source range.
62+
SourceRange getExpanded(int delta) =>
63+
new SourceRange(offset - delta, delta + length + delta);
64+
65+
/// Return a source range with the same offset as this source range but whose
66+
/// length is [delta] characters longer than this source range.
67+
SourceRange getMoveEnd(int delta) => new SourceRange(offset, length + delta);
68+
69+
/// Return a source range with the same length as this source range but whose
70+
/// offset is [delta] characters after the offset of this source range.
71+
SourceRange getTranslated(int delta) =>
72+
new SourceRange(offset + delta, length);
73+
74+
/// Return the minimal source range that covers both this and the
75+
/// [otherRange].
76+
SourceRange getUnion(SourceRange otherRange) {
77+
int newOffset = math.min(offset, otherRange.offset);
78+
int newEnd = math.max(
79+
offset + length,
80+
otherRange.offset + otherRange.length,
81+
);
82+
return new SourceRange(newOffset, newEnd - newOffset);
83+
}
84+
85+
/// Return `true` if this source range intersects the [otherRange].
86+
bool intersects(SourceRange? otherRange) {
87+
if (otherRange == null) {
88+
return false;
89+
}
90+
if (end <= otherRange.offset) {
91+
return false;
92+
}
93+
if (offset >= otherRange.end) {
94+
return false;
95+
}
96+
return true;
97+
}
98+
99+
/// Return `true` if this source range starts in the [otherRange].
100+
bool startsIn(SourceRange otherRange) => otherRange.contains(offset);
101+
102+
@override
103+
String toString() => '[offset=$offset, length=$length]';
104+
}

pkg/analysis_server_plugin/api.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ dart:core:
165165
package:_fe_analyzer_shared/src/base/errors.dart:
166166
Diagnostic (referenced)
167167
DiagnosticCode (referenced)
168+
package:_fe_analyzer_shared/src/base/source_range.dart:
169+
SourceRange (referenced)
168170
package:_fe_analyzer_shared/src/scanner/token.dart:
169171
Token (referenced)
170172
package:analyzer/analysis_rule/analysis_rule.dart:
@@ -197,8 +199,6 @@ package:analyzer/dart/element/type_system.dart:
197199
TypeSystem (referenced)
198200
package:analyzer/instrumentation/service.dart:
199201
InstrumentationService (referenced)
200-
package:analyzer/source/source_range.dart:
201-
SourceRange (referenced)
202202
package:analyzer/src/dart/ast/ast.dart:
203203
AstNode (referenced)
204204
ClassDeclaration (referenced)

pkg/analyzer/lib/source/source_range.dart

Lines changed: 2 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -2,97 +2,5 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'dart:math' as math;
6-
7-
/// A source range defines a range of characters within source code.
8-
class SourceRange {
9-
/// An empty source range (a range with offset `0` and length `0`).
10-
static const SourceRange EMPTY = SourceRange(0, 0);
11-
12-
/// The 0-based index of the first character of the source range.
13-
final int offset;
14-
15-
/// The number of characters in the source range.
16-
final int length;
17-
18-
/// Initialize a newly created source range using the given [offset] and
19-
/// [length].
20-
const SourceRange(this.offset, this.length);
21-
22-
/// Return the 0-based index of the character immediately after this source
23-
/// range.
24-
int get end => offset + length;
25-
26-
@override
27-
int get hashCode => 31 * offset + length;
28-
29-
@override
30-
bool operator ==(Object other) {
31-
return other is SourceRange &&
32-
other.offset == offset &&
33-
other.length == length;
34-
}
35-
36-
/// Return `true` if [x] is in the interval `[offset, offset + length]`.
37-
bool contains(int x) => offset <= x && x <= offset + length;
38-
39-
/// Return `true` if [x] is in the interval `(offset, offset + length)`.
40-
bool containsExclusive(int x) => offset < x && x < offset + length;
41-
42-
/// Return `true` if the [otherRange] covers this source range.
43-
bool coveredBy(SourceRange otherRange) => otherRange.covers(this);
44-
45-
/// Return `true` if this source range covers the [otherRange].
46-
bool covers(SourceRange otherRange) =>
47-
offset <= otherRange.offset && otherRange.end <= end;
48-
49-
/// Return `true` if this source range ends inside the [otherRange].
50-
bool endsIn(SourceRange otherRange) {
51-
int thisEnd = end;
52-
return otherRange.contains(thisEnd);
53-
}
54-
55-
/// Return a source range covering [delta] characters before the start of this
56-
/// source range and [delta] characters after the end of this source range.
57-
SourceRange getExpanded(int delta) =>
58-
SourceRange(offset - delta, delta + length + delta);
59-
60-
/// Return a source range with the same offset as this source range but whose
61-
/// length is [delta] characters longer than this source range.
62-
SourceRange getMoveEnd(int delta) => SourceRange(offset, length + delta);
63-
64-
/// Return a source range with the same length as this source range but whose
65-
/// offset is [delta] characters after the offset of this source range.
66-
SourceRange getTranslated(int delta) => SourceRange(offset + delta, length);
67-
68-
/// Return the minimal source range that covers both this and the
69-
/// [otherRange].
70-
SourceRange getUnion(SourceRange otherRange) {
71-
int newOffset = math.min(offset, otherRange.offset);
72-
int newEnd = math.max(
73-
offset + length,
74-
otherRange.offset + otherRange.length,
75-
);
76-
return SourceRange(newOffset, newEnd - newOffset);
77-
}
78-
79-
/// Return `true` if this source range intersects the [otherRange].
80-
bool intersects(SourceRange? otherRange) {
81-
if (otherRange == null) {
82-
return false;
83-
}
84-
if (end <= otherRange.offset) {
85-
return false;
86-
}
87-
if (offset >= otherRange.end) {
88-
return false;
89-
}
90-
return true;
91-
}
92-
93-
/// Return `true` if this source range starts in the [otherRange].
94-
bool startsIn(SourceRange otherRange) => otherRange.contains(offset);
95-
96-
@override
97-
String toString() => '[offset=$offset, length=$length]';
98-
}
5+
export 'package:_fe_analyzer_shared/src/base/source_range.dart'
6+
show SourceRange;

pkg/analyzer_plugin/api.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,8 @@ package:_fe_analyzer_shared/src/base/errors.dart:
20722072
Diagnostic (referenced)
20732073
DiagnosticSeverity (referenced)
20742074
DiagnosticType (referenced)
2075+
package:_fe_analyzer_shared/src/base/source_range.dart:
2076+
SourceRange (referenced)
20752077
package:_fe_analyzer_shared/src/base/syntactic_entity.dart:
20762078
SyntacticEntity (referenced)
20772079
package:_fe_analyzer_shared/src/scanner/token.dart:
@@ -2107,8 +2109,6 @@ package:analyzer/file_system/overlay_file_system.dart:
21072109
OverlayResourceProvider (referenced)
21082110
package:analyzer/source/line_info.dart:
21092111
LineInfo (referenced)
2110-
package:analyzer/source/source_range.dart:
2111-
SourceRange (referenced)
21122112
package:analyzer/src/dart/analysis/byte_store.dart:
21132113
ByteStore (referenced)
21142114
package:analyzer/src/dart/ast/ast.dart:

pkg/front_end/test/spell_checking_list_code.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ counted
383383
counters
384384
covariances
385385
coverage
386+
covering
386387
cp
387388
cpu
388389
cr

0 commit comments

Comments
 (0)