-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
207 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ riderModule.iml | |
Build/win32 | ||
Build/linux | ||
Build/macosx | ||
.vs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 45 additions & 45 deletions
90
LanguageServer.Framework/Protocol/JsonRpc/MethodMessageConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,45 @@ | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace EmmyLua.LanguageServer.Framework.Protocol.JsonRpc; | ||
|
||
public class MethodMessageConverter : JsonConverter<MethodMessage> | ||
{ | ||
public override MethodMessage Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
using var jsonDoc = JsonDocument.ParseValue(ref reader); | ||
var root = jsonDoc.RootElement; | ||
var method = root.GetProperty("method").GetString(); | ||
var paramsJson = root.GetProperty("params").GetRawText(); | ||
if (method is not null) | ||
{ | ||
var methodType = JsonRpcHelper.GetMethodType(method); | ||
var methodParams = JsonSerializer.Deserialize(paramsJson, methodType, options); | ||
if (root.TryGetProperty("id", out var id)) | ||
{ | ||
if (id.ValueKind == JsonValueKind.Number) | ||
{ | ||
return new RequestMessage(id.GetInt32(), method, methodParams); | ||
} | ||
else if (id.ValueKind == JsonValueKind.String) | ||
{ | ||
return new RequestMessage(id.GetString()!, method, methodParams); | ||
} | ||
} | ||
else | ||
{ | ||
return new NotificationMessage(method, methodParams); | ||
} | ||
} | ||
|
||
// Fallback to default deserialization for other methods | ||
// This part needs to be adjusted based on your specific needs and structure | ||
return JsonSerializer.Deserialize<MethodMessage>(root.GetRawText(), options)!; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, MethodMessage value, JsonSerializerOptions options) | ||
{ | ||
JsonSerializer.Serialize(writer, value, options); | ||
} | ||
} | ||
|
||
// using System.Text.Json; | ||
// using System.Text.Json.Serialization; | ||
// | ||
// namespace EmmyLua.LanguageServer.Framework.Protocol.JsonRpc; | ||
// | ||
// public class MethodMessageConverter : JsonConverter<MethodMessage> | ||
// { | ||
// public override MethodMessage Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
// { | ||
// using var jsonDoc = JsonDocument.ParseValue(ref reader); | ||
// var root = jsonDoc.RootElement; | ||
// var method = root.GetProperty("method").GetString(); | ||
// var paramsJson = root.GetProperty("params").GetRawText(); | ||
// if (method is not null) | ||
// { | ||
// var methodType = JsonRpcHelper.GetMethodType(method); | ||
// var methodParams = JsonSerializer.Deserialize(paramsJson, methodType, options); | ||
// if (root.TryGetProperty("id", out var id)) | ||
// { | ||
// if (id.ValueKind == JsonValueKind.Number) | ||
// { | ||
// return new RequestMessage(id.GetInt32(), method, methodParams); | ||
// } | ||
// else if (id.ValueKind == JsonValueKind.String) | ||
// { | ||
// return new RequestMessage(id.GetString()!, method, methodParams); | ||
// } | ||
// } | ||
// else | ||
// { | ||
// return new NotificationMessage(method, methodParams); | ||
// } | ||
// } | ||
// | ||
// // Fallback to default deserialization for other methods | ||
// // This part needs to be adjusted based on your specific needs and structure | ||
// return JsonSerializer.Deserialize<MethodMessage>(root.GetRawText(), options)!; | ||
// } | ||
// | ||
// public override void Write(Utf8JsonWriter writer, MethodMessage value, JsonSerializerOptions options) | ||
// { | ||
// JsonSerializer.Serialize(writer, value, options); | ||
// } | ||
// } | ||
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
LanguageServer.Framework/Protocol/Notification/InitializedParams.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
namespace EmmyLua.LanguageServer.Framework.Protocol.Notification; | ||
using EmmyLua.LanguageServer.Framework.Protocol.JsonRpc; | ||
|
||
namespace EmmyLua.LanguageServer.Framework.Protocol.Notification; | ||
|
||
[JsonRpc("initialized")] | ||
public class InitializedParams | ||
{ | ||
|
||
} |
117 changes: 117 additions & 0 deletions
117
LanguageServer.SourceGenerator/JsonRpcMessageSourceGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Text; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
[Generator(LanguageNames.CSharp)] | ||
public class JsonRpcMessageSourceGenerator : IIncrementalGenerator | ||
{ | ||
public void Initialize(IncrementalGeneratorInitializationContext context) | ||
{ | ||
var classDeclarations = context.SyntaxProvider | ||
.CreateSyntaxProvider( | ||
predicate: (s, _) => | ||
s is ClassDeclarationSyntax declarationSyntax && declarationSyntax.AttributeLists.Count > 0, | ||
transform: (ctx, _) => ctx.Node as ClassDeclarationSyntax) | ||
.Where(m => m != null); | ||
// 2. 合并信息,生成代码 | ||
context.RegisterSourceOutput(classDeclarations.Collect(), (spc, classes) => | ||
{ | ||
var sourceBuilder = new StringBuilder(@" | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Collections.Generic; | ||
using EmmyLua.LanguageServer.Framework.Protocol.JsonRpc; | ||
namespace Generated | ||
{ | ||
public class JsonRpcMethodMessageConverter : JsonConverter<MethodMessage> | ||
{ | ||
private readonly Dictionary<string, Type> _methodMap = new Dictionary<string, Type>(); | ||
public JsonRpcMethodMessageConverter() | ||
{ | ||
"); | ||
var indent = 12; | ||
var indentString = new string(' ', indent); | ||
foreach (var classDeclaration in classes) | ||
{ | ||
var className = classDeclaration.Identifier.Text; | ||
var namespaceName = (classDeclaration.Parent as NamespaceDeclarationSyntax)?.Name.ToString(); | ||
var attribute = classDeclaration.AttributeLists | ||
.SelectMany(a => a.Attributes) | ||
.FirstOrDefault(it=>it.Name.ToString() == "JsonRpc"); | ||
if (attribute == null) | ||
{ | ||
continue; | ||
} | ||
var methodName = (attribute.ArgumentList?.Arguments[0].Expression as LiteralExpressionSyntax)?.Token | ||
.ValueText; | ||
if (string.IsNullOrEmpty(namespaceName)) | ||
{ | ||
sourceBuilder.AppendLine($@"{indentString}_methodMap[""{methodName}""] = typeof({className});"); | ||
} | ||
else | ||
{ | ||
sourceBuilder.AppendLine( | ||
$@"{indentString}_methodMap[""{methodName}""] = typeof({namespaceName}.{className});"); | ||
} | ||
} | ||
sourceBuilder.Append(@" | ||
} | ||
public override MethodMessage Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
using var jsonDoc = JsonDocument.ParseValue(ref reader); | ||
var root = jsonDoc.RootElement; | ||
var method = root.GetProperty(""method"").GetString(); | ||
var paramsJson = root.GetProperty(""params"").GetRawText(); | ||
if (method is not null) | ||
{ | ||
if (_methodMap.TryGetValue(method, out var methodType)) | ||
{ | ||
var methodParams = JsonSerializer.Deserialize(paramsJson, methodType, options); | ||
if (root.TryGetProperty(""id"", out var id)) | ||
{ | ||
if (id.ValueKind == JsonValueKind.Number) | ||
{ | ||
return new RequestMessage(id.GetInt32(), method, methodParams); | ||
} | ||
else if (id.ValueKind == JsonValueKind.String) | ||
{ | ||
return new RequestMessage(id.GetString()!, method, methodParams); | ||
} | ||
} | ||
else | ||
{ | ||
return new NotificationMessage(method, methodParams); | ||
} | ||
} | ||
} | ||
// Fallback to default deserialization for other methods | ||
// This part needs to be adjusted based on your specific needs and structure | ||
return JsonSerializer.Deserialize<MethodMessage>(root.GetRawText(), options)!; | ||
} | ||
public override void Write(Utf8JsonWriter writer, MethodMessage value, JsonSerializerOptions options) | ||
{ | ||
JsonSerializer.Serialize(writer, value, options); | ||
} | ||
} | ||
}"); | ||
spc.AddSource("JsonRpcMethodMessageConverter.g.cs", | ||
SourceText.From(sourceBuilder.ToString(), Encoding.UTF8)); | ||
}); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
LanguageServer.SourceGenerator/LanguageServer.SourceGenerator.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0-beta1.24225.1"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.3.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
8 changes: 8 additions & 0 deletions
8
LanguageServer.SourceGenerator/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"profiles": { | ||
"LanguageServer.SourceGenerator": { | ||
"commandName": "DebugRoslynComponent", | ||
"targetProject": "../LanguageServer.Test/LanguageServer.Test.csproj" | ||
} | ||
} | ||
} |