Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 25, 2025

This PR implements the Scope() method for the CSharpCodeBuilder class to automatically manage indentation levels using the using statement pattern.

Overview

The new Scope() method returns a disposable ScopeHandler struct that:

  • Increments indentation when created
  • Automatically decrements indentation when disposed (via using statement)
  • Supports nested scopes
  • Handles exceptions gracefully

Usage

var builder = new CSharpCodeBuilder();

builder.AppendLine("if (condition)");
using (builder.Scope())
{
    builder.AppendLine("return true;");
}
builder.AppendLine("return false;");

// Generates:
// if (condition)
//     return true;
// return false;

Implementation Details

  • ScopeHandler struct: A public disposable struct implementing IDisposable and IEquatable<ScopeHandler>
  • Thread-safe: Uses existing thread-safe IncrementIndent() and DecrementIndent() methods
  • Exception-safe: Proper cleanup even when exceptions occur within scopes
  • Supports both tabs and spaces: Works with existing indentation configuration

Benefits

  • Simplifies code generation: No need to manually manage indentation levels
  • Prevents indentation errors: Automatic cleanup ensures proper nesting
  • Improves readability: Clear visual indication of code structure in builder usage
  • Familiar pattern: Uses standard C# using statement for resource management

Testing

Added comprehensive test suite covering:

  • Basic scope creation and disposal
  • Nested scopes with multiple levels
  • Exception handling within scopes
  • Integration with existing indentation methods
  • Complex code generation scenarios
  • Edge cases and safety features

All existing tests continue to pass (279 total tests).

Fixes #14.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@Copilot Copilot AI changed the title [WIP] Add Scope method for indentation management Add Scope method for automatic indentation management Aug 25, 2025
@Copilot Copilot AI requested a review from samtrion August 25, 2025 08:16
Copilot finished work on behalf of samtrion August 25, 2025 08:16
Copilot AI and others added 5 commits August 26, 2025 11:04
Co-authored-by: samtrion <3283596+samtrion@users.noreply.github.com>
…th comprehensive tests

Co-authored-by: samtrion <3283596+samtrion@users.noreply.github.com>
Co-authored-by: samtrion <3283596+samtrion@users.noreply.github.com>
Copy link

codecov bot commented Aug 26, 2025

Codecov Report

❌ Patch coverage is 56.25000% with 7 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.26%. Comparing base (45354b4) to head (adfe30b).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...c/NetEvolve.CodeBuilder/CSharpCodeBuilder.Scope.cs 50.00% 5 Missing ⚠️
...lve.CodeBuilder/CSharpCodeBuilder.Documentation.cs 60.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #15      +/-   ##
==========================================
+ Coverage   80.09%   83.26%   +3.17%     
==========================================
  Files          12       13       +1     
  Lines         221      233      +12     
  Branches       70       73       +3     
==========================================
+ Hits          177      194      +17     
+ Misses         26       23       -3     
+ Partials       18       16       -2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@samtrion samtrion marked this pull request as ready for review August 27, 2025 09:53
@samtrion samtrion merged commit 20a3b3d into main Aug 27, 2025
8 of 9 checks passed
@samtrion samtrion deleted the copilot/fix-14 branch August 27, 2025 09:53
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.

Add Scope method for indentation management
2 participants