|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Reflection; |
| 4 | +using DryIoc; |
| 5 | +using Microsoft.Extensions.Configuration; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using Microsoft.Extensions.DependencyInjection.Extensions; |
| 8 | +using Microsoft.Extensions.Options; |
| 9 | +using OmniSharp.Extensions.JsonRpc; |
| 10 | +using OmniSharp.Extensions.LanguageServer.Protocol.Client; |
| 11 | +using OmniSharp.Extensions.LanguageServer.Protocol.Client.WorkDone; |
| 12 | +using OmniSharp.Extensions.LanguageServer.Protocol.Models; |
| 13 | +using OmniSharp.Extensions.LanguageServer.Shared; |
| 14 | + |
| 15 | +namespace OmniSharp.Extensions.LanguageServer.Client |
| 16 | +{ |
| 17 | + public static class LanguageClientServiceCollectionExtensions |
| 18 | + { |
| 19 | + internal static IContainer AddLanguageClientInternals(this IContainer container, LanguageClientOptions options, IServiceProvider outerServiceProvider) |
| 20 | + { |
| 21 | + if (options.Receiver == null) |
| 22 | + { |
| 23 | + throw new ArgumentException("Receiver is missing!", nameof(options)); |
| 24 | + } |
| 25 | + |
| 26 | + container = container.AddLanguageProtocolInternals(options); |
| 27 | + |
| 28 | + container.RegisterInstance(options.ClientCapabilities); |
| 29 | + container.RegisterInstanceMany(options.Receiver); |
| 30 | + if (options.OnUnhandledException != null) |
| 31 | + { |
| 32 | + container.RegisterInstance(options.OnUnhandledException); |
| 33 | + } |
| 34 | + else |
| 35 | + { |
| 36 | + container.RegisterDelegate(_ => new OnUnhandledExceptionHandler(e => _.GetRequiredService<LanguageClient>().Shutdown()), reuse: Reuse.Singleton); |
| 37 | + } |
| 38 | + |
| 39 | + container.RegisterMany<TextDocumentLanguageClient>(serviceTypeCondition: type => type.Name.Contains(nameof(TextDocumentLanguageClient)), reuse: Reuse.Singleton); |
| 40 | + container.RegisterMany<ClientLanguageClient>(serviceTypeCondition: type => type.Name.Contains(nameof(ClientLanguageClient)), reuse: Reuse.Singleton); |
| 41 | + container.RegisterMany<GeneralLanguageClient>(serviceTypeCondition: type => type.Name.Contains(nameof(GeneralLanguageClient)), reuse: Reuse.Singleton); |
| 42 | + container.RegisterMany<WindowLanguageClient>(serviceTypeCondition: type => type.Name.Contains(nameof(WindowLanguageClient)), reuse: Reuse.Singleton); |
| 43 | + container.RegisterMany<WorkspaceLanguageClient>(serviceTypeCondition: type => type.Name.Contains(nameof(WorkspaceLanguageClient)), reuse: Reuse.Singleton); |
| 44 | + container.RegisterInstance<IOptionsFactory<LanguageClientOptions>>(new ValueOptionsFactory<LanguageClientOptions>(options)); |
| 45 | + |
| 46 | + container.RegisterMany<LanguageClient>(serviceTypeCondition: type => type == typeof(ILanguageClient) || type == typeof(LanguageClient), reuse: Reuse.Singleton); |
| 47 | + |
| 48 | + container.RegisterInstance(options.ClientInfo ?? new ClientInfo() { |
| 49 | + Name = Assembly.GetEntryAssembly()?.GetName().Name, |
| 50 | + Version = Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyInformationalVersionAttribute>() |
| 51 | + ?.InformationalVersion ?? |
| 52 | + Assembly.GetEntryAssembly()?.GetCustomAttribute<AssemblyVersionAttribute>()?.Version |
| 53 | + }); |
| 54 | + |
| 55 | + var providedConfiguration = options.Services.FirstOrDefault(z => z.ServiceType == typeof(IConfiguration) && z.ImplementationInstance is IConfiguration); |
| 56 | + container.RegisterDelegate<IConfiguration>(_ => { |
| 57 | + var builder = new ConfigurationBuilder(); |
| 58 | + var outerConfiguration = outerServiceProvider?.GetService<IConfiguration>(); |
| 59 | + if (outerConfiguration != null) |
| 60 | + { |
| 61 | + builder.AddConfiguration(outerConfiguration, false); |
| 62 | + } |
| 63 | + |
| 64 | + if (providedConfiguration != null) |
| 65 | + { |
| 66 | + builder.AddConfiguration(providedConfiguration.ImplementationInstance as IConfiguration); |
| 67 | + } |
| 68 | + |
| 69 | + //var didChangeConfigurationProvider = _.GetRequiredService<DidChangeConfigurationProvider>(); |
| 70 | + return builder |
| 71 | + //.AddConfiguration(didChangeConfigurationProvider) |
| 72 | + .Build(); |
| 73 | + }, |
| 74 | + reuse: Reuse.Singleton); |
| 75 | + |
| 76 | + container.RegisterMany<LanguageClientWorkDoneManager>(reuse: Reuse.Singleton); |
| 77 | + container.RegisterMany<LanguageClientWorkspaceFoldersManager>(serviceTypeCondition: type => options.WorkspaceFolders || type != typeof(IJsonRpcHandler), reuse: Reuse.Singleton); |
| 78 | + container.RegisterMany<LanguageClientRegistrationManager>(serviceTypeCondition: type => options.DynamicRegistration || type != typeof(IJsonRpcHandler), reuse: Reuse.Singleton); |
| 79 | + |
| 80 | + return container; |
| 81 | + } |
| 82 | + |
| 83 | + public static IServiceCollection AddLanguageClient(this IServiceCollection services, Action<LanguageClientOptions> configureOptions = null) |
| 84 | + { |
| 85 | + return AddLanguageClient(services, Options.DefaultName, configureOptions); |
| 86 | + } |
| 87 | + |
| 88 | + public static IServiceCollection AddLanguageClient(this IServiceCollection services, string name, Action<LanguageClientOptions> configureOptions = null) |
| 89 | + { |
| 90 | + // If we get called multiple times we're going to remove the default server |
| 91 | + // and force consumers to use the resolver. |
| 92 | + if (services.Any(d => d.ServiceType == typeof(LanguageClient) || d.ServiceType == typeof(ILanguageClient))) |
| 93 | + { |
| 94 | + services.RemoveAll<LanguageClient>(); |
| 95 | + services.RemoveAll<ILanguageClient>(); |
| 96 | + services.AddSingleton<ILanguageClient>(_ => |
| 97 | + throw new NotSupportedException("LanguageClient has been registered multiple times, you must use LanguageClient instead")); |
| 98 | + services.AddSingleton<LanguageClient>(_ => |
| 99 | + throw new NotSupportedException("LanguageClient has been registered multiple times, you must use LanguageClient instead")); |
| 100 | + } |
| 101 | + |
| 102 | + services |
| 103 | + .AddOptions() |
| 104 | + .AddLogging(); |
| 105 | + services.TryAddSingleton<LanguageClientResolver>(); |
| 106 | + services.TryAddSingleton(_ => _.GetRequiredService<LanguageClientResolver>().Get(name)); |
| 107 | + services.TryAddSingleton<ILanguageClient>(_ => _.GetRequiredService<LanguageClientResolver>().Get(name)); |
| 108 | + |
| 109 | + if (configureOptions != null) |
| 110 | + { |
| 111 | + services.Configure(name, configureOptions); |
| 112 | + } |
| 113 | + |
| 114 | + return services; |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments