-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathDebugAdapterClientServiceCollectionExtensions.cs
More file actions
145 lines (131 loc) · 6.78 KB
/
Copy pathDebugAdapterClientServiceCollectionExtensions.cs
File metadata and controls
145 lines (131 loc) · 6.78 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
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
using DryIoc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using OmniSharp.Extensions.DebugAdapter.Client.Configuration;
using OmniSharp.Extensions.DebugAdapter.Protocol.Client;
using OmniSharp.Extensions.DebugAdapter.Protocol.Requests;
using OmniSharp.Extensions.DebugAdapter.Shared;
using OmniSharp.Extensions.JsonRpc;
namespace OmniSharp.Extensions.DebugAdapter.Client
{
public static class DebugAdapterClientServiceCollectionExtensions
{
internal static IContainer AddDebugAdapterClientInternals(
this IContainer container, DebugAdapterClientOptions options, IServiceProvider? outerServiceProvider
)
{
container = container.AddDebugAdapterProtocolInternals(options);
if (options.OnUnhandledException != null)
{
container.RegisterInstance(options.OnUnhandledException);
}
else
{
container.RegisterDelegate(_ => new OnUnhandledExceptionHandler(_ => { }), Reuse.Singleton);
}
container.RegisterInstance<IOptionsFactory<DebugAdapterClientOptions>>(new ValueOptionsFactory<DebugAdapterClientOptions>(options));
container.RegisterInstance(
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,
}
);
container.RegisterInstance(options.RequestProcessIdentifier);
container.RegisterMany<DebugAdapterClientProgressManager>(nonPublicServiceTypes: true, reuse: Reuse.Singleton);
container.RegisterMany<DebugAdapterClient>(
serviceTypeCondition: type => type == typeof(IDebugAdapterClient) || type == typeof(DebugAdapterClient),
reuse: Reuse.Singleton,
setup: Setup.With(condition: req => req.IsResolutionRoot || req.Container.Resolve<IInsanceHasStarted>().Started)
);
container.RegisterMany<DefaultDebugAdapterClientFacade>(
serviceTypeCondition: type => type.IsClass || ( !type.Name.Contains("Proxy") && typeof(DefaultDebugAdapterClientFacade).GetInterfaces()
.Except(typeof(DefaultDebugAdapterClientFacade).BaseType!.GetInterfaces()).Any(z => type == z) ),
reuse: Reuse.Singleton
);
// container.
var providedConfiguration =
options.Services.FirstOrDefault(z => z.ServiceType == typeof(IConfiguration) && z.ImplementationInstance is IConfiguration);
container.RegisterDelegate<IConfiguration>(
_ =>
{
var builder = new ConfigurationBuilder();
if (outerServiceProvider != null)
{
var outerConfiguration = outerServiceProvider.GetService<IConfiguration>();
if (outerConfiguration != null)
{
builder.CustomAddConfiguration(outerConfiguration, false);
}
}
if (providedConfiguration != null)
{
builder.CustomAddConfiguration(( providedConfiguration.ImplementationInstance as IConfiguration )!);
}
return builder.Build();
},
Reuse.Singleton
);
return container;
}
public static IServiceCollection AddDebugAdapterClient(this IServiceCollection services, Action<DebugAdapterClientOptions>? configureOptions = null)
{
return AddDebugAdapterClient(services, Options.DefaultName, configureOptions);
}
public static IServiceCollection AddDebugAdapterClient(
this IServiceCollection services, string name, Action<DebugAdapterClientOptions>? configureOptions = null
)
{
// If we get called multiple times we're going to remove the default server
// and force consumers to use the resolver.
if (services.Any(d => d.ServiceType == typeof(DebugAdapterClient) || d.ServiceType == typeof(IDebugAdapterClient)))
{
services.RemoveAll<DebugAdapterClient>();
services.RemoveAll<IDebugAdapterClient>();
services.RemoveAll<IDebugAdapterClientFacade>();
services.AddSingleton<IDebugAdapterClient>(
_ =>
throw new NotSupportedException(
"DebugAdapterClient has been registered multiple times, you must use DebugAdapterClientResolver instead"
)
);
services.AddSingleton<IDebugAdapterClientFacade>(
_ =>
throw new NotSupportedException(
"DebugAdapterClient has been registered multiple times, you must use DebugAdapterClientResolver instead"
)
);
services.AddSingleton<DebugAdapterClient>(
_ =>
throw new NotSupportedException(
"DebugAdapterClient has been registered multiple times, you must use DebugAdapterClientResolver instead"
)
);
}
services
.AddOptions()
.AddLogging();
services.TryAddSingleton<DebugAdapterClientResolver>();
services.TryAddSingleton(_ => _.GetRequiredService<DebugAdapterClientResolver>().Get(name));
services.TryAddSingleton<IDebugAdapterClient>(_ => _.GetRequiredService<DebugAdapterClientResolver>().Get(name));
services.TryAddSingleton<IDebugAdapterClientFacade>(_ => _.GetRequiredService<DebugAdapterClientResolver>().Get(name));
if (configureOptions != null)
{
services.Configure(name, configureOptions);
}
return services;
}
}
}