-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathCodeLensConnectionHandler.cs
More file actions
101 lines (86 loc) · 3.11 KB
/
CodeLensConnectionHandler.cs
File metadata and controls
101 lines (86 loc) · 3.11 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
#nullable enable
using System.Collections.Generic;
using CodeiumVS.Packets;
namespace CodeiumVS
{
using System;
using System.Diagnostics;
using System.IO.Pipes;
using System.Linq;
using System.Threading.Tasks;
using StreamJsonRpc;
using CodeLensConnections =
System.Collections.Concurrent.ConcurrentDictionary<System.Guid, CodeLensConnectionHandler>;
using CodeLensDetails =
System.Collections.Concurrent.ConcurrentDictionary<System.Guid, FunctionInfo>;
public class CodeLensConnectionHandler : IRemoteVisualStudio, IDisposable
{
private static readonly CodeLensConnections connections = new CodeLensConnections();
private static readonly CodeLensDetails detailsData = new CodeLensDetails();
private JsonRpc? rpc;
private Guid? dataPointId;
public static async Task AcceptCodeLensConnections()
{
try
{
while (true)
{
var stream =
new NamedPipeServerStream(PipeName.Get(Process.GetCurrentProcess().Id),
PipeDirection.InOut,
NamedPipeServerStream.MaxAllowedServerInstances,
PipeTransmissionMode.Byte,
PipeOptions.Asynchronous);
await stream.WaitForConnectionAsync().Caf();
_ = HandleConnection(stream);
}
}
catch (Exception ex)
{
throw;
}
static async Task HandleConnection(NamedPipeServerStream stream)
{
try
{
var handler = new CodeLensConnectionHandler();
var rpc = JsonRpc.Attach(stream, handler);
handler.rpc = rpc;
await rpc.Completion;
handler.Dispose();
stream.Dispose();
}
catch (Exception ex)
{
CodeiumVSPackage.Instance.LogAsync("Handle Connection Error");
}
}
}
public void Dispose()
{
if (dataPointId.HasValue)
{
_ = connections.TryRemove(dataPointId.Value, out var _);
_ = detailsData.TryRemove(dataPointId.Value, out var _);
}
}
// Called from each CodeLensDataPoint via JSON RPC.
public void RegisterCodeLensDataPoint(Guid id)
{
dataPointId = id;
connections[id] = this;
}
public static void
StoreDetailsData(Guid id, FunctionInfo closestFunction) => detailsData[id] = closestFunction;
public static FunctionInfo GetDetailsData(Guid id) => detailsData[id];
public static async Task RefreshCodeLensDataPoint(Guid id)
{
if (!connections.TryGetValue(id, out var conn))
throw new InvalidOperationException($"CodeLens data point {id} was not registered.");
Debug.Assert(conn.rpc != null);
await conn.rpc!.InvokeAsync(nameof(IRemoteCodeLens.Refresh)).Caf();
}
public static async Task RefreshAllCodeLensDataPoints() =>
await Task.WhenAll(connections.Keys.Select(RefreshCodeLensDataPoint)).Caf();
}
}