-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathLanguageClientServiceCollectionExtensions.cs
140 lines (125 loc) · 7.39 KB
/
LanguageClientServiceCollectionExtensions.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using DryIoc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Client.Configuration;
using OmniSharp.Extensions.LanguageServer.Protocol.Client;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.WorkDone;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Shared;
namespace OmniSharp.Extensions.LanguageServer.Client
{
public static class LanguageClientServiceCollectionExtensions
{
internal static IContainer AddLanguageClientInternals(this IContainer container, LanguageClientOptions options, IServiceProvider? outerServiceProvider)
{
container = container.AddLanguageProtocolInternals(options);
container.RegisterInstance(options.ClientCapabilities);
container.RegisterMany<LspClientReceiver>(
reuse: Reuse.Singleton,
nonPublicServiceTypes: true,
ifAlreadyRegistered: IfAlreadyRegistered.Keep
);
if (!EqualityComparer<OnUnhandledExceptionHandler?>.Default.Equals( options.OnUnhandledException, default))
{
container.RegisterInstance(options.OnUnhandledException);
}
else
{
#pragma warning disable 4014
container.RegisterDelegate(_ => new OnUnhandledExceptionHandler(e => _.GetRequiredService<LanguageClient>().Shutdown()), Reuse.Singleton);
#pragma warning restore 4014
}
container.RegisterMany<TextDocumentLanguageClient>(serviceTypeCondition: type => type.Name.Contains(nameof(TextDocumentLanguageClient)), reuse: Reuse.Singleton);
container.RegisterMany<ClientLanguageClient>(serviceTypeCondition: type => type.Name.Contains(nameof(ClientLanguageClient)), reuse: Reuse.Singleton);
container.RegisterMany<GeneralLanguageClient>(serviceTypeCondition: type => type.Name.Contains(nameof(GeneralLanguageClient)), reuse: Reuse.Singleton);
container.RegisterMany<WindowLanguageClient>(serviceTypeCondition: type => type.Name.Contains(nameof(WindowLanguageClient)), reuse: Reuse.Singleton);
container.RegisterMany<WorkspaceLanguageClient>(serviceTypeCondition: type => type.Name.Contains(nameof(WorkspaceLanguageClient)), reuse: Reuse.Singleton);
container.RegisterMany<DefaultLanguageClientFacade>(
serviceTypeCondition: type => type.IsClass || !type.Name.Contains("Proxy") && typeof(DefaultLanguageClientFacade).GetInterfaces()
.Except(typeof(DefaultLanguageClientFacade).BaseType!.GetInterfaces()).Any(z => type == z),
reuse: Reuse.Singleton
);
container.RegisterInstance<IOptionsFactory<LanguageClientOptions>>(new ValueOptionsFactory<LanguageClientOptions>(options));
container.RegisterMany<LanguageClient>(
serviceTypeCondition: type => type == typeof(ILanguageClient) || type == typeof(LanguageClient),
reuse: Reuse.Singleton,
setup: Setup.With(condition: req => req.IsResolutionRoot || req.Container.Resolve<IInsanceHasStarted>().Started)
);
container.RegisterInstance(
options.ClientInfo ?? new ClientInfo {
Name = Assembly.GetEntryAssembly()?.GetName().ToString() ?? string.Empty,
Version = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
?.InformationalVersion ??
Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyVersionAttribute>()?.Version
?? string.Empty
}
);
var providedConfiguration = options.Services.FirstOrDefault(z => z.ServiceType == typeof(IConfiguration) && z.ImplementationInstance is IConfiguration);
container.RegisterDelegate<IConfiguration>(
_ => {
var builder = new ConfigurationBuilder();
var outerConfiguration = outerServiceProvider?.GetService<IConfiguration>();
if (outerConfiguration != null)
{
builder.CustomAddConfiguration(outerConfiguration, false);
}
if (providedConfiguration != null)
{
builder.CustomAddConfiguration((providedConfiguration.ImplementationInstance as IConfiguration)!);
}
//var didChangeConfigurationProvider = _.GetRequiredService<DidChangeConfigurationProvider>();
return builder
//.AddConfiguration(didChangeConfigurationProvider)
.Build();
},
Reuse.Singleton
);
container.RegisterMany<LanguageClientWorkDoneManager>(Reuse.Singleton);
container.RegisterMany<LanguageClientWorkspaceFoldersManager>(
serviceTypeCondition: type => options.WorkspaceFolders || type != typeof(IJsonRpcHandler), reuse: Reuse.Singleton
);
container.RegisterMany<LanguageClientRegistrationManager>(
serviceTypeCondition: type => options.DynamicRegistration || type != typeof(IJsonRpcHandler), reuse: Reuse.Singleton
);
return container;
}
public static IServiceCollection AddLanguageClient(this IServiceCollection services, Action<LanguageClientOptions>? configureOptions = null) =>
AddLanguageClient(services, Options.DefaultName, configureOptions);
public static IServiceCollection AddLanguageClient(this IServiceCollection services, string name, Action<LanguageClientOptions>? 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(LanguageClient) || d.ServiceType == typeof(ILanguageClient)))
{
services.RemoveAll<LanguageClient>();
services.RemoveAll<ILanguageClient>();
services.AddSingleton<ILanguageClient>(
_ =>
throw new NotSupportedException("LanguageClient has been registered multiple times, you must use LanguageClient instead")
);
services.AddSingleton<LanguageClient>(
_ =>
throw new NotSupportedException("LanguageClient has been registered multiple times, you must use LanguageClient instead")
);
}
services
.AddOptions()
.AddLogging();
services.TryAddSingleton<LanguageClientResolver>();
services.TryAddSingleton(_ => _.GetRequiredService<LanguageClientResolver>().Get(name));
services.TryAddSingleton<ILanguageClient>(_ => _.GetRequiredService<LanguageClientResolver>().Get(name));
if (configureOptions != null)
{
services.Configure(name, configureOptions);
}
return services;
}
}
}