Example #1
0
 public static IJsonRpcHandler InitializeHandler(ILspHandlerDescriptor descriptor, SupportedCapabilities supportedCapabilities, IJsonRpcHandler handler)
 {
     supportedCapabilities.SetCapability(descriptor, handler);
     return(handler);
 }
Example #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);
        }
        internal LanguageServer(LanguageServerOptions options) : base(options)
        {
            var services = options.Services;
            var configurationProvider = new DidChangeConfigurationProvider(this, options.ConfigurationBuilderAction);

            services.AddSingleton <IJsonRpcHandler>(configurationProvider);
            options.RequestProcessIdentifier ??= (options.SupportsContentModified
                ? new RequestProcessIdentifier(RequestProcessType.Parallel)
                : new RequestProcessIdentifier(RequestProcessType.Serial));

            services.AddSingleton <IConfiguration>(configurationProvider);
            services.AddSingleton(Configuration = configurationProvider);

            services.AddLogging(builder => options.LoggingBuilderAction(builder));
            services.AddSingleton <IOptionsMonitor <LoggerFilterOptions>, LanguageServerLoggerFilterOptions>();

            _serverInfo              = options.ServerInfo;
            _serverReceiver          = options.Receiver;
            _serializer              = options.Serializer;
            _supportedCapabilities   = new SupportedCapabilities();
            _textDocumentIdentifiers = new TextDocumentIdentifiers();
            var collection = new SharedHandlerCollection(_supportedCapabilities, _textDocumentIdentifiers);

            services.AddSingleton <IHandlersManager>(collection);
            _collection           = collection;
            _initializeDelegates  = options.InitializeDelegates;
            _initializedDelegates = options.InitializedDelegates;
            _startedDelegates     = options.StartedDelegates;

            services.AddSingleton <IOutputHandler>(_ => new OutputHandler(
                                                       options.Output,
                                                       options.Serializer,
                                                       options.Receiver.ShouldFilterOutput,
                                                       _.GetService <ILogger <OutputHandler> >())
                                                   );
            services.AddSingleton(_collection);
            services.AddSingleton(_textDocumentIdentifiers);
            services.AddSingleton(_serializer);
            services.AddSingleton <OmniSharp.Extensions.JsonRpc.ISerializer>(_serializer);
            services.AddSingleton(options.RequestProcessIdentifier);
            services.AddSingleton <OmniSharp.Extensions.JsonRpc.IReceiver>(options.Receiver);
            services.AddSingleton <ILspServerReceiver>(options.Receiver);

            services.AddTransient <IHandlerMatcher, TextDocumentMatcher>();
            services.AddSingleton <ILanguageServer>(this);
            services.AddTransient <IHandlerMatcher, ExecuteCommandMatcher>();
            services.AddTransient <IHandlerMatcher, ResolveCommandMatcher>();
            services.AddSingleton <LspRequestRouter>();
            services.AddSingleton <IRequestRouter <ILspHandlerDescriptor> >(_ => _.GetRequiredService <LspRequestRouter>());
            services.AddSingleton <IRequestRouter <IHandlerDescriptor> >(_ => _.GetRequiredService <LspRequestRouter>());
            services.AddSingleton <IResponseRouter, ResponseRouter>();
            services.AddTransient(typeof(IPipelineBehavior <,>), typeof(ResolveCommandPipeline <,>));

            services.AddSingleton <IProgressManager, ProgressManager>();
            services.AddSingleton <IJsonRpcHandler>(_ => _.GetRequiredService <IProgressManager>() as IJsonRpcHandler);
            services.AddSingleton <IServerWorkDoneManager, ServerWorkDoneManager>();
            services.AddSingleton <IJsonRpcHandler>(_ => _.GetRequiredService <IServerWorkDoneManager>() as IJsonRpcHandler);

            EnsureAllHandlersAreRegistered();

            var serviceProvider = services.BuildServiceProvider();

            _disposable.Add(serviceProvider);
            _serviceProvider = serviceProvider;
            collection.SetServiceProvider(_serviceProvider);

            var requestRouter = _serviceProvider.GetRequiredService <IRequestRouter <ILspHandlerDescriptor> >();

            _responseRouter        = _serviceProvider.GetRequiredService <IResponseRouter>();
            ProgressManager        = _serviceProvider.GetRequiredService <IProgressManager>();
            _serverWorkDoneManager = _serviceProvider.GetRequiredService <IServerWorkDoneManager>();
            _connection            = new Connection(
                options.Input,
                _serviceProvider.GetRequiredService <IOutputHandler>(),
                options.Receiver,
                options.RequestProcessIdentifier,
                _serviceProvider.GetRequiredService <IRequestRouter <IHandlerDescriptor> >(),
                _responseRouter,
                _serviceProvider.GetRequiredService <ILoggerFactory>(),
                options.OnUnhandledException ?? (e => { ForcefulShutdown(); }),
                options.CreateResponseException,
                options.MaximumRequestTimeout,
                options.SupportsContentModified,
                options.Concurrency
                );

            // We need to at least create Window here in case any handler does loggin in their constructor
            TextDocument = new TextDocumentLanguageServer(this, _serviceProvider);
            Client       = new ClientLanguageServer(this, _serviceProvider);
            General      = new GeneralLanguageServer(this, _serviceProvider);
            Window       = new WindowLanguageServer(this, _serviceProvider);
            Workspace    = new WorkspaceLanguageServer(this, _serviceProvider);

            _disposable.Add(_collection.Add(this));

            {
                var serviceHandlers    = _serviceProvider.GetServices <IJsonRpcHandler>().ToArray();
                var serviceIdentifiers = _serviceProvider.GetServices <ITextDocumentIdentifier>().ToArray();
                _disposable.Add(_textDocumentIdentifiers.Add(serviceIdentifiers));
                _disposable.Add(_collection.Add(serviceHandlers));
                options.AddLinks(_collection);
            }
        }