Skip to content

Commit 40d2a6f

Browse files
authored
Cohost code actions (#11147)
Fixes #10742 Needs dotnet/roslyn#75711 before it will build Also will need to merge in main once #11141 is merged
2 parents 390aced + 31469c4 commit 40d2a6f

File tree

59 files changed

+1667
-199
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1667
-199
lines changed

eng/Version.Details.xml

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,82 +11,82 @@
1111
<Sha>136e43e45e20bd58bf86eeabba0a0fa7e1a4b3ae</Sha>
1212
<SourceBuild RepoName="source-build-reference-packages" ManagedOnly="true" />
1313
</Dependency>
14-
<Dependency Name="Microsoft.Net.Compilers.Toolset" Version="4.13.0-1.24522.7">
14+
<Dependency Name="Microsoft.Net.Compilers.Toolset" Version="4.13.0-2.24554.8">
1515
<Uri>https://github.com/dotnet/roslyn</Uri>
16-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
16+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
1717
</Dependency>
18-
<Dependency Name="Microsoft.CommonLanguageServerProtocol.Framework" Version="4.13.0-1.24522.7">
18+
<Dependency Name="Microsoft.CommonLanguageServerProtocol.Framework" Version="4.13.0-2.24554.8">
1919
<Uri>https://github.com/dotnet/roslyn</Uri>
20-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
20+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
2121
</Dependency>
22-
<Dependency Name="Microsoft.CodeAnalysis.ExternalAccess.Razor" Version="4.13.0-1.24522.7">
22+
<Dependency Name="Microsoft.CodeAnalysis.ExternalAccess.Razor" Version="4.13.0-2.24554.8">
2323
<Uri>https://github.com/dotnet/roslyn</Uri>
24-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
24+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
2525
</Dependency>
26-
<Dependency Name="Microsoft.CodeAnalysis.Common" Version="4.13.0-1.24522.7">
26+
<Dependency Name="Microsoft.CodeAnalysis.Common" Version="4.13.0-2.24554.8">
2727
<Uri>https://github.com/dotnet/roslyn</Uri>
28-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
28+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
2929
</Dependency>
30-
<Dependency Name="Microsoft.CodeAnalysis.CSharp" Version="4.13.0-1.24522.7">
30+
<Dependency Name="Microsoft.CodeAnalysis.CSharp" Version="4.13.0-2.24554.8">
3131
<Uri>https://github.com/dotnet/roslyn</Uri>
32-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
32+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
3333
</Dependency>
34-
<Dependency Name="Microsoft.CodeAnalysis.CSharp.EditorFeatures" Version="4.13.0-1.24522.7">
34+
<Dependency Name="Microsoft.CodeAnalysis.CSharp.EditorFeatures" Version="4.13.0-2.24554.8">
3535
<Uri>https://github.com/dotnet/roslyn</Uri>
36-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
36+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
3737
</Dependency>
38-
<Dependency Name="Microsoft.CodeAnalysis.CSharp.Features" Version="4.13.0-1.24522.7">
38+
<Dependency Name="Microsoft.CodeAnalysis.CSharp.Features" Version="4.13.0-2.24554.8">
3939
<Uri>https://github.com/dotnet/roslyn</Uri>
40-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
40+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
4141
</Dependency>
42-
<Dependency Name="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.13.0-1.24522.7">
42+
<Dependency Name="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.13.0-2.24554.8">
4343
<Uri>https://github.com/dotnet/roslyn</Uri>
44-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
44+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
4545
</Dependency>
46-
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures" Version="4.13.0-1.24522.7">
46+
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures" Version="4.13.0-2.24554.8">
4747
<Uri>https://github.com/dotnet/roslyn</Uri>
48-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
48+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
4949
</Dependency>
50-
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Common" Version="4.13.0-1.24522.7">
50+
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Common" Version="4.13.0-2.24554.8">
5151
<Uri>https://github.com/dotnet/roslyn</Uri>
52-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
52+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
5353
</Dependency>
54-
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Text" Version="4.13.0-1.24522.7">
54+
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Text" Version="4.13.0-2.24554.8">
5555
<Uri>https://github.com/dotnet/roslyn</Uri>
56-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
56+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
5757
</Dependency>
58-
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Wpf" Version="4.13.0-1.24522.7">
58+
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Wpf" Version="4.13.0-2.24554.8">
5959
<Uri>https://github.com/dotnet/roslyn</Uri>
60-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
60+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
6161
</Dependency>
62-
<Dependency Name="Microsoft.CodeAnalysis.Remote.ServiceHub" Version="4.13.0-1.24522.7">
62+
<Dependency Name="Microsoft.CodeAnalysis.Remote.ServiceHub" Version="4.13.0-2.24554.8">
6363
<Uri>https://github.com/dotnet/roslyn</Uri>
64-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
64+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
6565
</Dependency>
66-
<Dependency Name="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="4.13.0-1.24522.7">
66+
<Dependency Name="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="4.13.0-2.24554.8">
6767
<Uri>https://github.com/dotnet/roslyn</Uri>
68-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
68+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
6969
</Dependency>
70-
<Dependency Name="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.13.0-1.24522.7">
70+
<Dependency Name="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.13.0-2.24554.8">
7171
<Uri>https://github.com/dotnet/roslyn</Uri>
72-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
72+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
7373
</Dependency>
74-
<Dependency Name="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.13.0-1.24522.7">
74+
<Dependency Name="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.13.0-2.24554.8">
7575
<Uri>https://github.com/dotnet/roslyn</Uri>
76-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
76+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
7777
</Dependency>
78-
<Dependency Name="Microsoft.VisualStudio.LanguageServices" Version="4.13.0-1.24522.7">
78+
<Dependency Name="Microsoft.VisualStudio.LanguageServices" Version="4.13.0-2.24554.8">
7979
<Uri>https://github.com/dotnet/roslyn</Uri>
80-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
80+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
8181
</Dependency>
82-
<Dependency Name="Microsoft.CodeAnalysis.Test.Utilities" Version="4.13.0-1.24522.7">
82+
<Dependency Name="Microsoft.CodeAnalysis.Test.Utilities" Version="4.13.0-2.24554.8">
8383
<Uri>https://github.com/dotnet/roslyn</Uri>
84-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
84+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
8585
</Dependency>
8686
<!-- Intermediate is necessary for source build. -->
87-
<Dependency Name="Microsoft.SourceBuild.Intermediate.roslyn" Version="4.13.0-1.24522.7">
87+
<Dependency Name="Microsoft.SourceBuild.Intermediate.roslyn" Version="4.13.0-2.24554.8">
8888
<Uri>https://github.com/dotnet/roslyn</Uri>
89-
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
89+
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
9090
<SourceBuild RepoName="roslyn" ManagedOnly="true" />
9191
</Dependency>
9292
</ProductDependencies>

eng/Versions.props

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,25 +53,25 @@
5353
<MicrosoftSourceBuildIntermediatearcadePackageVersion>9.0.0-beta.24516.2</MicrosoftSourceBuildIntermediatearcadePackageVersion>
5454
<MicrosoftDotNetXliffTasksPackageVersion>1.0.0-beta.23475.1</MicrosoftDotNetXliffTasksPackageVersion>
5555
<MicrosoftSourceBuildIntermediatexlifftasksPackageVersion>1.0.0-beta.23475.1</MicrosoftSourceBuildIntermediatexlifftasksPackageVersion>
56-
<MicrosoftNetCompilersToolsetPackageVersion>4.13.0-1.24522.7</MicrosoftNetCompilersToolsetPackageVersion>
57-
<MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>4.13.0-1.24522.7</MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>
58-
<MicrosoftCodeAnalysisExternalAccessRazorPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisExternalAccessRazorPackageVersion>
59-
<MicrosoftCodeAnalysisCommonPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisCommonPackageVersion>
60-
<MicrosoftCodeAnalysisCSharpPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisCSharpPackageVersion>
61-
<MicrosoftCodeAnalysisCSharpEditorFeaturesPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisCSharpEditorFeaturesPackageVersion>
62-
<MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>
63-
<MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>
64-
<MicrosoftCodeAnalysisEditorFeaturesPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisEditorFeaturesPackageVersion>
65-
<MicrosoftCodeAnalysisEditorFeaturesCommonPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisEditorFeaturesCommonPackageVersion>
66-
<MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>
67-
<MicrosoftCodeAnalysisEditorFeaturesWpfPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisEditorFeaturesWpfPackageVersion>
68-
<MicrosoftCodeAnalysisRemoteServiceHubPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisRemoteServiceHubPackageVersion>
69-
<MicrosoftCodeAnalysisTestUtilitiesPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisTestUtilitiesPackageVersion>
70-
<MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>
71-
<MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>
72-
<MicrosoftCodeAnalysisWorkspacesMSBuildPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisWorkspacesMSBuildPackageVersion>
73-
<MicrosoftSourceBuildIntermediateroslynPackageVersion>4.13.0-1.24522.7</MicrosoftSourceBuildIntermediateroslynPackageVersion>
74-
<MicrosoftVisualStudioLanguageServicesPackageVersion>4.13.0-1.24522.7</MicrosoftVisualStudioLanguageServicesPackageVersion>
56+
<MicrosoftNetCompilersToolsetPackageVersion>4.13.0-2.24554.8</MicrosoftNetCompilersToolsetPackageVersion>
57+
<MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>4.13.0-2.24554.8</MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>
58+
<MicrosoftCodeAnalysisExternalAccessRazorPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisExternalAccessRazorPackageVersion>
59+
<MicrosoftCodeAnalysisCommonPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisCommonPackageVersion>
60+
<MicrosoftCodeAnalysisCSharpPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisCSharpPackageVersion>
61+
<MicrosoftCodeAnalysisCSharpEditorFeaturesPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisCSharpEditorFeaturesPackageVersion>
62+
<MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>
63+
<MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>
64+
<MicrosoftCodeAnalysisEditorFeaturesPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisEditorFeaturesPackageVersion>
65+
<MicrosoftCodeAnalysisEditorFeaturesCommonPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisEditorFeaturesCommonPackageVersion>
66+
<MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>
67+
<MicrosoftCodeAnalysisEditorFeaturesWpfPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisEditorFeaturesWpfPackageVersion>
68+
<MicrosoftCodeAnalysisRemoteServiceHubPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisRemoteServiceHubPackageVersion>
69+
<MicrosoftCodeAnalysisTestUtilitiesPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisTestUtilitiesPackageVersion>
70+
<MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>
71+
<MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>
72+
<MicrosoftCodeAnalysisWorkspacesMSBuildPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisWorkspacesMSBuildPackageVersion>
73+
<MicrosoftSourceBuildIntermediateroslynPackageVersion>4.13.0-2.24554.8</MicrosoftSourceBuildIntermediateroslynPackageVersion>
74+
<MicrosoftVisualStudioLanguageServicesPackageVersion>4.13.0-2.24554.8</MicrosoftVisualStudioLanguageServicesPackageVersion>
7575
<!--
7676
Exception - Microsoft.Extensions.ObjectPool and System.Collections.Immutable packages are not updated by automation,
7777
but are present in Version.Details.xml for source-build PVP flow. See the comment in Version.Details.xml for more information.

eng/targets/Services.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,6 @@
3535
<ServiceHubService Include="Microsoft.VisualStudio.Razor.Diagnostics" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteDiagnosticsService+Factory" />
3636
<ServiceHubService Include="Microsoft.VisualStudio.Razor.Hover" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteHoverService+Factory" />
3737
<ServiceHubService Include="Microsoft.VisualStudio.Razor.Completion" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteCompletionService+Factory" />
38+
<ServiceHubService Include="Microsoft.VisualStudio.Razor.CodeActions" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteCodeActionsService+Factory" />
3839
</ItemGroup>
3940
</Project>
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
Source Location: (2:0,2 [13] x:\dir\subdir\Test\TestComponent.razor)
1+
Source Location: (2:0,2 [13] x:\dir\subdir\Test\TestComponent.razor)
22
|
33
#if true
4-
|
5-
Generated Location: (986:28,2 [13] )
4+
|
5+
Generated Location: (986:28,2 [13] )
66
|
77
#if true
8-
|
9-
10-
Source Location: (20:3,2 [11] x:\dir\subdir\Test\TestComponent.razor)
8+
|
9+
10+
Source Location: (20:3,2 [11] x:\dir\subdir\Test\TestComponent.razor)
1111
|
1212
#endif
13-
|
14-
Generated Location: (1120:36,2 [11] )
13+
|
14+
Generated Location: (1120:36,2 [11] )
1515
|
1616
#endif
17-
|
18-
17+
|
18+
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
Source Location: (2:0,2 [13] x:\dir\subdir\Test\TestComponent.razor)
1+
Source Location: (2:0,2 [13] x:\dir\subdir\Test\TestComponent.razor)
22
|
33
#if true
4-
|
5-
Generated Location: (733:21,0 [13] )
4+
|
5+
Generated Location: (733:21,0 [13] )
66
|
77
#if true
8-
|
9-
10-
Source Location: (20:3,2 [11] x:\dir\subdir\Test\TestComponent.razor)
8+
|
9+
10+
Source Location: (20:3,2 [11] x:\dir\subdir\Test\TestComponent.razor)
1111
|
1212
#endif
13-
|
14-
Generated Location: (877:30,0 [11] )
13+
|
14+
Generated Location: (877:30,0 [11] )
1515
|
1616
#endif
17-
|
18-
17+
|
18+

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpStatementTest/SwitchStatement_ListPattern.stree.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
Keyword;[case];
2424
Whitespace;[ ];
2525
LeftBracket;[[];
26-
CSharpOperator;[..];
26+
Dot;[.];
27+
Dot;[.];
2728
Comma;[,];
2829
Whitespace;[ ];
2930
NumericLiteral;[3];

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,13 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(VSCodeActionParams reque
8989
_ => []
9090
};
9191

92-
return await _codeActionsService.GetCodeActionsAsync(request, documentSnapshot, delegatedCodeActions, _supportsCodeActionResolve, cancellationToken).ConfigureAwait(false);
92+
return await _codeActionsService.GetCodeActionsAsync(
93+
request,
94+
documentSnapshot,
95+
delegatedCodeActions,
96+
delegatedDocumentUri: null, // We don't use delegatedDocumentUri in the LSP server, as we can trivially recalculate it
97+
_supportsCodeActionResolve,
98+
cancellationToken).ConfigureAwait(false);
9399
}
94100

95101
private async Task<RazorVSInternalCodeAction[]> GetHtmlCodeActionsAsync(IDocumentSnapshot documentSnapshot, VSCodeActionParams request, Guid correlationId, CancellationToken cancellationToken)

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,61 @@
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;
11+
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
912
using Microsoft.CodeAnalysis.Razor.Protocol;
1013
using Microsoft.CodeAnalysis.Razor.Protocol.CodeActions;
1114
using Microsoft.VisualStudio.LanguageServer.Protocol;
15+
using Microsoft.CodeAnalysis.Formatting;
1216

1317
namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions;
1418

1519
internal sealed class RoslynCodeActionHelpers(IClientConnection clientConnection) : IRoslynCodeActionHelpers
1620
{
21+
private static readonly Lazy<Workspace> s_workspace = new Lazy<Workspace>(() => new AdhocWorkspace());
22+
1723
private readonly IClientConnection _clientConnection = clientConnection;
1824

19-
public Task<string?> GetFormattedNewFileContentsAsync(string projectFilePath, Uri csharpFileUri, string newFileContent, CancellationToken cancellationToken)
25+
public async Task<string> GetFormattedNewFileContentsAsync(IProjectSnapshot projectSnapshot, Uri csharpFileUri, string newFileContent, CancellationToken cancellationToken)
2026
{
2127
var parameters = new FormatNewFileParams()
2228
{
2329
Project = new TextDocumentIdentifier
2430
{
25-
Uri = new Uri(projectFilePath, UriKind.Absolute)
31+
Uri = new Uri(projectSnapshot.FilePath, UriKind.Absolute)
2632
},
2733
Document = new TextDocumentIdentifier
2834
{
2935
Uri = csharpFileUri
3036
},
3137
Contents = newFileContent
3238
};
33-
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();
3453
}
3554

36-
public Task<TextEdit[]?> GetSimplifiedTextEditsAsync(Uri codeBehindUri, TextEdit edit, bool requiresVirtualDocument, CancellationToken cancellationToken)
55+
public Task<TextEdit[]?> GetSimplifiedTextEditsAsync(DocumentContext documentContext, Uri? codeBehindUri, TextEdit edit, CancellationToken cancellationToken)
3756
{
57+
var tdi = codeBehindUri is null
58+
? documentContext.GetTextDocumentIdentifierAndVersion()
59+
: new TextDocumentIdentifierAndVersion(new TextDocumentIdentifier() { Uri = codeBehindUri }, 1);
3860
var delegatedParams = new DelegatedSimplifyMethodParams(
39-
new TextDocumentIdentifierAndVersion(new TextDocumentIdentifier() { Uri = codeBehindUri }, 1),
40-
requiresVirtualDocument,
61+
tdi,
62+
RequiresVirtualDocument: codeBehindUri == null,
4163
edit);
4264

4365
return _clientConnection.SendRequestAsync<DelegatedSimplifyMethodParams, TextEdit[]?>(

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/RazorLanguageServer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ protected override ILspServices ConstructLspServices()
129129
services.AddSemanticTokensServices(featureOptions);
130130
services.AddDocumentManagementServices(featureOptions);
131131
services.AddFormattingServices(featureOptions);
132-
services.AddCodeActionsServices();
133132
services.AddOptionsServices(_lspOptions);
134133
services.AddTextDocumentServices(featureOptions);
135134

@@ -138,6 +137,8 @@ protected override ILspServices ConstructLspServices()
138137
// Diagnostics
139138
services.AddDiagnosticServices();
140139

140+
services.AddCodeActionsServices();
141+
141142
// Completion
142143
services.AddCompletionServices();
143144

0 commit comments

Comments
 (0)