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

Format document after each provider #59091

Merged
merged 4 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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 @@ -8,6 +8,7 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.CodeStyle;
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
using Microsoft.CodeAnalysis.Options;
using Microsoft.CodeAnalysis.Test.Utilities.Formatting;
using Roslyn.Test.Utilities;
using Xunit;
Expand Down Expand Up @@ -171,6 +172,31 @@ internal class C
});
}

[Fact]
public async Task TestAccessibilityModifiers_FileScopedNamespace()
{
await TestAsync(testCode: @"using System;

namespace Goo
{
class C
{
}
}",
expected: @"using System;

namespace Goo;
internal class C
{
}
",
options: new (OptionKey, object)[]
{
(new OptionKey(CSharpCodeStyleOptions.NamespaceDeclarations), new CodeStyleOption2<NamespaceDeclarationPreference>(NamespaceDeclarationPreference.FileScoped, NotificationOption2.Error)),
(new OptionKey(CodeStyleOptions2.RequireAccessibilityModifiers, Language), new CodeStyleOption2<AccessibilityModifiersRequired>(AccessibilityModifiersRequired.Always, NotificationOption2.Error))
});
}

[Fact]
[WorkItem(55703, "https://github.com/dotnet/roslyn/issues/55703")]
public async Task TestAccessibilityModifiers_IgnoresPartial()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ internal Task TestAsync<T>(string testCode, string expected, (Option2<T>, T)[]?
parseOptions);
}

internal Task TestAsync(string testCode, string expected, (OptionKey, object)[]? options = null, ParseOptions? parseOptions = null)
Copy link
Member

Choose a reason for hiding this comment

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

(OptionKey, object)[]? options = null

In the future, I recommend OptionsCollection for this. Very easy to initialize a new instance and set strongly-typed values. I'll look into updating this case after this PR merges.

{
return TestCoreAsync(testCode,
expected,
options,
parseOptions);
}

private async Task TestCoreAsync<T>(string testCode, string expected, (OptionKey, T)[]? options, ParseOptions? parseOptions)
{
using (var workspace = CreateTestWorkspace(testCode, parseOptions))
Expand All @@ -60,9 +68,6 @@ private async Task TestCoreAsync<T>(string testCode, string expected, (OptionKey
var formattingService = document.GetRequiredLanguageService<INewDocumentFormattingService>();
var formattedDocument = await formattingService.FormatNewDocumentAsync(document, hintDocument: null, CancellationToken.None);

// Format to match what AbstractEditorFactory does
formattedDocument = await Formatter.FormatAsync(formattedDocument);

var actual = await formattedDocument.GetTextAsync();
AssertEx.EqualOrDiff(expected, actual.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,14 @@ public async Task<Document> FormatNewDocumentAsync(Document document, Document?
// other, so this shouldn't cause problems.
try
{
// First we ask the provider to "format" the document. This could be formatting in terms
// of adjusting block scopes to file scopes etc., but it could also be more akin to fixers
// like adding access modifiers, or adding .ConfigureAwait() calls etc.
document = await provider.FormatNewDocumentAsync(document, hintDocument, cancellationToken).ConfigureAwait(false);

// Now that the above has changed the document, we need the formatter to make sure its correct
// before we call the next provider, otherwise they might not see things as they are meant to be.
document = await Formatter.FormatAsync(document, cancellationToken: cancellationToken).ConfigureAwait(false);
davidwengier marked this conversation as resolved.
Show resolved Hide resolved
davidwengier marked this conversation as resolved.
Show resolved Hide resolved
}
catch (Exception ex) when (FatalError.ReportAndCatchUnlessCanceled(ex, cancellationToken, ErrorSeverity.General))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,9 @@ private async Task FormatDocumentCreatedFromTemplateAsync(IVsHierarchy hierarchy
addedDocument = await formattingService.FormatNewDocumentAsync(addedDocument, hintDocument: null, cancellationToken).ConfigureAwait(true);
}

var rootToFormat = await addedDocument.GetRequiredSyntaxRootAsync(cancellationToken).ConfigureAwait(true);
var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(addedDocument, cancellationToken).ConfigureAwait(true);

// Format document
var unformattedText = await addedDocument.GetTextAsync(cancellationToken).ConfigureAwait(true);
var formattedRoot = Formatter.Format(rootToFormat, workspace.Services, formattingOptions, cancellationToken);
var formattedText = formattedRoot.GetText(unformattedText.Encoding, unformattedText.ChecksumAlgorithm);
var formattingOptions = await SyntaxFormattingOptions.FromDocumentAsync(addedDocument, cancellationToken).ConfigureAwait(true);
var formattedText = await addedDocument.GetTextAsync(cancellationToken).ConfigureAwait(true);

// Ensure the line endings are normalized. The formatter doesn't touch everything if it doesn't need to.
var targetLineEnding = formattingOptions.GetOption(FormattingOptions2.NewLine)!;
Expand Down