Skip to content

Commit bca554f

Browse files
DanTupcommit-bot@chromium.org
authored andcommitted
[analysis_server] Make LSP semantic tokens consistently use "property" for top levels
+ add a modifier to the "variable" used for import prefixes. Change-Id: If448c3a481a3471a63b58455acaef4ebcfd1f37c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/217981 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent c9b5354 commit bca554f

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ abstract class CustomSemanticTokenModifiers {
102102
/// https://github.com/dart-lang/dart-syntax-highlight/blob/84a8e84f79bc917ebd959a4587349c865dc945e0/grammars/dart.json#L244-L261
103103
static const control = SemanticTokenModifiers('control');
104104

105+
/// A modifier applied to the identifier for an import prefix.
106+
static const importPrefix = SemanticTokenModifiers('importPrefix');
107+
105108
/// A modifier applied to parameter references to indicate they are the name/label
106109
/// to allow theming them differently to the values. For example in the code
107110
/// `foo({String a}) => foo(a: a)` the a's will be differentiated as:
@@ -143,6 +146,7 @@ abstract class CustomSemanticTokenModifiers {
143146
static const values = [
144147
annotation,
145148
control,
149+
importPrefix,
146150
label,
147151
constructor,
148152
escape,

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ final highlightRegionTokenModifiers =
2121
HighlightRegionType.DYNAMIC_PARAMETER_DECLARATION: {
2222
SemanticTokenModifiers.declaration
2323
},
24+
HighlightRegionType.IMPORT_PREFIX: {
25+
CustomSemanticTokenModifiers.importPrefix,
26+
},
2427
HighlightRegionType.INSTANCE_FIELD_DECLARATION: {
2528
SemanticTokenModifiers.declaration
2629
},
@@ -142,9 +145,9 @@ final highlightRegionTokenTypes = {
142145
HighlightRegionType.TOP_LEVEL_GETTER_REFERENCE: SemanticTokenTypes.property,
143146
HighlightRegionType.TOP_LEVEL_SETTER_DECLARATION: SemanticTokenTypes.property,
144147
HighlightRegionType.TOP_LEVEL_SETTER_REFERENCE: SemanticTokenTypes.property,
145-
HighlightRegionType.TOP_LEVEL_VARIABLE: SemanticTokenTypes.variable,
148+
HighlightRegionType.TOP_LEVEL_VARIABLE: SemanticTokenTypes.property,
146149
HighlightRegionType.TOP_LEVEL_VARIABLE_DECLARATION:
147-
SemanticTokenTypes.variable,
150+
SemanticTokenTypes.property,
148151
HighlightRegionType.TYPE_ALIAS: SemanticTokenTypes.type,
149152
HighlightRegionType.TYPE_NAME_DYNAMIC: SemanticTokenTypes.type,
150153
HighlightRegionType.TYPE_PARAMETER: SemanticTokenTypes.typeParameter,

pkg/analysis_server/test/lsp/semantic_tokens_test.dart

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ class SemanticTokensTest extends AbstractLspAnalysisServerTest {
9090
_Token('import', SemanticTokenTypes.keyword),
9191
_Token("'other_file.dart'", SemanticTokenTypes.string),
9292
_Token('as', SemanticTokenTypes.keyword),
93-
_Token('other', SemanticTokenTypes.variable),
93+
_Token('other', SemanticTokenTypes.variable,
94+
[CustomSemanticTokenModifiers.importPrefix]),
9495
_Token('@', CustomSemanticTokenTypes.annotation),
9596
_Token('a', SemanticTokenTypes.property,
9697
[CustomSemanticTokenModifiers.annotation]),
@@ -117,14 +118,16 @@ class SemanticTokensTest extends AbstractLspAnalysisServerTest {
117118
[CustomSemanticTokenModifiers.constructor]),
118119
_Token(')', CustomSemanticTokenTypes.annotation),
119120
_Token('@', CustomSemanticTokenTypes.annotation),
120-
_Token('other', SemanticTokenTypes.variable),
121+
_Token('other', SemanticTokenTypes.variable,
122+
[CustomSemanticTokenModifiers.importPrefix]),
121123
_Token('.', CustomSemanticTokenTypes.annotation),
122124
_Token('C', SemanticTokenTypes.class_,
123125
[CustomSemanticTokenModifiers.annotation]),
124126
_Token('(', CustomSemanticTokenTypes.annotation),
125127
_Token(')', CustomSemanticTokenTypes.annotation),
126128
_Token('@', CustomSemanticTokenTypes.annotation),
127-
_Token('other', SemanticTokenTypes.variable),
129+
_Token('other', SemanticTokenTypes.variable,
130+
[CustomSemanticTokenModifiers.importPrefix]),
128131
_Token('.', CustomSemanticTokenTypes.annotation),
129132
_Token('C', SemanticTokenTypes.class_,
130133
[CustomSemanticTokenModifiers.annotation]),
@@ -214,26 +217,26 @@ class SemanticTokensTest extends AbstractLspAnalysisServerTest {
214217
_Token('MyClass', SemanticTokenTypes.class_,
215218
[CustomSemanticTokenModifiers.constructor]),
216219
_Token('final', SemanticTokenTypes.keyword),
217-
_Token('a', SemanticTokenTypes.variable,
220+
_Token('a', SemanticTokenTypes.property,
218221
[SemanticTokenModifiers.declaration]),
219222
_Token('MyClass', SemanticTokenTypes.class_,
220223
[CustomSemanticTokenModifiers.constructor]),
221224
_Token('final', SemanticTokenTypes.keyword),
222-
_Token('b', SemanticTokenTypes.variable,
225+
_Token('b', SemanticTokenTypes.property,
223226
[SemanticTokenModifiers.declaration]),
224227
_Token('MyClass', SemanticTokenTypes.class_,
225228
[CustomSemanticTokenModifiers.constructor]),
226229
_Token('named', SemanticTokenTypes.method,
227230
[CustomSemanticTokenModifiers.constructor]),
228231
_Token('final', SemanticTokenTypes.keyword),
229-
_Token('c', SemanticTokenTypes.variable,
232+
_Token('c', SemanticTokenTypes.property,
230233
[SemanticTokenModifiers.declaration]),
231234
_Token('MyClass', SemanticTokenTypes.class_,
232235
[CustomSemanticTokenModifiers.constructor]),
233236
_Token('factory', SemanticTokenTypes.method,
234237
[CustomSemanticTokenModifiers.constructor]),
235238
_Token('final', SemanticTokenTypes.keyword),
236-
_Token('d', SemanticTokenTypes.variable,
239+
_Token('d', SemanticTokenTypes.property,
237240
[SemanticTokenModifiers.declaration]),
238241
_Token('MyClass', SemanticTokenTypes.class_),
239242
_Token('named', SemanticTokenTypes.method,
@@ -584,7 +587,7 @@ class SemanticTokensTest extends AbstractLspAnalysisServerTest {
584587
plugin.HighlightRegion(plugin.HighlightRegionType.CLASS, 0, 5),
585588
plugin.HighlightRegion(plugin.HighlightRegionType.LITERAL_STRING, 6, 6),
586589
plugin.HighlightRegion(
587-
plugin.HighlightRegionType.TOP_LEVEL_VARIABLE_DECLARATION, 13, 8),
590+
plugin.HighlightRegionType.LOCAL_VARIABLE_DECLARATION, 13, 8),
588591
],
589592
);
590593
configureTestPlugin(notification: pluginResult.toNotification());
@@ -684,11 +687,11 @@ class SemanticTokensTest extends AbstractLspAnalysisServerTest {
684687
}
685688

686689
Future<void> test_lastLine_code() async {
687-
final content = 'String var;';
690+
final content = 'String bar;';
688691

689692
final expected = [
690693
_Token('String', SemanticTokenTypes.class_),
691-
_Token('var', SemanticTokenTypes.variable,
694+
_Token('bar', SemanticTokenTypes.property,
692695
[SemanticTokenModifiers.declaration]),
693696
];
694697

@@ -1032,12 +1035,12 @@ multi
10321035
_Token('c', SemanticTokenTypes.parameter),
10331036

10341037
_Token('const', SemanticTokenTypes.keyword),
1035-
_Token('string1', SemanticTokenTypes.variable,
1038+
_Token('string1', SemanticTokenTypes.property,
10361039
[SemanticTokenModifiers.declaration]),
10371040
_Token("'test'", SemanticTokenTypes.string),
10381041

10391042
_Token('const', SemanticTokenTypes.keyword),
1040-
_Token('string2', SemanticTokenTypes.variable,
1043+
_Token('string2', SemanticTokenTypes.property,
10411044
[SemanticTokenModifiers.declaration]),
10421045
_Token(r"'test1 ", SemanticTokenTypes.string),
10431046
_Token(r'$', CustomSemanticTokenTypes.source,
@@ -1059,12 +1062,12 @@ multi
10591062

10601063
// string3 is raw and should be treated as a single string.
10611064
_Token('const', SemanticTokenTypes.keyword),
1062-
_Token('string3', SemanticTokenTypes.variable,
1065+
_Token('string3', SemanticTokenTypes.property,
10631066
[SemanticTokenModifiers.declaration]),
10641067
_Token(r"r'$string1 ${string1.length}'", SemanticTokenTypes.string),
10651068
_Token('const', SemanticTokenTypes.keyword),
10661069

1067-
_Token('string4', SemanticTokenTypes.variable,
1070+
_Token('string4', SemanticTokenTypes.property,
10681071
[SemanticTokenModifiers.declaration]),
10691072
_Token("'''\n", SemanticTokenTypes.string),
10701073
_Token('multi\n', SemanticTokenTypes.string),
@@ -1092,7 +1095,7 @@ const string3 = 'unicode \u1234\u123499\u{123456}\u{12345699}';
10921095

10931096
final expected = [
10941097
_Token('const', SemanticTokenTypes.keyword),
1095-
_Token('string1', SemanticTokenTypes.variable,
1098+
_Token('string1', SemanticTokenTypes.property,
10961099
[SemanticTokenModifiers.declaration]),
10971100
_Token("'it", SemanticTokenTypes.string),
10981101
_Token(r"\'", SemanticTokenTypes.string,
@@ -1104,7 +1107,7 @@ const string3 = 'unicode \u1234\u123499\u{123456}\u{12345699}';
11041107
[CustomSemanticTokenModifiers.escape]),
11051108
_Token(r"'", SemanticTokenTypes.string),
11061109
_Token('const', SemanticTokenTypes.keyword),
1107-
_Token('string2', SemanticTokenTypes.variable,
1110+
_Token('string2', SemanticTokenTypes.property,
11081111
[SemanticTokenModifiers.declaration]),
11091112
_Token("'hex ", SemanticTokenTypes.string),
11101113
_Token(r'\x12', SemanticTokenTypes.string,
@@ -1114,7 +1117,7 @@ const string3 = 'unicode \u1234\u123499\u{123456}\u{12345699}';
11141117
// The 99 is not part of the escape
11151118
_Token("99'", SemanticTokenTypes.string),
11161119
_Token('const', SemanticTokenTypes.keyword),
1117-
_Token('string3', SemanticTokenTypes.variable,
1120+
_Token('string3', SemanticTokenTypes.property,
11181121
[SemanticTokenModifiers.declaration]),
11191122
_Token("'unicode ", SemanticTokenTypes.string),
11201123
_Token(r'\u1234', SemanticTokenTypes.string,
@@ -1149,13 +1152,20 @@ const string3 = 'unicode \u1234\u123499\u{123456}\u{12345699}';
11491152
bool get abc => true;
11501153
11511154
final funcTearOff = func;
1155+
1156+
void main() {
1157+
strings;
1158+
func;
1159+
abc;
1160+
funcTearOff;
1161+
}
11521162
''';
11531163

11541164
final expected = [
11551165
_Token('/// strings docs', SemanticTokenTypes.comment,
11561166
[SemanticTokenModifiers.documentation]),
11571167
_Token('const', SemanticTokenTypes.keyword),
1158-
_Token('strings', SemanticTokenTypes.variable,
1168+
_Token('strings', SemanticTokenTypes.property,
11591169
[SemanticTokenModifiers.declaration]),
11601170
_Token('String', SemanticTokenTypes.class_),
11611171
_Token('"test"', SemanticTokenTypes.string),
@@ -1179,9 +1189,17 @@ const string3 = 'unicode \u1234\u123499\u{123456}\u{12345699}';
11791189
[SemanticTokenModifiers.declaration]),
11801190
_Token('true', CustomSemanticTokenTypes.boolean),
11811191
_Token('final', SemanticTokenTypes.keyword),
1182-
_Token('funcTearOff', SemanticTokenTypes.variable,
1192+
_Token('funcTearOff', SemanticTokenTypes.property,
11831193
[SemanticTokenModifiers.declaration]),
11841194
_Token('func', SemanticTokenTypes.function),
1195+
_Token('void', SemanticTokenTypes.keyword,
1196+
[CustomSemanticTokenModifiers.void_]),
1197+
_Token('main', SemanticTokenTypes.function,
1198+
[SemanticTokenModifiers.declaration, SemanticTokenModifiers.static]),
1199+
_Token('strings', SemanticTokenTypes.property),
1200+
_Token('func', SemanticTokenTypes.function),
1201+
_Token('abc', SemanticTokenTypes.property),
1202+
_Token('funcTearOff', SemanticTokenTypes.property),
11851203
];
11861204

11871205
await initialize();

0 commit comments

Comments
 (0)