Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cohost code actions #11147

Merged
merged 23 commits into from
Nov 7, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
3376535
Create basic cohosting infrastrcuture
davidwengier Nov 1, 2024
6f61999
Complete cohosting code actions functionality
davidwengier Nov 1, 2024
61ca99b
Report better MEF composition errors in tests
davidwengier Nov 4, 2024
60ed8c0
Add delegated document Uri to code action data
davidwengier Nov 4, 2024
e829ffb
Add cohost resolve endpoint and tests
davidwengier Nov 4, 2024
b509f67
Fix code action translation to Roslyn types
davidwengier Nov 4, 2024
a813c22
Move helpers to base class
davidwengier Nov 4, 2024
8ba2bbf
Implement additional roslyn helpers, and add tests for more involved …
davidwengier Nov 4, 2024
0a3b902
Cohost eqivalent of https://github.com/dotnet/razor/pull/11141
davidwengier Nov 4, 2024
d7e1c01
Silly mistake number 1
davidwengier Nov 4, 2024
4ef1668
Bump Roslyn to 4.13.0-2.24554.8
davidwengier Nov 5, 2024
a217d9f
Merge remote-tracking branch 'upstream/main' into dev/dawengie/CodeAc…
davidwengier Nov 5, 2024
9b09b25
Update tests. No idea why these weren't failing to compile on my mach…
davidwengier Nov 5, 2024
673f828
Seal endpoints (silly mistake 2?)
davidwengier Nov 5, 2024
7338393
Update compiler test baselines
davidwengier Nov 5, 2024
39138eb
Merge remote-tracking branch 'upstream/main' into dev/dawengie/CodeAc…
davidwengier Nov 5, 2024
61b5d93
PR Feedback
davidwengier Nov 6, 2024
04da689
Merge remote-tracking branch 'upstream/main' into dev/dawengie/CodeAc…
davidwengier Nov 6, 2024
706dc93
FIx code actions resolve in VS.
davidwengier Nov 6, 2024
15c0333
Fix registration after merge
davidwengier Nov 6, 2024
6cd3c63
Fix namespace on test
davidwengier Nov 6, 2024
aebf195
Tweak roslyn helpers and nullability
davidwengier Nov 6, 2024
31469c4
Further tweak Roslyn helpers
davidwengier Nov 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 38 additions & 38 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,82 +11,82 @@
<Sha>136e43e45e20bd58bf86eeabba0a0fa7e1a4b3ae</Sha>
<SourceBuild RepoName="source-build-reference-packages" ManagedOnly="true" />
</Dependency>
<Dependency Name="Microsoft.Net.Compilers.Toolset" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.Net.Compilers.Toolset" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CommonLanguageServerProtocol.Framework" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CommonLanguageServerProtocol.Framework" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.ExternalAccess.Razor" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.ExternalAccess.Razor" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.Common" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.Common" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.CSharp" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.CSharp" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.CSharp.EditorFeatures" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.CSharp.EditorFeatures" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.CSharp.Features" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.CSharp.Features" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Common" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Common" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Text" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Text" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Wpf" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.EditorFeatures.Wpf" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.Remote.ServiceHub" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.Remote.ServiceHub" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.VisualBasic.Workspaces" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.Workspaces.Common" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.VisualStudio.LanguageServices" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.VisualStudio.LanguageServices" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<Dependency Name="Microsoft.CodeAnalysis.Test.Utilities" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.CodeAnalysis.Test.Utilities" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
</Dependency>
<!-- Intermediate is necessary for source build. -->
<Dependency Name="Microsoft.SourceBuild.Intermediate.roslyn" Version="4.13.0-1.24522.7">
<Dependency Name="Microsoft.SourceBuild.Intermediate.roslyn" Version="4.13.0-2.24554.8">
<Uri>https://github.com/dotnet/roslyn</Uri>
<Sha>77c6704c42b162e89095631f12e2661398a75ba7</Sha>
<Sha>8fc87e00bbe35131f7d3707621ae18e1af1aae55</Sha>
<SourceBuild RepoName="roslyn" ManagedOnly="true" />
</Dependency>
</ProductDependencies>
Expand Down
38 changes: 19 additions & 19 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,25 @@
<MicrosoftSourceBuildIntermediatearcadePackageVersion>9.0.0-beta.24516.2</MicrosoftSourceBuildIntermediatearcadePackageVersion>
<MicrosoftDotNetXliffTasksPackageVersion>1.0.0-beta.23475.1</MicrosoftDotNetXliffTasksPackageVersion>
<MicrosoftSourceBuildIntermediatexlifftasksPackageVersion>1.0.0-beta.23475.1</MicrosoftSourceBuildIntermediatexlifftasksPackageVersion>
<MicrosoftNetCompilersToolsetPackageVersion>4.13.0-1.24522.7</MicrosoftNetCompilersToolsetPackageVersion>
<MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>4.13.0-1.24522.7</MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>
<MicrosoftCodeAnalysisExternalAccessRazorPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisExternalAccessRazorPackageVersion>
<MicrosoftCodeAnalysisCommonPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisCommonPackageVersion>
<MicrosoftCodeAnalysisCSharpPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisCSharpPackageVersion>
<MicrosoftCodeAnalysisCSharpEditorFeaturesPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisCSharpEditorFeaturesPackageVersion>
<MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>
<MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>
<MicrosoftCodeAnalysisEditorFeaturesPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisEditorFeaturesPackageVersion>
<MicrosoftCodeAnalysisEditorFeaturesCommonPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisEditorFeaturesCommonPackageVersion>
<MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>
<MicrosoftCodeAnalysisEditorFeaturesWpfPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisEditorFeaturesWpfPackageVersion>
<MicrosoftCodeAnalysisRemoteServiceHubPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisRemoteServiceHubPackageVersion>
<MicrosoftCodeAnalysisTestUtilitiesPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisTestUtilitiesPackageVersion>
<MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>
<MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>
<MicrosoftCodeAnalysisWorkspacesMSBuildPackageVersion>4.13.0-1.24522.7</MicrosoftCodeAnalysisWorkspacesMSBuildPackageVersion>
<MicrosoftSourceBuildIntermediateroslynPackageVersion>4.13.0-1.24522.7</MicrosoftSourceBuildIntermediateroslynPackageVersion>
<MicrosoftVisualStudioLanguageServicesPackageVersion>4.13.0-1.24522.7</MicrosoftVisualStudioLanguageServicesPackageVersion>
<MicrosoftNetCompilersToolsetPackageVersion>4.13.0-2.24554.8</MicrosoftNetCompilersToolsetPackageVersion>
<MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>4.13.0-2.24554.8</MicrosoftCommonLanguageServerProtocolFrameworkPackageVersion>
<MicrosoftCodeAnalysisExternalAccessRazorPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisExternalAccessRazorPackageVersion>
<MicrosoftCodeAnalysisCommonPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisCommonPackageVersion>
<MicrosoftCodeAnalysisCSharpPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisCSharpPackageVersion>
<MicrosoftCodeAnalysisCSharpEditorFeaturesPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisCSharpEditorFeaturesPackageVersion>
<MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisCSharpFeaturesPackageVersion>
<MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisCSharpWorkspacesPackageVersion>
<MicrosoftCodeAnalysisEditorFeaturesPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisEditorFeaturesPackageVersion>
<MicrosoftCodeAnalysisEditorFeaturesCommonPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisEditorFeaturesCommonPackageVersion>
<MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisEditorFeaturesTextPackageVersion>
<MicrosoftCodeAnalysisEditorFeaturesWpfPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisEditorFeaturesWpfPackageVersion>
<MicrosoftCodeAnalysisRemoteServiceHubPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisRemoteServiceHubPackageVersion>
<MicrosoftCodeAnalysisTestUtilitiesPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisTestUtilitiesPackageVersion>
<MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisVisualBasicWorkspacesPackageVersion>
<MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisWorkspacesCommonPackageVersion>
<MicrosoftCodeAnalysisWorkspacesMSBuildPackageVersion>4.13.0-2.24554.8</MicrosoftCodeAnalysisWorkspacesMSBuildPackageVersion>
<MicrosoftSourceBuildIntermediateroslynPackageVersion>4.13.0-2.24554.8</MicrosoftSourceBuildIntermediateroslynPackageVersion>
<MicrosoftVisualStudioLanguageServicesPackageVersion>4.13.0-2.24554.8</MicrosoftVisualStudioLanguageServicesPackageVersion>
<!--
Exception - Microsoft.Extensions.ObjectPool and System.Collections.Immutable packages are not updated by automation,
but are present in Version.Details.xml for source-build PVP flow. See the comment in Version.Details.xml for more information.
Expand Down
1 change: 1 addition & 0 deletions eng/targets/Services.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
<ServiceHubService Include="Microsoft.VisualStudio.Razor.Diagnostics" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteDiagnosticsService+Factory" />
<ServiceHubService Include="Microsoft.VisualStudio.Razor.Hover" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteHoverService+Factory" />
<ServiceHubService Include="Microsoft.VisualStudio.Razor.Completion" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteCompletionService+Factory" />
<ServiceHubService Include="Microsoft.VisualStudio.Razor.CodeActions" ClassName="Microsoft.CodeAnalysis.Remote.Razor.RemoteCodeActionsService+Factory" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
Source Location: (2:0,2 [13] x:\dir\subdir\Test\TestComponent.razor)
Source Location: (2:0,2 [13] x:\dir\subdir\Test\TestComponent.razor)
|
#if true
|
Generated Location: (986:28,2 [13] )
|
Generated Location: (986:28,2 [13] )
|
#if true
|

