From 4166375a2c4da7aaaa75aaaada8f9c992cde4e38 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Tue, 10 May 2022 16:29:28 -0700 Subject: [PATCH 1/7] Add JSON colorization types to LSP --- .../SemanticTokens/SemanticTokensHelpers.cs | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs b/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs index cd5c0f0e192f3..6a72231e7db90 100644 --- a/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs +++ b/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; @@ -38,6 +39,19 @@ internal class SemanticTokensHelpers ClassificationTypeNames.ExtensionMethodName, ClassificationTypeNames.FieldName, ClassificationTypeNames.InterfaceName, + + ClassificationTypeNames.JsonArray, + ClassificationTypeNames.JsonComment, + ClassificationTypeNames.JsonConstructorName, + ClassificationTypeNames.JsonKeyword, + ClassificationTypeNames.JsonNumber, + ClassificationTypeNames.JsonObject, + ClassificationTypeNames.JsonOperator, + ClassificationTypeNames.JsonPropertyName, + ClassificationTypeNames.JsonPunctuation, + ClassificationTypeNames.JsonString, + ClassificationTypeNames.JsonText, + ClassificationTypeNames.LabelName, ClassificationTypeNames.LocalName, ClassificationTypeNames.MethodName, @@ -302,17 +316,25 @@ private static int[] ComputeTokens( var lastLineNumber = 0; var lastStartCharacter = 0; - for (var currentClassifiedSpanIndex = 0; currentClassifiedSpanIndex < classifiedSpans.Length; currentClassifiedSpanIndex++) + try + { + for (var currentClassifiedSpanIndex = 0; currentClassifiedSpanIndex < classifiedSpans.Length; currentClassifiedSpanIndex++) + { + currentClassifiedSpanIndex = ComputeNextToken( + lines, ref lastLineNumber, ref lastStartCharacter, classifiedSpans, + currentClassifiedSpanIndex, tokenTypesToIndex, + out var deltaLine, out var startCharacterDelta, out var tokenLength, + out var tokenType, out var tokenModifiers); + + data.AddRange(deltaLine, startCharacterDelta, tokenLength, tokenType, tokenModifiers); + } + } + catch (Exception) { - currentClassifiedSpanIndex = ComputeNextToken( - lines, ref lastLineNumber, ref lastStartCharacter, classifiedSpans, - currentClassifiedSpanIndex, tokenTypesToIndex, - out var deltaLine, out var startCharacterDelta, out var tokenLength, - out var tokenType, out var tokenModifiers); - data.AddRange(deltaLine, startCharacterDelta, tokenLength, tokenType, tokenModifiers); } + return data.ToArray(); } From 1d63baccadf99b7587f284760dd379bdb9614e02 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Tue, 10 May 2022 16:29:58 -0700 Subject: [PATCH 2/7] Add JSON colorization types to LSP --- .../SemanticTokens/SemanticTokensHelpers.cs | 22 ++++++------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs b/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs index 6a72231e7db90..28c5396cf6b73 100644 --- a/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs +++ b/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs @@ -316,25 +316,17 @@ private static int[] ComputeTokens( var lastLineNumber = 0; var lastStartCharacter = 0; - try - { - for (var currentClassifiedSpanIndex = 0; currentClassifiedSpanIndex < classifiedSpans.Length; currentClassifiedSpanIndex++) - { - currentClassifiedSpanIndex = ComputeNextToken( - lines, ref lastLineNumber, ref lastStartCharacter, classifiedSpans, - currentClassifiedSpanIndex, tokenTypesToIndex, - out var deltaLine, out var startCharacterDelta, out var tokenLength, - out var tokenType, out var tokenModifiers); - - data.AddRange(deltaLine, startCharacterDelta, tokenLength, tokenType, tokenModifiers); - } - } - catch (Exception) + for (var currentClassifiedSpanIndex = 0; currentClassifiedSpanIndex < classifiedSpans.Length; currentClassifiedSpanIndex++) { + currentClassifiedSpanIndex = ComputeNextToken( + lines, ref lastLineNumber, ref lastStartCharacter, classifiedSpans, + currentClassifiedSpanIndex, tokenTypesToIndex, + out var deltaLine, out var startCharacterDelta, out var tokenLength, + out var tokenType, out var tokenModifiers); + data.AddRange(deltaLine, startCharacterDelta, tokenLength, tokenType, tokenModifiers); } - return data.ToArray(); } From aa243fb4215fb27eda27a660536a552a2828e57d Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Tue, 10 May 2022 16:30:38 -0700 Subject: [PATCH 3/7] Add JSON colorization types to LSP --- .../Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs b/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs index 28c5396cf6b73..0f135beefbcc7 100644 --- a/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs +++ b/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs @@ -2,7 +2,6 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -using System; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; From 14f9c77254c924c05ec09e110c152a8726f77548 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Tue, 10 May 2022 16:29:28 -0700 Subject: [PATCH 4/7] Add JSON colorization types to LSP --- .../Handler/SemanticTokens/SemanticTokensHelpers.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs b/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs index cd5c0f0e192f3..0f135beefbcc7 100644 --- a/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs +++ b/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs @@ -38,6 +38,19 @@ internal class SemanticTokensHelpers ClassificationTypeNames.ExtensionMethodName, ClassificationTypeNames.FieldName, ClassificationTypeNames.InterfaceName, + + ClassificationTypeNames.JsonArray, + ClassificationTypeNames.JsonComment, + ClassificationTypeNames.JsonConstructorName, + ClassificationTypeNames.JsonKeyword, + ClassificationTypeNames.JsonNumber, + ClassificationTypeNames.JsonObject, + ClassificationTypeNames.JsonOperator, + ClassificationTypeNames.JsonPropertyName, + ClassificationTypeNames.JsonPunctuation, + ClassificationTypeNames.JsonString, + ClassificationTypeNames.JsonText, + ClassificationTypeNames.LabelName, ClassificationTypeNames.LocalName, ClassificationTypeNames.MethodName, From 43abf4af5a339fc89e91d70a4caa5032271012bf Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Fri, 13 May 2022 01:45:10 -0700 Subject: [PATCH 5/7] Add test --- .../SemanticTokensRangeTests.cs | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/src/Features/LanguageServer/ProtocolUnitTests/SemanticTokens/SemanticTokensRangeTests.cs b/src/Features/LanguageServer/ProtocolUnitTests/SemanticTokens/SemanticTokensRangeTests.cs index ae0f70de56044..5c455b7f9fa84 100644 --- a/src/Features/LanguageServer/ProtocolUnitTests/SemanticTokens/SemanticTokensRangeTests.cs +++ b/src/Features/LanguageServer/ProtocolUnitTests/SemanticTokens/SemanticTokensRangeTests.cs @@ -5,6 +5,7 @@ #nullable enable using System; +using System.Collections.Immutable; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -294,5 +295,92 @@ void M() await VerifyNoMultiLineTokens(testLspServer, results).ConfigureAwait(false); Assert.Equal(expectedResults.Data, results); } + + [Fact] + public void TestGetSemanticTokensRange_AssertCustomTokenTypes() + { + Assert.Collection(SemanticTokensHelpers.RoslynCustomTokenTypes, + t => Assert.Equal(ClassificationTypeNames.ClassName, t), + t => Assert.Equal(ClassificationTypeNames.ConstantName, t), + t => Assert.Equal(ClassificationTypeNames.ControlKeyword, t), + t => Assert.Equal(ClassificationTypeNames.DelegateName, t), + t => Assert.Equal(ClassificationTypeNames.EnumMemberName, t), + t => Assert.Equal(ClassificationTypeNames.EnumName, t), + t => Assert.Equal(ClassificationTypeNames.EventName, t), + t => Assert.Equal(ClassificationTypeNames.ExcludedCode, t), + t => Assert.Equal(ClassificationTypeNames.ExtensionMethodName, t), + t => Assert.Equal(ClassificationTypeNames.FieldName, t), + t => Assert.Equal(ClassificationTypeNames.InterfaceName, t), + + t => Assert.Equal(ClassificationTypeNames.JsonArray, t), + t => Assert.Equal(ClassificationTypeNames.JsonComment, t), + t => Assert.Equal(ClassificationTypeNames.JsonConstructorName, t), + t => Assert.Equal(ClassificationTypeNames.JsonKeyword, t), + t => Assert.Equal(ClassificationTypeNames.JsonNumber, t), + t => Assert.Equal(ClassificationTypeNames.JsonObject, t), + t => Assert.Equal(ClassificationTypeNames.JsonOperator, t), + t => Assert.Equal(ClassificationTypeNames.JsonPropertyName, t), + t => Assert.Equal(ClassificationTypeNames.JsonPunctuation, t), + t => Assert.Equal(ClassificationTypeNames.JsonString, t), + t => Assert.Equal(ClassificationTypeNames.JsonText, t), + + t => Assert.Equal(ClassificationTypeNames.LabelName, t), + t => Assert.Equal(ClassificationTypeNames.LocalName, t), + t => Assert.Equal(ClassificationTypeNames.MethodName, t), + t => Assert.Equal(ClassificationTypeNames.ModuleName, t), + t => Assert.Equal(ClassificationTypeNames.NamespaceName, t), + t => Assert.Equal(ClassificationTypeNames.OperatorOverloaded, t), + t => Assert.Equal(ClassificationTypeNames.ParameterName, t), + t => Assert.Equal(ClassificationTypeNames.PropertyName, t), + + // Preprocessor + t => Assert.Equal(ClassificationTypeNames.PreprocessorKeyword, t), + t => Assert.Equal(ClassificationTypeNames.PreprocessorText, t), + + t => Assert.Equal(ClassificationTypeNames.Punctuation, t), + t => Assert.Equal(ClassificationTypeNames.RecordClassName, t), + t => Assert.Equal(ClassificationTypeNames.RecordStructName, t), + + // Regex + t => Assert.Equal(ClassificationTypeNames.RegexAlternation, t), + t => Assert.Equal(ClassificationTypeNames.RegexAnchor, t), + t => Assert.Equal(ClassificationTypeNames.RegexCharacterClass, t), + t => Assert.Equal(ClassificationTypeNames.RegexComment, t), + t => Assert.Equal(ClassificationTypeNames.RegexGrouping, t), + t => Assert.Equal(ClassificationTypeNames.RegexOtherEscape, t), + t => Assert.Equal(ClassificationTypeNames.RegexQuantifier, t), + t => Assert.Equal(ClassificationTypeNames.RegexSelfEscapedCharacter, t), + t => Assert.Equal(ClassificationTypeNames.RegexText, t), + + t => Assert.Equal(ClassificationTypeNames.StringEscapeCharacter, t), + t => Assert.Equal(ClassificationTypeNames.StructName, t), + t => Assert.Equal(ClassificationTypeNames.Text, t), + t => Assert.Equal(ClassificationTypeNames.TypeParameterName, t), + t => Assert.Equal(ClassificationTypeNames.VerbatimStringLiteral, t), + t => Assert.Equal(ClassificationTypeNames.WhiteSpace, t), + + // XML + t => Assert.Equal(ClassificationTypeNames.XmlDocCommentAttributeName, t), + t => Assert.Equal(ClassificationTypeNames.XmlDocCommentAttributeQuotes, t), + t => Assert.Equal(ClassificationTypeNames.XmlDocCommentAttributeValue, t), + t => Assert.Equal(ClassificationTypeNames.XmlDocCommentCDataSection, t), + t => Assert.Equal(ClassificationTypeNames.XmlDocCommentComment, t), + t => Assert.Equal(ClassificationTypeNames.XmlDocCommentDelimiter, t), + t => Assert.Equal(ClassificationTypeNames.XmlDocCommentEntityReference, t), + t => Assert.Equal(ClassificationTypeNames.XmlDocCommentName, t), + t => Assert.Equal(ClassificationTypeNames.XmlDocCommentProcessingInstruction, t), + t => Assert.Equal(ClassificationTypeNames.XmlDocCommentText, t), + t => Assert.Equal(ClassificationTypeNames.XmlLiteralAttributeName, t), + t => Assert.Equal(ClassificationTypeNames.XmlLiteralAttributeQuotes, t), + t => Assert.Equal(ClassificationTypeNames.XmlLiteralAttributeValue, t), + t => Assert.Equal(ClassificationTypeNames.XmlLiteralCDataSection, t), + t => Assert.Equal(ClassificationTypeNames.XmlLiteralComment, t), + t => Assert.Equal(ClassificationTypeNames.XmlLiteralDelimiter, t), + t => Assert.Equal(ClassificationTypeNames.XmlLiteralEmbeddedExpression, t), + t => Assert.Equal(ClassificationTypeNames.XmlLiteralEntityReference, t), + t => Assert.Equal(ClassificationTypeNames.XmlLiteralName, t), + t => Assert.Equal(ClassificationTypeNames.XmlLiteralProcessingInstruction, t), + t => Assert.Equal(ClassificationTypeNames.XmlLiteralText, t)); + } } } From cba6c46468c742a021f048396b4e4940f895fcac Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Fri, 13 May 2022 15:53:53 -0700 Subject: [PATCH 6/7] Update test --- .../SemanticTokens/SemanticTokensHelpers.cs | 6 +- .../SemanticTokensRangeTests.cs | 95 +++---------------- 2 files changed, 16 insertions(+), 85 deletions(-) diff --git a/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs b/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs index 0f135beefbcc7..73281170ddc0f 100644 --- a/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs +++ b/src/Features/LanguageServer/Protocol/Handler/SemanticTokens/SemanticTokensHelpers.cs @@ -111,8 +111,8 @@ internal class SemanticTokensHelpers // TO-DO: Expand this mapping once support for custom token types is added: // https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1085998 - private static readonly Dictionary s_classificationTypeToSemanticTokenTypeMap = - new Dictionary + internal static readonly Dictionary ClassificationTypeToSemanticTokenTypeMap = + new() { [ClassificationTypeNames.Comment] = LSP.SemanticTokenTypes.Comment, [ClassificationTypeNames.Identifier] = LSP.SemanticTokenTypes.Variable, @@ -420,7 +420,7 @@ private static int ComputeNextToken( private static int GetTokenTypeIndex(string classificationType, Dictionary tokenTypesToIndex) { - if (!s_classificationTypeToSemanticTokenTypeMap.TryGetValue(classificationType, out var tokenTypeStr)) + if (!ClassificationTypeToSemanticTokenTypeMap.TryGetValue(classificationType, out var tokenTypeStr)) { tokenTypeStr = classificationType; } diff --git a/src/Features/LanguageServer/ProtocolUnitTests/SemanticTokens/SemanticTokensRangeTests.cs b/src/Features/LanguageServer/ProtocolUnitTests/SemanticTokens/SemanticTokensRangeTests.cs index 5c455b7f9fa84..b6bdf6a91098d 100644 --- a/src/Features/LanguageServer/ProtocolUnitTests/SemanticTokens/SemanticTokensRangeTests.cs +++ b/src/Features/LanguageServer/ProtocolUnitTests/SemanticTokens/SemanticTokensRangeTests.cs @@ -299,88 +299,19 @@ void M() [Fact] public void TestGetSemanticTokensRange_AssertCustomTokenTypes() { - Assert.Collection(SemanticTokensHelpers.RoslynCustomTokenTypes, - t => Assert.Equal(ClassificationTypeNames.ClassName, t), - t => Assert.Equal(ClassificationTypeNames.ConstantName, t), - t => Assert.Equal(ClassificationTypeNames.ControlKeyword, t), - t => Assert.Equal(ClassificationTypeNames.DelegateName, t), - t => Assert.Equal(ClassificationTypeNames.EnumMemberName, t), - t => Assert.Equal(ClassificationTypeNames.EnumName, t), - t => Assert.Equal(ClassificationTypeNames.EventName, t), - t => Assert.Equal(ClassificationTypeNames.ExcludedCode, t), - t => Assert.Equal(ClassificationTypeNames.ExtensionMethodName, t), - t => Assert.Equal(ClassificationTypeNames.FieldName, t), - t => Assert.Equal(ClassificationTypeNames.InterfaceName, t), - - t => Assert.Equal(ClassificationTypeNames.JsonArray, t), - t => Assert.Equal(ClassificationTypeNames.JsonComment, t), - t => Assert.Equal(ClassificationTypeNames.JsonConstructorName, t), - t => Assert.Equal(ClassificationTypeNames.JsonKeyword, t), - t => Assert.Equal(ClassificationTypeNames.JsonNumber, t), - t => Assert.Equal(ClassificationTypeNames.JsonObject, t), - t => Assert.Equal(ClassificationTypeNames.JsonOperator, t), - t => Assert.Equal(ClassificationTypeNames.JsonPropertyName, t), - t => Assert.Equal(ClassificationTypeNames.JsonPunctuation, t), - t => Assert.Equal(ClassificationTypeNames.JsonString, t), - t => Assert.Equal(ClassificationTypeNames.JsonText, t), - - t => Assert.Equal(ClassificationTypeNames.LabelName, t), - t => Assert.Equal(ClassificationTypeNames.LocalName, t), - t => Assert.Equal(ClassificationTypeNames.MethodName, t), - t => Assert.Equal(ClassificationTypeNames.ModuleName, t), - t => Assert.Equal(ClassificationTypeNames.NamespaceName, t), - t => Assert.Equal(ClassificationTypeNames.OperatorOverloaded, t), - t => Assert.Equal(ClassificationTypeNames.ParameterName, t), - t => Assert.Equal(ClassificationTypeNames.PropertyName, t), - - // Preprocessor - t => Assert.Equal(ClassificationTypeNames.PreprocessorKeyword, t), - t => Assert.Equal(ClassificationTypeNames.PreprocessorText, t), - - t => Assert.Equal(ClassificationTypeNames.Punctuation, t), - t => Assert.Equal(ClassificationTypeNames.RecordClassName, t), - t => Assert.Equal(ClassificationTypeNames.RecordStructName, t), - - // Regex - t => Assert.Equal(ClassificationTypeNames.RegexAlternation, t), - t => Assert.Equal(ClassificationTypeNames.RegexAnchor, t), - t => Assert.Equal(ClassificationTypeNames.RegexCharacterClass, t), - t => Assert.Equal(ClassificationTypeNames.RegexComment, t), - t => Assert.Equal(ClassificationTypeNames.RegexGrouping, t), - t => Assert.Equal(ClassificationTypeNames.RegexOtherEscape, t), - t => Assert.Equal(ClassificationTypeNames.RegexQuantifier, t), - t => Assert.Equal(ClassificationTypeNames.RegexSelfEscapedCharacter, t), - t => Assert.Equal(ClassificationTypeNames.RegexText, t), - - t => Assert.Equal(ClassificationTypeNames.StringEscapeCharacter, t), - t => Assert.Equal(ClassificationTypeNames.StructName, t), - t => Assert.Equal(ClassificationTypeNames.Text, t), - t => Assert.Equal(ClassificationTypeNames.TypeParameterName, t), - t => Assert.Equal(ClassificationTypeNames.VerbatimStringLiteral, t), - t => Assert.Equal(ClassificationTypeNames.WhiteSpace, t), - - // XML - t => Assert.Equal(ClassificationTypeNames.XmlDocCommentAttributeName, t), - t => Assert.Equal(ClassificationTypeNames.XmlDocCommentAttributeQuotes, t), - t => Assert.Equal(ClassificationTypeNames.XmlDocCommentAttributeValue, t), - t => Assert.Equal(ClassificationTypeNames.XmlDocCommentCDataSection, t), - t => Assert.Equal(ClassificationTypeNames.XmlDocCommentComment, t), - t => Assert.Equal(ClassificationTypeNames.XmlDocCommentDelimiter, t), - t => Assert.Equal(ClassificationTypeNames.XmlDocCommentEntityReference, t), - t => Assert.Equal(ClassificationTypeNames.XmlDocCommentName, t), - t => Assert.Equal(ClassificationTypeNames.XmlDocCommentProcessingInstruction, t), - t => Assert.Equal(ClassificationTypeNames.XmlDocCommentText, t), - t => Assert.Equal(ClassificationTypeNames.XmlLiteralAttributeName, t), - t => Assert.Equal(ClassificationTypeNames.XmlLiteralAttributeQuotes, t), - t => Assert.Equal(ClassificationTypeNames.XmlLiteralAttributeValue, t), - t => Assert.Equal(ClassificationTypeNames.XmlLiteralCDataSection, t), - t => Assert.Equal(ClassificationTypeNames.XmlLiteralComment, t), - t => Assert.Equal(ClassificationTypeNames.XmlLiteralDelimiter, t), - t => Assert.Equal(ClassificationTypeNames.XmlLiteralEmbeddedExpression, t), - t => Assert.Equal(ClassificationTypeNames.XmlLiteralEntityReference, t), - t => Assert.Equal(ClassificationTypeNames.XmlLiteralName, t), - t => Assert.Equal(ClassificationTypeNames.XmlLiteralProcessingInstruction, t), - t => Assert.Equal(ClassificationTypeNames.XmlLiteralText, t)); + var fields = typeof(ClassificationTypeNames).GetFields(); + foreach (var field in fields) + { + var value = (string?)field.GetValue(null); + if (value is null || + SemanticTokensHelpers.ClassificationTypeToSemanticTokenTypeMap.ContainsKey(value) || + ClassificationTypeNames.AdditiveTypeNames.Contains(value)) + { + continue; + } + + Assert.True(SemanticTokensHelpers.RoslynCustomTokenTypes.Contains(value), $"Missing token type {value}."); + } } } } From 80d427573eaf1fcf6f88ef2deadb138af4ee88e6 Mon Sep 17 00:00:00 2001 From: Allison Chou Date: Tue, 17 May 2022 12:27:18 -0700 Subject: [PATCH 7/7] Change test to theory --- .../SemanticTokensRangeTests.cs | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/Features/LanguageServer/ProtocolUnitTests/SemanticTokens/SemanticTokensRangeTests.cs b/src/Features/LanguageServer/ProtocolUnitTests/SemanticTokens/SemanticTokensRangeTests.cs index b6bdf6a91098d..6141f2da65ab2 100644 --- a/src/Features/LanguageServer/ProtocolUnitTests/SemanticTokens/SemanticTokensRangeTests.cs +++ b/src/Features/LanguageServer/ProtocolUnitTests/SemanticTokens/SemanticTokensRangeTests.cs @@ -5,10 +5,12 @@ #nullable enable using System; +using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using System.Threading; using System.Threading.Tasks; +using Castle.DynamicProxy.Internal; using Microsoft.CodeAnalysis.Classification; using Microsoft.CodeAnalysis.LanguageServer.Handler.SemanticTokens; using Microsoft.VisualStudio.LanguageServer.Protocol; @@ -296,22 +298,15 @@ void M() Assert.Equal(expectedResults.Data, results); } - [Fact] - public void TestGetSemanticTokensRange_AssertCustomTokenTypes() - { - var fields = typeof(ClassificationTypeNames).GetFields(); - foreach (var field in fields) - { - var value = (string?)field.GetValue(null); - if (value is null || - SemanticTokensHelpers.ClassificationTypeToSemanticTokenTypeMap.ContainsKey(value) || - ClassificationTypeNames.AdditiveTypeNames.Contains(value)) - { - continue; - } + [Theory, MemberData(nameof(ClassificationTypeNamesToMatch))] + public void TestGetSemanticTokensRange_AssertCustomTokenTypes(string fieldName) + => Assert.True(SemanticTokensHelpers.RoslynCustomTokenTypes.Contains(fieldName), $"Missing token type {fieldName}."); + + public static IEnumerable ClassificationTypeNamesToMatch => typeof(ClassificationTypeNames).GetAllFields().Where( + field => field.GetValue(null) is string value && + !SemanticTokensHelpers.ClassificationTypeToSemanticTokenTypeMap.ContainsKey(value) && + !ClassificationTypeNames.AdditiveTypeNames.Contains(value) && + value is not ClassificationTypeNames.ReassignedVariable).Select(field => new object[] { (string)field.GetValue(null) }); - Assert.True(SemanticTokensHelpers.RoslynCustomTokenTypes.Contains(value), $"Missing token type {value}."); - } - } } }