Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Contributor

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 @@ -42,6 +42,8 @@ public async Task<Document> FormatNewDocumentAsync(Document document, Document?
try
{
document = await provider.FormatNewDocumentAsync(document, hintDocument, cancellationToken).ConfigureAwait(false);

document = await Formatter.FormatAsync(document, cancellationToken: cancellationToken).ConfigureAwait(false);
}
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