Skip to content

Commit

Permalink
Fix response error
Browse files Browse the repository at this point in the history
  • Loading branch information
CppCXY committed Jul 7, 2024
1 parent be51aae commit b392ba2
Show file tree
Hide file tree
Showing 15 changed files with 90 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace EmmyLua.LanguageServer.Framework.Protocol.Capabilities.Client;
namespace EmmyLua.LanguageServer.Framework.Protocol.Capabilities.Client.NotebookDocumentClientCapabilities;

public class NotebookDocumentSyncClientCapabilities
{
Expand Down
2 changes: 1 addition & 1 deletion LanguageServer.Framework/Protocol/JsonProtocolContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using EmmyLua.LanguageServer.Framework.Protocol.Model.File;
using EmmyLua.LanguageServer.Framework.Protocol.Model.Kind;
using EmmyLua.LanguageServer.Framework.Protocol.Model.WorkDoneProgress;
using EmmyLua.LanguageServer.Framework.Protocol.Server.Request.Initialize;
using EmmyLua.LanguageServer.Framework.Protocol.Request.Initialize;
using Range = System.Range;

namespace EmmyLua.LanguageServer.Framework.Protocol;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization;
using EmmyLua.LanguageServer.Framework.Protocol.JsonRpc;
using EmmyLua.LanguageServer.Framework.Protocol.Model.Util;

namespace EmmyLua.LanguageServer.Framework.Protocol.Server.Notification;
namespace EmmyLua.LanguageServer.Framework.Protocol.Notification;

[JsonRpc("$/cancelRequest")]
public class CancelParams
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace EmmyLua.LanguageServer.Framework.Protocol.Notification;

public class InitializedParams
{

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text.Json.Serialization;
using EmmyLua.LanguageServer.Framework.Protocol.JsonRpc;

namespace EmmyLua.LanguageServer.Framework.Protocol.Server.Notification.Progress;
namespace EmmyLua.LanguageServer.Framework.Protocol.Notification.Progress;

[JsonRpc("$/progress")]
public class ProgressParams
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace EmmyLua.LanguageServer.Framework.Protocol.Server.Request.Initialize;
namespace EmmyLua.LanguageServer.Framework.Protocol.Request.Initialize;

public class ClientInfo
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using EmmyLua.LanguageServer.Framework.Protocol.Capabilities;
using EmmyLua.LanguageServer.Framework.Protocol.Capabilities.Client;
using EmmyLua.LanguageServer.Framework.Protocol.Capabilities.Client.ClientCapabilities;
using EmmyLua.LanguageServer.Framework.Protocol.JsonRpc;
using EmmyLua.LanguageServer.Framework.Protocol.Model;

namespace EmmyLua.LanguageServer.Framework.Protocol.Server.Request.Initialize;
namespace EmmyLua.LanguageServer.Framework.Protocol.Request.Initialize;

[JsonRpc("initialize")]
public class InitializeParams
Expand All @@ -19,15 +17,15 @@ public class InitializeParams
*/
[JsonPropertyName("processId")]
public int? ProcessId { get; set; }

/**
* Information about the client
*
* @since 3.15.0
*/
[JsonPropertyName("clientInfo")]
public ClientInfo? ClientInfo { get; set; }

/**
* The locale the client is currently showing the user interface
* in. This must not necessarily be the locale of the operating
Expand All @@ -40,7 +38,7 @@ public class InitializeParams
*/
[JsonPropertyName("locale")]
public string? Locale { get; set; }

/**
* The rootPath of the workspace. Is null
* if no folder is open.
Expand All @@ -49,15 +47,15 @@ public class InitializeParams
*/
[JsonPropertyName("rootPath"), Obsolete("Use `rootUri` instead.")]
public string? RootPath { get; set; }

/**
* The rootUri of the workspace. Is null if no
* folder is open. If both `rootPath` and `rootUri` are set
* `rootUri` wins.
*/
[JsonPropertyName("rootUri")]
public DocumentUri? RootUri { get; set; }

/**
* User provided initialization options.
*/
Expand All @@ -75,7 +73,7 @@ public class InitializeParams
*/
[JsonPropertyName("trace")]
public TraceValue? Trace { get; set; }

/**
* The workspace folders configured in the client when the server starts.
* This property is only available if the client supports workspace folders.
Expand All @@ -86,4 +84,4 @@ public class InitializeParams
*/
[JsonPropertyName("workspaceFolders")]
public List<WorkspaceFolder>? WorkspaceFolders { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Text.Json.Serialization;
using EmmyLua.LanguageServer.Framework.Protocol.Capabilities.Server;

namespace EmmyLua.LanguageServer.Framework.Protocol.Server.Request.Initialize;
namespace EmmyLua.LanguageServer.Framework.Protocol.Request.Initialize;

public class InitializeResult
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Text.Json.Serialization;

namespace EmmyLua.LanguageServer.Framework.Protocol.Server.Request.Initialize;
namespace EmmyLua.LanguageServer.Framework.Protocol.Request.Initialize;

public class ServerInfo
{
Expand Down
12 changes: 10 additions & 2 deletions LanguageServer.Framework/Server/Handler/InitializeHandlerBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using EmmyLua.LanguageServer.Framework.Protocol.JsonRpc;
using EmmyLua.LanguageServer.Framework.Protocol.Server.Request.Initialize;
using EmmyLua.LanguageServer.Framework.Protocol.Notification;
using EmmyLua.LanguageServer.Framework.Protocol.Request.Initialize;
using EmmyLua.LanguageServer.Framework.Server.Handler.Base;

namespace EmmyLua.LanguageServer.Framework.Server.Handler;
Expand All @@ -12,4 +13,11 @@ public virtual Task<InitializeResult> Handle(InitializeParams request, Cancellat
Console.Error.Write("hello world");
return Task.FromResult(new InitializeResult());
}
}

[JsonRpc("initialized")]
public virtual Task Handle(InitializedParams request, CancellationToken cancellationToken)
{
Console.Error.Write("hello world2");
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@ namespace EmmyLua.LanguageServer.Framework.Server.JsonProtocol;

public class JsonProtocolWriter(Stream output, JsonSerializerOptions jsonSerializerOptions)
{
private StreamWriter Writer { get; } = new(output, Encoding.UTF8);

public async Task WriteAsync(object? message, Type messageType)
public void Write(object? message, Type messageType)
{
var json = JsonSerializer.Serialize(message, messageType, jsonSerializerOptions);
var contentLength = Encoding.UTF8.GetByteCount(json);
await Writer.WriteLineAsync($"Content-Length: {contentLength}\r\n");
await Writer.WriteAsync(json);
await Writer.FlushAsync();
var writeContent = $"Content-Length:{contentLength}\r\n\r\n{json}";
var writeContentBytes = Encoding.UTF8.GetBytes(writeContent);
output.Write(writeContentBytes);
}
}
}
78 changes: 41 additions & 37 deletions LanguageServer.Framework/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ public class LanguageServer
{
TypeInfoResolver = JsonProtocolContext.Default
};

private JsonProtocolReader Reader { get; }

private JsonProtocolWriter Writer { get; }

private bool IsInitialized { get; set; }

private bool IsRunning { get; set; }
Expand All @@ -45,7 +45,7 @@ private LanguageServer(Stream input, Stream output)
Reader = new JsonProtocolReader(input, JsonSerializerOptions);
Writer = new JsonProtocolWriter(output, JsonSerializerOptions);
}

public static LanguageServer From(Stream input, Stream output)
{
return new LanguageServer(input, output);
Expand All @@ -56,7 +56,7 @@ public LanguageServer SupportMultiThread()
Scheduler = new MultiThreadScheduler();
return this;
}

public void AddJsonSerializeContext(JsonSerializerContext serializerContext)
{
JsonSerializerOptions.TypeInfoResolverChain.Add(serializerContext);
Expand All @@ -74,9 +74,10 @@ public LanguageServer WithServices(Action<IServiceCollection> servicesAction)
return this;
}

public async Task SendNotification(NotificationMessage notification)
public Task SendNotification(NotificationMessage notification)
{
await Writer.WriteAsync(notification, typeof(NotificationMessage));
Writer.Write(notification, typeof(NotificationMessage));
return Task.CompletedTask;
}

private void InitServices()
Expand Down Expand Up @@ -130,55 +131,58 @@ public async Task Run()
while (!IsRunning)
{
var message = await Reader.ReadAsync();
switch (message)
Scheduler.Schedule(async () =>
{
case RequestMessage request:
switch (message)
{
if (RequestHandlers.TryGetValue(request.Method, out var handler))
case RequestMessage request:
{
var result = handler.Method.Invoke(handler.Instance,
[request.Params, CancellationToken.None]);
if (result is Task task)
if (RequestHandlers.TryGetValue(request.Method, out var handler))
{
await task.ConfigureAwait(false);
if (task.GetType().IsGenericType)
var result = handler.Method.Invoke(handler.Instance,
[request.Params, CancellationToken.None]);
if (result is Task task)
{
result = task.GetType().GetProperty("Result")?.GetValue(task);
await Writer.WriteAsync(result, handler.ReturnType);
}
else
{
await Writer.WriteAsync(result, handler.ReturnType);
await task.ConfigureAwait(false);
if (task.GetType().IsGenericType)
{
result = task.GetType().GetProperty("Result")?.GetValue(task);
Writer.Write(result, handler.ReturnType);
}
else
{
Writer.Write(result, handler.ReturnType);
}
}
}
}
break;
}
case NotificationMessage notification:
{
if (notification.Method == "shutdown")
{
IsRunning = true;
break;
}

if (NotificationHandlers.TryGetValue(notification.Method, out var handler))
case NotificationMessage notification:
{
var result = handler.Method.Invoke(handler.Instance, [notification.Params]);
if (result is Task task)
if (notification.Method == "shutdown")
{
await task.ConfigureAwait(false);
IsRunning = true;
}
}
break;
if (NotificationHandlers.TryGetValue(notification.Method, out var handler))
{
var result = handler.Method.Invoke(handler.Instance, [notification.Params]);
if (result is Task task)
{
await task.ConfigureAwait(false);
}
}
break;
}
}
}
});
}
}
catch (Exception e)
{
Console.Error.WriteLine(e);
}
}
}
}
4 changes: 2 additions & 2 deletions LanguageServer.Framework/Server/Scheduler/IScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public interface IScheduler
{

}
public void Schedule(Func<Task> action);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@

public class MultiThreadScheduler : IScheduler
{

}
public void Schedule(Func<Task> action)
{
Task.Run(action);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

public class SingleThreadScheduler : IScheduler
{
}
public void Schedule(Func<Task> action)
{
action().Wait();
}
}

0 comments on commit b392ba2

Please sign in to comment.