Skip to content

Commit 9d015fe

Browse files
DanTupcommit-bot@chromium.org
authored andcommitted
[analyzer] Add a "constructor" modifier for LSP semantic tokens
Fixes dart-lang/sdk#45861. Change-Id: Ib97e14d6e23dfd0c48e1ad8abb18b42a4c2f9cb1 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/197401 Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
1 parent 4f5b72c commit 9d015fe

File tree

3 files changed

+54
-5
lines changed

3 files changed

+54
-5
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,13 @@ abstract class CustomSemanticTokenModifiers {
104104
/// - parameter
105105
static const label = SemanticTokenModifiers('label');
106106

107+
/// A modifier applied to constructors to allow colouring them differently
108+
/// to class names that are not constructors.
109+
static const constructor = SemanticTokenModifiers('constructor');
110+
107111
/// All custom semantic token modifiers, used to populate the LSP Legend which must
108112
/// include all used modifiers.
109-
static const values = [control, label];
113+
static const values = [control, label, constructor];
110114
}
111115

112116
abstract class CustomSemanticTokenTypes {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ final highlightRegionTokenModifiers =
1515
HighlightRegionType.COMMENT_DOCUMENTATION: {
1616
SemanticTokenModifiers.documentation
1717
},
18+
HighlightRegionType.CONSTRUCTOR: {CustomSemanticTokenModifiers.constructor},
1819
HighlightRegionType.DYNAMIC_LOCAL_VARIABLE_DECLARATION: {
1920
SemanticTokenModifiers.declaration
2021
},

pkg/analysis_server/test/lsp/semantic_tokens_test.dart

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,46 @@ class SemanticTokensTest extends AbstractLspAnalysisServerTest {
8282
expect(decoded, equals(expected));
8383
}
8484

85+
Future<void> test_class_constructors() async {
86+
final content = '''
87+
class MyClass {
88+
MyClass();
89+
MyClass.named();
90+
}
91+
92+
final a = MyClass();
93+
final b = MyClass.named();
94+
''';
95+
96+
final expected = [
97+
_Token('class', SemanticTokenTypes.keyword),
98+
_Token('MyClass', SemanticTokenTypes.class_),
99+
_Token('MyClass', SemanticTokenTypes.class_),
100+
_Token('MyClass', SemanticTokenTypes.class_),
101+
_Token('named', SemanticTokenTypes.class_,
102+
[CustomSemanticTokenModifiers.constructor]),
103+
_Token('final', SemanticTokenTypes.keyword),
104+
_Token('a', SemanticTokenTypes.variable,
105+
[SemanticTokenModifiers.declaration]),
106+
_Token('MyClass', SemanticTokenTypes.class_,
107+
[CustomSemanticTokenModifiers.constructor]),
108+
_Token('final', SemanticTokenTypes.keyword),
109+
_Token('b', SemanticTokenTypes.variable,
110+
[SemanticTokenModifiers.declaration]),
111+
_Token('MyClass', SemanticTokenTypes.class_,
112+
[CustomSemanticTokenModifiers.constructor]),
113+
_Token('named', SemanticTokenTypes.class_,
114+
[CustomSemanticTokenModifiers.constructor])
115+
];
116+
117+
await initialize();
118+
await openFile(mainFileUri, withoutMarkers(content));
119+
120+
final tokens = await getSemanticTokens(mainFileUri);
121+
final decoded = decodeSemanticTokens(content, tokens);
122+
expect(decoded, equals(expected));
123+
}
124+
85125
Future<void> test_class_fields() async {
86126
final content = '''
87127
class MyClass {
@@ -119,7 +159,8 @@ class SemanticTokensTest extends AbstractLspAnalysisServerTest {
119159
_Token('final', SemanticTokenTypes.keyword),
120160
_Token('a', SemanticTokenTypes.variable,
121161
[SemanticTokenModifiers.declaration]),
122-
_Token('MyClass', SemanticTokenTypes.class_),
162+
_Token('MyClass', SemanticTokenTypes.class_,
163+
[CustomSemanticTokenModifiers.constructor]),
123164
_Token('print', SemanticTokenTypes.function),
124165
_Token('a', SemanticTokenTypes.variable),
125166
_Token('myField', SemanticTokenTypes.property),
@@ -197,7 +238,8 @@ class SemanticTokensTest extends AbstractLspAnalysisServerTest {
197238
_Token('final', SemanticTokenTypes.keyword),
198239
_Token('a', SemanticTokenTypes.variable,
199240
[SemanticTokenModifiers.declaration]),
200-
_Token('MyClass', SemanticTokenTypes.class_),
241+
_Token('MyClass', SemanticTokenTypes.class_,
242+
[CustomSemanticTokenModifiers.constructor]),
201243
_Token('print', SemanticTokenTypes.function),
202244
_Token('a', SemanticTokenTypes.variable),
203245
_Token('myGetter', SemanticTokenTypes.property),
@@ -255,7 +297,8 @@ class SemanticTokensTest extends AbstractLspAnalysisServerTest {
255297
_Token('final', SemanticTokenTypes.keyword),
256298
_Token('a', SemanticTokenTypes.variable,
257299
[SemanticTokenModifiers.declaration]),
258-
_Token('MyClass', SemanticTokenTypes.class_),
300+
_Token('MyClass', SemanticTokenTypes.class_,
301+
[CustomSemanticTokenModifiers.constructor]),
259302
_Token('a', SemanticTokenTypes.variable),
260303
_Token('myMethod', SemanticTokenTypes.method),
261304
_Token('MyClass', SemanticTokenTypes.class_),
@@ -443,7 +486,8 @@ class SemanticTokensTest extends AbstractLspAnalysisServerTest {
443486
_Token('a', SemanticTokenTypes.variable,
444487
[SemanticTokenModifiers.declaration]),
445488
_Token('new', SemanticTokenTypes.keyword),
446-
_Token('Object', SemanticTokenTypes.class_),
489+
_Token('Object', SemanticTokenTypes.class_,
490+
[CustomSemanticTokenModifiers.constructor]),
447491
_Token('await', SemanticTokenTypes.keyword,
448492
[CustomSemanticTokenModifiers.control]),
449493
_Token('null', SemanticTokenTypes.keyword),

0 commit comments

Comments
 (0)