/// <summary> /// The main asynchronous program entry-point. /// </summary> /// <returns> /// A <see cref="Task"/> representing program operation. /// </returns> static async Task AsyncMain() { Log.Information("Initialising language server..."); LanguageServer languageServer = new LanguageServer( input: Console.OpenStandardInput(2048), output: Console.OpenStandardOutput(2048), loggerFactory: new MSLogging.LoggerFactory().AddSerilog(Log.Logger.ForContext <LanguageServer>()) ); languageServer.AddHandler( new ConfigurationHandler() ); languageServer.AddHandler( new DummyHandler(languageServer) ); Log.Information("Starting language server..."); var initTask = languageServer.Initialize(); languageServer.Shutdown += shutdownRequested => { Log.Information("Language server shutdown (ShutDownRequested={ShutDownRequested}).", shutdownRequested); }; Log.Information("Language server initialised; waiting for shutdown."); await initTask; Log.Information("Waiting for shutdown..."); await languageServer.WasShutDown; }
static async Task MainAsync(string[] args) { /*while (!System.Diagnostics.Debugger.IsAttached) * { * await Task.Delay(100); * }*/ var server = new LanguageServer( Console.OpenStandardInput(), Console.OpenStandardOutput(), new LoggerFactory()); var manager = new GherkinManager(); server.OnInitialize(request => { manager.HandleStartup(UrlSanitizer.SanitizeUrl(request.RootUri.OriginalString)); return(Task.CompletedTask); }); server.AddHandler(new GherkinDocumentHandler(server, manager)); //server.AddHandler(new CsharpDocumentHandler(server, manager)); await server.Initialize(); await server.WaitForExit; }
public void GetHoverTextFromDocument() { var server = new LanguageServer(); server.Initialize(new InitializeParams(0, Array.Empty <WorkspaceFolder>())); server.TextDocumentDidOpen(new DidOpenTextDocumentParams(new TextDocumentItem("file:///some-uri", "some-language-id", 1, "(setf sum (+ 1 1))"))); var hover = server.TextDocumentHover(new HoverParams(new TextDocumentIdentifier("file:///some-uri"), new Position(0, 3))); Assert.Contains("(DEFMACRO SETF (...) ...)", hover.Contents.Value); }
public async Task Start() { _server.LogMessage(new LogMessageParams() { Message = "Starting server...", Type = MessageType.Log }); await _server.Initialize(); _server.LogMessage(new LogMessageParams() { Message = "initialized...", Type = MessageType.Log }); var logger = _loggerFactory.CreateLogger(typeof(LanguageServerHost)); WorkspaceInitializer.Initialize(_serviceProvider, _compositionHost); // Kick on diagnostics var diagnosticHandler = _handlers.GetAll() .OfType <IRequestHandler <DiagnosticsRequest, DiagnosticsResponse> >(); foreach (var handler in diagnosticHandler) { await handler.Handle(new DiagnosticsRequest()); } logger.LogInformation($"Omnisharp server running using Lsp at location '{_environment.TargetDirectory}' on host {_environment.HostProcessId}."); Console.CancelKeyPress += (sender, e) => { _cancellationTokenSource.Cancel(); e.Cancel = true; }; if (_environment.HostProcessId != -1) { try { var hostProcess = Process.GetProcessById(_environment.HostProcessId); hostProcess.EnableRaisingEvents = true; hostProcess.OnExit(() => _cancellationTokenSource.Cancel()); } catch { // If the process dies before we get here then request shutdown // immediately _cancellationTokenSource.Cancel(); } } }
public void GetHoverTextAfterIncrementalUpdate() { var server = new LanguageServer(); server.Initialize(new InitializeParams(0, Array.Empty <WorkspaceFolder>())); server.TextDocumentDidOpen(new DidOpenTextDocumentParams(new TextDocumentItem("file:///some-uri", "some-language-id", 1, "(defun add (a b) (+ a b))"))); // incremental update sets `Range` and `RangeLength` to non-null values server.TextDocumentDidChange(new DidChangeTextDocumentParams(new VersionedTextDocumentIdentifier("file:///some-uri", 2), new[] { new TextDocumentContentChangeEvent(new Protocol.Range(new Position(0, 1), new Position(0, 6)), 5, "defmacro") })); var hover = server.TextDocumentHover(new HoverParams(new TextDocumentIdentifier("file:///some-uri"), new Position(0, 3))); Assert.Contains("(DEFMACRO DEFMACRO (...) ...)", hover.Contents.Value); }
public void DocumentIsUpdatedWithIncrementalChangeEvent() { var server = new LanguageServer(); server.Initialize(new InitializeParams(0, Array.Empty <WorkspaceFolder>())); server.TextDocumentDidOpen(new DidOpenTextDocumentParams(new TextDocumentItem("file:///some-uri", "some-language-id", 1, "(defun add (a b) (+ a b))"))); // incremental update sets `Range` and `RangeLength` to non-null values server.TextDocumentDidChange(new DidChangeTextDocumentParams(new VersionedTextDocumentIdentifier("file:///some-uri", 2), new[] { new TextDocumentContentChangeEvent(new Protocol.Range(new Position(0, 1), new Position(0, 6)), 5, "defmacro") })); var contents = server.GetDocumentContents("file:///some-uri"); Assert.Equal("(defmacro add (a b) (+ a b))", contents); }
static async Task Main(string[] args) { var server = new LanguageServer(Console.OpenStandardInput(), Console.OpenStandardOutput(), new LoggerFactory()); var parser = new SimpleIniParser(server); server.AddHandlers( new TextDocumentHandler(server, parser), new CompletionHandler(parser) ); await server.Initialize(); await server.WaitForExit; }
/// <summary> /// Run a language server over the specified streams. /// </summary> /// <param name="input"> /// The input stream. /// </param> /// <param name="output"> /// The output stream. /// </param> /// <returns> /// A <see cref="Task"/> representing the operation. /// </returns> static async Task RunLanguageServer(Stream input, Stream output) { if (input == null) { throw new ArgumentNullException(nameof(input)); } if (output == null) { throw new ArgumentNullException(nameof(output)); } Log.Information("Initialising language server..."); LanguageServer languageServer = new LanguageServer(input, output, loggerFactory: new MSLogging.LoggerFactory().AddSerilog(Log.Logger.ForContext <LanguageServer>()) ); languageServer.AddHandler( new ConfigurationHandler() ); languageServer.AddHandler( new HoverHandler() ); languageServer.AddHandler( new DummyHandler(languageServer) ); languageServer.OnInitialize(parameters => { JToken options = parameters.InitializationOptions as JToken; Log.Information("Server received initialisation options: {Options}", options?.ToString(Newtonsoft.Json.Formatting.None)); return(Task.CompletedTask); }); Log.Information("Starting language server..."); languageServer.Shutdown += shutdownRequested => { Log.Information("Language server shutdown (ShutDownRequested={ShutDownRequested}).", shutdownRequested); }; languageServer.Exit += exitCode => { Log.Information("Language server exit (ExitCode={ExitCode}).", exitCode); }; await languageServer.Initialize(); Log.Information("Language server has shut down."); }
static async Task MainAsync(string[] args) { //while (!System.Diagnostics.Debugger.IsAttached) //{ // await Task.Delay(100); //} var server = new LanguageServer(Console.OpenStandardInput(), Console.OpenStandardOutput(), new LoggerFactory()); server.AddHandler(new TextDocumentHandler(server)); await server.Initialize(); await server.WasShutDown; }
async Task ListenForMessages() { this.messageLoopSyncContext = SynchronizationContext.Current; // Ensure that the console is using UTF-8 encoding System.Console.InputEncoding = Encoding.UTF8; System.Console.OutputEncoding = Encoding.UTF8; // Open the standard input/output streams this.inputStream = System.Console.OpenStandardInput(); this.outputStream = System.Console.OpenStandardOutput(); IMessageSerializer messageSerializer = null; IMessageProcessor messageProcessor = null; // Use a different serializer and message processor based // on whether this instance should host a language server // debug adapter. if (this.runDebugAdapter) { DebugAdapter debugAdapter = new DebugAdapter(); debugAdapter.Initialize(); messageProcessor = debugAdapter; messageSerializer = new V8MessageSerializer(); } else { // Set up the LanguageServer LanguageServer languageServer = new LanguageServer(); languageServer.Initialize(); messageProcessor = languageServer; messageSerializer = new JsonRpcMessageSerializer(); } // Set up the reader and writer this.messageReader = new MessageReader( this.inputStream, messageSerializer); this.messageWriter = new MessageWriter( this.outputStream, messageSerializer); // Set up the console host which will send events // through the MessageWriter this.consoleHost = new StdioConsoleHost(messageWriter); // Set up the PowerShell session this.editorSession = new EditorSession(); this.editorSession.StartSession(this.consoleHost); this.editorSession.PowerShellContext.OutputWritten += powerShellContext_OutputWritten; if (this.runDebugAdapter) { // Attach to debugger events from the PowerShell session this.editorSession.DebugService.DebuggerStopped += DebugService_DebuggerStopped; } // Run the message loop bool isRunning = true; while (isRunning) { Message newMessage = null; try { // Read a message from stdin newMessage = await this.messageReader.ReadMessage(); } catch (MessageParseException e) { // TODO: Write an error response Logger.Write( LogLevel.Error, "Could not parse a message that was received:\r\n\r\n" + e.ToString()); // Continue the loop continue; } // Process the message await messageProcessor.ProcessMessage( newMessage, this.editorSession, this.messageWriter); } }