Skip to content
New issue

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

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

Already on GitHub? # to your account

Add the TypeDefinitionHandler to the LSP #2461

Merged
merged 1 commit into from
Oct 24, 2022
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
Original file line number Diff line number Diff line change
@@ -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<IJsonRpcHandler> Enumerate(RequestHandlers handlers)
{
foreach (var (selector, handler) in handlers.OfType<Mef.IRequestHandler<GotoTypeDefinitionRequest, GotoTypeDefinitionResponse>>())
if (handler != null)
yield return new OmniSharpTypeDefinitionHandler(handler, selector);
}

private readonly Mef.IRequestHandler<GotoTypeDefinitionRequest, GotoTypeDefinitionResponse> _definitionHandler;
private readonly DocumentSelector _documentSelector;

public OmniSharpTypeDefinitionHandler(Mef.IRequestHandler<GotoTypeDefinitionRequest, GotoTypeDefinitionResponse> definitionHandler, DocumentSelector documentSelector)
{
_definitionHandler = definitionHandler;
_documentSelector = documentSelector;
}

public override async Task<LocationOrLocationLinks> 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<TypeDefinition, LocationOrLocationLink>(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
};
}
}
}
1 change: 1 addition & 0 deletions src/OmniSharp.LanguageServerProtocol/LanguageServerHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down