-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathLanguageServerSession.cs
More file actions
114 lines (94 loc) · 3.6 KB
/
LanguageServerSession.cs
File metadata and controls
114 lines (94 loc) · 3.6 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using JsonRpc.DynamicProxy.Client;
using JsonRpc.Client;
using JsonRpc.Contracts;
using JsonRpc.Server;
using LanguageServer.VsCode.Contracts;
using LanguageServer.VsCode.Contracts.Client;
using LanguageServer.VsCode.Server;
namespace DemoLanguageServer
{
public class LanguageServerSession
{
private readonly CancellationTokenSource cts = new CancellationTokenSource();
public LanguageServerSession(JsonRpcClient rpcClient, IJsonRpcContractResolver contractResolver)
{
RpcClient = rpcClient ?? throw new ArgumentNullException(nameof(rpcClient));
var builder = new JsonRpcProxyBuilder {ContractResolver = contractResolver};
Client = new ClientProxy(builder, rpcClient);
Documents = new ConcurrentDictionary<Uri, SessionDocument>();
DiagnosticProvider = new DiagnosticProvider();
}
public CancellationToken CancellationToken => cts.Token;
public JsonRpcClient RpcClient { get; }
public ClientProxy Client { get; }
public ConcurrentDictionary<Uri, SessionDocument> Documents { get; }
public DiagnosticProvider DiagnosticProvider { get; }
public LanguageServerSettings Settings { get; set; } = new LanguageServerSettings();
public void StopServer()
{
cts.Cancel();
}
}
public class SessionDocument
{
/// <summary>
/// Actually makes the changes to the inner document per this milliseconds.
/// </summary>
private const int RenderChangesDelay = 100;
public SessionDocument(TextDocumentItem doc)
{
Document = TextDocument.Load<FullTextDocument>(doc);
}
private Task updateChangesDelayTask;
private readonly object syncLock = new object();
private List<TextDocumentContentChangeEvent> impendingChanges = new List<TextDocumentContentChangeEvent>();
public event EventHandler DocumentChanged;
public TextDocument Document { get; set; }
public void NotifyChanges(IEnumerable<TextDocumentContentChangeEvent> changes)
{
lock (syncLock)
{
if (impendingChanges == null)
impendingChanges = changes.ToList();
else
impendingChanges.AddRange(changes);
}
if (updateChangesDelayTask == null || updateChangesDelayTask.IsCompleted)
{
updateChangesDelayTask = Task.Delay(RenderChangesDelay);
updateChangesDelayTask.ContinueWith(t => Task.Run((Action)MakeChanges));
}
}
private void MakeChanges()
{
List<TextDocumentContentChangeEvent> localChanges;
lock (syncLock)
{
localChanges = impendingChanges;
if (localChanges == null || localChanges.Count == 0) return;
impendingChanges = null;
}
Document = Document.ApplyChanges(localChanges);
if (impendingChanges == null)
{
localChanges.Clear();
lock (syncLock)
{
if (impendingChanges == null)
impendingChanges = localChanges;
}
}
OnDocumentChanged();
}
protected virtual void OnDocumentChanged()
{
DocumentChanged?.Invoke(this, EventArgs.Empty);
}
}
}