-
Notifications
You must be signed in to change notification settings - Fork 417
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 #2461 from JoeRobich/add-lsp-typedefinition-handler
Add the TypeDefinitionHandler to the LSP
- Loading branch information
Showing
2 changed files
with
65 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/OmniSharp.LanguageServerProtocol/Handlers/OmniSharpTypeDefinitionHandler.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,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 | ||
}; | ||
} | ||
} | ||
} |
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