Skip to content

Commit cc4c1ac

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Go to Super handles field declarations
Fixes: #60465 Change-Id: I8b9e0231d3e7f6e7d9b75fdcf6b16311d470a7fe Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/420120 Reviewed-by: Paul Berry <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Commit-Queue: Paul Berry <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]> Auto-Submit: Felipe Morschel <[email protected]>
1 parent b067916 commit cc4c1ac

File tree

3 files changed

+151
-7
lines changed

3 files changed

+151
-7
lines changed

pkg/analysis_server/lib/src/lsp/handlers/custom/handler_super.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,16 @@ class SuperHandler
6464

6565
/// Returns whether [node] is something that can be considered to have a
6666
/// "super" (a class or a class member).
67-
bool _canHaveSuper(AstNode node) =>
68-
node is ClassDeclaration || node is ClassMember;
67+
bool _canHaveSuper(AstNode node) {
68+
AstNode? testNode = node;
69+
if (testNode
70+
case VariableDeclaration(parent: VariableDeclarationList list) ||
71+
VariableDeclarationList list) {
72+
// This says if the variable is a field or null if it isn't.
73+
testNode = list.parent;
74+
}
75+
return testNode is ClassDeclaration || testNode is ClassMember;
76+
}
6977
}
7078

7179
class _SuperComputer {
@@ -96,7 +104,7 @@ class _SuperComputer {
96104

97105
var inheritanceManager = session.inheritanceManager;
98106

99-
if (element is! ExecutableElement2) {
107+
if (element is! ExecutableElement2 && element is! FieldElement2) {
100108
return null;
101109
}
102110

pkg/analysis_server/lib/src/lsp/mapping.dart

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ import 'package:analyzer/source/source_range.dart' as server;
3232
import 'package:analyzer/src/dart/analysis/search.dart'
3333
as server
3434
show DeclarationKind;
35+
import 'package:analyzer/src/dart/element/element.dart';
3536
import 'package:analyzer/src/error/codes.dart';
37+
import 'package:analyzer/src/utilities/extensions/string.dart'
38+
show IntExtension;
3639
import 'package:analyzer_plugin/protocol/protocol_common.dart' as plugin;
3740
import 'package:analyzer_plugin/src/utilities/client_uri_converter.dart';
3841
import 'package:collection/collection.dart';
@@ -511,8 +514,19 @@ lsp.Location? fragmentToLocation(
511514
var libraryFragment = fragment.libraryFragment!;
512515
var sourcePath = libraryFragment.source.fullName;
513516

514-
var nameOffset = fragment.nameOffset2;
515-
var nameLength = fragment.name2?.length;
517+
int? nameOffset;
518+
int? nameLength;
519+
if (fragment case PropertyAccessorElementImpl(
520+
:var isSynthetic,
521+
:var nonSynthetic,
522+
) when isSynthetic) {
523+
var element = nonSynthetic;
524+
nameOffset = element.nameOffset.nullIfNegative;
525+
nameLength = element.name?.length;
526+
} else {
527+
nameOffset = fragment.nameOffset2;
528+
nameLength = fragment.name2?.length;
529+
}
516530

517531
// For unnamed constructors, use the type name as the target location.
518532
if (nameOffset == null && fragment is ConstructorFragment) {

pkg/analysis_server/test/lsp/super_test.dart

Lines changed: 124 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,76 @@ class B extends A {
170170
);
171171
}
172172

173-
Future<void> test_getter() async {
173+
Future<void> test_field_field() async {
174+
await verifyGoToSuper(
175+
TestCode.parse('''
176+
class A {
177+
String? [!foo!];
178+
}
179+
180+
class B extends A {}
181+
182+
class C extends B {
183+
@override
184+
String? fo^o;
185+
}
186+
'''),
187+
);
188+
}
189+
190+
Future<void> test_field_getter() async {
191+
await verifyGoToSuper(
192+
TestCode.parse('''
193+
class A {
194+
String? get [!foo!] => null;
195+
}
196+
197+
class B extends A {}
198+
199+
class C extends B {
200+
@override
201+
String? fo^o;
202+
}
203+
'''),
204+
);
205+
}
206+
207+
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/60469')
208+
Future<void> test_field_setter() async {
209+
await verifyGoToSuper(
210+
TestCode.parse('''
211+
class A {
212+
set [!foo!](String? value) {}
213+
}
214+
215+
class B extends A {}
216+
217+
class C extends B {
218+
@override
219+
String? fo^o;
220+
}
221+
'''),
222+
);
223+
}
224+
225+
Future<void> test_getter_field() async {
226+
await verifyGoToSuper(
227+
TestCode.parse('''
228+
class A {
229+
String [!foo!] = '';
230+
}
231+
232+
class B extends A {}
233+
234+
class C extends B {
235+
@override
236+
String get fo^o => '';
237+
}
238+
'''),
239+
);
240+
}
241+
242+
Future<void> test_getter_getter() async {
174243
await verifyGoToSuper(
175244
TestCode.parse('''
176245
class A {
@@ -179,6 +248,24 @@ class A {
179248
180249
class B extends A {}
181250
251+
class C extends B {
252+
@override
253+
String get fo^o => '';
254+
}
255+
'''),
256+
);
257+
}
258+
259+
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/60469')
260+
Future<void> test_getter_setter() async {
261+
await verifyGoToSuper(
262+
TestCode.parse('''
263+
class A {
264+
set [!foo!](String value) {}
265+
}
266+
267+
class B extends A {}
268+
182269
class C extends B {
183270
@override
184271
String get fo^o => '';
@@ -385,7 +472,42 @@ class C extends B {
385472
);
386473
}
387474

388-
Future<void> test_setter() async {
475+
Future<void> test_setter_field() async {
476+
await verifyGoToSuper(
477+
TestCode.parse('''
478+
class A {
479+
String [!foo!] = '';
480+
}
481+
482+
class B extends A {}
483+
484+
class C extends B {
485+
@override
486+
set fo^o(String value) {}
487+
}
488+
'''),
489+
);
490+
}
491+
492+
@FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/60469')
493+
Future<void> test_setter_getter() async {
494+
await verifyGoToSuper(
495+
TestCode.parse('''
496+
class A {
497+
String get [!foo!] => '';
498+
}
499+
500+
class B extends A {}
501+
502+
class C extends B {
503+
@override
504+
set fo^o(String value) {}
505+
}
506+
'''),
507+
);
508+
}
509+
510+
Future<void> test_setter_setter() async {
389511
await verifyGoToSuper(
390512
TestCode.parse('''
391513
class A {

0 commit comments

Comments
 (0)