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

Re-architect formatting to prepare for cohosting (and for fun!) #10778

Merged
merged 28 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
97ac536
Move add usings helper code down to the workspaces layer
davidwengier Aug 21, 2024
8984db9
Combine back into one on type formatting pass
davidwengier Aug 21, 2024
fba3f1e
Rename some methods
davidwengier Aug 21, 2024
2c39c20
Random bits of cleanup before some bigger moves
davidwengier Aug 21, 2024
e45f094
Make it more explicit that we only do code action formatting for C#
davidwengier Aug 21, 2024
a702716
Be SUPER specific about our APIs, what they accept, and what they return
davidwengier Aug 21, 2024
e56b85e
Pass Html edits into the formatting service, so that the service does…
davidwengier Aug 21, 2024
3f57810
Get HtmlFormatter from DI
davidwengier Aug 21, 2024
38868db
Minor cleanup
davidwengier Aug 21, 2024
ca142be
Move HtmlFormattingPass down
davidwengier Aug 21, 2024
1143900
Formalize Razor formatting options so we can have a single RazorForma…
davidwengier Aug 21, 2024
2165fcb
One last minor cleanup
davidwengier Aug 22, 2024
d635754
Be explicit about formatting passes
davidwengier Aug 22, 2024
4e12117
Move files to their own folder
davidwengier Aug 22, 2024
6f1c6ef
Remove unnecessary base class
davidwengier Aug 22, 2024
f74150d
Separate html formatting passes, so we can remove the IsFormatOnType …
davidwengier Aug 22, 2024
ef44f4f
Separate out the files in the Html formatter passes.
davidwengier Aug 22, 2024
45b8495
List -> PooledArrayBuilder
davidwengier Aug 22, 2024
5caa48f
Remove Kind property from FormattingResult
davidwengier Aug 22, 2024
fa4e8ef
Remove FormattingResult since it's now useless
davidwengier Aug 22, 2024
587e72a
Simple PR feedback
davidwengier Aug 23, 2024
6622a0f
Missed the readonly bit
davidwengier Aug 23, 2024
3b79428
Merge remote-tracking branch 'upstream/main' into FormattingLayering
davidwengier Aug 23, 2024
2157169
More PR feedback
davidwengier Aug 23, 2024
19134c2
PR feedback
davidwengier Aug 27, 2024
1de43a0
Remove stale TODO
davidwengier Aug 28, 2024
d0e2a57
Merge remote-tracking branch 'upstream/main' into FormattingLayering
davidwengier Aug 29, 2024
ee83a6b
Fixes after merge
davidwengier Aug 29, 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
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,9 @@ void Method() { }
[Benchmark(Description = "Formatting")]
public async Task RazorCSharpFormattingAsync()
{
var options = new FormattingOptions()
{
TabSize = 4,
InsertSpaces = true
};

var documentContext = new DocumentContext(DocumentUri, DocumentSnapshot, projectContext: null);

var edits = await RazorFormattingService.FormatAsync(documentContext, range: null, options, CancellationToken.None);
var edits = await RazorFormattingService.GetDocumentFormattingEditsAsync(documentContext, htmlEdits: [], range: null, RazorFormattingOptions.Default, CancellationToken.None);

#if DEBUG
// For debugging purposes only.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -185,19 +185,21 @@ public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, V
}

// For C# we run the edit through our formatting engine
var edits = new[] { delegatedResponse.TextEdit };
Debug.Assert(positionInfo.LanguageKind == RazorLanguageKind.CSharp);

var mappedEdits = delegatedResponse.TextEditFormat == InsertTextFormat.Snippet
? await _razorFormattingService.FormatSnippetAsync(documentContext, positionInfo.LanguageKind, edits, originalRequest.Options, cancellationToken).ConfigureAwait(false)
: await _razorFormattingService.FormatOnTypeAsync(documentContext, positionInfo.LanguageKind, edits, originalRequest.Options, hostDocumentIndex: 0, triggerCharacter: '\0', cancellationToken).ConfigureAwait(false);
if (mappedEdits is not [{ } edit])
var options = RazorFormattingOptions.From(originalRequest.Options, _optionsMonitor.CurrentValue.CodeBlockBraceOnNextLine);

var mappedEdit = delegatedResponse.TextEditFormat == InsertTextFormat.Snippet
? await _razorFormattingService.GetCSharpSnippetFormattingEditAsync(documentContext, [delegatedResponse.TextEdit], options, cancellationToken).ConfigureAwait(false)
: await _razorFormattingService.GetSingleCSharpEditAsync(documentContext, delegatedResponse.TextEdit, options, cancellationToken).ConfigureAwait(false);
if (mappedEdit is null)
{
return null;
}

