-
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 documents when they're created
- Loading branch information
1 parent
5a72d94
commit 8a2b8af
Showing
6 changed files
with
115 additions
and
33 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
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