Table of Contents
CodeMerger Documentation
Overview
CodeMerger is a tool designed to intelligently merge C# code modifications from LLM (Language Learning Model) outputs with existing source code. It preserves original code structure while incorporating changes and additions proposed by the LLM.
Features
- Merges code changes while maintaining original namespaces and class structures
- Handles partial code updates using continuation markers
- Supports adding new members (fields, properties, methods)
- Preserves code formatting and structure
- Provides detailed logging of merge operations
Core Components
cCodeMerger
The main class responsible for code merging operations. Uses the Roslyn API for C# syntax analysis and manipulation.
Key Features
- Namespace preservation and mapping
- Class member merging
- Method body combining with continuation support
- Detailed merge logging
Usage Example
var merger = new cCodeMerger();
var logger = new LogViewModel();
var result = merger.MergeCode(
originalCode: File.ReadAllText("original.cs"),
llmCode: File.ReadAllText("llm.cs"),
outputPath: "merged.cs",
logger: logger
);
Special Markers
Continuation Marker
// ... resto del código ...
Used in LLM code to indicate where original code should be preserved after the modifications.
Section Markers
The merger adds these markers automatically to highlight changes:
// ----------------------------------------
// Added: New method
// ----------------------------------------
// Modified: Updated implementation
Merging Process
1. Code Analysis
- Parses both original and LLM code using Roslyn
- Maps namespaces and classes
- Identifies matching methods and members
2. Member Processing
-
Fields
- Updates existing fields if modified in LLM code
- Adds new fields from LLM code
-
Properties
- Preserves original properties unless modified
- Incorporates new properties from LLM code
-
Methods
- Merges method bodies using continuation markers
- Preserves original code after continuation points
- Adds new methods from LLM code
3. Code Organization
- Maintains original namespace structure
- Preserves class hierarchies
- Adds clear separation markers for changes
Best Practices
Code Structure
- Keep namespace declarations consistent between original and LLM code
- Use continuation markers for partial updates
- Maintain consistent method signatures
Error Handling
The merger provides detailed error information through:
- Success/failure status in
MergeResult
- Diagnostic messages in the log
- Detailed debug information during merging
Technical Requirements
- .NET 9.0 or later
- Microsoft.CodeAnalysis.CSharp package
- AvalonEdit (for UI components)
Limitations
- Only supports C# code
- Requires syntactically valid input code
- Best results with matching namespace structures
Logging
The merger provides detailed logging through the LogViewModel
with different log levels:
- Debug: Detailed processing information
- Info: General operation status
- Warning: Potential issues
- Error: Operation failures
This documentation covers the main aspects of the CodeMerger tool. For more specific details, refer to the inline code comments and XML documentation.