Skip to content

Commit b0f8c9e

Browse files
Made ITextDocumentSyncHandler optional, moved its parts out into different areas and generate TextDocumentSync options individually
1 parent 2ce1d00 commit b0f8c9e

11 files changed

+177
-162
lines changed

sample/SampleServer/TextDocumentHandler.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,7 @@ public TextDocumentHandler(ILanguageServer router)
2929
_router = router;
3030
}
3131

32-
public TextDocumentSyncOptions Options { get; } = new TextDocumentSyncOptions()
33-
{
34-
WillSaveWaitUntil = false,
35-
WillSave = true,
36-
Change = TextDocumentSyncKind.Full,
37-
Save = new SaveOptions()
38-
{
39-
IncludeText = true
40-
},
41-
OpenClose = true
42-
};
32+
public TextDocumentSyncKind Change { get; } = TextDocumentSyncKind.Full;
4333

4434
public Task Handle(DidChangeTextDocumentParams notification, CancellationToken token)
4535
{
@@ -56,7 +46,7 @@ TextDocumentChangeRegistrationOptions IRegistration<TextDocumentChangeRegistrati
5646
return new TextDocumentChangeRegistrationOptions()
5747
{
5848
DocumentSelector = _documentSelector,
59-
SyncKind = Options.Change
49+
SyncKind = Change
6050
};
6151
}
6252

@@ -98,7 +88,7 @@ TextDocumentSaveRegistrationOptions IRegistration<TextDocumentSaveRegistrationOp
9888
return new TextDocumentSaveRegistrationOptions()
9989
{
10090
DocumentSelector = _documentSelector,
101-
IncludeText = Options.Save.IncludeText
91+
IncludeText = true
10292
};
10393
}
10494
public TextDocumentAttributes GetTextDocumentAttributes(Uri uri)

src/Protocol/Client/Capabilities/SynchronizationCapability.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities
66
{
7-
public class SynchronizationCapability : DynamicCapability, ConnectedCapability<ITextDocumentSyncHandler>
7+
public class SynchronizationCapability : DynamicCapability
88
{
99
/// <summary>
1010
/// The client supports sending will save notifications.

src/Protocol/Document/IDidChangeTextDocumentHandler.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using OmniSharp.Extensions.JsonRpc;
22
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
33
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
4+
using OmniSharp.Extensions.LanguageServer.Protocol.Server.Capabilities;
45

56
// ReSharper disable CheckNamespace
67

@@ -13,5 +14,9 @@ public static partial class DocumentNames
1314
}
1415

1516
[Serial, Method(DidChange)]
16-
public interface IDidChangeTextDocumentHandler : IJsonRpcNotificationHandler<DidChangeTextDocumentParams>, IRegistration<TextDocumentChangeRegistrationOptions>, ICapability<SynchronizationCapability> { }
17+
public interface IDidChangeTextDocumentHandler : IJsonRpcNotificationHandler<DidChangeTextDocumentParams>,
18+
IRegistration<TextDocumentChangeRegistrationOptions>, ICapability<SynchronizationCapability>
19+
{
20+
TextDocumentSyncKind Change { get; }
21+
}
1722
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace OmniSharp.Extensions.LanguageServer.Protocol
4+
{
5+
public interface ITextDocumentIdentifier
6+
{
7+
/// <summary>
8+
/// Returns the attributes for the document at the given URI. This can return null.
9+
/// </summary>
10+
/// <param name="uri"></param>
11+
/// <returns></returns>
12+
TextDocumentAttributes GetTextDocumentAttributes(Uri uri);
13+
}
14+
}
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
1-
using System;
21
using OmniSharp.Extensions.LanguageServer.Protocol.Server.Capabilities;
32

43
// ReSharper disable CheckNamespace
54

65
namespace OmniSharp.Extensions.LanguageServer.Protocol
76
{
8-
public interface ITextDocumentSyncHandler : IDidChangeTextDocumentHandler, IDidOpenTextDocumentHandler, IDidCloseTextDocumentHandler, IDidSaveTextDocumentHandler
7+
public interface ITextDocumentSyncHandler : IDidChangeTextDocumentHandler, IDidOpenTextDocumentHandler, IDidCloseTextDocumentHandler, IDidSaveTextDocumentHandler, ITextDocumentIdentifier
98
{
10-
TextDocumentSyncOptions Options { get; }
11-
/// <summary>
12-
/// Returns the attributes for the document at the given URI. This can return null.
13-
/// </summary>
14-
/// <param name="uri"></param>
15-
/// <returns></returns>
16-
TextDocumentAttributes GetTextDocumentAttributes(Uri uri);
179
}
1810
}

src/Server/Abstractions/IHandlerCollection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ public interface IHandlerCollection : IEnumerable<ILspHandlerDescriptor>
1313
IDisposable Add(IEnumerable<IJsonRpcHandler> handlers);
1414
bool ContainsHandler(Type type);
1515
bool ContainsHandler(TypeInfo typeInfo);
16-
IEnumerable<ITextDocumentSyncHandler> TextDocumentSyncHandlers();
16+
IEnumerable<ITextDocumentIdentifier> TextDocumentIdentifiers();
1717
}
1818
}

