-
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
Add missing LSP Handlers #2463
Merged
Merged
Add missing LSP Handlers #2463
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
99347ef
Update DocumentOnTypeFormatting triggers
JoeRobich c8f1bef
Sort LSP Hander registration
JoeRobich 0505f2f
Add LSP SemanticTokensHandler
JoeRobich 0b7cbf2
Add LSP FoldingRangeHandler
JoeRobich 89e2cec
Add LSP DocumentHighlightHandler
JoeRobich 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
69 changes: 69 additions & 0 deletions
69
src/OmniSharp.LanguageServerProtocol/Handlers/OmniSharpDocumentHighlightHandler.cs
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using OmniSharp.Extensions.JsonRpc; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Document; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Models; | ||
using OmniSharp.Models; | ||
using OmniSharp.Models.FindUsages; | ||
|
||
namespace OmniSharp.LanguageServerProtocol.Handlers | ||
{ | ||
internal sealed class OmniSharpDocumentHighlightHandler : DocumentHighlightHandlerBase | ||
{ | ||
public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers) | ||
{ | ||
foreach (var (selector, handler) in handlers | ||
.OfType<Mef.IRequestHandler<FindUsagesRequest, QuickFixResponse>>()) | ||
if (handler != null) | ||
yield return new OmniSharpDocumentHighlightHandler(handler, selector); | ||
} | ||
|
||
private readonly Mef.IRequestHandler<FindUsagesRequest, QuickFixResponse> _findUsagesHandler; | ||
private readonly DocumentSelector _documentSelector; | ||
|
||
public OmniSharpDocumentHighlightHandler(Mef.IRequestHandler<FindUsagesRequest, QuickFixResponse> findUsagesHandler, DocumentSelector documentSelector) | ||
{ | ||
_findUsagesHandler = findUsagesHandler; | ||
_documentSelector = documentSelector; | ||
} | ||
|
||
public override async Task<DocumentHighlightContainer> Handle(DocumentHighlightParams request, CancellationToken token) | ||
{ | ||
// TODO: Utilize Roslyn ExternalAccess to take advantage of HighlightingService. | ||
|
||
var omnisharpRequest = new FindUsagesRequest | ||
{ | ||
FileName = Helpers.FromUri(request.TextDocument.Uri), | ||
Column = Convert.ToInt32(request.Position.Character), | ||
Line = Convert.ToInt32(request.Position.Line), | ||
OnlyThisFile = true, | ||
ExcludeDefinition = false | ||
}; | ||
|
||
var omnisharpResponse = await _findUsagesHandler.Handle(omnisharpRequest); | ||
|
||
if (omnisharpResponse.QuickFixes is null) | ||
{ | ||
return new DocumentHighlightContainer(); | ||
} | ||
|
||
return new DocumentHighlightContainer(omnisharpResponse.QuickFixes.Select(x => new DocumentHighlight | ||
{ | ||
Kind = DocumentHighlightKind.Read, | ||
Range = x.ToRange() | ||
})); | ||
} | ||
|
||
protected override DocumentHighlightRegistrationOptions CreateRegistrationOptions(DocumentHighlightCapability capability, ClientCapabilities clientCapabilities) | ||
{ | ||
return new DocumentHighlightRegistrationOptions() | ||
{ | ||
DocumentSelector = _documentSelector | ||
}; | ||
} | ||
} | ||
} |
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
75 changes: 75 additions & 0 deletions
75
src/OmniSharp.LanguageServerProtocol/Handlers/OmniSharpFoldingRangeHandler.cs
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using OmniSharp.Extensions.JsonRpc; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Document; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Models; | ||
using OmniSharp.Models.V2; | ||
using static OmniSharp.LanguageServerProtocol.Helpers; | ||
|
||
namespace OmniSharp.LanguageServerProtocol.Handlers | ||
{ | ||
class OmniSharpFoldingRangenHandler : FoldingRangeHandlerBase | ||
{ | ||
public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers) | ||
{ | ||
foreach (var (selector, handler) in handlers.OfType<Mef.IRequestHandler<BlockStructureRequest, BlockStructureResponse>>()) | ||
if (handler != null) | ||
yield return new OmniSharpFoldingRangenHandler(handler, selector); | ||
} | ||
|
||
private readonly Mef.IRequestHandler<BlockStructureRequest, BlockStructureResponse> _definitionHandler; | ||
private readonly DocumentSelector _documentSelector; | ||
|
||
public OmniSharpFoldingRangenHandler(Mef.IRequestHandler<BlockStructureRequest, BlockStructureResponse> definitionHandler, DocumentSelector documentSelector) | ||
{ | ||
_definitionHandler = definitionHandler; | ||
_documentSelector = documentSelector; | ||
} | ||
|
||
public override async Task<Container<FoldingRange>> Handle(FoldingRangeRequestParam request, CancellationToken token) | ||
{ | ||
var omnisharpRequest = new BlockStructureRequest() | ||
{ | ||
FileName = FromUri(request.TextDocument.Uri) | ||
}; | ||
|
||
var omnisharpResponse = await _definitionHandler.Handle(omnisharpRequest); | ||
|
||
if (omnisharpResponse.Spans is null) | ||
{ | ||
return new Container<FoldingRange>(); | ||
} | ||
|
||
return new Container<FoldingRange>(omnisharpResponse.Spans.Select(block => new FoldingRange() | ||
{ | ||
StartLine = block.Range.Start.Line, | ||
StartCharacter = block.Range.Start.Column, | ||
EndLine = block.Range.End.Line, | ||
EndCharacter = block.Range.End.Column, | ||
Kind = ConvertKind(block.Kind), | ||
})); | ||
} | ||
|
||
private static FoldingRangeKind? ConvertKind(string kind) | ||
{ | ||
return kind switch | ||
{ | ||
CodeFoldingBlockKinds.Comment => FoldingRangeKind.Comment, | ||
CodeFoldingBlockKinds.Imports => FoldingRangeKind.Imports, | ||
CodeFoldingBlockKinds.Region => FoldingRangeKind.Region, | ||
_ => null | ||
}; | ||
} | ||
|
||
protected override FoldingRangeRegistrationOptions CreateRegistrationOptions(FoldingRangeCapability capability, ClientCapabilities clientCapabilities) | ||
{ | ||
return new FoldingRangeRegistrationOptions() | ||
{ | ||
DocumentSelector = _documentSelector | ||
}; | ||
} | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
src/OmniSharp.LanguageServerProtocol/Handlers/OmniSharpSemanticTokensHandler.cs
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 |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using OmniSharp.Extensions.JsonRpc; | ||
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities; | ||
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 | ||
{ | ||
public static IEnumerable<IJsonRpcHandler> Enumerate(RequestHandlers handlers) | ||
{ | ||
foreach (var (selector, handler) in handlers.OfType<Mef.IRequestHandler<SemanticHighlightRequest, SemanticHighlightResponse>>()) | ||
if (handler != null) | ||
yield return new OmniSharpSemanticTokensHandler(handler, selector); | ||
} | ||
|
||
private readonly Mef.IRequestHandler<SemanticHighlightRequest, SemanticHighlightResponse> _definitionHandler; | ||
private readonly DocumentSelector _documentSelector; | ||
|
||
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(); | ||
|
||
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(); | ||
|
||
private readonly SemanticTokensLegend _legend = new() | ||
{ | ||
TokenTypes = new Container<SemanticTokenType>(_tokenTypes.Values), | ||
TokenModifiers = new Container<SemanticTokenModifier>(_tokenModifiers.Values), | ||
}; | ||
|
||
public OmniSharpSemanticTokensHandler(Mef.IRequestHandler<SemanticHighlightRequest, SemanticHighlightResponse> definitionHandler, DocumentSelector documentSelector) | ||
{ | ||
_definitionHandler = definitionHandler; | ||
_documentSelector = documentSelector; | ||
} | ||
|
||
protected override async Task Tokenize(SemanticTokensBuilder builder, ITextDocumentIdentifierParams identifier, CancellationToken cancellationToken) | ||
{ | ||
var omnisharpRequest = new SemanticHighlightRequest() | ||
{ | ||
FileName = FromUri(identifier.TextDocument.Uri) | ||
}; | ||
|
||
var omnisharpResponse = await _definitionHandler.Handle(omnisharpRequest); | ||
|
||
if (omnisharpResponse.Spans is null) | ||
{ | ||
return; | ||
} | ||
|
||
foreach (var span in omnisharpResponse.Spans) | ||
{ | ||
var range = new Range(span.StartLine, span.StartColumn, span.EndLine, span.EndColumn); | ||
builder.Push(range, _tokenTypes[span.Type], span.Modifiers.Select(modifier => _tokenModifiers[modifier])); | ||
} | ||
} | ||
|
||
protected override Task<SemanticTokensDocument> GetSemanticTokensDocument(ITextDocumentIdentifierParams @params, CancellationToken cancellationToken) | ||
{ | ||
return Task.FromResult(new SemanticTokensDocument(_legend)); | ||
} | ||
|
||
protected override SemanticTokensRegistrationOptions CreateRegistrationOptions(SemanticTokensCapability capability, ClientCapabilities clientCapabilities) | ||
{ | ||
return new SemanticTokensRegistrationOptions() | ||
{ | ||
DocumentSelector = _documentSelector, | ||
Full = new SemanticTokensCapabilityRequestFull | ||
{ | ||
Delta = false | ||
}, | ||
Range = true, | ||
Legend = _legend | ||
}; | ||
} | ||
} | ||
} |
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
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
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.
interesting - findusages returns a quick fix response that actually contains document highlights? Some strange naming
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.
a lot of history behind it 😀