return new VSInternalDocumentOnAutoInsertResponseItem()
{
TextEdit = edit,
TextEdit = mappedEdit,
TextEditFormat = delegatedResponse.TextEditFormat,
};
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,6 @@ internal sealed class DefaultCSharpCodeActionResolver(
IClientConnection clientConnection,
IRazorFormattingService razorFormattingService) : CSharpCodeActionResolver(clientConnection)
{
// Usually when we need to format code, we utilize the formatting options provided
// by the platform. However, we aren't provided such options in the case of code actions
// so we use a default (and commonly used) configuration.
private static readonly FormattingOptions s_defaultFormattingOptions = new FormattingOptions()
{
TabSize = 4,
InsertSpaces = true,
OtherOptions = new Dictionary<string, object>
{
{ "trimTrailingWhitespace", true },
{ "insertFinalNewline", true },
{ "trimFinalNewlines", true },
},
};

private readonly IDocumentContextFactory _documentContextFactory = documentContextFactory;
private readonly IRazorFormattingService _razorFormattingService = razorFormattingService;

Expand Down Expand Up @@ -80,11 +65,10 @@ public async override Task<CodeAction> ResolveAsync(

// Remaps the text edits from the generated C# to the razor file,
// as well as applying appropriate formatting.
var formattedEdits = await _razorFormattingService.FormatCodeActionAsync(
var formattedEdit = await _razorFormattingService.GetCSharpCodeActionEditAsync(
documentContext,
RazorLanguageKind.CSharp,
csharpTextEdits,
s_defaultFormattingOptions,
RazorFormattingOptions.Default,
cancellationToken).ConfigureAwait(false);

cancellationToken.ThrowIfCancellationRequested();
Expand All @@ -99,7 +83,7 @@ public async override Task<CodeAction> ResolveAsync(
new TextDocumentEdit()
{
TextDocument = codeDocumentIdentifier,
Edits = formattedEdits,
Edits = formattedEdit is null ? [] : [formattedEdit],
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.AspNetCore.Razor.Threading;
using Microsoft.CodeAnalysis.ExternalAccess.Razor;
using Microsoft.CodeAnalysis.Razor.Formatting;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;

Expand Down Expand Up @@ -138,7 +139,7 @@ private static ImmutableArray<RazorVSInternalCodeAction> ProcessCodeActionsVSCod
var fqnCodeAction = CreateFQNCodeAction(context, diagnostic, codeAction, fqn);
typeAccessibilityCodeActions.Add(fqnCodeAction);

if (AddUsingsCodeActionProviderHelper.TryCreateAddUsingResolutionParams(fqn, context.Request.TextDocument.Uri, additionalEdit: null, out var @namespace, out var resolutionParams))
if (AddUsingsCodeActionResolver.TryCreateAddUsingResolutionParams(fqn, context.Request.TextDocument.Uri, additionalEdit: null, out var @namespace, out var resolutionParams))
{
var addUsingCodeAction = RazorCodeActionFactory.CreateAddComponentUsing(@namespace, newTagName: null, resolutionParams);
typeAccessibilityCodeActions.Add(addUsingCodeAction);
Expand Down Expand Up @@ -191,7 +192,7 @@ private static ImmutableArray<RazorVSInternalCodeAction> ProcessCodeActionsVS(
// For add using suggestions, the code action title is of the form:
// `using System.Net;`
else if (codeAction.Name is not null && codeAction.Name.Equals(RazorPredefinedCodeFixProviderNames.AddImport, StringComparison.Ordinal) &&
AddUsingsCodeActionProviderHelper.TryExtractNamespace(codeAction.Title, out var @namespace, out var prefix))
AddUsingsHelper.TryExtractNamespace(codeAction.Title, out var @namespace, out var prefix))
{
codeAction.Title = $"{prefix}@using {@namespace}";
typeAccessibilityCodeActions.Add(codeAction.WrapResolvableCodeAction(context, LanguageServerConstants.CodeActions.Default));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static async Task RemapAndFixHtmlCodeActionEditAsync(IEditMappingService

foreach (var edit in documentEdits)
{
edit.Edits = HtmlFormatter.FixHtmlTestEdits(htmlSourceText, edit.Edits);
edit.Edits = HtmlFormatter.FixHtmlTextEdits(htmlSourceText, edit.Edits);
}

codeAction.Edit = new WorkspaceEdit
Expand Down
Loading