Skip to content

Comments

Add TriviaPiece.commentValue convenience property#3269

Open
William-Laverty wants to merge 3 commits intoswiftlang:mainfrom
William-Laverty:feature/trivia-piece-comment-value
Open

Add TriviaPiece.commentValue convenience property#3269
William-Laverty wants to merge 3 commits intoswiftlang:mainfrom
William-Laverty:feature/trivia-piece-comment-value

Conversation

@William-Laverty
Copy link

Summary

Adds a commentValue computed property on TriviaPiece that returns the text of comment pieces and nil for non-comment pieces.

extension TriviaPiece {
  public var commentValue: String? { get }
}

Behavior

Piece commentValue
.lineComment("// hello") "// hello"
.blockComment("/* world */") "/* world */"
.docLineComment("/// docs") "/// docs"
.docBlockComment("/** docs */") "/** docs */"
.spaces(4), .newlines(1), etc. nil

This complements the existing isComment property and the higher-level Trivia.docCommentValue, providing a simple way to extract the raw text from an individual trivia piece without pattern matching.

Tests

Added TriviaPieceCommentValueTests covering all four comment kinds and all non-comment piece types.

Resolves #1890

Adds a computed property on `TriviaPiece` that returns the text of comment
pieces (`.lineComment`, `.blockComment`, `.docLineComment`,
`.docBlockComment`) and `nil` for all non-comment pieces.

This complements the existing `isComment` property and the higher-level
`Trivia.docCommentValue`, providing a simple way to extract the raw text
from an individual trivia piece without pattern matching.

Includes tests for all comment kinds and non-comment pieces.

Resolves swiftlang#1890
@ahoppen
Copy link
Member

ahoppen commented Feb 15, 2026

The key functionality of this property should be to strip the // etc introducers and just give the actual comment text, similar to docCommentValue, as explained in the discussion of #1890.

Updated commentValue to strip // /// /* */ /** */ markers and return
only the actual comment content, consistent with docCommentValue behavior
as described in swiftlang#1890.

Updated tests to match the new expected values.
@William-Laverty
Copy link
Author

Updated commentValue to strip the //, ///, /* */, and /** */ introducers and return only the actual comment content — consistent with docCommentValue behaviour as described in #1890. Tests updated accordingly. Ready for re-review!

@rintaro
Copy link
Member

rintaro commented Feb 20, 2026

Sorry for not reviewing sooner. Could you handle cases like:

/*
 * Hello, folks!
 * // Apples and oranges
 * I am a banana!
 */

as described in #1890 (comment)
Apparently the consensus was returning "Hello, folks!\n// Apples and oranges\nI am a banana!" for this case

@rintaro
Copy link
Member

rintaro commented Feb 20, 2026

Also adding a Release Notes entry would be appreciated 🙏
https://github.com/swiftlang/swift-syntax/blob/main/Release%20Notes/604.md

- Strip leading '*' decoration from multiline block comments
- Skip empty first/last lines in multiline block comments
- Add release notes entry for commentValue in 604.md
- Add tests for multiline block comment handling

Addresses review feedback from @rintaro
@William-Laverty
Copy link
Author

Thanks for the review @rintaro! I've pushed a fix that:

  • Handles multiline block comments with * decoration — strips leading whitespace + * + optional space from each line, and skips empty first/last lines
  • Added a Release Notes entry in 604.md
  • Added tests covering the multiline stripping (with and without * decoration, and doc block comments)

For the example you mentioned:

/*
 * Hello, folks!
 * // Apples and oranges
 * I am a banana!
 */

This now returns "Hello, folks!\n// Apples and oranges\nI am a banana!" as expected.

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.

Please add a TriviaPiece.commentValue property

3 participants