Skip to content

Preserve document line endings in code fixes - #4360

Merged
AArnott merged 2 commits into
masterfrom
aarnott-animated-waffle
Oct 20, 2026
Merged

AArnott merged 2 commits into
masterfrom
aarnott-animated-waffle

Conversation

@AArnott

@AArnott AArnott commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Why

Code fixes that insert or rearrange lines should follow the document's existing newline sequence instead of introducing CRLF/LF mismatches.

What changed

Centralized newline selection around nearby source text, with workspace formatting options as a fallback when the document has no line endings. Updated affected documentation, layout, readability, and ordering fixes. SA1600 safely separates inline declarations before inserting docs, and SA1518 recognizes CR, LF, and CRLF for EOF checks.

Added regression coverage for LF insertion/removal, inline constructors, and CR-only EOF handling. The analyzer change scans only the existing EOF whitespace span; it does not add work to unrelated syntax nodes.

Validation

  • 973 affected-rule tests passed on the C# 6 compiler project.
  • 1,551 affected-rule tests passed on the C# 15 compiler project.
  • git diff --check passed.

Fixes #3656

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott
AArnott marked this pull request as ready for review October 20, 2026 01:10
Copilot AI balanced review requested due to automatic review settings October 20, 2026 01:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

SA1600 can produce invalid code for inline declarations, and SA1518 leaves required-newline diagnostics unresolved for CR-only documents.

2 open issues
What changed in this PR

Updates code fixes to preserve nearby document line endings instead of introducing mismatched newline sequences, addressing #3656.

Changes:

  • Centralizes newline selection with workspace options as a fallback.
  • Applies newline preservation across documentation, layout, readability, and ordering fixes.
  • Adds LF regression tests for SA1507 and SA1600.
File Description
test/​StyleCop.Analyzers.Test/​LayoutRules/​SA1507UnitTests.cs Adds LF regression coverage.
test/​StyleCop.Analyzers.Test/​DocumentationRules/​SA1600UnitTests.cs Tests LF constructor documentation.
src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1132CodeFixProvider.cs Preserves newlines when splitting declarations.
src/​StyleCop.Analyzers.CodeFixes/​ReadabilityRules/​SA1103CodeFixProvider.cs Preserves query-expression newlines.
src/​StyleCop.Analyzers.CodeFixes/​OrderingRules/​ElementOrderCodeFixProvider.cs Preserves newlines during member reordering.
src/​StyleCop.Analyzers.CodeFixes/​LayoutRules/​SA1518CodeFixProvider.cs Selects document newlines at EOF.
src/​StyleCop.Analyzers.CodeFixes/​LayoutRules/​SA1516CodeFixProvider.cs Preserves member-separation newlines.
src/​StyleCop.Analyzers.CodeFixes/​LayoutRules/​SA1515CodeFixProvider.cs Preserves blank lines before comments.
src/​StyleCop.Analyzers.CodeFixes/​LayoutRules/​SA1514CodeFixProvider.cs Preserves spacing before documentation.
src/​StyleCop.Analyzers.CodeFixes/​LayoutRules/​SA1507CodeFixProvider.cs Preserves newlines when removing blank lines.
src/​StyleCop.Analyzers.CodeFixes/​LayoutRules/​SA1504CodeFixProvider.cs Preserves accessor-layout newlines.
src/​StyleCop.Analyzers.CodeFixes/​LayoutRules/​SA1502CodeFixProvider.cs Preserves expanded-element newlines.
src/​StyleCop.Analyzers.CodeFixes/​LayoutRules/​SA1501CodeFixProvider.cs Preserves statement-layout newlines.
src/​StyleCop.Analyzers.CodeFixes/​LayoutRules/​SA1500CodeFixProvider.cs Preserves brace-layout newlines.
src/​StyleCop.Analyzers.CodeFixes/​Helpers/​FormattingHelper.cs Adds shared newline-selection overloads.
src/​StyleCop.Analyzers.CodeFixes/​DocumentationRules/​SA1642SA1643CodeFixProvider.cs Preserves constructor-summary newlines.
src/​StyleCop.Analyzers.CodeFixes/​DocumentationRules/​SA1615SA1616CodeFixProvider.cs Preserves returns-documentation newlines.
src/​StyleCop.Analyzers.CodeFixes/​DocumentationRules/​SA1609SA1610CodeFixProvider.cs Preserves value-documentation newlines.
src/​StyleCop.Analyzers.CodeFixes/​DocumentationRules/​SA1600CodeFixProvider.cs Revises documentation insertion and indentation.
src/​StyleCop.Analyzers.CodeFixes/​DocumentationRules/​InheritdocCodeFixProvider.cs Preserves inheritdoc newlines.

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings October 20, 2026 01:26

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Cross-provider formatting and SA1600’s text rewrite need human validation alongside resolution of the retained correctness and coverage concerns.

No open issues 🎉

2 previously missed issues

In code that hasn't changed since last review

Medium severity Add LF regression tests for documentation fixers and Fix All

src/​StyleCop.Analyzers.CodeFixes/​Helpers/​FormattingHelper.cs:43

Add LF regression tests for the documentation fixes using this overload: Inheritdoc, SA1609, SA1615, and SA1642. Keep the verifier's CRLF workspace option and check the final output through VerifyCSharpFixAsync, including Fix All. Their existing tests do not explicitly exercise LF input. The new SA1600 test is not a substitute: SA1600 now returns a text edit, while these providers return syntax roots that can undergo code-action formatting. Tests should verify that this later processing preserves the selected newline.

Medium severity Handle all Roslyn-supported newline characters in SA1518 fixer

src/​StyleCop.Analyzers/​LayoutRules/​SA1518UseLineEndingsCorrectlyAtEndOfFile.cs:157

With newlineAtEndOfFile = require, a file such as class C { }\u2028 is diagnosed because this loop does not count U+2028. The updated SA1518 fixer selects that same newline from SourceText.Lines, so applying the fix leaves the diagnostic unresolved. U+0085 and U+2029 have the same problem. Count all these Roslyn-supported newline characters and add regression tests that verify the diagnostic clears after fixing.

2 resolved since last review

Review effort: Balanced

@codecov

codecov Bot commented Oct 20, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 98.79518% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.65%. Comparing base (2b8bc7d) to head (c59daea).
⚠️ Report is 9 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4360      +/-   ##
==========================================
+ Coverage   97.62%   97.65%   +0.02%     
==========================================
  Files        1336     1342       +6     
  Lines      149792   150600     +808     
  Branches     5098     5149      +51     
==========================================
+ Hits       146239   147061     +822     
+ Misses       2265     2238      -27     
- Partials     1288     1301      +13     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@AArnott
AArnott merged commit aaffe1c into master Oct 20, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-animated-waffle branch October 20, 2026 01:42
This was referenced Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Review and update code fixers to use correct new line character sequence

2 participants