diff --git a/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs b/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs index f021e8c203279..4152b9f006c78 100644 --- a/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs +++ b/src/EditorFeatures/CSharpTest/Formatting/FormattingEngineTests.cs @@ -4,6 +4,7 @@ #nullable disable +using System; using System.Collections.Generic; using System.Linq; using System.Threading; @@ -1633,6 +1634,23 @@ protected void Render() await AssertFormatAsync(expected, code, spans: null); } + [WpfTheory] + [CombinatorialData] + [WorkItem(59637, "https://github.com/dotnet/roslyn/issues/59637")] + public async Task FormatAttributeAtEndOfFile(bool trailingNewLine) + { + var endOfFile = trailingNewLine ? Environment.NewLine : ""; + var code = $@"using System.Diagnostics.CodeAnalysis; + +[assembly:SuppressMessage(""Globalization"", ""CA1308: Normalize strings to uppercase"", Justification = ""My reason"", Scope = ""member"", Target = ""~M:Method"") ] {endOfFile}"; + var expected = $@"using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage(""Globalization"", ""CA1308: Normalize strings to uppercase"", Justification = ""My reason"", Scope = ""member"", Target = ""~M:Method"")]{endOfFile}"; + + await AssertFormatAsync(expected, code, spans: null); + await AssertFormatAsync(expected, expected, spans: null); + } + [WorkItem(30787, "https://github.com/dotnet/roslyn/issues/30787")] [WpfFact] public void DoSmartIndentOpenBraceEvenWithFormatWhileTypingOff1() diff --git a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Formatting/Rules/SpacingFormattingRule.cs b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Formatting/Rules/SpacingFormattingRule.cs index 8dbd9d3efef09..030bf1220427b 100644 --- a/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Formatting/Rules/SpacingFormattingRule.cs +++ b/src/Workspaces/SharedUtilitiesAndExtensions/Compiler/CSharp/Formatting/Rules/SpacingFormattingRule.cs @@ -248,6 +248,12 @@ public override AbstractFormattingRule WithOptions(SyntaxFormattingOptions optio return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpacesIfOnSingleLine); } + // [Attribute1]$${EOF} + if (currentToken.IsKind(SyntaxKind.EndOfFileToken)) + { + return CreateAdjustSpacesOperation(0, AdjustSpacesOption.ForceSpacesIfOnSingleLine); + } + // [Attribute]$$ int Prop { ... } return CreateAdjustSpacesOperation(1, AdjustSpacesOption.ForceSpacesIfOnSingleLine); }