src/Server/HandlerCollection.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ namespace OmniSharp.Extensions.LanguageServer.Server
1313
class HandlerCollection : IHandlerCollection
1414
{
1515
internal readonly HashSet<HandlerDescriptor> _handlers = new HashSet<HandlerDescriptor>();
16-
internal readonly HashSet<ITextDocumentSyncHandler> _documentSyncHandlers = new HashSet<ITextDocumentSyncHandler>();
16+
internal readonly HashSet<ITextDocumentIdentifier> _textDocumentIdentifier = new HashSet<ITextDocumentIdentifier>();
1717

18-
public IEnumerable<ITextDocumentSyncHandler> TextDocumentSyncHandlers()
18+
public IEnumerable<ITextDocumentIdentifier> TextDocumentIdentifiers()
1919
{
20-
return _documentSyncHandlers;
20+
return _textDocumentIdentifier;
2121
}
2222

2323
public IEnumerator<ILspHandlerDescriptor> GetEnumerator()
@@ -59,9 +59,9 @@ public IDisposable Add(params IJsonRpcHandler[] handlers)
5959
foreach (var descriptor in descriptors)
6060
{
6161
_handlers.Add(descriptor);
62-
if (descriptor.Handler is ITextDocumentSyncHandler documentSyncHandler)
62+
if (descriptor.Handler is ITextDocumentIdentifier documentSyncHandler)
6363
{
64-
_documentSyncHandlers.Add(documentSyncHandler);
64+
_textDocumentIdentifier.Add(documentSyncHandler);
6565
}
6666
}
6767

@@ -101,7 +101,7 @@ private HandlerDescriptor GetDescriptor(string method, Type implementedType, IJs
101101
capability,
102102
() => {
103103
_handlers.RemoveWhere(instance => instance.Handler == handler);
104-
_documentSyncHandlers.RemoveWhere(instance => instance == handler);
104+
_textDocumentIdentifier.RemoveWhere(instance => instance == handler);
105105
});
106106
}
107107

src/Server/LanguageServer.cs

Lines changed: 17 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -24,103 +24,7 @@
2424

