-
-
Notifications
You must be signed in to change notification settings - Fork 205
Respect .editorconfig end_of_line and charset in generated code
#2114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
4199944 to
3194a3e
Compare
|
Thank you for this contribution. Before I start the review, I’d like to clarify a few open questions:
|
|
Thank you @latonz! I am still working on this. Let me consider your feedback and update the PR or at least report back. |
end_of_line and charset in generated code
|
@latonz I think this is ready for your review. Your thoughtful questions led to changing the implementation for the better:
Here is a report I created with Claude that delves a little deeper. Please let me know what you think. Any feedback is welcome! Thank you for this wonderful library! |
Generated mapper files (.g.cs) now honor .editorconfig settings: - end_of_line: supports lf, crlf, cr (defaults to crlf) - charset: supports utf-8, utf-8-bom, utf-16be, utf-16le (defaults to utf-8 without BOM) This allows generated code to match the project's line ending and encoding preferences, avoiding git diffs and post-processing requirements. Co-Authored-By: Claude Opus 4.5 <[email protected]>
Use the mapper source file path (.cs) instead of trying to create a synthetic tree with the generated file path (.g.cs). This provides reliable editorconfig support with a documented limitation. Note: [*.g.cs] patterns are not supported due to a Roslyn limitation. Users should configure settings in [*.cs] or [*] sections. Co-Authored-By: Claude Opus 4.5 <[email protected]>
Instead of GetText().ToString().Replace() which creates 2 string allocations for non-CRLF line endings, use SyntaxNode.WriteTo() with a custom LineEndingTextWriter that replaces CRLF on-the-fly. This reduces allocations from 2 to 1 for LF/CR configurations while maintaining the fast path (no replacement) for CRLF (default). Co-Authored-By: Claude Opus 4.5 <[email protected]>
Clarifies that settings are read from the source file containing the mapper declaration, not from generated files. Co-Authored-By: Claude Opus 4.5 <[email protected]>
Co-Authored-By: Claude Opus 4.5 <[email protected]>
72bed81 to
76d8d14
Compare
Respect .editorconfig
end_of_lineandcharsetin generated codeDescription
Generated mapper files (
.g.cs) now respect.editorconfigsettings for line endings and character encoding.Problem
Generated code always used CRLF (
\r\n) line endings and UTF-8 without BOM, regardless of the project's.editorconfigconfiguration. This caused issues for projects configured with different line endings.Solution
Read
end_of_lineandcharsetsettings from the source file's.editorconfigviaAnalyzerConfigOptionsProviderand apply them to the generated output.Supported settings:
end_of_line:lf,crlf,cr(defaults tocrlf)charset:utf-8,utf-8-bom,utf-16be,utf-16le(defaults toutf-8without BOM)Known Limitation
Due to a Roslyn limitation, editorconfig settings are read from the source file containing the mapper declaration, not from generated
.g.csfiles. This means[*.g.cs]patterns in.editorconfigwill not be applied. Users should configure settings in[*.cs]or[*]sections.Files changed:
LineEndingTextWriter.cs- Streaming line ending replacementIncrementalValuesProviderExtensions.cs- Read editorconfig settings and apply encodingMapperNode.cs- StoreEndOfLineandCharsetpropertiesMapperGenerator.cs- Pass settings from source file to outputAddresses this discussion
Checklist