Skip to content

Commit

Permalink
refactor: 替换 Assemblies 属性为 Packages 属性
Browse files Browse the repository at this point in the history
在多个文件中进行了以下重构:
- `CodeCheckRequest.cs`、`HoverInfoRequest.cs`、`SignatureHelpRequest.cs`、`TabCompletionRequest.cs` 中移除了构造函数和 `Assemblies` 属性,添加了 `Packages` 属性,并将 `Assemblies` 属性替换为 `Packages` 属性。
- `CodeRunner.cs` 中移除了下载 NuGet 包的注释,添加了加载 NuGet 包的代码。
- `CompletitionRequestHandler.cs` 中添加了 `monacoEditorCSharp.DataHelpers` 的引用,并在处理请求的方法中添加了加载 NuGet 包的代码。
- `DownloadNugetPackages.cs` 中修改了加载 NuGet 包的逻辑,添加了对 `netstandard2.1` 的支持,并添加了 `DownloadAllPackages` 方法。
- `Program.cs` 中修改了处理请求的逻辑,添加了对 `Packages` 属性的处理,并将其转换为字符串传递给处理方法。
- `csharpLanguageProvider.js` 中修改了请求的构造逻辑,添加了对 `Packages` 属性的处理,并将其包含在请求中。
  • Loading branch information
gaoconggit committed Dec 16, 2024
1 parent 2e8b04a commit 465d601
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 60 deletions.
12 changes: 4 additions & 8 deletions MonacoRoslynCompletionProvider/Api/CodeCheckRequest.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
namespace MonacoRoslynCompletionProvider.Api
using System.Collections.Generic;

