Skip to content

Respect document line endings in C# code actions and refactorings#84347

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-code-action-fixer-line-endings
Draft

Respect document line endings in C# code actions and refactorings#84347
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-code-action-fixer-line-endings

Conversation

Copilot AI commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Several C# code actions were introducing hard-coded CRLF trivia into documents that otherwise used LF, which caused Linux runs to produce mixed line endings and forced a set of issue-tracked tests to stay disabled. This PR updates the affected generators/refactorings to follow the document/host newline convention instead of assuming Windows line endings.

  • Shared newline generation

    • Switch shared C#/VB syntax services from hard-coded ElasticCarriageReturnLineFeed to elastic end-of-line trivia based on the current environment newline.
    • Update shared C# code-generation helpers that synthesize missing braces or insert generated members so they no longer inject CRLF by default.
  • Direct C# fixer/refactoring call sites

    • Replace direct CRLF trivia construction in affected refactorings/code fixes, including:
      • split/merge if statements
      • convert if to switch
      • convert auto-property to full property
      • convert to record
      • extract method
      • introduce variable
    • Keep inserted blank lines and block formatting elastic, but no longer Windows-specific.
  • Raw string conversion

    • Normalize embedded line endings when converting regular/interpolated strings to raw strings so generated source text follows the target document newline style.
    • Add a local helper for line-ending normalization compatible with the feature assembly targets.
  • Issue-tracked tests

    • Re-enable the C# tests that were disabled specifically for this newline regression so they can validate LF/CRLF behavior again.
// Before
OpenBraceToken.WithLeadingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed)

// After
OpenBraceToken.WithLeadingTrivia(SyntaxFactory.ElasticEndOfLine(Environment.NewLine))
Co-authored-by: mwiemer-microsoft <80539004+mwiemer-microsoft@users.noreply.github.com>
Co-authored-by: mwiemer-microsoft <80539004+mwiemer-microsoft@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix hard coded windows line endings in code action fixer Jun 30, 2026
Copilot AI requested a review from mwiemer-microsoft June 30, 2026 22:23
.Append(XmlText(XmlTextNewLine(lineFormattingOptions.NewLine, continueXmlDocumentationComment: false)))],
EndOfDocumentationCommentToken
.WithTrailingTrivia(DocumentationCommentExterior("*/"), ElasticCarriageReturnLineFeed));
.WithTrailingTrivia(DocumentationCommentExterior("*/"), ElasticEndOfLine(lineFormattingOptions.NewLine)));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this is weird, and shouldn't be necessary. by virtue of being elastic, the formatter should replace this with the right newline based on the options passed into it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I never identified where elastic newlines were supposed to be rewritten. I took at stab at adding it to the trivia rewriter (#83224) but haven't had time to review all the tests that broke due to changed formatting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

4 participants