Skip to content

Commit aebf195

Browse files
committed
Tweak roslyn helpers and nullability
1 parent 6cd3c63 commit aebf195

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/RoslynCodeActionHelpers.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@
55
using System.Threading;
66
using System.Threading.Tasks;
77
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
8+
using Microsoft.CodeAnalysis.CSharp;
9+
using Microsoft.CodeAnalysis;
810
using Microsoft.CodeAnalysis.Razor.CodeActions;
911
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
1012
using Microsoft.CodeAnalysis.Razor.Protocol;
1113
using Microsoft.CodeAnalysis.Razor.Protocol.CodeActions;
1214
using Microsoft.VisualStudio.LanguageServer.Protocol;
15+
using Microsoft.CodeAnalysis.Formatting;
1316

1417
namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions;
1518

1619
internal sealed class RoslynCodeActionHelpers(IClientConnection clientConnection) : IRoslynCodeActionHelpers
1720
{
21+
private static readonly Lazy<Workspace> s_workspace = new Lazy<Workspace>(() => new AdhocWorkspace());
22+
1823
private readonly IClientConnection _clientConnection = clientConnection;
1924

20-
public Task<string?> GetFormattedNewFileContentsAsync(IProjectSnapshot projectSnapshot, Uri csharpFileUri, string newFileContent, CancellationToken cancellationToken)
25+
public async Task<string> GetFormattedNewFileContentsAsync(IProjectSnapshot projectSnapshot, Uri csharpFileUri, string newFileContent, CancellationToken cancellationToken)
2126
{
2227
var parameters = new FormatNewFileParams()
2328
{
@@ -31,7 +36,20 @@ internal sealed class RoslynCodeActionHelpers(IClientConnection clientConnection
3136
},
3237
Contents = newFileContent
3338
};
34-
return _clientConnection.SendRequestAsync<FormatNewFileParams, string?>(CustomMessageNames.RazorFormatNewFileEndpointName, parameters, cancellationToken);
39+
40+
var fixedContent = await _clientConnection.SendRequestAsync<FormatNewFileParams, string?>(CustomMessageNames.RazorFormatNewFileEndpointName, parameters, cancellationToken).ConfigureAwait(false);
41+
42+
if (fixedContent is not null)
43+
{
44+
return fixedContent;
45+
}
46+
47+
// Sadly we can't use a "real" workspace here, because we don't have access. If we use our workspace, it wouldn't have the right settings
48+
// for C# formatting, only Razor formatting, and we have no access to Roslyn's real workspace, since it could be in another process.
49+
var node = await CSharpSyntaxTree.ParseText(newFileContent, cancellationToken: cancellationToken).GetRootAsync(cancellationToken).ConfigureAwait(false);
50+
node = Formatter.Format(node, s_workspace.Value, cancellationToken: cancellationToken);
51+
52+
return node.ToFullString();
3553
}
3654

3755
public Task<TextEdit[]?> GetSimplifiedTextEditsAsync(DocumentContext documentContext, Uri? codeBehindUri, TextEdit edit, CancellationToken cancellationToken)

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/ExtractToCodeBehindCodeActionResolver.cs

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
using Microsoft.AspNetCore.Razor.Language.Intermediate;
1111
using Microsoft.AspNetCore.Razor.PooledObjects;
1212
using Microsoft.AspNetCore.Razor.Utilities;
13-
using Microsoft.CodeAnalysis.CSharp;
14-
using Microsoft.CodeAnalysis.Formatting;
1513
using Microsoft.CodeAnalysis.Razor.CodeActions.Models;
1614
using Microsoft.CodeAnalysis.Razor.Formatting;
1715
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
@@ -26,8 +24,6 @@ internal class ExtractToCodeBehindCodeActionResolver(
2624
LanguageServerFeatureOptions languageServerFeatureOptions,
2725
IRoslynCodeActionHelpers roslynCodeActionHelpers) : IRazorCodeActionResolver
2826
{
29-
private static readonly Workspace s_workspace = new AdhocWorkspace();
30-
3127
private readonly LanguageServerFeatureOptions _languageServerFeatureOptions = languageServerFeatureOptions;
3228
private readonly IRoslynCodeActionHelpers _roslynCodeActionHelpers = roslynCodeActionHelpers;
3329

@@ -134,18 +130,6 @@ private async Task<string> GenerateCodeBehindClassAsync(IProjectSnapshot project
134130

135131
var newFileContent = builder.ToString();
136132

137-
var fixedContent = await _roslynCodeActionHelpers.GetFormattedNewFileContentsAsync(project, codeBehindUri, newFileContent, cancellationToken).ConfigureAwait(false);
138-
139-
if (fixedContent is null)
140-
{
141-
// Sadly we can't use a "real" workspace here, because we don't have access. If we use our workspace, it wouldn't have the right settings
142-
// for C# formatting, only Razor formatting, and we have no access to Roslyn's real workspace, since it could be in another process.
143-
var node = await CSharpSyntaxTree.ParseText(newFileContent, cancellationToken: cancellationToken).GetRootAsync(cancellationToken).ConfigureAwait(false);
144-
node = Formatter.Format(node, s_workspace, cancellationToken: cancellationToken);
145-
146-
return node.ToFullString();
147-
}
148-
149-
return fixedContent;
133+
return await _roslynCodeActionHelpers.GetFormattedNewFileContentsAsync(project, codeBehindUri, newFileContent, cancellationToken).ConfigureAwait(false);
150134
}
151135
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/CodeActions/Razor/IRoslynCodeActionHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Microsoft.CodeAnalysis.Razor.CodeActions;
1111

1212
internal interface IRoslynCodeActionHelpers
1313
{
14-
Task<string?> GetFormattedNewFileContentsAsync(IProjectSnapshot projectSnapshot, Uri csharpFileUri, string newFileContent, CancellationToken cancellationToken);
14+
Task<string> GetFormattedNewFileContentsAsync(IProjectSnapshot projectSnapshot, Uri csharpFileUri, string newFileContent, CancellationToken cancellationToken);
1515

1616
/// <summary>
1717
/// Apply the edit to the specified document, get Roslyn to simplify it, and return the simplified edit

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/CodeActions/RoslynCodeActionHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ namespace Microsoft.CodeAnalysis.Remote.Razor;
2222
[Export(typeof(IRoslynCodeActionHelpers)), Shared]
2323
internal sealed class RoslynCodeActionHelpers : IRoslynCodeActionHelpers
2424
{
25-
public Task<string?> GetFormattedNewFileContentsAsync(IProjectSnapshot projectSnapshot, Uri csharpFileUri, string newFileContent, CancellationToken cancellationToken)
25+
public Task<string> GetFormattedNewFileContentsAsync(IProjectSnapshot projectSnapshot, Uri csharpFileUri, string newFileContent, CancellationToken cancellationToken)
2626
{
2727
Debug.Assert(projectSnapshot is RemoteProjectSnapshot);
2828
var project = ((RemoteProjectSnapshot)projectSnapshot).Project;
2929

3030
var document = project.AddDocument(RazorUri.GetDocumentFilePathFromUri(csharpFileUri), newFileContent);
3131

32-
return ExternalHandlers.CodeActions.GetFormattedNewFileContentAsync(document, cancellationToken)!;
32+
return ExternalHandlers.CodeActions.GetFormattedNewFileContentAsync(document, cancellationToken);
3333
}
3434

3535
public async Task<TextEdit[]?> GetSimplifiedTextEditsAsync(DocumentContext documentContext, Uri? codeBehindUri, TextEdit edit, CancellationToken cancellationToken)

0 commit comments

Comments
 (0)