namespace MonacoRoslynCompletionProvider.Api
{
public class CodeCheckRequest : IRequest
{
public CodeCheckRequest()
{ }

public CodeCheckRequest(string code, string[] assemblies)
{
this.Code = code;
this.Assemblies = assemblies;
}

public virtual string Code { get; set; }

public virtual string[] Assemblies { get; set; }
public List<Package> Packages { get; set; }
}
}
2 changes: 1 addition & 1 deletion MonacoRoslynCompletionProvider/Api/CodeRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static async Task<RunResult> RunProgramCodeAsync(string code, string nuge

try
{
// 下载 NuGet 包
// 加载 NuGet 包
var nugetAssemblies = DownloadNugetPackages.LoadPackages(nuget);

// 解析代码
Expand Down
13 changes: 4 additions & 9 deletions MonacoRoslynCompletionProvider/Api/HoverInfoRequest.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
namespace MonacoRoslynCompletionProvider.Api
using System.Collections.Generic;

namespace MonacoRoslynCompletionProvider.Api
{
public class HoverInfoRequest : IRequest
{
public HoverInfoRequest()
{ }

public HoverInfoRequest(string code, int position, string[] assemblies)
{
this.Code = code;
this.Position = position;
this.Assemblies = assemblies;
}

public virtual string Code { get; set; }

public virtual int Position { get; set; }

public virtual string[] Assemblies { get; set; }

public List<Package> Packages { get; set; }
}
}
13 changes: 4 additions & 9 deletions MonacoRoslynCompletionProvider/Api/SignatureHelpRequest.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
namespace MonacoRoslynCompletionProvider.Api
using System.Collections.Generic;

namespace MonacoRoslynCompletionProvider.Api
{
public class SignatureHelpRequest : IRequest
{
public SignatureHelpRequest()
{ }

public SignatureHelpRequest(string code, int position, string[] assemblies)
{
this.Code = code;
this.Position = position;
this.Assemblies = assemblies;
}

public virtual string Code { get; set; }

public virtual int Position { get; set; }

public virtual string[] Assemblies { get; set; }
public List<Package> Packages { get; set; }
}
}
13 changes: 4 additions & 9 deletions MonacoRoslynCompletionProvider/Api/TabCompletionRequest.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
namespace MonacoRoslynCompletionProvider.Api
using System.Collections.Generic;

namespace MonacoRoslynCompletionProvider.Api
{
public class TabCompletionRequest : IRequest
{
public TabCompletionRequest()
{ }

public TabCompletionRequest(string code, int position, string[] assemblies)
{
this.Code = code;
this.Position = position;
this.Assemblies = assemblies;
}

public virtual string Code { get; set; }

public virtual int Position { get; set; }

public virtual string[] Assemblies { get; set; }

public List<Package> Packages { get; set; }
}
}
27 changes: 18 additions & 9 deletions MonacoRoslynCompletionProvider/CompletitionRequestHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.Host;
using System;
using System.Collections.Generic;
using monacoEditorCSharp.DataHelpers;

namespace MonacoRoslynCompletionProvider
{
Expand All @@ -26,38 +27,46 @@ .. AppDomain.CurrentDomain.GetAssemblies()
".\\Dll\\CSRedisCore.dll",
".\\Dll\\RestSharp.dll",
];
public async static Task<TabCompletionResult[]> Handle(TabCompletionRequest tabCompletionRequest)
public async static Task<TabCompletionResult[]> Handle(TabCompletionRequest tabCompletionRequest, string nuget)
{
var workspace = CompletionWorkspace.Create(tabCompletionRequest.Assemblies = s_assemblies);
// 加载 NuGet 包
var nugetAssembliesArray = DownloadNugetPackages.LoadPackages(nuget).Select(a => a.Location).ToArray();
var workspace = CompletionWorkspace.Create([.. nugetAssembliesArray, .. s_assemblies]);
var document = await workspace.CreateDocument(tabCompletionRequest.Code);
return await document.GetTabCompletion(tabCompletionRequest.Position, CancellationToken.None);
}

public async static Task<HoverInfoResult> Handle(HoverInfoRequest hoverInfoRequest)
public async static Task<HoverInfoResult> Handle(HoverInfoRequest hoverInfoRequest, string nuget)
{
var workspace = CompletionWorkspace.Create(hoverInfoRequest.Assemblies = s_assemblies);
// 加载 NuGet 包
var nugetAssembliesArray = DownloadNugetPackages.LoadPackages(nuget).Select(a => a.Location).ToArray();

var workspace = CompletionWorkspace.Create([.. nugetAssembliesArray, .. s_assemblies]);
var document = await workspace.CreateDocument(hoverInfoRequest.Code);
return await document.GetHoverInformation(hoverInfoRequest.Position, CancellationToken.None);
}

public async static Task<CodeCheckResult[]> Handle(CodeCheckRequest codeCheckRequest)
public async static Task<CodeCheckResult[]> Handle(CodeCheckRequest codeCheckRequest, string nuget)
{
var workspace = CompletionWorkspace.Create(codeCheckRequest.Assemblies = s_assemblies);
// 加载 NuGet 包
var nugetAssembliesArray = DownloadNugetPackages.LoadPackages(nuget).Select(a => a.Location).ToArray();

var workspace = CompletionWorkspace.Create([.. nugetAssembliesArray, .. s_assemblies]);
var document = await workspace.CreateDocument(codeCheckRequest.Code);
return await document.GetCodeCheckResults(CancellationToken.None);
}

public async static Task<SignatureHelpResult> Handle(SignatureHelpRequest signatureHelpRequest)
public async static Task<SignatureHelpResult> Handle(SignatureHelpRequest signatureHelpRequest, string nuget)
{
var workspace = CompletionWorkspace.Create(signatureHelpRequest.Assemblies = s_assemblies);
var nugetAssembliesArray = DownloadNugetPackages.LoadPackages(nuget).Select(a => a.Location).ToArray();
var workspace = CompletionWorkspace.Create([.. nugetAssembliesArray, .. s_assemblies]);
var document = await workspace.CreateDocument(signatureHelpRequest.Code);
return await document.GetSignatureHelp(signatureHelpRequest.Position, CancellationToken.None);
}

//格式化代码
public static string FormatCode(string sourceCode)
{

var assemblies = new[]
{
Assembly.Load("Microsoft.CodeAnalysis"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public static List<Assembly> LoadPackages(string packages)
{
try
{
if (file.EndsWith(@$"net8.0\{downloadItem}.dll") || file.Contains(@$"netstandard2.0\{downloadItem}.dll"))
var fileName = Path.GetFileName(file);
if (file.EndsWith(@$"net8.0\{fileName}") ||
file.Contains(@$"netstandard2.0\{fileName}") ||
file.Contains(@$"netstandard2.1\{fileName}"))
{
var assembly = Assembly.LoadFrom(file);
assemblies.Add(assembly);
Expand All @@ -62,7 +65,7 @@ public static List<Assembly> LoadPackages(string packages)
}
return assemblies;
}

public static void DownloadAllPackages(string packages)
{
if (!String.IsNullOrWhiteSpace(packages))
Expand Down
12 changes: 8 additions & 4 deletions SharpPad/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,32 @@
if (e.Request.Path.Value?.EndsWith("complete") == true)
{
var tabCompletionRequest = JsonSerializer.Deserialize<TabCompletionRequest>(text);
var tabCompletionResults = await CompletitionRequestHandler.Handle(tabCompletionRequest);
string nugetPackages = string.Join(" ", tabCompletionRequest?.Packages.Select(p => $"{p.Id},{p.Version};{Environment.NewLine}") ?? []);
var tabCompletionResults = await CompletitionRequestHandler.Handle(tabCompletionRequest, nugetPackages);
await JsonSerializer.SerializeAsync(e.Response.Body, tabCompletionResults);
return;
}
else if (e.Request.Path.Value?.EndsWith("signature") == true)
{
var signatureHelpRequest = JsonSerializer.Deserialize<SignatureHelpRequest>(text);
var signatureHelpResult = await CompletitionRequestHandler.Handle(signatureHelpRequest);
string nugetPackages = string.Join(" ", signatureHelpRequest?.Packages.Select(p => $"{p.Id},{p.Version};{Environment.NewLine}") ?? []);
var signatureHelpResult = await CompletitionRequestHandler.Handle(signatureHelpRequest, nugetPackages);
await JsonSerializer.SerializeAsync(e.Response.Body, signatureHelpResult);
return;
}
else if (e.Request.Path.Value?.EndsWith("hover") == true)
{
var hoverInfoRequest = JsonSerializer.Deserialize<HoverInfoRequest>(text);
var hoverInfoResult = await CompletitionRequestHandler.Handle(hoverInfoRequest);
string nugetPackages = string.Join(" ", hoverInfoRequest?.Packages.Select(p => $"{p.Id},{p.Version};{Environment.NewLine}") ?? []);
var hoverInfoResult = await CompletitionRequestHandler.Handle(hoverInfoRequest, nugetPackages);
await JsonSerializer.SerializeAsync(e.Response.Body, hoverInfoResult);
return;
}
else if (e.Request.Path.Value?.EndsWith("codeCheck") == true)
{
var codeCheckRequest = JsonSerializer.Deserialize<CodeCheckRequest>(text);
var codeCheckResults = await CompletitionRequestHandler.Handle(codeCheckRequest);
string nugetPackages = string.Join(" ", codeCheckRequest?.Packages.Select(p => $"{p.Id},{p.Version};{Environment.NewLine}") ?? []);
var codeCheckResults = await CompletitionRequestHandler.Handle(codeCheckRequest, nugetPackages);
await JsonSerializer.SerializeAsync(e.Response.Body, codeCheckResults);
return;
}
Expand Down
39 changes: 30 additions & 9 deletions SharpPad/wwwroot/csharpLanguageProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
case 'run': endPoint = '/completion/run'; break;
case 'addPackages': endPoint = '/completion/addPackages'; break;
}

// 延迟超过1秒后才显示加载中样式
const notification = document.getElementById('notification');
const showNotificationDelay = 1000; // 1 second
Expand All @@ -18,7 +18,7 @@
notification.style.backgroundColor = 'rgba(33, 150, 243, 0.9)';
notification.style.display = 'block';
}, showNotificationDelay);

try {
if (type === 'run') {
// 使用 POST 请求发送代码和包信息
Expand Down Expand Up @@ -51,18 +51,21 @@

function registerCsharpProvider() {

var assemblies = [
];

monaco.languages.registerCompletionItemProvider('csharp', {
triggerCharacters: [".", " "],
provideCompletionItems: async (model, position) => {
let suggestions = [];

const file = getCurrentFile();
const packages = file?.nugetConfig?.packages || [];

let request = {
Code: model.getValue(),
Position: model.getOffsetAt(position),
Assemblies: assemblies
Packages: packages.map(p => ({
Id: p.id,
Version: p.version
}))
}

let resultQ = await sendRequest("complete", request);
Expand All @@ -88,10 +91,16 @@ function registerCsharpProvider() {

provideSignatureHelp: async (model, position, token, context) => {

const file = getCurrentFile();
const packages = file?.nugetConfig?.packages || [];

let request = {
Code: model.getValue(),
Position: model.getOffsetAt(position),
Assemblies: assemblies
Packages: packages.map(p => ({
Id: p.id,
Version: p.version
}))
}

let resultQ = await sendRequest("signature", request);
Expand Down Expand Up @@ -130,10 +139,16 @@ function registerCsharpProvider() {
monaco.languages.registerHoverProvider('csharp', {
provideHover: async function (model, position) {

const file = getCurrentFile();
const packages = file?.nugetConfig?.packages || [];

let request = {
Code: model.getValue(),
Position: model.getOffsetAt(position),
Assemblies: assemblies
Packages: packages.map(p => ({
Id: p.id,
Version: p.version
}))
}

let resultQ = await sendRequest("hover", request);
Expand All @@ -157,9 +172,15 @@ function registerCsharpProvider() {
monaco.editor.onDidCreateModel(function (model) {
async function validate() {

const file = getCurrentFile();
const packages = file?.nugetConfig?.packages || [];

let request = {
Code: model.getValue(),
Assemblies: assemblies
Packages: packages.map(p => ({
Id: p.id,
Version: p.version
}))
}

let resultQ = await sendRequest("codeCheck", request)
Expand Down

0 comments on commit 465d601

Please sign in to comment.