-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathLanguageServerExtensions.cs
51 lines (49 loc) · 1.74 KB
/
LanguageServerExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using JsonRpc.Messages;
using JsonRpc.Server;
namespace LanguageServer.VsCode
{
/// <summary>
/// Provides extension methods for implementing a Language Server.
/// </summary>
public static class LanguageServerExtensions
{
/// <summary>
/// Interprets any <see cref="OperationCanceledException" /> thrown by the service
/// as <c>RequestCancelled</c> error per definition in Language Server Protocol.
/// </summary>
public static void UseCancellationHandling(this JsonRpcServiceHostBuilder builder)
{
if (builder == null) throw new ArgumentNullException(nameof(builder));
builder.Intercept(async (context, next) =>
{
try
{
await next();
}
catch (OperationCanceledException ex)
{
if (ex.CancellationToken == context.CancellationToken
|| ex.CancellationToken == CancellationToken.None)
{
context.Response.Error = new ResponseError(Utility.RequestCancelledErrorCode, ex.Message);
}
}
});
}
/// <summary>
/// Determines whether the specified document URI indeicates an "untitled" document.
/// </summary>
/// <remarks>
/// The URI of an untitled document has the following structure: <c>untitled:xxxxx</c>.
/// </remarks>
public static bool IsUntitled(this Uri documentUri)
{
return documentUri.Scheme == "untitled";
}
}
}