Source Location: (20:3,2 [11] x:\dir\subdir\Test\TestComponent.razor)
|
Source Location: (20:3,2 [11] x:\dir\subdir\Test\TestComponent.razor)
|
#endif
|
Generated Location: (1120:36,2 [11] )
|
Generated Location: (1120:36,2 [11] )
|
#endif
|

|
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
Source Location: (2:0,2 [13] x:\dir\subdir\Test\TestComponent.razor)
Source Location: (2:0,2 [13] x:\dir\subdir\Test\TestComponent.razor)
|
#if true
|
Generated Location: (733:21,0 [13] )
|
Generated Location: (733:21,0 [13] )
|
#if true
|

Source Location: (20:3,2 [11] x:\dir\subdir\Test\TestComponent.razor)
|
Source Location: (20:3,2 [11] x:\dir\subdir\Test\TestComponent.razor)
|
#endif
|
Generated Location: (877:30,0 [11] )
|
Generated Location: (877:30,0 [11] )
|
#endif
|

|
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
Keyword;[case];
Whitespace;[ ];
LeftBracket;[[];
CSharpOperator;[..];
Dot;[.];
Dot;[.];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like result of dotnet/roslyn#75549.

Comma;[,];
Whitespace;[ ];
NumericLiteral;[3];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(VSCodeActionParams reque
_ => []
};

return await _codeActionsService.GetCodeActionsAsync(request, documentSnapshot, delegatedCodeActions, _supportsCodeActionResolve, cancellationToken).ConfigureAwait(false);
return await _codeActionsService.GetCodeActionsAsync(
request,
documentSnapshot,
delegatedCodeActions,
delegatedDocumentUri: null, // We don't use delegatedDocumentUri in the LSP server, as we can trivially recalculate it
_supportsCodeActionResolve,
cancellationToken).ConfigureAwait(false);
}

