@@ -16,6 +16,8 @@ namespace StyleCop.Analyzers.ReadabilityRules
1616 using Microsoft . CodeAnalysis . CodeFixes ;
1717 using Microsoft . CodeAnalysis . CSharp ;
1818 using Microsoft . CodeAnalysis . CSharp . Syntax ;
19+ using Microsoft . CodeAnalysis . Options ;
20+ using Microsoft . CodeAnalysis . Text ;
1921 using StyleCop . Analyzers . Helpers ;
2022
2123 /// <summary>
@@ -58,22 +60,24 @@ public override Task RegisterCodeFixesAsync(CodeFixContext context)
5860 private static async Task < Document > GetTransformedDocumentAsync ( Document document , Diagnostic diagnostic , CancellationToken cancellationToken )
5961 {
6062 var syntaxRoot = await document . GetSyntaxRootAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
63+ var sourceText = await document . GetTextAsync ( cancellationToken ) . ConfigureAwait ( false ) ;
64+ var options = document . Project . Solution . Workspace . Options ;
6165 var nodeInSourceSpan = syntaxRoot . FindNode ( diagnostic . Location . SourceSpan , getInnermostNodeForTie : true ) ;
6266 AttributeListSyntax attributeList = nodeInSourceSpan . FirstAncestorOrSelf < AttributeListSyntax > ( ) ;
6367
6468 var settings = SettingsHelper . GetStyleCopSettingsInCodeFix ( document . Project . AnalyzerOptions , syntaxRoot . SyntaxTree , cancellationToken ) ;
6569 var indentationSteps = IndentationHelper . GetIndentationSteps ( settings . Indentation , attributeList ) ;
6670 var indentationTrivia = IndentationHelper . GenerateWhitespaceTrivia ( settings . Indentation , indentationSteps ) ;
6771
68- List < AttributeListSyntax > newAttributeLists = GetNewAttributeList ( attributeList , indentationTrivia ) ;
72+ List < AttributeListSyntax > newAttributeLists = GetNewAttributeList ( attributeList , indentationTrivia , sourceText , options ) ;
6973
7074 var newSyntaxRoot = syntaxRoot . ReplaceNode ( attributeList , newAttributeLists ) ;
7175 var newDocument = document . WithSyntaxRoot ( newSyntaxRoot . WithoutFormatting ( ) ) ;
7276
7377 return newDocument ;
7478 }
7579
76- private static List < AttributeListSyntax > GetNewAttributeList ( AttributeListSyntax attributeList , SyntaxTrivia indentationTrivia )
80+ private static List < AttributeListSyntax > GetNewAttributeList ( AttributeListSyntax attributeList , SyntaxTrivia indentationTrivia , SourceText sourceText , OptionSet options )
7781 {
7882 var newAttributeLists = new List < AttributeListSyntax > ( ) ;
7983
@@ -90,7 +94,7 @@ private static List<AttributeListSyntax> GetNewAttributeList(AttributeListSyntax
9094
9195 newAttributeList = ( i == ( attributeList . Attributes . Count - 1 ) )
9296 ? newAttributeList . WithTrailingTrivia ( attributeList . GetTrailingTrivia ( ) )
93- : newAttributeList . WithTrailingTrivia ( SyntaxFactory . CarriageReturnLineFeed ) ;
97+ : newAttributeList . WithTrailingTrivia ( FormattingHelper . GetEndOfLineForCodeFix ( attributeList . CloseBracketToken , sourceText , options ) ) ;
9498
9599 newAttributeLists . Add ( newAttributeList ) ;
96100 }
@@ -114,6 +118,8 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
114118 }
115119
116120 var syntaxRoot = await document . GetSyntaxRootAsync ( fixAllContext . CancellationToken ) . ConfigureAwait ( false ) ;
121+ var sourceText = await document . GetTextAsync ( fixAllContext . CancellationToken ) . ConfigureAwait ( false ) ;
122+ var options = document . Project . Solution . Workspace . Options ;
117123 var settings = SettingsHelper . GetStyleCopSettingsInCodeFix ( document . Project . AnalyzerOptions , syntaxRoot . SyntaxTree , fixAllContext . CancellationToken ) ;
118124
119125 // 🐉 Need to eagerly evaluate this with ToList() to ensure nodes are not garbage collected between the
@@ -126,7 +132,7 @@ protected override async Task<SyntaxNode> FixAllInDocumentAsync(FixAllContext fi
126132 {
127133 var indentationSteps = IndentationHelper . GetIndentationSteps ( settings . Indentation , attributeList ) ;
128134 var indentationTrivia = IndentationHelper . GenerateWhitespaceTrivia ( settings . Indentation , indentationSteps ) ;
129- newRoot = newRoot . ReplaceNode ( newRoot . GetCurrentNode ( attributeList ) , GetNewAttributeList ( attributeList , indentationTrivia ) ) ;
135+ newRoot = newRoot . ReplaceNode ( newRoot . GetCurrentNode ( attributeList ) , GetNewAttributeList ( attributeList , indentationTrivia , sourceText , options ) ) ;
130136 }
131137
132138 return newRoot ;
0 commit comments