Skip to content

Commit

Permalink
Merge pull request #2232 from OmniSharp/bugfix/record-struct-highlight
Browse files Browse the repository at this point in the history
handle RecordStructName in semantic highlighting classification
  • Loading branch information
filipw committed Sep 7, 2021
2 parents 8144773 + 72030e7 commit 4f7ed0f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,16 @@ private static SemanticHighlightSpan CreateSemanticSpan(IEnumerable<ClassifiedRe

var linePos = lines.GetLinePositionSpan(span.TextSpan);

var type = SemanticHighlightClassification.Text;
_classificationMap.TryGetValue(span.ClassificationType, out type);

return new SemanticHighlightSpan
{
StartLine = linePos.Start.Line,
EndLine = linePos.End.Line,
StartColumn = linePos.Start.Character,
EndColumn = linePos.End.Character,
Type = _classificationMap[span.ClassificationType],
Type = type,
Modifiers = modifiers
};
}
Expand All @@ -100,7 +103,7 @@ class ClassifiedResult
}

private static readonly Dictionary<string, SemanticHighlightClassification> _classificationMap =
new Dictionary<string, SemanticHighlightClassification>
new()
{
[ClassificationTypeNames.Comment] = SemanticHighlightClassification.Comment,
[ClassificationTypeNames.ExcludedCode] = SemanticHighlightClassification.ExcludedCode,
Expand All @@ -126,6 +129,7 @@ class ClassifiedResult
[ClassificationTypeNames.InterfaceName] = SemanticHighlightClassification.InterfaceName,
[ClassificationTypeNames.ModuleName] = SemanticHighlightClassification.ModuleName,
[ClassificationTypeNames.StructName] = SemanticHighlightClassification.StructName,
[ClassificationTypeNames.RecordStructName] = SemanticHighlightClassification.StructName,
[ClassificationTypeNames.TypeParameterName] = SemanticHighlightClassification.TypeParameterName,
[ClassificationTypeNames.FieldName] = SemanticHighlightClassification.FieldName,
[ClassificationTypeNames.EnumMemberName] = SemanticHighlightClassification.EnumMemberName,
Expand Down Expand Up @@ -171,7 +175,7 @@ class ClassifiedResult
};

private static readonly Dictionary<string, SemanticHighlightModifier> _modifierMap =
new Dictionary<string, SemanticHighlightModifier>
new()
{
[ClassificationTypeNames.StaticSymbol] = SemanticHighlightModifier.Static,
};
Expand Down
40 changes: 40 additions & 0 deletions tests/OmniSharp.Roslyn.CSharp.Tests/SemanticHighlightFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,45 @@ record R1(string S, int I);
);
}

[Fact]
public async Task SemanticHighlightRecordStructName()
{
var testFile = new TestFile("a.cs", @"
R1 r1 = new R1(string.Empty, 1);
record struct R1(string S, int I);
");

var highlights = await GetSemanticHighlightsForFileAsync(testFile);

AssertSyntax(highlights, testFile.Content.Code, 0,
StructName("R1"),
Local("r1"),
Operator("="),
Keyword("new"),
StructName("R1"),
Punctuation("("),
Keyword("string"),
Operator("."),
Field("Empty", SemanticHighlightModifier.Static),
Punctuation(","),
Number("1"),
Punctuation(")"),
Punctuation(";"),

Keyword("record"),
Keyword("struct"),
StructName("R1"),
Punctuation("("),
Keyword("string"),
Parameter("S"),
Punctuation(","),
Keyword("int"),
Parameter("I"),
Punctuation(")"),
Punctuation(";")
);
}

private Task<SemanticHighlightSpan[]> GetSemanticHighlightsForFileAsync(TestFile testFile)
{
return GetSemanticHighlightsAsync(testFile, range: null);
Expand Down Expand Up @@ -352,6 +391,7 @@ private static void AssertSyntax(SemanticHighlightSpan[] highlights, string code
private static (SemanticHighlightClassification type, string text, SemanticHighlightModifier[] modifiers) Method(string text, params SemanticHighlightModifier[] modifiers) => (SemanticHighlightClassification.MethodName, text, modifiers);
private static (SemanticHighlightClassification type, string text, SemanticHighlightModifier[] modifiers) Local(string text, params SemanticHighlightModifier[] modifiers) => (SemanticHighlightClassification.LocalName, text, modifiers);
private static (SemanticHighlightClassification type, string text, SemanticHighlightModifier[] modifiers) ClassName(string text, params SemanticHighlightModifier[] modifiers) => (SemanticHighlightClassification.ClassName, text, modifiers);
private static (SemanticHighlightClassification type, string text, SemanticHighlightModifier[] modifiers) StructName(string text, params SemanticHighlightModifier[] modifiers) => (SemanticHighlightClassification.StructName, text, modifiers);
private static (SemanticHighlightClassification type, string text, SemanticHighlightModifier[] modifiers) Field(string text, params SemanticHighlightModifier[] modifiers) => (SemanticHighlightClassification.FieldName, text, modifiers);
private static (SemanticHighlightClassification type, string text, SemanticHighlightModifier[] modifiers) Identifier(string text, params SemanticHighlightModifier[] modifiers) => (SemanticHighlightClassification.Identifier, text, modifiers);
private static (SemanticHighlightClassification type, string text, SemanticHighlightModifier[] modifiers) Parameter(string text, params SemanticHighlightModifier[] modifiers) => (SemanticHighlightClassification.ParameterName, text, modifiers);
Expand Down

0 comments on commit 4f7ed0f

Please sign in to comment.