2525
namespace OmniSharp.Extensions.LanguageServer.Server
2626
{
27-
public class LanguageServerOptions
28-
{
29-
public LanguageServerOptions()
30-
{
31-
}
32-
33-
public Stream Input { get; set; }
34-
public Stream Output { get; set; }
35-
public ILoggerFactory LoggerFactory { get; set; }
36-
public ISerializer Serializer { get; set; }
37-
public IHandlerCollection Handlers { get; set; } = new HandlerCollection();
38-
public IRequestProcessIdentifier RequestProcessIdentifier { get; set; }
39-
public ILspReciever Reciever { get; set; } = new LspReciever();
40-
public IServiceCollection Services { get; set; } = new ServiceCollection();
41-
internal List<Type> HandlerTypes { get; set; } = new List<Type>();
42-
internal List<Assembly> HandlerAssemblies { get; set; } = new List<Assembly>();
43-
internal bool AddDefaultLoggingProvider { get; set; }
44-
}
45-
46-
public static class LanguageServerOptionsExtensions
47-
{
48-
public static LanguageServerOptions WithInput(this LanguageServerOptions options, Stream input)
49-
{
50-
options.Input = input;
51-
return options;
52-
}
53-
54-
public static LanguageServerOptions WithOutput(this LanguageServerOptions options, Stream output)
55-
{
56-
options.Output = output;
57-
return options;
58-
}
59-
60-
public static LanguageServerOptions WithLoggerFactory(this LanguageServerOptions options, ILoggerFactory loggerFactory)
61-
{
62-
options.LoggerFactory = loggerFactory;
63-
return options;
64-
}
65-
66-
public static LanguageServerOptions WithRequestProcessIdentifier(this LanguageServerOptions options, IRequestProcessIdentifier requestProcessIdentifier)
67-
{
68-
options.RequestProcessIdentifier = requestProcessIdentifier;
69-
return options;
70-
}
71-
72-
public static LanguageServerOptions WithSerializer(this LanguageServerOptions options, ISerializer serializer)
73-
{
74-
options.Serializer = serializer;
75-
return options;
76-
}
77-
78-
public static LanguageServerOptions WithReciever(this LanguageServerOptions options, ILspReciever reciever)
79-
{
80-
options.Reciever = reciever;
81-
return options;
82-
}
83-
84-
public static LanguageServerOptions AddHandler<T>(this LanguageServerOptions options)
85-
where T : class, IJsonRpcHandler
86-
{
87-
options.Services.AddSingleton<IJsonRpcHandler, T>();
88-
return options;
89-
}
90-
91-
public static LanguageServerOptions AddHandler<T>(this LanguageServerOptions options, T handler)
92-
where T : IJsonRpcHandler
93-
{
94-
options.Services.AddSingleton<IJsonRpcHandler>(handler);
95-
return options;
96-
}
97-
98-
public static LanguageServerOptions AddHandlers(this LanguageServerOptions options, Type type)
99-
{
100-
options.HandlerTypes.Add(type);
101-
return options;
102-
}
103-
104-
public static LanguageServerOptions AddHandlers(this LanguageServerOptions options, TypeInfo typeInfo)
105-
{
106-
options.HandlerTypes.Add(typeInfo.AsType());
107-
return options;
108-
}
109-
110-
public static LanguageServerOptions AddHandlers(this LanguageServerOptions options, Assembly assembly)
111-
{
112-
options.HandlerAssemblies.Add(assembly);
113-
return options;
114-
}
115-
116-
public static LanguageServerOptions AddDefaultLoggingProvider(this LanguageServerOptions options)
117-
{
118-
options.AddDefaultLoggingProvider = true;
119-
return options;
120-
}
121-
}
122-
123-
public class LanguageServer : ILanguageServer, IInitializeHandler, IInitializedHandler, IDisposable, IAwaitableTermination
27+
public class LanguageServer : ILanguageServer, IInitializeHandler, IInitializedHandler, IAwaitableTermination
12428
{
12529
private readonly Connection _connection;
12630
private readonly ILspRequestRouter _requestRouter;
@@ -372,42 +276,28 @@ async Task<InitializeResult> IRequestHandler<InitializeParams, InitializeResult>
372276
};
373277
}
374278