private async Task<RazorVSInternalCodeAction[]> GetHtmlCodeActionsAsync(IDocumentSnapshot documentSnapshot, VSCodeActionParams request, Guid correlationId, CancellationToken cancellationToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,61 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Razor.CodeActions;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Protocol.CodeActions;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.CodeAnalysis.Formatting;

namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions;

internal sealed class RoslynCodeActionHelpers(IClientConnection clientConnection) : IRoslynCodeActionHelpers
{
private static readonly Lazy<Workspace> s_workspace = new Lazy<Workspace>(() => new AdhocWorkspace());

private readonly IClientConnection _clientConnection = clientConnection;

public Task<string?> GetFormattedNewFileContentsAsync(string projectFilePath, Uri csharpFileUri, string newFileContent, CancellationToken cancellationToken)
public async Task<string> GetFormattedNewFileContentsAsync(IProjectSnapshot projectSnapshot, Uri csharpFileUri, string newFileContent, CancellationToken cancellationToken)
{
var parameters = new FormatNewFileParams()
{
Project = new TextDocumentIdentifier
{
Uri = new Uri(projectFilePath, UriKind.Absolute)
Uri = new Uri(projectSnapshot.FilePath, UriKind.Absolute)
},
Document = new TextDocumentIdentifier
{
Uri = csharpFileUri
},
Contents = newFileContent
};
return _clientConnection.SendRequestAsync<FormatNewFileParams, string?>(CustomMessageNames.RazorFormatNewFileEndpointName, parameters, cancellationToken);

var fixedContent = await _clientConnection.SendRequestAsync<FormatNewFileParams, string?>(CustomMessageNames.RazorFormatNewFileEndpointName, parameters, cancellationToken).ConfigureAwait(false);

if (fixedContent is not null)
{
return fixedContent;
}

// 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
// for C# formatting, only Razor formatting, and we have no access to Roslyn's real workspace, since it could be in another process.
var node = await CSharpSyntaxTree.ParseText(newFileContent, cancellationToken: cancellationToken).GetRootAsync(cancellationToken).ConfigureAwait(false);
node = Formatter.Format(node, s_workspace.Value, cancellationToken: cancellationToken);

return node.ToFullString();
}

public Task<TextEdit[]?> GetSimplifiedTextEditsAsync(Uri codeBehindUri, TextEdit edit, bool requiresVirtualDocument, CancellationToken cancellationToken)
public Task<TextEdit[]?> GetSimplifiedTextEditsAsync(DocumentContext documentContext, Uri? codeBehindUri, TextEdit edit, CancellationToken cancellationToken)
{
var tdi = codeBehindUri is null
? documentContext.GetTextDocumentIdentifierAndVersion()
: new TextDocumentIdentifierAndVersion(new TextDocumentIdentifier() { Uri = codeBehindUri }, 1);
var delegatedParams = new DelegatedSimplifyMethodParams(
new TextDocumentIdentifierAndVersion(new TextDocumentIdentifier() { Uri = codeBehindUri }, 1),
requiresVirtualDocument,
tdi,
RequiresVirtualDocument: codeBehindUri == null,
edit);

return _clientConnection.SendRequestAsync<DelegatedSimplifyMethodParams, TextEdit[]?>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ protected override ILspServices ConstructLspServices()
services.AddSemanticTokensServices(featureOptions);
services.AddDocumentManagementServices(featureOptions);
services.AddFormattingServices(featureOptions);
services.AddCodeActionsServices();
services.AddOptionsServices(_lspOptions);
services.AddTextDocumentServices(featureOptions);

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

services.AddCodeActionsServices();

// Completion
services.AddCompletionServices();

Expand Down
Loading
Loading