Skip to content

Commit

Permalink
Merge pull request #26 from OmniSharp/latest-lsp
Browse files Browse the repository at this point in the history
Reacting to latest changes for LSP
  • Loading branch information
david-driscoll authored Oct 10, 2017
2 parents f76a391 + 6be75f3 commit 561a96d
Show file tree
Hide file tree
Showing 22 changed files with 509 additions and 40 deletions.
20 changes: 1 addition & 19 deletions src/Lsp/HandlerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public IDisposable Add(IEnumerable<IJsonRpcHandler> handlers)
.ImplementedInterfaces
.Where(x => !string.IsNullOrWhiteSpace(LspHelper.GetMethodName(x))))
{
var @interface = GetHandlerInterface(implementedInterface);
var @interface = HandlerTypeHelpers.GetHandlerInterface(implementedInterface);
var registration = UnwrapGenericType(typeof(IRegistration<>), implementedInterface);
var capability = UnwrapGenericType(typeof(ICapability<>), implementedInterface);

Expand Down Expand Up @@ -72,24 +72,6 @@ public IDisposable Add(IEnumerable<IJsonRpcHandler> handlers)
return new ImutableDisposable(descriptors);
}

private static readonly Type[] HandlerTypes = { typeof(INotificationHandler), typeof(INotificationHandler<>), typeof(IRequestHandler<>), typeof(IRequestHandler<,>), };

private bool IsValidInterface(Type type)
{
if (type.GetTypeInfo().IsGenericType)
{
return HandlerTypes.Contains(type.GetGenericTypeDefinition());
}
return HandlerTypes.Contains(type);
}

private Type GetHandlerInterface(Type type)
{
return type?.GetTypeInfo()
.ImplementedInterfaces
.First(IsValidInterface);
}

