From f72724231ee2e90c66719bc2e598ba728d096766 Mon Sep 17 00:00:00 2001 From: LPeter1997 Date: Thu, 19 Oct 2023 20:24:10 +0200 Subject: [PATCH] All fixed --- .../Capabilities/DidDeleteFiles.cs | 2 +- .../Capabilities/TextDocumentSync.cs | 6 ++-- src/Draco.LanguageServer/Program.cs | 2 -- src/Draco.Lsp/Server/LanguageClientProxy.cs | 33 +++++++++++++++---- .../Server/LanguageServerConnection.cs | 9 +++-- .../Server/LanguageServerMethodHandler.cs | 13 ++------ .../TextDocument/ITextDocumentDidChange.cs | 2 +- .../TextDocument/ITextDocumentDidClose.cs | 2 +- .../TextDocument/ITextDocumentDidOpen.cs | 2 +- .../Server/Workspace/IDidDeleteFiles.cs | 4 +-- 10 files changed, 44 insertions(+), 31 deletions(-) diff --git a/src/Draco.LanguageServer/Capabilities/DidDeleteFiles.cs b/src/Draco.LanguageServer/Capabilities/DidDeleteFiles.cs index d3e262265..6d9e8b1a0 100644 --- a/src/Draco.LanguageServer/Capabilities/DidDeleteFiles.cs +++ b/src/Draco.LanguageServer/Capabilities/DidDeleteFiles.cs @@ -21,7 +21,7 @@ internal partial class DracoLanguageServer : IDidDeleteFiles } }; - public async Task DidDeleteFilesAsync(DeleteFilesParams param, CancellationToken cancellationToken) + public async Task DidDeleteFilesAsync(DeleteFilesParams param) { foreach (var file in param.Files) { diff --git a/src/Draco.LanguageServer/Capabilities/TextDocumentSync.cs b/src/Draco.LanguageServer/Capabilities/TextDocumentSync.cs index 485ac6bc6..d4d0897c7 100644 --- a/src/Draco.LanguageServer/Capabilities/TextDocumentSync.cs +++ b/src/Draco.LanguageServer/Capabilities/TextDocumentSync.cs @@ -9,15 +9,15 @@ namespace Draco.LanguageServer; internal sealed partial class DracoLanguageServer : ITextDocumentSync { - public async Task TextDocumentDidOpenAsync(DidOpenTextDocumentParams param, CancellationToken cancellationToken) + public async Task TextDocumentDidOpenAsync(DidOpenTextDocumentParams param) { await this.PublishDiagnosticsAsync(param.TextDocument.Uri); } - public Task TextDocumentDidCloseAsync(DidCloseTextDocumentParams param, CancellationToken cancellationToken) => + public Task TextDocumentDidCloseAsync(DidCloseTextDocumentParams param) => Task.CompletedTask; - public async Task TextDocumentDidChangeAsync(DidChangeTextDocumentParams param, CancellationToken cancellationToken) + public async Task TextDocumentDidChangeAsync(DidChangeTextDocumentParams param) { var uri = param.TextDocument.Uri; var change = param.ContentChanges.First(); diff --git a/src/Draco.LanguageServer/Program.cs b/src/Draco.LanguageServer/Program.cs index f463c236b..e0c372174 100644 --- a/src/Draco.LanguageServer/Program.cs +++ b/src/Draco.LanguageServer/Program.cs @@ -45,8 +45,6 @@ internal static async Task Main(string[] args) internal static async Task RunServerAsync(bool stdioFlag) { - await Task.Delay(10000); - var transportKind = GetTransportKind(stdioFlag); var transportStream = BuildTransportStream(transportKind); diff --git a/src/Draco.Lsp/Server/LanguageClientProxy.cs b/src/Draco.Lsp/Server/LanguageClientProxy.cs index 3bcc7ca1c..d9d229384 100644 --- a/src/Draco.Lsp/Server/LanguageClientProxy.cs +++ b/src/Draco.Lsp/Server/LanguageClientProxy.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Concurrent; +using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; @@ -41,11 +42,32 @@ internal class LanguageClientProxy : DispatchProxy private object? ProxyRpc(MethodInfo method, object?[] arguments) { var handler = this.handlers.GetOrAdd(method, m => new(m, this)); - var args = handler.SupportsCancellation ? arguments[..^1] : arguments; if (handler.IsRequest) { - // It's a request + // Build up args + var args = new List(); + // Method name + args.Add(handler.MethodName); + // Parameter + if (handler.AcceptsParams) + { + args.Add(arguments[0]); + } + else + { + args.Add(null); + } + // CT + if (handler.SupportsCancellation) + { + args.Add(arguments[1]!); + } + else + { + args.Add(CancellationToken.None); + } + // Extract return type var returnType = method.ReturnType; @@ -54,17 +76,14 @@ internal class LanguageClientProxy : DispatchProxy returnType = returnType.GetGenericArguments()[0]; } - // TODO: cancellation token - var ct = CancellationToken.None; - return SendRequestMethod .MakeGenericMethod(returnType) - .Invoke(this.Connection, new[] { handler.MethodName, args.SingleOrDefault(), ct }); + .Invoke(this.Connection, args.ToArray()); } else { // It's a notification - return this.Connection.SendNotificationAsync(handler.MethodName, args.SingleOrDefault()); + return this.Connection.SendNotificationAsync(handler.MethodName, arguments.SingleOrDefault()); } } } diff --git a/src/Draco.Lsp/Server/LanguageServerConnection.cs b/src/Draco.Lsp/Server/LanguageServerConnection.cs index 8b4dc54a4..fb412097e 100644 --- a/src/Draco.Lsp/Server/LanguageServerConnection.cs +++ b/src/Draco.Lsp/Server/LanguageServerConnection.cs @@ -32,7 +32,12 @@ private LspMessageAdapter() Id = id, Params = @params, }; - public static LspMessage CreateCancelRequest(int id) => throw new NotImplementedException(); + public static LspMessage CreateCancelRequest(int id) => CreateNotification( + "$/cancelRequest", + JsonSerializer.SerializeToElement(new CancelParams + { + Id = id, + })); public static LspMessage CreateOkResponse(object id, JsonElement okResult) => new ResponseMessage { Jsonrpc = "2.0", @@ -43,7 +48,7 @@ private LspMessageAdapter() { Jsonrpc = "2.0", Id = ToId(id), - Error = (ResponseError)errorResult, + Error = errorResult, }; public static LspMessage CreateNotification(string method, JsonElement @params) => new NotificationMessage { diff --git a/src/Draco.Lsp/Server/LanguageServerMethodHandler.cs b/src/Draco.Lsp/Server/LanguageServerMethodHandler.cs index fa99f1362..be0d6bc2d 100644 --- a/src/Draco.Lsp/Server/LanguageServerMethodHandler.cs +++ b/src/Draco.Lsp/Server/LanguageServerMethodHandler.cs @@ -103,19 +103,12 @@ public LanguageServerMethodHandler(MethodInfo handlerMethod, object? target) this.DeclaredReturnType = returnType; } - public Task InvokeNotification(object?[] args) - { - Debug.WriteLine($"InvokeNotification {this.MethodName} START"); - var result = (Task)this.handlerMethod.Invoke(this.target, args.ToArray())!; - Debug.WriteLine($"InvokeNotification {this.MethodName} END"); - return result; - } + public Task InvokeNotification(object?[] args) => + (Task)this.handlerMethod.Invoke(this.target, args.ToArray())!; public async Task InvokeRequest(object?[] args) { - Debug.WriteLine($"InvokeRequest {this.MethodName} START"); var task = (Task)this.handlerMethod.Invoke(this.target, args.ToArray())!; - Debug.WriteLine($"InvokeRequest {this.MethodName} END"); await task; if (this.DeclaredReturnType == typeof(Task)) @@ -124,9 +117,7 @@ public Task InvokeNotification(object?[] args) } else { - Debug.WriteLine($"EEEE {this.MethodName} START"); var getResult = (MethodInfo)task.GetType().GetMemberWithSameMetadataDefinitionAs(taskGetResult); - Debug.WriteLine($"EEEE {this.MethodName} END"); return getResult.Invoke(task, null); } } diff --git a/src/Draco.Lsp/Server/TextDocument/ITextDocumentDidChange.cs b/src/Draco.Lsp/Server/TextDocument/ITextDocumentDidChange.cs index 53c33432c..30d8b6f91 100644 --- a/src/Draco.Lsp/Server/TextDocument/ITextDocumentDidChange.cs +++ b/src/Draco.Lsp/Server/TextDocument/ITextDocumentDidChange.cs @@ -12,5 +12,5 @@ public interface ITextDocumentDidChange public TextDocumentChangeRegistrationOptions DidChangeRegistrationOptions { get; } [Notification("textDocument/didChange", Mutating = true)] - public Task TextDocumentDidChangeAsync(DidChangeTextDocumentParams param, CancellationToken cancellationToken); + public Task TextDocumentDidChangeAsync(DidChangeTextDocumentParams param); } diff --git a/src/Draco.Lsp/Server/TextDocument/ITextDocumentDidClose.cs b/src/Draco.Lsp/Server/TextDocument/ITextDocumentDidClose.cs index 79f9bdd28..1ac0fa0be 100644 --- a/src/Draco.Lsp/Server/TextDocument/ITextDocumentDidClose.cs +++ b/src/Draco.Lsp/Server/TextDocument/ITextDocumentDidClose.cs @@ -12,5 +12,5 @@ public interface ITextDocumentDidClose public TextDocumentRegistrationOptions DidCloseRegistrationOptions { get; } [Notification("textDocument/didClose", Mutating = true)] - public Task TextDocumentDidCloseAsync(DidCloseTextDocumentParams param, CancellationToken cancellationToken); + public Task TextDocumentDidCloseAsync(DidCloseTextDocumentParams param); } diff --git a/src/Draco.Lsp/Server/TextDocument/ITextDocumentDidOpen.cs b/src/Draco.Lsp/Server/TextDocument/ITextDocumentDidOpen.cs index 8050a8830..3822ec2be 100644 --- a/src/Draco.Lsp/Server/TextDocument/ITextDocumentDidOpen.cs +++ b/src/Draco.Lsp/Server/TextDocument/ITextDocumentDidOpen.cs @@ -12,5 +12,5 @@ public interface ITextDocumentDidOpen public TextDocumentRegistrationOptions DidOpenRegistrationOptions { get; } [Notification("textDocument/didOpen", Mutating = true)] - public Task TextDocumentDidOpenAsync(DidOpenTextDocumentParams param, CancellationToken cancellationToken); + public Task TextDocumentDidOpenAsync(DidOpenTextDocumentParams param); } diff --git a/src/Draco.Lsp/Server/Workspace/IDidDeleteFiles.cs b/src/Draco.Lsp/Server/Workspace/IDidDeleteFiles.cs index b31fb12db..8c3eda79b 100644 --- a/src/Draco.Lsp/Server/Workspace/IDidDeleteFiles.cs +++ b/src/Draco.Lsp/Server/Workspace/IDidDeleteFiles.cs @@ -11,6 +11,6 @@ public interface IDidDeleteFiles [RegistrationOptions("workspace/didDeleteFiles")] public FileOperationRegistrationOptions DidDeleteFileRegistrationOptions { get; } - [Notification("workspace/didDeleteFiles")] - public Task DidDeleteFilesAsync(DeleteFilesParams param, CancellationToken cancellationToken); + [Notification("workspace/didDeleteFiles", Mutating = true)] + public Task DidDeleteFilesAsync(DeleteFilesParams param); }