示例#1
0
 internal LanguageServer(
     Stream input,
     Stream output,
     ILspReciever reciever,
     IRequestProcessIdentifier requestProcessIdentifier,
     ISerializer serializer,
     IServiceCollection services,
     IEnumerable <Assembly> assemblies,
     IEnumerable <IJsonRpcHandler> handlers,
     IEnumerable <Type> handlerTypes,
     IEnumerable <(string name, IJsonRpcHandler handler)> namedHandlers,
示例#2
0
        internal LanguageServer(
            Stream input,
            Stream output,
            ILspReciever reciever,
            IRequestProcessIdentifier requestProcessIdentifier,
            ILoggerFactory loggerFactory,
            ISerializer serializer,
            IServiceCollection services,
            IEnumerable <Assembly> assemblies,
            IEnumerable <InitializeDelegate> initializeDelegates,
            IEnumerable <InitializedDelegate> initializedDelegates)
        {
            var outputHandler = new OutputHandler(output, serializer);

            services.AddLogging();
            _reciever              = reciever;
            _serializer            = serializer;
            _supportedCapabilities = new SupportedCapabilities();
            _collection            = new HandlerCollection(_supportedCapabilities);
            _initializeDelegates   = initializeDelegates;
            _initializedDelegates  = initializedDelegates;

            services.AddSingleton <IOutputHandler>(outputHandler);
            services.AddSingleton(_collection);
            services.AddSingleton(_serializer);
            services.AddSingleton <OmniSharp.Extensions.JsonRpc.ISerializer>(_serializer);
            services.AddSingleton(requestProcessIdentifier);
            services.AddSingleton <OmniSharp.Extensions.JsonRpc.IReciever>(reciever);
            services.AddSingleton <ILspReciever>(reciever);
            services.AddSingleton(loggerFactory);

            services.AddJsonRpcMediatR(assemblies);
            services.AddTransient <IHandlerMatcher, TextDocumentMatcher>();
            services.AddSingleton <Protocol.Server.ILanguageServer>(this);
            services.AddSingleton <ILanguageServer>(this);
            services.AddTransient <IHandlerMatcher, ExecuteCommandMatcher>();
            services.AddTransient <IHandlerMatcher, ResolveCommandMatcher>();
            services.AddSingleton <ILspRequestRouter, LspRequestRouter>();
            services.AddSingleton <IRequestRouter>(_ => _.GetRequiredService <ILspRequestRouter>());
            services.AddSingleton <IResponseRouter, ResponseRouter>();
            services.AddTransient(typeof(IPipelineBehavior <,>), typeof(ResolveCommandPipeline <,>));

            var foundHandlers = services
                                .Where(x => typeof(IJsonRpcHandler).IsAssignableFrom(x.ServiceType) && x.ServiceType != typeof(IJsonRpcHandler))
                                .ToArray();

            // Handlers are created at the start and maintained as a singleton
            foreach (var handler in foundHandlers)
            {
                services.Remove(handler);

                if (handler.ImplementationFactory != null)
                {
                    services.Add(ServiceDescriptor.Singleton(typeof(IJsonRpcHandler), handler.ImplementationFactory));
                }
                else if (handler.ImplementationInstance != null)
                {
                    services.Add(ServiceDescriptor.Singleton(typeof(IJsonRpcHandler), handler.ImplementationInstance));
                }
                else
                {
                    services.Add(ServiceDescriptor.Singleton(typeof(IJsonRpcHandler), handler.ImplementationType));
                }
            }

            _serviceProvider = services.BuildServiceProvider();

            _requestRouter  = _serviceProvider.GetRequiredService <ILspRequestRouter>();
            _responseRouter = _serviceProvider.GetRequiredService <IResponseRouter>();
            _connection     = ActivatorUtilities.CreateInstance <Connection>(_serviceProvider, input);

            _exitHandler = new ExitHandler(_shutdownHandler);

            _disposable.Add(
                AddHandlers(this, _shutdownHandler, _exitHandler, new CancelRequestHandler(_requestRouter))
                );

            var handlers = _serviceProvider.GetServices <IJsonRpcHandler>().ToArray();

            _collection.Add(handlers);

            Document  = new LanguageServerDocument(_responseRouter);
            Client    = new LanguageServerClient(_responseRouter);
            Window    = new LanguageServerWindow(_responseRouter);
            Workspace = new LanguageServerWorkspace(_responseRouter);
        }