375-
var textSyncHandlers = _collection
376-
.Select(x => x.Handler)
377-
.OfType<ITextDocumentSyncHandler>()
378-
.ToArray();
279+
var textDocumentSyncKind = _collection.ContainsHandler(typeof(IDidChangeTextDocumentHandler))
280+
? _collection
281+
.OfType<IDidChangeTextDocumentHandler>()
282+
.Where(x => x.Change != TextDocumentSyncKind.None)
283+
.Min(z => z.Change)
284+
: TextDocumentSyncKind.None;
379285

380286
if (_clientVersion == ClientVersion.Lsp2)
381287
{
382-
if (textSyncHandlers.Any())
383-
{
384-
serverCapabilities.TextDocumentSync = textSyncHandlers
385-
.Where(x => x.Options.Change != TextDocumentSyncKind.None)
386-
.Min<ITextDocumentSyncHandler, TextDocumentSyncKind>(z => z.Options.Change);
387-
}
388-
else
389-
{
390-
serverCapabilities.TextDocumentSync = TextDocumentSyncKind.None;
391-
}
288+
serverCapabilities.TextDocumentSync = textDocumentSyncKind;
392289
}
393290
else
394291
{
395-
if (ccp.HasStaticHandler(textDocumentCapabilities.Synchronization))
396-
{
397-
// TODO: Merge options
398-
serverCapabilities.TextDocumentSync =
399-
textSyncHandlers.FirstOrDefault()?.Options ?? new TextDocumentSyncOptions() {
400-
Change = TextDocumentSyncKind.None,
401-
OpenClose = false,
402-
Save = new SaveOptions() { IncludeText = false },
403-
WillSave = false,
404-
WillSaveWaitUntil = false
405-
};
406-
}
407-
else
408-
{
409-
serverCapabilities.TextDocumentSync = TextDocumentSyncKind.None;
410-
}
292+
serverCapabilities.TextDocumentSync = new TextDocumentSyncOptions() {
293+
Change = textDocumentSyncKind,
294+
OpenClose = _collection.ContainsHandler(typeof(IDidOpenTextDocumentHandler)) || _collection.ContainsHandler(typeof(IDidCloseTextDocumentHandler)),
295+
Save = _collection.ContainsHandler(typeof(IDidSaveTextDocumentHandler)) ?
296+
new SaveOptions() { IncludeText = true /* TODO: Make configurable */ } :
297+
null,
298+
WillSave = _collection.ContainsHandler(typeof(IWillSaveTextDocumentHandler)),
299+
WillSaveWaitUntil = _collection.ContainsHandler(typeof(IWillSaveWaitUntilTextDocumentHandler))
300+
};
411301
}
412302

413303
_reciever.Initialized();

src/Server/LanguageServerOptions.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Reflection;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Logging;
7+
using OmniSharp.Extensions.JsonRpc;
8+
using OmniSharp.Extensions.LanguageServer.Server.Abstractions;
9+
using ISerializer = OmniSharp.Extensions.LanguageServer.Protocol.Serialization.ISerializer;
10+
11+
namespace OmniSharp.Extensions.LanguageServer.Server
12+
{
13+
public class LanguageServerOptions
14+
{
15+
public LanguageServerOptions()
16+
{
17+
}
18+
19+
public Stream Input { get; set; }
20+
public Stream Output { get; set; }
21+
public ILoggerFactory LoggerFactory { get; set; }
22+
public ISerializer Serializer { get; set; }
23+
public IHandlerCollection Handlers { get; set; } = new HandlerCollection();
24+
public IRequestProcessIdentifier RequestProcessIdentifier { get; set; }
25+
public ILspReciever Reciever { get; set; } = new LspReciever();
26+
public IServiceCollection Services { get; set; } = new ServiceCollection();
27+
internal List<Type> HandlerTypes { get; set; } = new List<Type>();
28+
internal List<Assembly> HandlerAssemblies { get; set; } = new List<Assembly>();
29+
internal bool AddDefaultLoggingProvider { get; set; }
30+
}
31+
}

0 commit comments

Comments
 (0)