-
Notifications
You must be signed in to change notification settings - Fork 420
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2463 from JoeRobich/add-lsp-handlers
Add missing LSP Handlers
- Loading branch information
Showing
7 changed files
with
274 additions
and
21 deletions.
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