-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathDebugAdapterClient.cs
198 lines (170 loc) · 8.5 KB
/
DebugAdapterClient.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using System.Reactive.Threading.Tasks;
using System.Threading;
using System.Threading.Tasks;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using OmniSharp.Extensions.DebugAdapter.Protocol;
using OmniSharp.Extensions.DebugAdapter.Protocol.Events;
using OmniSharp.Extensions.DebugAdapter.Protocol.Requests;
using OmniSharp.Extensions.DebugAdapter.Shared;
using OmniSharp.Extensions.JsonRpc;
using IOutputHandler = OmniSharp.Extensions.JsonRpc.IOutputHandler;
using OutputHandler = OmniSharp.Extensions.JsonRpc.OutputHandler;
namespace OmniSharp.Extensions.DebugAdapter.Client
{
public class DebugAdapterClient : JsonRpcServerBase, IDebugAdapterClient, IInitializedHandler
{
private readonly DebugAdapterHandlerCollection _collection;
private readonly IEnumerable<OnClientStartedDelegate> _startedDelegates;
private readonly CompositeDisposable _disposable = new CompositeDisposable();
private readonly Connection _connection;
private readonly IClientProgressManager _progressManager;
private readonly DapReceiver _receiver;
private readonly ISubject<InitializedEvent> _initializedComplete = new AsyncSubject<InitializedEvent>();
public static Task<IDebugAdapterClient> From(Action<DebugAdapterClientOptions> optionsAction)
{
return From(optionsAction, CancellationToken.None);
}
public static Task<IDebugAdapterClient> From(DebugAdapterClientOptions options)
{
return From(options, CancellationToken.None);
}
public static Task<IDebugAdapterClient> From(Action<DebugAdapterClientOptions> optionsAction, CancellationToken token)
{
var options = new DebugAdapterClientOptions();
optionsAction(options);
return From(options, token);
}
public static IDebugAdapterClient PreInit(Action<DebugAdapterClientOptions> optionsAction)
{
var options = new DebugAdapterClientOptions();
optionsAction(options);
return PreInit(options);
}
public static async Task<IDebugAdapterClient> From(DebugAdapterClientOptions options, CancellationToken token)
{
var server = (DebugAdapterClient) PreInit(options);
await server.Initialize(token);
return server;
}
/// <summary>
/// Create the server without connecting to the client
///
/// Mainly used for unit testing
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
public static IDebugAdapterClient PreInit(DebugAdapterClientOptions options)
{
return new DebugAdapterClient(options);
}
internal DebugAdapterClient(DebugAdapterClientOptions options) : base(options)
{
var services = options.Services;
services.AddLogging(builder => options.LoggingBuilderAction(builder));
ClientSettings = new InitializeRequestArguments() {
Locale = options.Locale,
AdapterId = options.AdapterId,
ClientId = options.ClientId,
ClientName = options.ClientName,
PathFormat = options.PathFormat,
ColumnsStartAt1 = options.ColumnsStartAt1,
LinesStartAt1 = options.LinesStartAt1,
SupportsMemoryReferences = options.SupportsMemoryReferences,
SupportsProgressReporting = options.SupportsProgressReporting,
SupportsVariablePaging = options.SupportsVariablePaging,
SupportsVariableType = options.SupportsVariableType,
SupportsRunInTerminalRequest = options.SupportsRunInTerminalRequest,
};
var serializer = options.Serializer;
var collection = new DebugAdapterHandlerCollection();
services.AddSingleton<IHandlersManager>(collection);
_collection = collection;
// _initializeDelegates = initializeDelegates;
// _initializedDelegates = initializedDelegates;
_startedDelegates = options.StartedDelegates;
var receiver = _receiver = new DapReceiver();
services.AddSingleton<IOutputHandler>(_ =>
new OutputHandler(options.Output, options.Serializer, receiver.ShouldFilterOutput, _.GetService<ILogger<OutputHandler>>()));
services.AddSingleton(_collection);
services.AddSingleton(serializer);
services.AddSingleton(options.RequestProcessIdentifier);
services.AddSingleton(receiver);
services.AddSingleton<IDebugAdapterClient>(this);
services.AddSingleton<DebugAdapterRequestRouter>();
services.AddSingleton<IRequestRouter<IHandlerDescriptor>>(_ => _.GetRequiredService<DebugAdapterRequestRouter>());
services.AddSingleton<IResponseRouter, DapResponseRouter>();
services.AddSingleton<IClientProgressManager, ClientProgressManager>();
services.AddSingleton(_ => _.GetRequiredService<IClientProgressManager>() as IJsonRpcHandler);
EnsureAllHandlersAreRegistered();
var serviceProvider = services.BuildServiceProvider();
_disposable.Add(serviceProvider);
IServiceProvider serviceProvider1 = serviceProvider;
var responseRouter = serviceProvider1.GetRequiredService<IResponseRouter>();
ResponseRouter = responseRouter;
_progressManager = serviceProvider1.GetRequiredService<IClientProgressManager>();
_connection = new Connection(
options.Input,
serviceProvider1.GetRequiredService<IOutputHandler>(),
receiver,
options.RequestProcessIdentifier,
serviceProvider1.GetRequiredService<IRequestRouter<IHandlerDescriptor>>(),
responseRouter,
serviceProvider1.GetRequiredService<ILoggerFactory>(),
options.OnUnhandledException ?? (e => { }),
options.CreateResponseException,
options.MaximumRequestTimeout,
false,
options.Concurrency
);
var serviceHandlers = serviceProvider1.GetServices<IJsonRpcHandler>().ToArray();
_collection.Add(this);
_disposable.Add(_collection.Add(serviceHandlers));
options.AddLinks(_collection);
}
public async Task Initialize(CancellationToken token)
{
RegisterCapabilities(ClientSettings);
_connection.Open();
var serverParams = await this.RequestInitialize(ClientSettings, token);
ServerSettings = serverParams;
_receiver.Initialized();
await _initializedComplete.ToTask(token);
await _startedDelegates.Select(@delegate => Observable.FromAsync(() => @delegate(this, serverParams, token)))
.ToObservable()
.Merge()
.LastOrDefaultAsync()
.ToTask(token);
}
Task<Unit> IRequestHandler<InitializedEvent, Unit>.Handle(InitializedEvent request, CancellationToken cancellationToken)
{
_initializedComplete.OnNext(request);
_initializedComplete.OnCompleted();
return Unit.Task;
}
private void RegisterCapabilities(InitializeRequestArguments capabilities)
{
capabilities.SupportsRunInTerminalRequest ??= _collection.ContainsHandler(typeof(IRunInTerminalHandler));
capabilities.SupportsProgressReporting ??= _collection.ContainsHandler(typeof(IProgressStartHandler)) &&
_collection.ContainsHandler(typeof(IProgressUpdateHandler)) &&
_collection.ContainsHandler(typeof(IProgressEndHandler));
}
protected override IResponseRouter ResponseRouter { get; }
protected override IHandlersManager HandlersManager => _collection;
public InitializeRequestArguments ClientSettings { get; }
public InitializeResponse ServerSettings { get; private set; }
public IClientProgressManager ProgressManager => _progressManager;
public void Dispose()
{
_disposable?.Dispose();
_connection?.Dispose();
}
}
}