-
Notifications
You must be signed in to change notification settings - Fork 419
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
{ | ||
|
@@ -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); | ||
|
||
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()))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
{ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.