Hide nonVirtual elements in override completions.

Fixes https://github.com/dart-lang/sdk/issues/55628

Change-Id: I70dcf5b47dae2faef30a47a4e3f811e735a1eeec
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/371742
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Keerti Parthasarathy <keertip@google.com>
This commit is contained in:
Keerti Parthasarathy 2024-06-14 23:45:19 +00:00 committed by Commit Queue
parent 4138277ee6
commit 627cd3047d
2 changed files with 47 additions and 0 deletions

View file

@ -43,6 +43,10 @@ class OverrideHelper {
var element = interfaceMap[name];
// Gracefully degrade if the overridden element has not been resolved.
if (element != null) {
if (_hasNonVirtualAnnotation(element)) {
continue;
}
var invokeSuper = interface.isSuperImplemented(name);
var matcherScore = math.max(
math.max(state.matcher.score('override'),
@ -63,6 +67,17 @@ class OverrideHelper {
}
}
/// Checks if the [element] has the `@nonVirtual` annotation.
bool _hasNonVirtualAnnotation(ExecutableElement element) {
if (element is PropertyAccessorElement && element.isSynthetic) {
var variable = element.variable2;
if (variable != null && variable.hasNonVirtual) {
return true;
}
}
return element.hasNonVirtual;
}
/// Return the list of names that belong to the [interface] of a class, but
/// are not yet declared in the class.
List<Name> _namesToOverride(Uri libraryUri, Interface interface) {

View file

@ -644,6 +644,7 @@ mixin OverrideTestCases on AbstractCompletionDriverTest {
@override
Future<void> setUp() async {
await super.setUp();
writeTestPackageConfig(meta: true);
printerConfiguration = printer.Configuration(
filter: (suggestion) {
@ -1236,6 +1237,37 @@ class A {
void foo01() {}
}
class B extends A {
@override
foo^
}
''');
assertResponse(r'''
replacement
left: 3
suggestions
void foo01() {
// TODO: implement foo01
super.foo01();
}
kind: override
displayText: foo01() { }
selection: 48 14
''');
}
Future<void> test_class_method_fromExtends_withOverride_nonVirtual() async {
await computeSuggestions('''
import 'package:meta/meta.dart';
class A {
void foo01() {}
@nonVirtual
void foo02() {}
}
class B extends A {
@override
foo^