[analyzer] Tag constructor names as methods instead of classes in LSP semantic tokens

Fixes https://github.com/Dart-Code/Dart-Code/issues/3374.

Change-Id: Icb6e6b01e3abd77aa9da02ac9d9c9d62b77e2c21
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/201831
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Danny Tuppeny 2021-06-01 15:22:23 +00:00 committed by commit-bot@chromium.org
parent af957d9578
commit 10b83a4882
3 changed files with 35 additions and 6 deletions

View file

@ -160,6 +160,8 @@ class DartUnitHighlightsComputer {
}
// prepare type
HighlightRegionType type;
SemanticTokenTypes? semanticType;
Set<SemanticTokenModifiers>? semanticModifiers;
var parent = node.parent;
var grandParent = parent?.parent;
if (parent is TypeName &&
@ -167,13 +169,20 @@ class DartUnitHighlightsComputer {
grandParent.parent is InstanceCreationExpression) {
// new Class()
type = HighlightRegionType.CONSTRUCTOR;
semanticType = SemanticTokenTypes.class_;
semanticModifiers = {CustomSemanticTokenModifiers.constructor};
} else if (element.isEnum) {
type = HighlightRegionType.ENUM;
} else {
type = HighlightRegionType.CLASS;
}
// add region
return _addRegion_node(node, type);
return _addRegion_node(
node,
type,
semanticTokenType: semanticType,
semanticTokenModifiers: semanticModifiers,
);
}
bool _addIdentifierRegion_constructor(SimpleIdentifier node) {
@ -181,7 +190,14 @@ class DartUnitHighlightsComputer {
if (element is! ConstructorElement) {
return false;
}
return _addRegion_node(node, HighlightRegionType.CONSTRUCTOR);
return _addRegion_node(
node,
HighlightRegionType.CONSTRUCTOR,
// For semantic tokens, constructor names are coloured like methods but
// have a modifier applied.
semanticTokenType: SemanticTokenTypes.method,
semanticTokenModifiers: {CustomSemanticTokenModifiers.constructor},
);
}
bool _addIdentifierRegion_dynamicLocal(SimpleIdentifier node) {

View file

@ -15,7 +15,6 @@ final highlightRegionTokenModifiers =
HighlightRegionType.COMMENT_DOCUMENTATION: {
SemanticTokenModifiers.documentation
},
HighlightRegionType.CONSTRUCTOR: {CustomSemanticTokenModifiers.constructor},
HighlightRegionType.DYNAMIC_LOCAL_VARIABLE_DECLARATION: {
SemanticTokenModifiers.declaration
},
@ -88,7 +87,6 @@ final highlightRegionTokenTypes = {
HighlightRegionType.COMMENT_BLOCK: SemanticTokenTypes.comment,
HighlightRegionType.COMMENT_DOCUMENTATION: SemanticTokenTypes.comment,
HighlightRegionType.COMMENT_END_OF_LINE: SemanticTokenTypes.comment,
HighlightRegionType.CONSTRUCTOR: SemanticTokenTypes.class_,
HighlightRegionType.DYNAMIC_LOCAL_VARIABLE_DECLARATION:
SemanticTokenTypes.variable,
HighlightRegionType.DYNAMIC_LOCAL_VARIABLE_REFERENCE:

View file

@ -87,10 +87,12 @@ class SemanticTokensTest extends AbstractLspAnalysisServerTest {
class MyClass {
MyClass();
MyClass.named();
factory MyClass.factory() => MyClass();
}
final a = MyClass();
final b = MyClass.named();
final c = MyClass.factory();
''';
final expected = [
@ -98,7 +100,13 @@ class SemanticTokensTest extends AbstractLspAnalysisServerTest {
_Token('MyClass', SemanticTokenTypes.class_),
_Token('MyClass', SemanticTokenTypes.class_),
_Token('MyClass', SemanticTokenTypes.class_),
_Token('named', SemanticTokenTypes.class_,
_Token('named', SemanticTokenTypes.method,
[CustomSemanticTokenModifiers.constructor]),
_Token('factory', SemanticTokenTypes.keyword),
_Token('MyClass', SemanticTokenTypes.class_),
_Token('factory', SemanticTokenTypes.method,
[CustomSemanticTokenModifiers.constructor]),
_Token('MyClass', SemanticTokenTypes.class_,
[CustomSemanticTokenModifiers.constructor]),
_Token('final', SemanticTokenTypes.keyword),
_Token('a', SemanticTokenTypes.variable,
@ -110,7 +118,14 @@ class SemanticTokensTest extends AbstractLspAnalysisServerTest {
[SemanticTokenModifiers.declaration]),
_Token('MyClass', SemanticTokenTypes.class_,
[CustomSemanticTokenModifiers.constructor]),
_Token('named', SemanticTokenTypes.class_,
_Token('named', SemanticTokenTypes.method,
[CustomSemanticTokenModifiers.constructor]),
_Token('final', SemanticTokenTypes.keyword),
_Token('c', SemanticTokenTypes.variable,
[SemanticTokenModifiers.declaration]),
_Token('MyClass', SemanticTokenTypes.class_,
[CustomSemanticTokenModifiers.constructor]),
_Token('factory', SemanticTokenTypes.method,
[CustomSemanticTokenModifiers.constructor])
];