-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Call Roslyn to format new code behind documents (#9263)
Fixes #4330 Fixes #8766 Includes #9262 so just review from 8a2b8af onwards Goes with dotnet/roslyn#69878 and dotnet/vscode-csharp#6329 I logged #9264 to follow up with a better test, though strictly speaking the one I've added id exhaustive :) Behaviour of Extract to Code Behind before this change. Note the many using statements and block scoped namespace. ![ExtractToCodeBehindBefore](https://github.com/dotnet/razor/assets/754264/10ac5595-b3b2-44c2-a1a7-66664d3c8f1b) Behaviour after this change. Note the file scoped namespace, and file header. ![ExtractToCodeBehindAfter](https://github.com/dotnet/razor/assets/754264/65160715-bc98-4336-b19d-37f9b14adf94) The squiggle on `NavigationManager` is due to an `.editorconfig` rule I have on in that project, requiring `this.` qualification, so is unrelated.
- Loading branch information
Showing
6 changed files
with
170 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...or/src/Microsoft.AspNetCore.Razor.LanguageServer/CodeActions/Razor/FormatNewFileParams.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Runtime.Serialization; | ||
using Microsoft.VisualStudio.LanguageServer.Protocol; | ||
|
||
namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions.Razor; | ||
|
||
[DataContract] | ||
internal record FormatNewFileParams | ||
{ | ||
[DataMember(Name = "document")] | ||
public required TextDocumentIdentifier Document { get; set; } | ||
|
||
[DataMember(Name = "project")] | ||
public required TextDocumentIdentifier Project { get; set; } | ||
|
||
[DataMember(Name = "contents")] | ||
public required string Contents { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/Razor/src/Microsoft.VisualStudio.LanguageServerClient.Razor/Endpoints/FormatNewFile.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the MIT license. See License.txt in the project root for license information. | ||
|
||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Razor.LanguageServer.CodeActions.Razor; | ||
using Microsoft.AspNetCore.Razor.LanguageServer.Common; | ||
using Microsoft.VisualStudio.LanguageServer.Protocol; | ||
using Newtonsoft.Json.Linq; | ||
using StreamJsonRpc; | ||
|
||
namespace Microsoft.VisualStudio.LanguageServerClient.Razor; | ||
|
||
internal partial class RazorCustomMessageTarget | ||
{ | ||
[JsonRpcMethod(CustomMessageNames.RazorFormatNewFileEndpointName, UseSingleObjectParameterDeserialization = true)] | ||
public async Task<string?> FormatNewFileAsync(FormatNewFileParams request, CancellationToken cancellationToken) | ||
{ | ||
// This endpoint is special because it deals with a file that doesn't exist yet, so there is no document syncing necessary! | ||
var response = await _requestInvoker.ReinvokeRequestOnServerAsync<FormatNewFileParams, string?>( | ||
RazorLSPConstants.RoslynFormatNewFileEndpointName, | ||
RazorLSPConstants.RazorCSharpLanguageServerName, | ||
SupportsFormatNewFile, | ||
request, | ||
cancellationToken).ConfigureAwait(false); | ||
|
||
return response.Result; | ||
} | ||
|
||
private static bool SupportsFormatNewFile(JToken token) | ||
{ | ||
var serverCapabilities = token.ToObject<VSInternalServerCapabilities>(); | ||
|
||
return serverCapabilities?.Experimental is JObject experimental | ||
&& experimental.TryGetValue(RazorLSPConstants.RoslynFormatNewFileEndpointName, out var supportsFormatNewFile) | ||
&& supportsFormatNewFile.ToObject<bool>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.