Skip to content

Conversation

@dzsquared
Copy link
Contributor

@dzsquared dzsquared commented Jan 29, 2026

Description

resolves #20

enables the code formatter to preserve T-SQL comments, including

-- comments leading any T-SQL

/* comments
that span
multiple lines */

-- single line comments anywhere in the code

/********************
gigantic multiline
masterpiece comments
**********/


/*** comments
-- where more comments
are nested within
***/

Code Changes

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a PreserveComments option to SqlScriptGeneratorOptions to enable the script generator to preserve T-SQL comments (both single-line -- and multi-line /* */) in the generated output. This addresses issue #20 which requested comment preservation for code formatting/pretty-printing scenarios.

Changes:

  • Adds PreserveComments boolean option to SqlScriptGeneratorOptions with default value false for backward compatibility
  • Implements comment tracking and emission logic in SqlScriptGeneratorVisitor.Comments.cs
  • Adds helper classes CommentInfo and CommentPosition to manage comment metadata
  • Modifies TSqlScript and TSqlBatch visitors to call comment emission logic before/after fragments
  • Adds comprehensive unit tests in ScriptGeneratorTests.cs to verify comment preservation behavior
  • Adds baseline test scripts SingleLineCommentTests170.sql and MultiLineCommentTests170.sql

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 9 comments.

Show a summary per file
File Description
SqlScriptGeneratorOptions.xml Adds PreserveComments boolean setting with false default
SqlScriptGeneratorVisitor.Comments.cs Core implementation of comment tracking and emission logic
SqlScriptGeneratorVisitor.TSqlScript.cs Integrates comment handling at script level
SqlScriptGeneratorVisitor.TSqlBatch.cs Integrates comment handling at batch and statement levels
CommentInfo.cs Helper class to track comment metadata
CommentPosition.cs Enum defining comment positions (Leading/Trailing/Standalone)
ScriptGeneratorTests.cs 13 comprehensive unit tests for comment preservation
Only170SyntaxTests.cs Adds comment test scripts to baseline test suite
SingleLineCommentTests170.sql Test script with single-line comments
MultiLineCommentTests170.sql Test script with multi-line comments
Baselines170/SingleLineCommentTests170.sql Expected output with comments stripped (default behavior)
Baselines170/MultiLineCommentTests170.sql Expected output with comments stripped (default behavior)

Comment on lines +13 to +24
#region Comment Tracking Fields

/// <summary>
/// Tracks the last token index processed for comment emission.
/// Used to find comments between visited fragments.
/// </summary>
private int _lastProcessedTokenIndex = -1;

/// <summary>
/// The current script's token stream, set when visiting begins.
/// </summary>
private IList<TSqlParserToken> _currentTokenStream;
Copy link

Copilot AI Jan 29, 2026

Choose a reason for hiding this comment

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

The _lastProcessedTokenIndex and _currentTokenStream fields are instance fields of SqlScriptGeneratorVisitor without any thread synchronization. If the same SqlScriptGenerator instance is used concurrently from multiple threads (which is not a typical pattern but is possible), these fields could be corrupted. Consider documenting that SqlScriptGenerator instances are not thread-safe and should not be shared across threads, or add appropriate thread-safety mechanisms.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@llali curious your thoughts on thread safety for scriptgenerator. The overall overhead of a shared vs individual instances of the script generator across threads for common scenarios seems minimal so the justification doesn't seem to be there, but I'm potentially missing something.

@dzsquared
Copy link
Contributor Author

the-simpsons-homer-simpson

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.

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.

Provide an option in the SqlNNNScriptGenerator classes to preserve comments

2 participants