Skip to content

Commit 37bce89

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Use FragmentImpl.nameEnd instead of nameLength.
Change-Id: If02b26cd8e1346aa34c8978dc286499975905a95 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/439547 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent 51311c3 commit 37bce89

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

pkg/analysis_server/lib/src/computer/computer_call_hierarchy.dart

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,22 @@ class CallHierarchyItem {
135135
element = _nonSynthetic(element);
136136
var fragment = element.firstFragment as FragmentImpl;
137137

138-
// Compilation units will return -1 for nameOffset which is not valid, so
139-
// use 0:0.
140-
return fragment.nameOffset == -1
141-
? SourceRange(0, 0)
142-
: SourceRange(fragment.nameOffset, fragment.nameLength);
138+
var nameOffset = fragment.nameOffset2;
139+
var nameEnd = fragment.nameEnd;
140+
if (nameOffset != null && nameEnd != null) {
141+
return SourceRange(nameOffset, nameEnd - nameOffset);
142+
}
143+
144+
// For unnamed constructors, use the type name.
145+
if (fragment is ConstructorFragmentImpl) {
146+
var typeNameOffset = fragment.typeNameOffset;
147+
var typeName = fragment.typeName;
148+
if (typeName != null && typeNameOffset != null) {
149+
return SourceRange(typeNameOffset, typeName.length);
150+
}
151+
}
152+
153+
return SourceRange(0, 0);
143154
}
144155

145156
static Element _nonSynthetic(Element element) {

pkg/analyzer/lib/src/dart/element/element.dart

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,7 @@ class ConstructorFragmentImpl extends ExecutableFragmentImpl
959959
@override
960960
int? periodOffset;
961961

962+
@override
962963
int? nameEnd;
963964

964965
@override
@@ -1064,16 +1065,6 @@ class ConstructorFragmentImpl extends ExecutableFragmentImpl
10641065
@override
10651066
String get name2 => name;
10661067

1067-
@override
1068-
int get nameLength {
1069-
var nameEnd = this.nameEnd;
1070-
if (nameEnd == null) {
1071-
return 0;
1072-
} else {
1073-
return nameEnd - nameOffset;
1074-
}
1075-
}
1076-
10771068
@override
10781069
int get offset => isSynthetic ? enclosingElement.offset : nameOffset;
10791070

@@ -3008,10 +2999,7 @@ class FormalParameterElementImpl extends PromotableElementImpl
30082999
TypeImpl type,
30093000
ParameterKind parameterKind,
30103001
) {
3011-
var fragment = FormalParameterFragmentImpl.synthetic(
3012-
name,
3013-
parameterKind,
3014-
);
3002+
var fragment = FormalParameterFragmentImpl.synthetic(name, parameterKind);
30153003
return FormalParameterElementImpl(fragment)..type = type;
30163004
}
30173005

@@ -3754,9 +3742,16 @@ abstract class FragmentImpl implements Fragment {
37543742
@override
37553743
String? get name2 => name;
37563744

3757-
/// The length of the name of this element in the file that contains the
3758-
/// declaration of this element, or `0` if this element does not have a name.
3759-
int get nameLength => displayName.length;
3745+
/// The offset after the last character of the name, or `null` if there is
3746+
/// no declaration in code for this fragment, or the name is absent.
3747+
int? get nameEnd {
3748+
if (nameOffset2 case var nameOffset?) {
3749+
if (name case var name?) {
3750+
return nameOffset + name.length;
3751+
}
3752+
}
3753+
return null;
3754+
}
37603755

37613756
/// The analysis session in which this element is defined.
37623757
AnalysisSession? get session {

pkg/analyzer/lib/src/dart/micro/utils.dart

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,14 @@ class ReferencesCollector extends GeneralizingAstVisitor<void> {
348348
visitConstructorDeclaration(covariant ConstructorDeclarationImpl node) {
349349
var fragment = node.declaredFragment;
350350
if (fragment?.element == element) {
351-
if (fragment!.name == 'new') {
351+
if ((node.period, node.name) case (var period?, var nameToken?)) {
352+
var offset = period.offset;
353+
var length = nameToken.end - offset;
354+
references.add(MatchInfo(offset, length, MatchKind.DECLARATION));
355+
} else {
352356
references.add(
353-
MatchInfo(
354-
fragment.nameOffset + fragment.nameLength,
355-
0,
356-
MatchKind.DECLARATION,
357-
),
357+
MatchInfo(node.returnType.end, 0, MatchKind.DECLARATION),
358358
);
359-
} else {
360-
var offset = node.period!.offset;
361-
var length = node.name!.end - offset;
362-
references.add(MatchInfo(offset, length, MatchKind.DECLARATION));
363359
}
364360
}
365361
super.visitConstructorDeclaration(node);

0 commit comments

Comments
 (0)