Skip to content

Commit b589834

Browse files
Updated sample server with example of folding range (seems to work with the latest version)
1 parent f97fdb1 commit b589834

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

sample/SampleServer/Program.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Threading.Tasks;
44
using Microsoft.Extensions.Logging;
55
using OmniSharp.Extensions.LanguageServer;
6+
using OmniSharp.Extensions.LanguageServer.Protocol.Server;
67
using OmniSharp.Extensions.LanguageServer.Server;
78

89
namespace SampleServer
@@ -29,6 +30,8 @@ static async Task MainAsync(string[] args)
2930
.AddDefaultLoggingProvider()
3031
.WithMinimumLogLevel(LogLevel.Trace)
3132
.WithHandler<TextDocumentHandler>()
33+
.WithHandler<DidChangeWatchedFilesHandler>()
34+
.WithHandler<FoldingRangeHandler>()
3235
);
3336

3437
await server.WaitForExit;

sample/SampleServer/TextDocumentHandler.cs

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ class TextDocumentHandler : ITextDocumentSyncHandler
1818
private readonly OmniSharp.Extensions.LanguageServer.Protocol.Server.ILanguageServer _router;
1919

2020
private readonly DocumentSelector _documentSelector = new DocumentSelector(
21-
new DocumentFilter()
22-
{
21+
new DocumentFilter() {
2322
Pattern = "**/*.cs"
2423
}
2524
);
@@ -35,8 +34,7 @@ public TextDocumentHandler(OmniSharp.Extensions.LanguageServer.Protocol.Server.I
3534

3635
public Task<Unit> Handle(DidChangeTextDocumentParams notification, CancellationToken token)
3736
{
38-
_router.Window.LogMessage(new LogMessageParams()
39-
{
37+
_router.Window.LogMessage(new LogMessageParams() {
4038
Type = MessageType.Log,
4139
Message = "Hello World!!!!"
4240
});
@@ -45,8 +43,7 @@ public Task<Unit> Handle(DidChangeTextDocumentParams notification, CancellationT
4543

4644
TextDocumentChangeRegistrationOptions IRegistration<TextDocumentChangeRegistrationOptions>.GetRegistrationOptions()
4745
{
48-
return new TextDocumentChangeRegistrationOptions()
49-
{
46+
return new TextDocumentChangeRegistrationOptions() {
5047
DocumentSelector = _documentSelector,
5148
SyncKind = Change
5249
};
@@ -60,8 +57,7 @@ public void SetCapability(SynchronizationCapability capability)
6057
public async Task<Unit> Handle(DidOpenTextDocumentParams notification, CancellationToken token)
6158
{
6259
await Task.Yield();
63-
_router.Window.LogMessage(new LogMessageParams()
64-
{
60+
_router.Window.LogMessage(new LogMessageParams() {
6561
Type = MessageType.Log,
6662
Message = "Hello World!!!!"
6763
});
@@ -70,8 +66,7 @@ public async Task<Unit> Handle(DidOpenTextDocumentParams notification, Cancellat
7066

7167
TextDocumentRegistrationOptions IRegistration<TextDocumentRegistrationOptions>.GetRegistrationOptions()
7268
{
73-
return new TextDocumentRegistrationOptions()
74-
{
69+
return new TextDocumentRegistrationOptions() {
7570
DocumentSelector = _documentSelector,
7671
};
7772
}
@@ -88,8 +83,7 @@ public Task<Unit> Handle(DidSaveTextDocumentParams notification, CancellationTok
8883

8984
TextDocumentSaveRegistrationOptions IRegistration<TextDocumentSaveRegistrationOptions>.GetRegistrationOptions()
9085
{
91-
return new TextDocumentSaveRegistrationOptions()
92-
{
86+
return new TextDocumentSaveRegistrationOptions() {
9387
DocumentSelector = _documentSelector,
9488
IncludeText = true
9589
};
@@ -99,4 +93,53 @@ public TextDocumentAttributes GetTextDocumentAttributes(Uri uri)
9993
return new TextDocumentAttributes(uri, "csharp");
10094
}
10195
}
96+
97+
class FoldingRangeHandler : IFoldingRangeHandler
98+
{
99+
private FoldingRangeCapability _capability;
100+
101+
public TextDocumentRegistrationOptions GetRegistrationOptions()
102+
{
103+
return new TextDocumentRegistrationOptions() {
104+
DocumentSelector = DocumentSelector.ForLanguage("csharp")
105+
};
106+
}
107+
108+
public Task<Container<FoldingRange>> Handle(FoldingRangeRequestParam request,
109+
CancellationToken cancellationToken)
110+
{
111+
return Task.FromResult(new Container<FoldingRange>(new FoldingRange() {
112+
StartLine = 10,
113+
EndLine = 20,
114+
Kind = FoldingRangeKind.Region,
115+
EndCharacter = 0,
116+
StartCharacter = 0
117+
}));
118+
}
119+
120+
public void SetCapability(FoldingRangeCapability capability)
121+
{
122+
_capability = capability;
123+
}
124+
}
125+
126+
class DidChangeWatchedFilesHandler : IDidChangeWatchedFilesHandler
127+
{
128+
private DidChangeWatchedFilesCapability _capability;
129+
130+
public object GetRegistrationOptions()
131+
{
132+
return new object();
133+
}
134+
135+
public Task<Unit> Handle(DidChangeWatchedFilesParams request, CancellationToken cancellationToken)
136+
{
137+
return Unit.Task;
138+
}
139+
140+
public void SetCapability(DidChangeWatchedFilesCapability capability)
141+
{
142+
_capability = capability;
143+
}
144+
}
102145
}

src/Server/LspRequestRouter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ namespace OmniSharp.Extensions.LanguageServer.Server
2121
{
2222
internal class LspRequestRouter : RequestRouterBase<ILspHandlerDescriptor>, IRequestRouter<IHandlerDescriptor>
2323
{
24-
private readonly IEnumerable<ILspHandlerDescriptor> _collection;
24+
private readonly IHandlerCollection _collection;
2525
private readonly IEnumerable<IHandlerMatcher> _handlerMatchers;
2626

2727
public LspRequestRouter(
28-
IEnumerable<ILspHandlerDescriptor> collection,
28+
IHandlerCollection collection,
2929
ILoggerFactory loggerFactory,
3030
IEnumerable<IHandlerMatcher> handlerMatchers,
3131
ISerializer serializer,
@@ -56,7 +56,7 @@ private ILspHandlerDescriptor FindDescriptor(string method, JToken @params)
5656
var descriptor = _collection.FirstOrDefault(x => x.Method == method);
5757
if (descriptor is null)
5858
{
59-
_logger.LogDebug("Unable to find {Method}, methods found include {Methods}", method, string.Join(", ", _collection.Select(x => x.Method + ":" + x.HandlerType?.FullName)));
59+
_logger.LogDebug("Unable to find {Method}, methods found include {Methods}", method, string.Join(", ", _collection.Select(x => x.Method + ":" + x.Handler?.GetType()?.FullName)));
6060
return null;
6161
}
6262

@@ -68,7 +68,7 @@ private ILspHandlerDescriptor FindDescriptor(string method, JToken @params)
6868

6969
return _handlerMatchers.SelectMany(strat => strat.FindHandler(paramsValue, lspHandlerDescriptors)).FirstOrDefault() ?? descriptor;
7070
}
71-
71+
7272
IHandlerDescriptor IRequestRouter<IHandlerDescriptor>.GetDescriptor(Notification notification) => GetDescriptor(notification);
7373
IHandlerDescriptor IRequestRouter<IHandlerDescriptor>.GetDescriptor(Request request) => GetDescriptor(request);
7474
Task IRequestRouter<IHandlerDescriptor>.RouteNotification(IHandlerDescriptor descriptor, Notification notification, CancellationToken token) =>

0 commit comments

Comments
 (0)