Skip to content
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
33 changes: 26 additions & 7 deletions src/Lsp/HandlerDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Abstractions;
Expand All @@ -7,7 +8,7 @@

namespace OmniSharp.Extensions.LanguageServer
{
class HandlerDescriptor : ILspHandlerDescriptor, IDisposable
class HandlerDescriptor : ILspHandlerDescriptor, IDisposable, IEquatable<HandlerDescriptor>
{
private readonly Action _disposeAction;

Expand Down Expand Up @@ -96,16 +97,34 @@ private static void SetCapability<T>(ICapability<T> capability, T instance)

public override bool Equals(object obj)
{
if (obj is HandlerDescriptor handler)
{
return handler.HandlerType == HandlerType && handler.Key == Key;
}
return false;
return Equals(obj as HandlerDescriptor);
}

public bool Equals(HandlerDescriptor other)
{
return other != null &&
EqualityComparer<Type>.Default.Equals(HandlerType, other.HandlerType) &&
Method == other.Method &&
Key == other.Key;
}

public override int GetHashCode()
{
return Tuple.Create(HandlerType, Key).GetHashCode();
var hashCode = -45133801;
hashCode = hashCode * -1521134295 + EqualityComparer<Type>.Default.GetHashCode(HandlerType);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Method);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Key);
return hashCode;
}

public static bool operator ==(HandlerDescriptor descriptor1, HandlerDescriptor descriptor2)
{
return EqualityComparer<HandlerDescriptor>.Default.Equals(descriptor1, descriptor2);
}

public static bool operator !=(HandlerDescriptor descriptor1, HandlerDescriptor descriptor2)
{
return !(descriptor1 == descriptor2);
}
}
}
73 changes: 67 additions & 6 deletions test/Lsp.Tests/HandlerResolverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using OmniSharp.Extensions.LanguageServer.Protocol;
using NSubstitute;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer;
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
using Xunit;
using HandlerCollection = OmniSharp.Extensions.LanguageServer.HandlerCollection;
Expand All @@ -17,7 +18,6 @@ namespace Lsp.Tests
{
public class HandlerResolverTests
{

[Theory]
[InlineData(typeof(IInitializeHandler), "initialize", 1)]
[InlineData(typeof(IInitializedHandler), "initialized", 1)]
Expand All @@ -35,11 +35,11 @@ public void Should_Contain_AllDefinedMethods(Type requestHandler, string key, in
}

[Theory]
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didOpen", 4)]
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didChange", 4)]
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didClose", 4)]
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didSave", 4)]
public void Should_Contain_AllDefinedTextDocumentSyncMethods(Type requestHandler, string key, int count)
[InlineData("textDocument/didOpen", 4)]
[InlineData("textDocument/didChange", 4)]
[InlineData("textDocument/didClose", 4)]
[InlineData("textDocument/didSave", 4)]
public void Should_Contain_AllDefinedTextDocumentSyncMethods(string key, int count)
{
var handler = new HandlerCollection();
var sub = (IJsonRpcHandler)TextDocumentSyncHandlerExtensions.With(DocumentSelector.ForPattern("**/*.something"));
Expand All @@ -49,6 +49,50 @@ public void Should_Contain_AllDefinedTextDocumentSyncMethods(Type requestHandler
handler._handlers.Count.Should().Be(count);
}

[Theory]
[InlineData("exit", 4)]
[InlineData("shutdown", 4)]
[InlineData("initialized", 4)]
[InlineData("initialize", 4)]
public void Should_Contain_AllDefinedLanguageServerMethods(string key, int count)
{
var handler = new HandlerCollection();
handler.Add(
Substitute.For<IInitializeHandler>(),
Substitute.For<IInitializedHandler>(),
Substitute.For<IExitHandler>(),
Substitute.For<IShutdownHandler>()
);
handler._handlers.Should().Contain(x => x.Method == key);
handler._handlers.Count.Should().Be(count);
}

[Theory]
[InlineData("exit", 4)]
[InlineData("shutdown", 4)]
[InlineData("initialized", 4)]
[InlineData("initialize", 4)]
public void Should_Contain_AllDefinedLanguageServerMethods_GivenDuplicates(string key, int count)
{
var handler = new HandlerCollection();
handler.Add(
Substitute.For<IInitializeHandler>(),
Substitute.For<IInitializedHandler>(),
Substitute.For<IExitHandler>(),
Substitute.For<IShutdownHandler>(),
Substitute.For<IInitializeHandler>(),
Substitute.For<IInitializedHandler>(),
Substitute.For<IExitHandler>(),
Substitute.For<IShutdownHandler>(),
Substitute.For<IInitializeHandler>(),
Substitute.For<IInitializedHandler>(),
Substitute.For<IExitHandler>(),
Substitute.For<IShutdownHandler>()
);
handler._handlers.Should().Contain(x => x.Method == key);
handler._handlers.Count.Should().Be(count);
}

[Theory]
[InlineData("textDocument/didOpen", 8)]
[InlineData("textDocument/didChange", 8)]
Expand Down Expand Up @@ -107,5 +151,22 @@ public void Should_Contain_AllDefinedMethods_OnLanguageServer_WithDifferentKeys(
handler._handlers.Should().Contain(x => x.Method == key2);
handler._handlers.Count.Should().Be(count);
}

[Fact]
public void Should_BeAwesome()
{
var handler = new HandlerCollection();

handler.Add(
Substitute.For<IExitHandler>(),
Substitute.For<IInitializeHandler>(),
Substitute.For<IInitializedHandler>(),
Substitute.For<IShutdownHandler>()
);

handler._handlers.Should().Contain(x => x.Method == "exit");
handler._handlers.Should().Contain(x => x.Method == "shutdown");
handler._handlers.Count.Should().Be(4);
}
}
}