Skip to content

Commit

Permalink
Cleaned up logging. Added range helper. Added hover handler
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll committed Sep 25, 2017
1 parent 379fc46 commit 851d1c4
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 22 deletions.
23 changes: 3 additions & 20 deletions src/OmniSharp.LanguageServerProtocol/Handlers/DefinitionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Mef;
using OmniSharp.Models.GotoDefinition;
using static OmniSharp.LanguageServerProtocol.Helpers;

namespace OmniSharp.LanguageServerProtocol.Handlers
{
Expand All @@ -20,14 +21,12 @@ class DefinitionHandler : IDefinitionHandler
private DefinitionCapability _capability;
private readonly IRequestHandler<GotoDefinitionRequest, GotoDefinitionResponse> _definitionHandler;
private readonly DocumentSelector _documentSelector;
private readonly ILogger _logger;

[ImportingConstructor]
public DefinitionHandler(IEnumerable<IRequestHandler> handlers, DocumentSelector documentSelector, ILogger logger)
public DefinitionHandler(IEnumerable<IRequestHandler> handlers, DocumentSelector documentSelector)
{
_definitionHandler = handlers.OfType<IRequestHandler<GotoDefinitionRequest, GotoDefinitionResponse>>().Single();
_documentSelector = documentSelector;
_logger = logger;
}

public TextDocumentRegistrationOptions GetRegistrationOptions()
Expand All @@ -47,28 +46,12 @@ public async Task<LocationOrLocations> Handle(TextDocumentPositionParams request
Line = Convert.ToInt32(request.Position.Line)
};

_logger.LogInformation(JsonConvert.SerializeObject(omnisharpRequest));

var omnisharpResponse = await _definitionHandler.Handle(omnisharpRequest);

_logger.LogInformation(JsonConvert.SerializeObject(omnisharpResponse));

return new LocationOrLocations(new Location()
{
Uri = Helpers.ToUri(omnisharpResponse.FileName),
Range = new Range()
{
Start = new Position()
{
Character = omnisharpResponse.Column,
Line = omnisharpResponse.Line,
},
End = new Position()
{
Character = omnisharpResponse.Column,
Line = omnisharpResponse.Line,
}
}
Range = ToRange((omnisharpResponse.Column, omnisharpResponse.Line))
});
}

Expand Down
62 changes: 62 additions & 0 deletions src/OmniSharp.LanguageServerProtocol/Handlers/HoverHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Collections.Generic;
using System.Composition;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using OmniSharp.Extensions.LanguageServer.Capabilities.Client;
using OmniSharp.Extensions.LanguageServer.Models;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Mef;
using OmniSharp.Models.TypeLookup;

namespace OmniSharp.LanguageServerProtocol.Handlers
{
[Shared, Export(typeof(HoverHandler))]
class HoverHandler : IHoverHandler
{
private HoverCapability _capability;
private readonly IRequestHandler<TypeLookupRequest, TypeLookupResponse> _definitionHandler;
private readonly DocumentSelector _documentSelector;

[ImportingConstructor]
public HoverHandler(IEnumerable<IRequestHandler> handlers, DocumentSelector documentSelector)
{
_definitionHandler = handlers.OfType<IRequestHandler<TypeLookupRequest, TypeLookupResponse>>().Single();
_documentSelector = documentSelector;
}

public TextDocumentRegistrationOptions GetRegistrationOptions()
{
return new TextDocumentRegistrationOptions()
{
DocumentSelector = _documentSelector
};
}

public async Task<Hover> Handle(TextDocumentPositionParams request, CancellationToken token)
{
var omnisharpRequest = new TypeLookupRequest()
{
FileName = Helpers.FromUri(request.TextDocument.Uri),
Column = Convert.ToInt32(request.Position.Character),
Line = Convert.ToInt32(request.Position.Line),
IncludeDocumentation = true
};

var omnisharpResponse = await _definitionHandler.Handle(omnisharpRequest);

return new Hover()
{
// TODO: Range? We don't currently have that!
// Range =
Contents = new MarkedStringContainer(omnisharpResponse.Type, omnisharpResponse.Documentation)
};
}

public void SetCapability(HoverCapability capability)
{
_capability = capability;
}
}
}
24 changes: 23 additions & 1 deletion src/OmniSharp.LanguageServerProtocol/Helpers.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using OmniSharp.Extensions.LanguageServer.Models;
using OmniSharp.Models;
using OmniSharp.Models.Diagnostics;
Expand Down Expand Up @@ -79,5 +78,28 @@ public static string FromUri(Uri uri)
}
return uri.LocalPath;
}

public static Range ToRange((int column, int line) location)
{
return new Range()
{
Start = ToPosition(location),
End = ToPosition(location)
};
}

public static Position ToPosition((int column, int line) location)
{
return new Position(location.column, location.line);
}

public static Range ToRange((int column, int line) start, (int column, int line) end)
{
return new Range()
{
Start = new Position(start.column, start.line),
End = new Position(end.column, end.line)
};
}
}
}
6 changes: 5 additions & 1 deletion src/OmniSharp.LanguageServerProtocol/LanguageServerHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ private void CreateCompositionHost(InitializeParams initializeParams)

_compositionHost = compositionHostBuilder.Build();

// TODO: Get these with metadata so we can attach languages
// This will thne let us build up a better document filter, and add handles foreach type of handler
// This will mean that we will have a strategy to create handlers from the interface type
_handlers = _compositionHost.GetExports<IRequestHandler>();
}

Expand All @@ -122,7 +125,8 @@ private Task Initialize(InitializeParams initializeParams)
var workspace = _compositionHost.GetExport<OmniSharpWorkspace>();

_server.AddHandler(new TextDocumentSyncHandler(_handlers, documentSelector, workspace));
_server.AddHandler(new DefinitionHandler(_handlers, documentSelector, _loggerFactory.CreateLogger(typeof(DefinitionHandler))));
_server.AddHandler(new DefinitionHandler(_handlers, documentSelector));
_server.AddHandler(new HoverHandler(_handlers, documentSelector));

_server.LogMessage(new LogMessageParams() {
Message = "Added handlers... waiting for initialize...",
Expand Down

0 comments on commit 851d1c4

Please sign in to comment.