From f71069a0ffcfec1ffa144c189abd360af113c91f Mon Sep 17 00:00:00 2001 From: Joey Robichaud Date: Wed, 19 Oct 2022 15:37:04 -0700 Subject: [PATCH] Add the TypeDefinitionHandler to the LSP --- .../OmniSharpTypeDefinitionHandler.cs | 64 +++++++++++++++++++ .../LanguageServerHost.cs | 1 + 2 files changed, 65 insertions(+) create mode 100644 src/OmniSharp.LanguageServerProtocol/Handlers/OmniSharpTypeDefinitionHandler.cs diff --git a/src/OmniSharp.LanguageServerProtocol/Handlers/OmniSharpTypeDefinitionHandler.cs b/src/OmniSharp.LanguageServerProtocol/Handlers/OmniSharpTypeDefinitionHandler.cs new file mode 100644 index 0000000000..e250f7dc58 --- /dev/null +++ b/src/OmniSharp.LanguageServerProtocol/Handlers/OmniSharpTypeDefinitionHandler.cs @@ -0,0 +1,64 @@ +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.GotoTypeDefinition; +using static OmniSharp.LanguageServerProtocol.Helpers; + +namespace OmniSharp.LanguageServerProtocol.Handlers +{ + class OmniSharpTypeDefinitionHandler : TypeDefinitionHandlerBase + { + public static IEnumerable Enumerate(RequestHandlers handlers) + { + foreach (var (selector, handler) in handlers.OfType>()) + if (handler != null) + yield return new OmniSharpTypeDefinitionHandler(handler, selector); + } + + private readonly Mef.IRequestHandler _definitionHandler; + private readonly DocumentSelector _documentSelector; + + public OmniSharpTypeDefinitionHandler(Mef.IRequestHandler definitionHandler, DocumentSelector documentSelector) + { + _definitionHandler = definitionHandler; + _documentSelector = documentSelector; + } + + public override async Task Handle(TypeDefinitionParams request, CancellationToken token) + { + var omnisharpRequest = new GotoTypeDefinitionRequest() + { + FileName = FromUri(request.TextDocument.Uri), + Column = Convert.ToInt32(request.Position.Character), + Line = Convert.ToInt32(request.Position.Line) + }; + + var omnisharpResponse = await _definitionHandler.Handle(omnisharpRequest); + + if (omnisharpResponse.Definitions == null) + { + return new LocationOrLocationLinks(); + } + + return new LocationOrLocationLinks(omnisharpResponse.Definitions.Select(definition => new Location() + { + Uri = definition.Location.FileName, + Range = ToRange(definition.Location.Range) + })); + } + + protected override TypeDefinitionRegistrationOptions CreateRegistrationOptions(TypeDefinitionCapability capability, ClientCapabilities clientCapabilities) + { + return new TypeDefinitionRegistrationOptions() + { + DocumentSelector = _documentSelector + }; + } + } +} diff --git a/src/OmniSharp.LanguageServerProtocol/LanguageServerHost.cs b/src/OmniSharp.LanguageServerProtocol/LanguageServerHost.cs index ea5af6d28d..7fff1087a3 100644 --- a/src/OmniSharp.LanguageServerProtocol/LanguageServerHost.cs +++ b/src/OmniSharp.LanguageServerProtocol/LanguageServerHost.cs @@ -371,6 +371,7 @@ internal static void RegisterHandlers(ILanguageServer server, CompositionHost co server.Register(s => { foreach (var handler in OmniSharpTextDocumentSyncHandler.Enumerate(handlers, workspace, documentVersions) + .Concat(OmniSharpTypeDefinitionHandler.Enumerate(handlers)) .Concat(OmniSharpDefinitionHandler.Enumerate(handlers)) .Concat(OmniSharpHoverHandler.Enumerate(handlers)) .Concat(OmniSharpCompletionHandler.Enumerate(handlers))