Skip to content

Commit

Permalink
💡 Options for C# editor feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnkwlp committed Feb 5, 2024
1 parent cc2f1da commit 3982c8c
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Passingwind.Abp.ElsaModule;

public class CSharpEditorOptions
{
public bool EnableCodeAnalysis { get; set; }
public bool EnableCompletion { get; set; } = true;
public bool EnableHoverInfo { get; set; } = true;
public bool EnableSignatures { get; set; } = true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public override void ConfigureServices(ServiceConfigurationContext context)
context.Services.AddTransient<IWorkflowCSharpEditorService, WorkflowCSharpEditorService>();

context.Services.AddCSharpScriptEngine();

context.Services.AddOptions<CSharpEditorOptions>();
}

public override void PostConfigureServices(ServiceConfigurationContext context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.CodeAnalysis.Shared.Extensions;
using Microsoft.CodeAnalysis.Text;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Passingwind.Abp.ElsaModule.CSharp;
using Passingwind.Abp.ElsaModule.Scripting.CSharp;
using Passingwind.CSharpScriptEngine;
Expand All @@ -26,12 +27,14 @@ public class WorkflowCSharpEditorService : IWorkflowCSharpEditorService
private readonly ILogger<WorkflowCSharpEditorService> _logger;
private readonly ICSharpTypeDefinitionService _cSharpTypeDefinitionService;
private readonly ICSharpScriptWorkspace _cSharpScriptWorkspace;
private readonly CSharpEditorOptions _cSharpEditorOptions;

public WorkflowCSharpEditorService(ILogger<WorkflowCSharpEditorService> logger, ICSharpTypeDefinitionService cSharpTypeDefinitionService, ICSharpScriptWorkspace cSharpScriptWorkspace)
public WorkflowCSharpEditorService(ILogger<WorkflowCSharpEditorService> logger, ICSharpTypeDefinitionService cSharpTypeDefinitionService, ICSharpScriptWorkspace cSharpScriptWorkspace, IOptions<CSharpEditorOptions> cSharpEditorOptions)
{
_logger = logger;
_cSharpTypeDefinitionService = cSharpTypeDefinitionService;
_cSharpScriptWorkspace = cSharpScriptWorkspace;
_cSharpEditorOptions = cSharpEditorOptions.Value;
}

protected async Task<ICSharpScriptProject> GetProjectAsync(WorkflowDefinition workflowDefinition, CancellationToken cancellationToken = default)
Expand All @@ -53,6 +56,11 @@ protected async Task<ICSharpScriptProject> GetProjectAsync(WorkflowDefinition wo

public async Task<WorkflowCSharpEditorCodeAnalysisResult> GetCodeAnalysisAsync(WorkflowDefinition workflowDefinition, string textId, string text, CancellationToken cancellationToken = default)
{
if (!_cSharpEditorOptions.EnableCodeAnalysis)
{
return new WorkflowCSharpEditorCodeAnalysisResult();
}

var project = await GetProjectAsync(workflowDefinition, cancellationToken);

project.CreateOrUpdateDocument(textId, text);
Expand Down Expand Up @@ -114,6 +122,11 @@ public async Task<WorkflowCSharpEditorFormatterResult> CodeFormatterAsync(string

public async Task<WorkflowCSharpEditorCompletionResult> GetCompletionAsync(WorkflowDefinition workflowDefinition, string textId, string text, int position, CancellationToken cancellationToken = default)
{
if (!_cSharpEditorOptions.EnableCompletion)
{
return new WorkflowCSharpEditorCompletionResult();
}

var project = await GetProjectAsync(workflowDefinition, cancellationToken);

if (position > text.Length)
Expand Down Expand Up @@ -170,6 +183,11 @@ static WorkflowCSharpEditorCompletionItemKind MapKind(SymbolKind symbolKind)

public async Task<WorkflowCSharpEditorHoverInfoResult> GetHoverInfoAsync(WorkflowDefinition workflowDefinition, string textId, string text, int position, CancellationToken cancellationToken = default)
{
if (!_cSharpEditorOptions.EnableHoverInfo)
{
return null;
}

var project = await GetProjectAsync(workflowDefinition, cancellationToken);

var document = project.CreateOrUpdateDocument(textId, text);
Expand Down Expand Up @@ -263,6 +281,11 @@ public async Task<WorkflowCSharpEditorHoverInfoResult> GetHoverInfoAsync(Workflo

public async Task<WorkflowCSharpEditorSignatureResult> GetSignaturesAsync(WorkflowDefinition workflowDefinition, string textId, string text, int position, CancellationToken cancellationToken = default)
{
if (!_cSharpEditorOptions.EnableSignatures)
{
return null;
}

var project = await GetProjectAsync(workflowDefinition, cancellationToken);

var document = project.CreateOrUpdateDocument(textId, text);
Expand Down
2 changes: 1 addition & 1 deletion src/Passingwind.CSharpScript/CSharpScriptHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public static ScriptOptions CreateScriptOptions(IEnumerable<string>? imports = n
var options = ScriptOptions.Default
.WithReferences(DefaultRuntimeMetadataReference)
.AddImports(DefaultImports)
.WithMetadataResolver(CSharpScriptMetadataReferenceResolver.Instance)
.WithMetadataResolver(NuGetMetadataReferenceResolver.Instance)
.WithSourceResolver(SourceFileResolver.Default)
.WithAllowUnsafe(false)
.WithCheckOverflow(true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,54 @@
using System;
/*
* https://github.com/dotnet-script/dotnet-script/blob/master/src/Dotnet.Script.DependencyModel.Nuget/NuGetMetadataReferenceResolver.cs
*/

using System;
using System.Collections.Immutable;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Scripting;

namespace Passingwind.CSharpScriptEngine;

public class CSharpScriptMetadataReferenceResolver : MetadataReferenceResolver
/// <summary>
/// A <see cref="MetadataReferenceResolver"/> decorator that handles
/// references to NuGet packages in scripts.
/// </summary>
public class NuGetMetadataReferenceResolver : MetadataReferenceResolver
{
public static CSharpScriptMetadataReferenceResolver Instance { get; } = new CSharpScriptMetadataReferenceResolver(ScriptMetadataResolver.Default);
public static NuGetMetadataReferenceResolver Instance { get; } = new NuGetMetadataReferenceResolver(ScriptMetadataResolver.Default);

private readonly ScriptMetadataResolver _resolver;
private readonly MetadataReferenceResolver _metadataReferenceResolver;

public CSharpScriptMetadataReferenceResolver(ScriptMetadataResolver resolver)
/// <summary>
/// Initializes a new instance of the <see cref="NuGetMetadataReferenceResolver"/> class.
/// </summary>
/// <param name="metadataReferenceResolver">The target <see cref="MetadataReferenceResolver"/>.</param>
public NuGetMetadataReferenceResolver(MetadataReferenceResolver metadataReferenceResolver)
{
_resolver = resolver;
_metadataReferenceResolver = metadataReferenceResolver;
}

public override bool Equals(object? other) => _resolver.Equals(other);
public override bool Equals(object? other)
{
return _metadataReferenceResolver.Equals(other);
}

public override int GetHashCode() => _resolver.GetHashCode();
public override int GetHashCode()
{
return _metadataReferenceResolver.GetHashCode();
}

public override ImmutableArray<PortableExecutableReference> ResolveReference(string reference, string? baseFilePath, MetadataReferenceProperties properties)
{
// #r nuget:
if (reference.StartsWith("nuget", StringComparison.OrdinalIgnoreCase))
if (reference.StartsWith("nuget", StringComparison.OrdinalIgnoreCase) || reference.StartsWith("sdk", StringComparison.OrdinalIgnoreCase))
{
// HACK We need to return something here to "mark" the reference as resolved.
// https://github.com/dotnet/roslyn/blob/master/src/Compilers/Core/Portable/ReferenceManager/CommonReferenceManager.Resolution.cs#L838
return ImmutableArray<PortableExecutableReference>.Empty.Add(MetadataReference.CreateFromFile(typeof(CSharpScriptMetadataReferenceResolver).GetTypeInfo().Assembly.Location));
return ImmutableArray<PortableExecutableReference>.Empty.Add(
MetadataReference.CreateFromFile(typeof(NuGetMetadataReferenceResolver).GetTypeInfo().Assembly.Location));
}

return _resolver.ResolveReference(reference, baseFilePath, properties);
return _metadataReferenceResolver.ResolveReference(reference, baseFilePath, properties);
}
}
2 changes: 1 addition & 1 deletion src/Passingwind.CSharpScript/CSharpScriptWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ protected CSharpCompilationOptions CreateDefaultCompilationOptions(IEnumerable<s
// generalDiagnosticOption: ReportDiagnostic.Default,
specificDiagnosticOptions: _specificDiagnosticOptions,
concurrentBuild: false,
metadataReferenceResolver: CSharpScriptMetadataReferenceResolver.Instance,
metadataReferenceResolver: NuGetMetadataReferenceResolver.Instance,
nullableContextOptions: NullableContextOptions.Enable);
}

Expand Down
5 changes: 4 additions & 1 deletion src/Passingwind.ElsaDesigner/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[csharp]": {
"editor.defaultFormatter": "ms-dotnettools.csharp"
}
}
}

0 comments on commit 3982c8c

Please sign in to comment.