[analysis_server] Add semantic highlighting for sealed class modifier.

Enable the 'sealed-class' experiment for the highlighting test and add the highlighting for the modifier.

Change-Id: I6affdab506c29ab9d92b2d6315a93d09396bafaa
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/277201
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Kallen Tu <kallentu@google.com>
This commit is contained in:
Kallen Tu 2022-12-27 23:24:40 +00:00 committed by Commit Queue
parent d5eeeb3585
commit cdd9e43b2c
4 changed files with 45 additions and 4 deletions

View file

@ -664,6 +664,7 @@ class _DartUnitHighlightsComputerVisitor extends RecursiveAstVisitor<void> {
void visitClassDeclaration(ClassDeclaration node) { void visitClassDeclaration(ClassDeclaration node) {
computer._addRegion_token( computer._addRegion_token(
node.abstractKeyword, HighlightRegionType.BUILT_IN); node.abstractKeyword, HighlightRegionType.BUILT_IN);
computer._addRegion_token(node.sealedKeyword, HighlightRegionType.BUILT_IN);
computer._addRegion_token(node.classKeyword, HighlightRegionType.KEYWORD); computer._addRegion_token(node.classKeyword, HighlightRegionType.KEYWORD);
computer._addRegion_token(node.name, HighlightRegionType.CLASS); computer._addRegion_token(node.name, HighlightRegionType.CLASS);
super.visitClassDeclaration(node); super.visitClassDeclaration(node);
@ -673,6 +674,7 @@ class _DartUnitHighlightsComputerVisitor extends RecursiveAstVisitor<void> {
void visitClassTypeAlias(ClassTypeAlias node) { void visitClassTypeAlias(ClassTypeAlias node) {
computer._addRegion_token( computer._addRegion_token(
node.abstractKeyword, HighlightRegionType.BUILT_IN); node.abstractKeyword, HighlightRegionType.BUILT_IN);
computer._addRegion_token(node.sealedKeyword, HighlightRegionType.BUILT_IN);
super.visitClassTypeAlias(node); super.visitClassTypeAlias(node);
} }
@ -1079,6 +1081,7 @@ class _DartUnitHighlightsComputerVisitor extends RecursiveAstVisitor<void> {
@override @override
void visitMixinDeclaration(MixinDeclaration node) { void visitMixinDeclaration(MixinDeclaration node) {
computer._addRegion_token(node.sealedKeyword, HighlightRegionType.BUILT_IN);
computer._addRegion_token(node.mixinKeyword, HighlightRegionType.BUILT_IN); computer._addRegion_token(node.mixinKeyword, HighlightRegionType.BUILT_IN);
super.visitMixinDeclaration(node); super.visitMixinDeclaration(node);
} }

View file

@ -8,6 +8,7 @@ import 'package:analysis_server/protocol/protocol.dart';
import 'package:analysis_server/protocol/protocol_constants.dart'; import 'package:analysis_server/protocol/protocol_constants.dart';
import 'package:analysis_server/protocol/protocol_generated.dart'; import 'package:analysis_server/protocol/protocol_generated.dart';
import 'package:analyzer/source/source_range.dart'; import 'package:analyzer/source/source_range.dart';
import 'package:analyzer/src/dart/analysis/experiments.dart';
import 'package:analyzer/src/test_utilities/test_code_format.dart'; import 'package:analyzer/src/test_utilities/test_code_format.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart'; import 'package:analyzer_plugin/protocol/protocol_common.dart';
import 'package:collection/collection.dart'; import 'package:collection/collection.dart';
@ -25,6 +26,10 @@ void main() {
@reflectiveTest @reflectiveTest
class AnalysisNotificationHighlightsTest extends HighlightsTestSupport { class AnalysisNotificationHighlightsTest extends HighlightsTestSupport {
@override
List<String> get experiments =>
[...super.experiments, EnableString.sealed_class];
void assertHighlightText(TestCode testCode, int index, String expected) { void assertHighlightText(TestCode testCode, int index, String expected) {
var actual = _getHighlightText(testCode, index); var actual = _getHighlightText(testCode, index);
if (actual != expected) { if (actual != expected) {
@ -364,6 +369,22 @@ void f() {
assertNoRegion(HighlightRegionType.BUILT_IN, 'of = 2'); assertNoRegion(HighlightRegionType.BUILT_IN, 'of = 2');
} }
Future<void> test_BUILT_IN_sealed() async {
addTestFile('''
sealed class A {}
sealed mixin M {}
sealed class B = Object with M;
void f() {
var sealed = 42;
}
''');
await prepareHighlights();
assertHasRegion(HighlightRegionType.BUILT_IN, 'sealed class A');
assertHasRegion(HighlightRegionType.BUILT_IN, 'sealed mixin M');
assertHasRegion(HighlightRegionType.BUILT_IN, 'sealed class B');
assertNoRegion(HighlightRegionType.BUILT_IN, 'sealed = 42');
}
Future<void> test_BUILT_IN_set() async { Future<void> test_BUILT_IN_set() async {
addTestFile(''' addTestFile('''
set aaa(x) {} set aaa(x) {}

View file

@ -1001,10 +1001,12 @@ abstract class ClassAugmentationDeclaration
/// The declaration of a class. /// The declaration of a class.
/// ///
/// classDeclaration ::= /// classDeclaration ::=
/// 'abstract'? 'class' name [TypeParameterList]? /// classModifiers? 'class' name [TypeParameterList]?
/// [ExtendsClause]? [WithClause]? [ImplementsClause]? /// [ExtendsClause]? [WithClause]? [ImplementsClause]?
/// '{' [ClassMember]* '}' /// '{' [ClassMember]* '}'
/// ///
/// classModifiers ::= 'sealed' | 'abstract'
///
/// Clients may not extend, implement or mix-in this class. /// Clients may not extend, implement or mix-in this class.
abstract class ClassDeclaration implements ClassOrAugmentationDeclaration { abstract class ClassDeclaration implements ClassOrAugmentationDeclaration {
@override @override
@ -1090,6 +1092,9 @@ abstract class ClassOrAugmentationDeclaration
/// Returns the right curly bracket. /// Returns the right curly bracket.
Token get rightBracket; Token get rightBracket;
/// Return the 'sealed' keyword, or `null` if the keyword was absent.
Token? get sealedKeyword;
/// Returns the type parameters for the class, or `null` if the class does /// Returns the type parameters for the class, or `null` if the class does
/// not have any type parameters. /// not have any type parameters.
TypeParameterList? get typeParameters; TypeParameterList? get typeParameters;
@ -1127,6 +1132,9 @@ abstract class ClassTypeAlias implements TypeAlias {
/// implements clause. /// implements clause.
ImplementsClause? get implementsClause; ImplementsClause? get implementsClause;
/// Return the 'sealed' keyword, or `null` if the keyword was absent.
Token? get sealedKeyword;
/// Return the name of the superclass of the class being declared. /// Return the name of the superclass of the class being declared.
NamedType get superclass; NamedType get superclass;
@ -3752,7 +3760,7 @@ abstract class MixinAugmentationDeclaration
/// The declaration of a mixin. /// The declaration of a mixin.
/// ///
/// mixinDeclaration ::= /// mixinDeclaration ::=
/// 'mixin' name [TypeParameterList]? /// 'sealed'? 'mixin' name [TypeParameterList]?
/// [OnClause]? [ImplementsClause]? '{' [ClassMember]* '}' /// [OnClause]? [ImplementsClause]? '{' [ClassMember]* '}'
/// ///
/// Clients may not extend, implement or mix-in this class. /// Clients may not extend, implement or mix-in this class.
@ -3821,6 +3829,9 @@ abstract class MixinOrAugmentationDeclaration
/// Returns the right curly bracket. /// Returns the right curly bracket.
Token get rightBracket; Token get rightBracket;
/// Return the 'sealed' keyword, or `null` if the keyword was absent.
Token? get sealedKeyword;
/// Returns the type parameters for the mixin, or `null` if the mixin does /// Returns the type parameters for the mixin, or `null` if the mixin does
/// not have any type parameters. /// not have any type parameters.
TypeParameterList? get typeParameters; TypeParameterList? get typeParameters;

View file

@ -1891,10 +1891,13 @@ class ChildEntity {
/// The declaration of a class. /// The declaration of a class.
/// ///
/// classDeclaration ::= /// classDeclaration ::=
/// 'abstract'? 'class' [SimpleIdentifier] [TypeParameterList]? /// classModifiers? 'class' [SimpleIdentifier] [TypeParameterList]?
/// ([ExtendsClause] [WithClause]?)? /// ([ExtendsClause] [WithClause]?)?
/// [ImplementsClause]? /// [ImplementsClause]?
/// '{' [ClassMember]* '}' /// '{' [ClassMember]* '}'
///
/// classModifiers ::= 'sealed' | 'abstract'
///
class ClassDeclarationImpl extends NamedCompilationUnitMemberImpl class ClassDeclarationImpl extends NamedCompilationUnitMemberImpl
implements ClassDeclaration { implements ClassDeclaration {
/// The 'abstract' keyword, or `null` if the keyword was absent. /// The 'abstract' keyword, or `null` if the keyword was absent.
@ -1908,6 +1911,7 @@ class ClassDeclarationImpl extends NamedCompilationUnitMemberImpl
final Token? inlineKeyword; final Token? inlineKeyword;
/// The 'sealed' keyword, or `null` if the keyword was absent. /// The 'sealed' keyword, or `null` if the keyword was absent.
@override
final Token? sealedKeyword; final Token? sealedKeyword;
/// The 'augment' keyword, or `null` if the keyword was absent. /// The 'augment' keyword, or `null` if the keyword was absent.
@ -2127,6 +2131,7 @@ class ClassTypeAliasImpl extends TypeAliasImpl implements ClassTypeAlias {
/// The token for the 'sealed' keyword, or `null` if this is not defining a /// The token for the 'sealed' keyword, or `null` if this is not defining a
/// sealed class. /// sealed class.
@override
final Token? sealedKeyword; final Token? sealedKeyword;
/// The token for the 'augment' keyword, or `null` if this is not defining an /// The token for the 'augment' keyword, or `null` if this is not defining an
@ -8918,7 +8923,7 @@ class MethodInvocationImpl extends InvocationExpressionImpl
/// The declaration of a mixin. /// The declaration of a mixin.
/// ///
/// mixinDeclaration ::= /// mixinDeclaration ::=
/// metadata? 'mixin' [SimpleIdentifier] [TypeParameterList]? /// 'sealed'? metadata? 'mixin' [SimpleIdentifier] [TypeParameterList]?
/// [RequiresClause]? [ImplementsClause]? '{' [ClassMember]* '}' /// [RequiresClause]? [ImplementsClause]? '{' [ClassMember]* '}'
class MixinDeclarationImpl extends NamedCompilationUnitMemberImpl class MixinDeclarationImpl extends NamedCompilationUnitMemberImpl
implements MixinDeclaration { implements MixinDeclaration {
@ -8926,6 +8931,7 @@ class MixinDeclarationImpl extends NamedCompilationUnitMemberImpl
final Token? augmentKeyword; final Token? augmentKeyword;
/// Return the 'sealed' keyword, or `null` if the keyword was absent. /// Return the 'sealed' keyword, or `null` if the keyword was absent.
@override
final Token? sealedKeyword; final Token? sealedKeyword;
@override @override