Ejemplo n.º 1
0
        public void Connect(bool debugMode)
        {
            var logWriter = CreateLogWriter(true);

            using (logWriter)
                using (var cin = Console.OpenStandardInput())
                    using (var bcin = new BufferedStream(cin))
                        using (var cout = Console.OpenStandardOutput())
                            using (var reader = new PartwiseStreamMessageReader(bcin))
                                using (var writer = new PartwiseStreamMessageWriter(cout)) {
                                    var contractResolver = new JsonRpcContractResolver {
                                        NamingStrategy          = new CamelCaseJsonRpcNamingStrategy(),
                                        ParameterValueConverter = new CamelCaseJsonValueConverter(),
                                    };
                                    var clientHandler = new StreamRpcClientHandler();
                                    var client        = new JsonRpcClient(clientHandler);
                                    if (debugMode)
                                    {
                                        // We want to capture log all the LSP server-to-client calls as well
                                        clientHandler.MessageSending += (_, e) => {
                                            lock (logWriter) {
                                                logWriter.WriteLine("<C{0}", e.Message);
                                            }
                                        };
                                        clientHandler.MessageReceiving += (_, e) => {
                                            lock (logWriter) {
                                                logWriter.WriteLine(">C{0}", e.Message);
                                            }
                                        };
                                    }

                                    var session = new LanguageServerSession(client, contractResolver);
                                    _serviceManager.AddService(new SettingsManager(_serviceManager));
                                    _serviceManager.AddService(new VsCodeClient(session.Client, _serviceManager));
                                    _serviceManager.AddService(new Controller(_serviceManager));

                                    // Configure & build service host
                                    var host          = BuildServiceHost(logWriter, contractResolver, debugMode);
                                    var serverHandler = new StreamRpcServerHandler(host,
                                                                                   StreamRpcServerHandlerOptions.ConsistentResponseSequence |
                                                                                   StreamRpcServerHandlerOptions.SupportsRequestCancellation);
                                    serverHandler.DefaultFeatures.Set(session);

                                    var cts = new CancellationTokenSource();
                                    // If we want server to stop, just stop the "source"
                                    using (serverHandler.Attach(reader, writer))
                                        using (clientHandler.Attach(reader, writer))
                                            using (new RConnection(_serviceManager, cts.Token)) {
                                                // Wait for the "stop" request.
                                                session.CancellationToken.WaitHandle.WaitOne();
                                                cts.Cancel();
                                            }
                                    logWriter?.WriteLine("Exited");
                                }
        }
Ejemplo n.º 2
0
 public void Exit() => LanguageServerSession.StopServer();