-
-
Notifications
You must be signed in to change notification settings - Fork 95
Fix source generated code indentation by properly handling CodeWriter initialization and multi-line object assignments #2807
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
Conversation
Co-authored-by: thomhurst <[email protected]>
…lization and MetadataGenerationHelper indentation levels Co-authored-by: thomhurst <[email protected]>
…of multi-line object initialization Co-authored-by: thomhurst <[email protected]>
…ine object initialization in source generated code Co-authored-by: thomhurst <[email protected]>
- Refactored metadata generation methods to use CodeWriter directly instead of returning strings - Fixed indentation by using SetIndentLevel instead of Indent/Unindent to avoid unwanted EnsureNewLine calls - All nested object initializer properties now have correct indentation levels - ClassMetadata lambda bodies now properly indent their content
|
Claude finished @Copilot's task —— View job Code Review for PR #2807 - Fix Source Generated Code IndentationI've thoroughly reviewed the pull request that fixes indentation issues in source generated code. Here's my comprehensive analysis: Todo List:
✅ Overall Assessment: Excellent FixThis PR addresses a critical maintainability issue with generated code formatting. The solution is well-architected and tackles the root causes systematically. 🔧 Code Quality & Best PracticesStrengths:
Architectural Decisions:
🐛 Potential Issues FoundCritical Issue - CodeWriter.cs:29: _isNewLine = true; // Fix: Always start at new line state for proper indentation✅ Good fix, but the comment could be clearer about why this is needed. Minor Concerns:
⚡ Performance ConsiderationsGenerally Good:
Minor Optimization Opportunity: 🔒 Security AssessmentNo Security Concerns Identified
🧪 Test CoverageExcellent Test Coverage:
Test Quality: MethodMetadata = new global::TUnit.Core.MethodMetadata
{
Type = typeof(global::TUnit.TestProject.NameOfArgumentTests), // ✅ Properly indented
Parameters = new global::TUnit.Core.ParameterMetadata[] // ✅ Proper alignment
{
new global::TUnit.Core.ParameterMetadata(typeof(string)) // ✅ Nested indentation correct
{
Name = "name", // ✅ Object initializer aligned
},
},
}📈 RecommendationsHigh Priority:
Medium Priority: Low Priority: 🎯 Final Verdict✅ Ready to Merge - This is a solid fix that addresses a real maintainability problem. The approach is sound, the implementation is correct, and the test coverage is comprehensive. The minor issues identified are opportunities for future refinement rather than blockers. The generated code will now be much more readable and maintainable, which is crucial for debugging source generator issues. |
|
Claude finished @Copilot's task —— View job Code Review for PR #2807 - Fix Source Generated Code IndentationTodo List:
✅ Overall Assessment: Excellent FixThis PR addresses a critical maintainability issue with generated code formatting. The solution is well-architected and tackles the root causes systematically. 🔧 Code Quality & Best PracticesStrengths:
Architectural Decisions:
🐛 Potential Issues FoundCritical Issue - CodeWriter.cs:29: _isNewLine = true; // Fix: Always start at new line state for proper indentation✅ Good fix, but the comment could be clearer about why this is needed. Minor Concerns:
⚡ Performance ConsiderationsGenerally Good:
Minor Optimization Opportunity: 🔒 Security AssessmentNo Security Concerns Identified
🧪 Test CoverageExcellent Test Coverage:
Test Quality: MethodMetadata = new global::TUnit.Core.MethodMetadata
{
Type = typeof(global::TUnit.TestProject.NameOfArgumentTests), // ✅ Properly indented
Parameters = new global::TUnit.Core.ParameterMetadata[] // ✅ Proper alignment
{
new global::TUnit.Core.ParameterMetadata(typeof(string)) // ✅ Nested indentation correct
{
Name = "name", // ✅ Object initializer aligned
},
},
}📈 RecommendationsHigh Priority:
Medium Priority: Low Priority: 🎯 Final Verdict✅ Ready to Merge - This is a solid fix that addresses a real maintainability problem. The approach is sound, the implementation is correct, and the test coverage is comprehensive. The minor issues identified are opportunities for future refinement rather than blockers. The generated code will now be much more readable and maintainable, which is crucial for debugging source generator issues. |
The source generated code in
*.verified.txtfiles had inconsistent indentation, particularly for complex object initializations likeMethodMetadata. The generated code showed improper alignment where opening braces and content were not indented correctly relative to their containing context.Problem
Looking at the generated code, the indentation issue was evident in patterns like this:
Root Causes
CodeWriter Initialization: When creating new
CodeWriterinstances withincludeHeader: false, the_isNewLinefield wasn't properly initialized totrue, preventing proper indentation on the first line.Context Loss: Inner code generation methods in
MetadataGenerationHelpercreated newCodeWriterinstances without inheriting the current indentation context from the parent writer.Multi-line Assignment Handling: Complex object assignments needed special handling where the first line continues the assignment and subsequent lines maintain proper relative indentation.
Solution
1. Fixed CodeWriter Initialization
_isNewLine = trueinitialization whenincludeHeader = falseto ensure proper indentation behavior.2. Added Indentation Context Propagation
MetadataGenerationHelpermethods to acceptcurrentIndentLevelparameter:GenerateMethodMetadataGenerateClassMetadataGetOrAddGenerateParameterMetadataArrayForMethodGenerateParameterMetadataArrayForConstructorGeneratePropertyMetadataArray3. Implemented Proper Multi-line Object Handling
SourceInformationWriter.GenerateMethodInformationwith sophisticated multi-line handling logicGenerateClassInformationandTestInformationGenerator.GenerateTestInformationResult
The generated code now has proper, consistent indentation:
This fix ensures that all source generated code follows consistent indentation patterns, making the codebase more maintainable and readable.
Fixes #2806.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.