Skip to content

Commit fed2134

Browse files
Added tests to validate that router routes requests correctly
1 parent b2c26e2 commit fed2134

18 files changed

+297
-66
lines changed

src/Lsp/Capabilities/Client/TextDocumentClientCapabilities.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
22
using Newtonsoft.Json.Serialization;
33

44
namespace OmniSharp.Extensions.LanguageServer.Capabilities.Client

src/Lsp/Capabilities/Client/WorkspaceClientCapabilites.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
22
using Newtonsoft.Json.Serialization;
33

44
namespace OmniSharp.Extensions.LanguageServer.Capabilities.Client
@@ -14,6 +14,8 @@ public class WorkspaceClientCapabilites
1414

1515
public Supports<WorkspaceEditCapability> WorkspaceEdit { get; set; }
1616

17+
private bool ShouldSerializeWorkspaceEdit() => WorkspaceEdit.IsSupported;
18+
1719
/// <summary>
1820
/// Capabilities specific to the `workspace/didChangeConfiguration` notification.
1921
/// </summary>

src/Lsp/Converters/SupportsConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Reflection;
33
using Newtonsoft.Json;
44
using OmniSharp.Extensions.LanguageServer.Capabilities.Client;
@@ -35,7 +35,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
3535
}
3636
else
3737
{
38-
serializer.Serialize(writer, false);
38+
serializer.Serialize(writer, null);
3939
}
4040
}
4141

@@ -60,4 +60,4 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
6060

6161
public override bool CanConvert(Type objectType) => objectType.GetGenericTypeDefinition() == typeof(Supports<>);
6262
}
63-
}
63+
}

src/Lsp/HandlerCollection.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -48,9 +48,11 @@ public IDisposable Add(params IJsonRpcHandler[] handlers)
4848
}
4949

5050
var key = "default";
51-
if (handler is IRegistration<TextDocumentRegistrationOptions> textDocumentRegistration)
51+
if (handler is IRegistration<TextDocumentRegistrationOptions>)
5252
{
53-
var options = textDocumentRegistration.GetRegistrationOptions();
53+
var options = GetTextDocumentRegistrationOptionsMethod
54+
.MakeGenericMethod(registration)
55+
.Invoke(handler, new object[] { handler }) as TextDocumentRegistrationOptions;
5456
key = options.DocumentSelector;
5557
}
5658

@@ -76,6 +78,15 @@ public IDisposable Add(params IJsonRpcHandler[] handlers)
7678
return new ImmutableDisposable(descriptors);
7779
}
7880

81+
private static readonly MethodInfo GetTextDocumentRegistrationOptionsMethod = typeof(HandlerCollection).GetTypeInfo()
82+
.GetMethod(nameof(GetTextDocumentRegistrationOptions), BindingFlags.Static | BindingFlags.NonPublic);
83+
84+
private static TextDocumentRegistrationOptions GetTextDocumentRegistrationOptions<T>(IRegistration<T> instance)
85+
where T : TextDocumentRegistrationOptions
86+
{
87+
return instance.GetRegistrationOptions();
88+
}
89+
7990
private Type UnwrapGenericType(Type genericType, Type type)
8091
{
8192
return type?.GetTypeInfo()

src/Lsp/InitializeDelegate.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
using System.Threading.Tasks;
2+
using OmniSharp.Extensions.LanguageServer.Models;
3+
4+
namespace OmniSharp.Extensions.LanguageServer
5+
{
6+
public delegate Task InitializeDelegate(InitializeParams request);
7+
}

src/Lsp/LanguageServer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
namespace OmniSharp.Extensions.LanguageServer
2020
{
21-
public delegate Task InitializeDelegate(InitializeParams request);
22-
2321
public class LanguageServer : ILanguageServer, IInitializeHandler, IInitializedHandler, IDisposable, IAwaitableTermination
2422
{
2523
private readonly Connection _connection;

src/Lsp/LspRequestRouter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -44,7 +44,7 @@ private string GetId(object id)
4444

4545
private ILspHandlerDescriptor FindDescriptor(string method, JToken @params)
4646
{
47-
var descriptor = _collection.FirstOrDefault(x => x.Method == method);
47+
var descriptor = _collection.FirstOrDefault(x => x.Method.Equals(method, StringComparison.OrdinalIgnoreCase));
4848
if (descriptor is null) return null;
4949

5050
if (_textDocumentSyncHandlers == null)

src/Lsp/Models/CodeActionParams.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
1-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
22
using Newtonsoft.Json.Serialization;
33

44
namespace OmniSharp.Extensions.LanguageServer.Models
55
{
6-
public interface ITextDocumentIdentifierParams
7-
{
8-
TextDocumentIdentifier TextDocument { get; }
9-
}
10-
116
/// <summary>
127
/// Params for the CodeActionRequest
138
/// </summary>

src/Lsp/Models/DocumentFilter.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
2+
using System.Linq;
23
using System.Text;
34
using Minimatch;
45
using Newtonsoft.Json;
@@ -108,5 +109,20 @@ public bool IsMatch(TextDocumentAttributes attributes)
108109

109110
return false;
110111
}
112+
113+
public static DocumentFilter ForPattern(string wildcard)
114+
{
115+
return new DocumentFilter() { Pattern = wildcard };
116+
}
117+
118+
public static DocumentFilter ForLanguage(string language)
119+
{
120+
return new DocumentFilter() { Language = language };
121+
}
122+
123+
public static DocumentFilter ForScheme(string scheme)
124+
{
125+
return new DocumentFilter() { Scheme = scheme };
126+
}
111127
}
112128
}

src/Lsp/Models/DocumentSelector.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Collections.ObjectModel;
33
using System.Linq;
44
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
@@ -47,5 +47,20 @@ public bool IsMatch(TextDocumentAttributes attributes)
4747
{
4848
return this.Any(z => z.IsMatch(attributes));
4949
}
50+
51+
public static DocumentSelector ForPattern(params string[] wildcards)
52+
{
53+
return new DocumentSelector(wildcards.Select(DocumentFilter.ForPattern));
54+
}
55+
56+
public static DocumentSelector ForLanguage(params string[] languages)
57+
{
58+
return new DocumentSelector(languages.Select(DocumentFilter.ForLanguage));
59+
}
60+
61+
public static DocumentSelector ForScheme(params string[] schemes)
62+
{
63+
return new DocumentSelector(schemes.Select(DocumentFilter.ForScheme));
64+
}
5065
}
5166
}

0 commit comments

Comments
 (0)