private Type UnwrapGenericType(Type genericType, Type type)
{
return type?.GetTypeInfo()
Expand Down
6 changes: 4 additions & 2 deletions src/Lsp/HandlerDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public HandlerDescriptor(string method, string key, IJsonRpcHandler handler, Typ

public Registration Registration
{
get {
get
{
if (!HasRegistration) return null;
if (_registration != null) return _registration;

Expand All @@ -47,7 +48,8 @@ public Registration Registration
.MakeGenericMethod(RegistrationType)
.Invoke(this, new object[] { Handler });

return _registration = new Registration() {
return _registration = new Registration()
{
Id = Guid.NewGuid().ToString(),
Method = Method,
RegisterOptions = options
Expand Down
28 changes: 28 additions & 0 deletions src/Lsp/HandlerTypeHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Linq;
using System.Reflection;
using OmniSharp.Extensions.JsonRpc;

namespace OmniSharp.Extensions.LanguageServer
{
public static class HandlerTypeHelpers
{
private static readonly Type[] HandlerTypes = { typeof(INotificationHandler), typeof(INotificationHandler<>), typeof(IRequestHandler<>), typeof(IRequestHandler<,>), };

private static bool IsValidInterface(Type type)
{
if (type.GetTypeInfo().IsGenericType)
{
return HandlerTypes.Contains(type.GetGenericTypeDefinition());
}
return HandlerTypes.Contains(type);
}

public static Type GetHandlerInterface(Type type)
{
return type?.GetTypeInfo()
.ImplementedInterfaces
.First(IsValidInterface);
}
}
}
2 changes: 1 addition & 1 deletion src/Lsp/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public Task Handle()

private bool HasHandler<T>()
{
return _collection.Any(z => z.HandlerType == typeof(T));
return _collection.Any(z => z.Handler is T);
}

private T GetOptions<O, T>(Func<O, T> action)
Expand Down
9 changes: 8 additions & 1 deletion src/Lsp/Models/CompletionItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ public class CompletionItem
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public TextEditContainer AdditionalTextEdits { get; set; }
/// <summary>
/// An optional set of characters that when pressed while this completion is active will accept it first and
/// then type that character. *Note* that all commit characters should have `length=1` and that superfluous
/// characters will be ignored.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public Container<string> CommitCharacters { get; set; }
/// <summary>
/// An optional command that is executed/// after* inserting this completion./// Note* that
/// additional modifications to the current document should be described with the
/// additionalTextEdits-property.
Expand All @@ -85,4 +92,4 @@ public class CompletionItem
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public object Data { get; set; }
}
}
}
19 changes: 19 additions & 0 deletions src/Lsp/Models/TextDocumentEdit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

namespace OmniSharp.Extensions.LanguageServer.Models
{
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class TextDocumentEdit
{
/// <summary>
/// The text document to change.
/// </summary>
public VersionedTextDocumentIdentifier TextDocument { get; set; }

/// <summary>
/// The edits to be applied.
/// </summary>
public Container<TextEdit> Edits { get; set; }
}
}
13 changes: 11 additions & 2 deletions src/Lsp/Models/WorkspaceEdit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ public class WorkspaceEdit
/// <summary>
/// Holds changes to existing resources.
/// </summary>
public IDictionary<Uri, IEnumerable<TextEdit>> Changes { get; set; } = new Dictionary<Uri, IEnumerable<TextEdit>>();
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public IDictionary<Uri, IEnumerable<TextEdit>> Changes { get; set; }
/// <summary>
/// An array of `TextDocumentEdit`s to express changes to n different text documents
/// where each text document edit addresses a specific version of a text document.
/// Whether a client supports versioned document edits is expressed via
/// `WorkspaceClientCapabilities.workspaceEdit.documentChanges`.
/// </summary>
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public Container<TextDocumentEdit> DocumentChanges { get; set; }
}
}
}
24 changes: 24 additions & 0 deletions src/Lsp/Protocol/General/CancelRequestExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using OmniSharp.Extensions.LanguageServer.Models;

// ReSharper disable CheckNamespace

namespace OmniSharp.Extensions.LanguageServer.Protocol
{
public static class CancelRequestExtensions
{
public static void CancelRequest(this ILanguageServer mediator, CancelParams @params)
{
mediator.SendNotification<CancelParams>("$/cancelRequest", @params);
}

public static void CancelRequest(this ILanguageServer mediator, string id)
{
mediator.SendNotification<CancelParams>("$/cancelRequest", new CancelParams() { Id = id });
}

public static void CancelRequest(this ILanguageServer mediator, long id)
{
mediator.SendNotification<CancelParams>("$/cancelRequest", new CancelParams() { Id = id });
}
}
}
2 changes: 1 addition & 1 deletion src/Lsp/Protocol/General/ICancelRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ namespace OmniSharp.Extensions.LanguageServer.Protocol
{
[Method("$/cancelRequest")]
public interface ICancelRequestHandler : INotificationHandler<CancelParams> { }
}
}
27 changes: 26 additions & 1 deletion src/Lsp/Protocol/Window/LogMessageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,30 @@ public static void LogMessage(this ILanguageServer mediator, LogMessageParams @p
{
mediator.SendNotification("window/logMessage", @params);
}

public static void Log(this ILanguageServer mediator, LogMessageParams @params)
{
mediator.LogMessage(@params);
}

public static void LogError(this ILanguageServer mediator, string message)
{
mediator.LogMessage(new LogMessageParams() { Type = MessageType.Error, Message = message });
}

public static void Log(this ILanguageServer mediator, string message)
{
mediator.LogMessage(new LogMessageParams() { Type = MessageType.Log, Message = message });
}

public static void LogWarning(this ILanguageServer mediator, string message)
{
mediator.LogMessage(new LogMessageParams() { Type = MessageType.Warning, Message = message });
}

public static void LogInfo(this ILanguageServer mediator, string message)
{
mediator.LogMessage(new LogMessageParams() { Type = MessageType.Info, Message = message });
}
}
}
}
27 changes: 26 additions & 1 deletion src/Lsp/Protocol/Window/ShowMessageExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,30 @@ public static void ShowMessage(this ILanguageServer mediator, ShowMessageParams
{
mediator.SendNotification("window/showMessage", @params);
}

public static void Show(this ILanguageServer mediator, ShowMessageParams @params)
{
mediator.ShowMessage(@params);
}

public static void ShowError(this ILanguageServer mediator, string message)
{
mediator.ShowMessage(new ShowMessageParams() { Type = MessageType.Error, Message = message });
}

public static void Show(this ILanguageServer mediator, string message)
{
mediator.ShowMessage(new ShowMessageParams() { Type = MessageType.Log, Message = message });
}

public static void ShowWarning(this ILanguageServer mediator, string message)
{
mediator.ShowMessage(new ShowMessageParams() { Type = MessageType.Warning, Message = message });
}

public static void ShowInfo(this ILanguageServer mediator, string message)
{
mediator.ShowMessage(new ShowMessageParams() { Type = MessageType.Info, Message = message });
}
}
}
}
12 changes: 11 additions & 1 deletion src/Lsp/Protocol/Window/ShowMessageRequestExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,15 @@ public static Task<MessageActionItem> ShowMessage(this ILanguageServer mediator,
{
return mediator.SendRequest<ShowMessageRequestParams, MessageActionItem>("window/showMessageRequest", @params);
}

public static Task<MessageActionItem> Show(this ILanguageServer mediator, ShowMessageRequestParams @params)
{
return mediator.ShowMessage(@params);
}

public static Task<MessageActionItem> Request(this ILanguageServer mediator, ShowMessageRequestParams @params)
{
return mediator.ShowMessage(@params);
}
}
}
}
4 changes: 2 additions & 2 deletions test/Lsp.Tests/Fixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private static string SerializeObjectInternal(object value, Type type, JsonSeria
jsonSerializer.Serialize(jsonWriter, value, type);
}

