|
5 | 5 |
|
6 | 6 | namespace CodeiumVS |
7 | 7 | { |
8 | | - using System; |
9 | | - using System.Diagnostics; |
10 | | - using System.IO.Pipes; |
11 | | - using System.Linq; |
12 | | - using System.Threading.Tasks; |
| 8 | +using System; |
| 9 | +using System.Diagnostics; |
| 10 | +using System.IO.Pipes; |
| 11 | +using System.Linq; |
| 12 | +using System.Threading.Tasks; |
13 | 13 |
|
14 | | - using StreamJsonRpc; |
| 14 | +using StreamJsonRpc; |
15 | 15 |
|
16 | | - using CodeLensConnections = System.Collections.Concurrent.ConcurrentDictionary<System.Guid, CodeLensConnectionHandler>; |
17 | | - using CodeLensDetails = System.Collections.Concurrent.ConcurrentDictionary<System.Guid, FunctionInfo>; |
| 16 | +using CodeLensConnections = |
| 17 | + System.Collections.Concurrent.ConcurrentDictionary<System.Guid, CodeLensConnectionHandler>; |
| 18 | +using CodeLensDetails = |
| 19 | + System.Collections.Concurrent.ConcurrentDictionary<System.Guid, FunctionInfo>; |
18 | 20 |
|
19 | | - public class CodeLensConnectionHandler : IRemoteVisualStudio, IDisposable |
20 | | - { |
21 | | - private static readonly CodeLensConnections connections = new CodeLensConnections(); |
22 | | - private static readonly CodeLensDetails detailsData = new CodeLensDetails(); |
| 21 | +public class CodeLensConnectionHandler : IRemoteVisualStudio, IDisposable |
| 22 | +{ |
| 23 | + private static readonly CodeLensConnections connections = new CodeLensConnections(); |
| 24 | + private static readonly CodeLensDetails detailsData = new CodeLensDetails(); |
23 | 25 |
|
24 | | - private JsonRpc? rpc; |
25 | | - private Guid? dataPointId; |
| 26 | + private JsonRpc? rpc; |
| 27 | + private Guid? dataPointId; |
26 | 28 |
|
27 | | - public static async Task AcceptCodeLensConnections() |
| 29 | + public static async Task AcceptCodeLensConnections() |
| 30 | + { |
| 31 | + try |
28 | 32 | { |
29 | | - try |
30 | | - { |
31 | | - while (true) |
32 | | - { |
33 | | - var stream = new NamedPipeServerStream( |
34 | | - PipeName.Get(Process.GetCurrentProcess().Id), |
35 | | - PipeDirection.InOut, |
36 | | - NamedPipeServerStream.MaxAllowedServerInstances, |
37 | | - PipeTransmissionMode.Byte, |
38 | | - PipeOptions.Asynchronous); |
39 | | - await stream.WaitForConnectionAsync().Caf(); |
40 | | - _ = HandleConnection(stream); |
41 | | - } |
42 | | - } |
43 | | - catch (Exception ex) |
| 33 | + while (true) |
44 | 34 | { |
45 | | - throw; |
46 | | - } |
47 | | - |
48 | | - static async Task HandleConnection(NamedPipeServerStream stream) |
49 | | - { |
50 | | - try |
51 | | - { |
52 | | - var handler = new CodeLensConnectionHandler(); |
53 | | - var rpc = JsonRpc.Attach(stream, handler); |
54 | | - handler.rpc = rpc; |
55 | | - await rpc.Completion; |
56 | | - handler.Dispose(); |
57 | | - stream.Dispose(); |
58 | | - } |
59 | | - catch (Exception ex) |
60 | | - { |
61 | | - CodeiumVSPackage.Instance.LogAsync("Handle Connection Error"); |
62 | | - } |
| 35 | + var stream = |
| 36 | + new NamedPipeServerStream(PipeName.Get(Process.GetCurrentProcess().Id), |
| 37 | + PipeDirection.InOut, |
| 38 | + NamedPipeServerStream.MaxAllowedServerInstances, |
| 39 | + PipeTransmissionMode.Byte, |
| 40 | + PipeOptions.Asynchronous); |
| 41 | + await stream.WaitForConnectionAsync().Caf(); |
| 42 | + _ = HandleConnection(stream); |
63 | 43 | } |
64 | 44 | } |
| 45 | + catch (Exception ex) |
| 46 | + { |
| 47 | + throw; |
| 48 | + } |
65 | 49 |
|
66 | | - public void Dispose() |
| 50 | + static async Task HandleConnection(NamedPipeServerStream stream) |
67 | 51 | { |
68 | | - if (dataPointId.HasValue) |
| 52 | + try |
69 | 53 | { |
70 | | - _ = connections.TryRemove(dataPointId.Value, out var _); |
71 | | - _ = detailsData.TryRemove(dataPointId.Value, out var _); |
| 54 | + var handler = new CodeLensConnectionHandler(); |
| 55 | + var rpc = JsonRpc.Attach(stream, handler); |
| 56 | + handler.rpc = rpc; |
| 57 | + await rpc.Completion; |
| 58 | + handler.Dispose(); |
| 59 | + stream.Dispose(); |
| 60 | + } |
| 61 | + catch (Exception ex) |
| 62 | + { |
| 63 | + CodeiumVSPackage.Instance.LogAsync("Handle Connection Error"); |
72 | 64 | } |
73 | 65 | } |
| 66 | + } |
74 | 67 |
|
75 | | - // Called from each CodeLensDataPoint via JSON RPC. |
76 | | - public void RegisterCodeLensDataPoint(Guid id) |
| 68 | + public void Dispose() |
| 69 | + { |
| 70 | + if (dataPointId.HasValue) |
77 | 71 | { |
78 | | - dataPointId = id; |
79 | | - connections[id] = this; |
| 72 | + _ = connections.TryRemove(dataPointId.Value, out var _); |
| 73 | + _ = detailsData.TryRemove(dataPointId.Value, out var _); |
80 | 74 | } |
| 75 | + } |
81 | 76 |
|
82 | | - public static void StoreDetailsData(Guid id, FunctionInfo closestFunction) => detailsData[id] = closestFunction; |
| 77 | + // Called from each CodeLensDataPoint via JSON RPC. |
| 78 | + public void RegisterCodeLensDataPoint(Guid id) |
| 79 | + { |
| 80 | + dataPointId = id; |
| 81 | + connections[id] = this; |
| 82 | + } |
83 | 83 |
|
84 | | - public static FunctionInfo GetDetailsData(Guid id) => detailsData[id]; |
| 84 | + public static void |
| 85 | + StoreDetailsData(Guid id, FunctionInfo closestFunction) => detailsData[id] = closestFunction; |
85 | 86 |
|
86 | | - public static async Task RefreshCodeLensDataPoint(Guid id) |
87 | | - { |
88 | | - if (!connections.TryGetValue(id, out var conn)) |
89 | | - throw new InvalidOperationException($"CodeLens data point {id} was not registered."); |
| 87 | + public static FunctionInfo GetDetailsData(Guid id) => detailsData[id]; |
90 | 88 |
|
91 | | - Debug.Assert(conn.rpc != null); |
92 | | - await conn.rpc!.InvokeAsync(nameof(IRemoteCodeLens.Refresh)).Caf(); |
93 | | - } |
| 89 | + public static async Task RefreshCodeLensDataPoint(Guid id) |
| 90 | + { |
| 91 | + if (!connections.TryGetValue(id, out var conn)) |
| 92 | + throw new InvalidOperationException($"CodeLens data point {id} was not registered."); |
94 | 93 |
|
95 | | - public static async Task RefreshAllCodeLensDataPoints() |
96 | | - => await Task.WhenAll(connections.Keys.Select(RefreshCodeLensDataPoint)).Caf(); |
| 94 | + Debug.Assert(conn.rpc != null); |
| 95 | + await conn.rpc!.InvokeAsync(nameof(IRemoteCodeLens.Refresh)).Caf(); |
97 | 96 | } |
| 97 | + |
| 98 | + public static async Task RefreshAllCodeLensDataPoints() => |
| 99 | + await Task.WhenAll(connections.Keys.Select(RefreshCodeLensDataPoint)).Caf(); |
| 100 | +} |
98 | 101 | } |
0 commit comments