Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use core LSP TokenTypes where possible and validate token names #2548

Merged
merged 1 commit into from
Jul 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.100-preview.3.23178.7",
"version": "8.0.100-preview.4.23260.5",
"rollForward": "patch"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Models.SemanticHighlight;
using OmniSharp.Roslyn.CSharp.Services.SemanticHighlight;
using static OmniSharp.LanguageServerProtocol.Helpers;

namespace OmniSharp.LanguageServerProtocol.Handlers
{
class OmniSharpSemanticTokensHandler : SemanticTokensHandlerBase
internal class OmniSharpSemanticTokensHandler : SemanticTokensHandlerBase
{
public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
{
Expand All @@ -22,34 +21,49 @@ public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
yield return new OmniSharpSemanticTokensHandler(handler, selector);
}

internal static readonly ImmutableDictionary<SemanticHighlightClassification, SemanticTokenType> _coreTokenMap =
new Dictionary<SemanticHighlightClassification, SemanticTokenType>()
{
[SemanticHighlightClassification.Comment] = SemanticTokenType.Comment,
[SemanticHighlightClassification.Keyword] = SemanticTokenType.Keyword,
[SemanticHighlightClassification.NumericLiteral] = SemanticTokenType.Number,
[SemanticHighlightClassification.Operator] = SemanticTokenType.Operator,
[SemanticHighlightClassification.StringLiteral] = SemanticTokenType.String,
[SemanticHighlightClassification.ClassName] = SemanticTokenType.Class,
[SemanticHighlightClassification.StructName] = SemanticTokenType.Struct,
[SemanticHighlightClassification.NamespaceName] = SemanticTokenType.Namespace,
[SemanticHighlightClassification.EnumName] = SemanticTokenType.Enum,
[SemanticHighlightClassification.InterfaceName] = SemanticTokenType.Interface,
[SemanticHighlightClassification.TypeParameterName] = SemanticTokenType.TypeParameter,
[SemanticHighlightClassification.ParameterName] = SemanticTokenType.Parameter,
[SemanticHighlightClassification.LocalName] = SemanticTokenType.Variable,
[SemanticHighlightClassification.PropertyName] = SemanticTokenType.Property,
[SemanticHighlightClassification.MethodName] = SemanticTokenType.Method,
[SemanticHighlightClassification.EnumMemberName] = SemanticTokenType.EnumMember,
[SemanticHighlightClassification.EventName] = SemanticTokenType.Event,
[SemanticHighlightClassification.PreprocessorKeyword] = SemanticTokenType.Macro,
[SemanticHighlightClassification.LabelName] = SemanticTokenType.Label,
}.ToImmutableDictionary();

private readonly Mef.IRequestHandler<SemanticHighlightRequest, SemanticHighlightResponse> _definitionHandler;
private readonly DocumentSelector _documentSelector;

private static string MakeLSPCompatibleString(string str)
=> char.ToLower(str[0]) + str.Substring(1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
=> char.ToLower(str[0]) + str.Substring(1);
=> char.ToLower(str[0]) + str[1..];

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will also say that I spent a minute trying to figure out if there was a way to do this in one simple line without doing a substring at all, using spans, but I would have to make a local out of char.ToLower(str[0]) to get that to work.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have to pull in Range to make your suggestion work for net472. Maybe we can clean this all up one day.


private static readonly ImmutableDictionary<SemanticHighlightClassification, SemanticTokenType> _tokenTypes
= SemanticHighlightService._classificationMap
.OrderBy(kvp => kvp.Value)
.Aggregate(
new Dictionary<SemanticHighlightClassification, SemanticTokenType>(),
(dictionary, kvp) =>
{
if (!dictionary.ContainsKey(kvp.Value))
dictionary.Add(kvp.Value, new SemanticTokenType(kvp.Key));
return dictionary;
})
.ToImmutableDictionary();
= System.Enum.GetValues(typeof(SemanticHighlightClassification))
.Cast<SemanticHighlightClassification>()
.ToImmutableDictionary(value => value,
// Use Core LSP token types where possible
value => _coreTokenMap.ContainsKey(value)
? _coreTokenMap[value]
: new SemanticTokenType(MakeLSPCompatibleString(value.ToString())));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously we had been using the Roslyn ClassificationTypeName which contained spaces. This changes us to use LSP TokenTypes when possible, otherwise SemanticHighlightClassification EnumMember names fixed up to begin with a lowercase letter.


private static readonly ImmutableDictionary<SemanticHighlightModifier, SemanticTokenModifier> _tokenModifiers
= SemanticHighlightService._modifierMap
.OrderBy(kvp => kvp.Value)
.Aggregate(
new Dictionary<SemanticHighlightModifier, SemanticTokenModifier>(),
(dictionary, kvp) =>
{
if (!dictionary.ContainsKey(kvp.Value))
dictionary.Add(kvp.Value, new SemanticTokenModifier(kvp.Key));
return dictionary;
})
.ToImmutableDictionary();
= System.Enum.GetValues(typeof(SemanticHighlightModifier))
.Cast<SemanticHighlightModifier>()
.ToImmutableDictionary(value => value, value => new SemanticTokenModifier(MakeLSPCompatibleString(value.ToString())));

private readonly SemanticTokensLegend _legend = new()
{
Expand Down
Loading