return sw.ToString()?.Replace("\r\n", "\n");//?.Replace("\n", "\r\n");
return sw.ToString()?.Replace("\r\n", "\n")?.TrimEnd();//?.Replace("\n", "\r\n");
}
}
}
}
4 changes: 2 additions & 2 deletions test/Lsp.Tests/JsonFixtureAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand All @@ -21,7 +21,7 @@ public override IEnumerable<object[]> GetData(MethodInfo testMethod)

using (var streamReader = new StreamReader(Resources.GetManifestResourceStream(fileName)))
{
yield return new object[] { streamReader.ReadToEnd()?.Replace("\r\n", "\n") };
yield return new object[] { streamReader.ReadToEnd()?.Replace("\r\n", "\n")?.TrimEnd() };
}
}
}
Expand Down
61 changes: 59 additions & 2 deletions test/Lsp.Tests/Models/ApplyWorkspaceEditParamsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ public class ApplyWorkspaceEditParamsTests
[Theory, JsonFixture]
public void SimpleTest(string expected)
{
var model = new ApplyWorkspaceEditParams() {
Edit = new WorkspaceEdit() {
var model = new ApplyWorkspaceEditParams()
{
Edit = new WorkspaceEdit()
{
Changes = new Dictionary<Uri, IEnumerable<TextEdit>>() {
{
new Uri("file:///abc/123/d.cs"), new [] {
Expand All @@ -37,5 +39,60 @@ public void SimpleTest(string expected)
var deresult = JsonConvert.DeserializeObject<ApplyWorkspaceEditParams>(expected);
deresult.ShouldBeEquivalentTo(model);
}

[Theory, JsonFixture]
public void DocumentChangesTest(string expected)
{
var model = new ApplyWorkspaceEditParams()
{
Edit = new WorkspaceEdit()
{
DocumentChanges = new Container<TextDocumentEdit>(
new TextDocumentEdit()
{
TextDocument = new VersionedTextDocumentIdentifier()
{
Version = 1,
Uri = new Uri("file:///abc/123/d.cs"),
},
Edits = new[] {
new TextEdit() {
NewText = "new text",
Range = new Range(new Position(1, 1), new Position(2,2))
},
new TextEdit() {
NewText = "new text2",
Range = new Range(new Position(3, 3), new Position(4,4))
}
}
},
new TextDocumentEdit()
{
TextDocument = new VersionedTextDocumentIdentifier()
{
Version = 1,
Uri = new Uri("file:///abc/123/b.cs"),
},
Edits = new[] {
new TextEdit() {
NewText = "new text2",
Range = new Range(new Position(1, 1), new Position(2,2))
},
new TextEdit() {
NewText = "new text3",
Range = new Range(new Position(3, 3), new Position(4,4))
}
}
}
)
}
};
var result = Fixture.SerializeObject(model);

result.Should().Be(expected);

var deresult = JsonConvert.DeserializeObject<ApplyWorkspaceEditParams>(expected);
deresult.ShouldBeEquivalentTo(model);
}
}
}
Loading

0 comments on commit 561a96d

Please sign in to comment.