Skip to content
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

Add commentValue property to Trivia for cleaned comments #2966

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

zyadtaha
Copy link

This PR continues the work from #2578, addressing the review comments and refining the behavior of Trivia.commentValue.

- Copied changes from PR swiftlang#2578 (swiftlang#2578)
- Original work by @adammcarter
- Manual copy due to inaccessible original branch
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the issue, @zyadtaha. I think the PR still needs some polishing regarding indentation handling. I left a few comments inline.

@zyadtaha zyadtaha force-pushed the trivia-piece-comment-value branch from 3ad450f to c23e3dc Compare February 16, 2025 10:07
@zyadtaha
Copy link
Author

Thanks for the feedback, @ahoppen! I've addressed the comments—let me know if anything else needs attention.

@zyadtaha zyadtaha requested a review from ahoppen February 17, 2025 08:15
Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thanks for the adjustments. I left a few more edge cases to consider as comments.

Add release note for Trivia.commentValue

Address PR review

Address second review
@zyadtaha zyadtaha force-pushed the trivia-piece-comment-value branch from c23e3dc to 04f700e Compare February 21, 2025 13:16
@zyadtaha zyadtaha requested a review from ahoppen February 21, 2025 13:17
@zyadtaha
Copy link
Author

Hey @ahoppen, just wanted to check if there's anything else needed from my side on this PR. Let me know if you have any concerns. Thanks!

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Thanks for the updates. I think there are still a few more issues that need to be addressed.


// Helper function to trim leading and trailing newlines
func trimNewlines(_ text: String) -> String {
let trimmed = text.drop(while: { $0 == "\n" })
Copy link
Member

Choose a reason for hiding this comment

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

You should also cover \r\n (Windows-style newlines) here as well.

func processBlockComment(_ text: String, prefix: String, suffix: String) -> String {
var text = text
text.removeFirst(prefix.count)
text.removeLast(suffix.count)
Copy link
Member

Choose a reason for hiding this comment

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

We can’t generally assume that the comment will end with */. For example, if you have a source file with an unterminated block comment, the end of file token will have the block comment as trivia but that trivia doesn’t end with */.


// Helper function to process multiline block comments
func processMultilineBlockComment(_ text: String) -> String {
var lines = text.split(separator: "\n", omittingEmptySubsequences: false).map(String.init)
Copy link
Member

Choose a reason for hiding this comment

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

Could you avoid constructing String here and instead work on Substring. Substring avoids memory allocations and is thus more performant.

let minIndentation =
lines
.filter { !$0.isEmpty }
.map { $0.prefix { $0 == " " }.count }
Copy link
Member

Choose a reason for hiding this comment

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

I guess we should also handle tabs as indentation.

return text
}

// Helper function to process multiline block comments
Copy link
Member

Choose a reason for hiding this comment

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

Could you be a little more specific in the comment what this function does? As it stands right now, the comment doesn’t really add any value, it just rephrases the function name.

lines.removeLast()
} else {
lines[lines.count - 1].removeLast(2)
lines[lines.count - 1] = trimWhitespace(lines[lines.count - 1])
Copy link
Member

Choose a reason for hiding this comment

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

This removes all whitespace from the last line, not just minIndentation, right?

Actually, on second reading, does their trim whitespace and then drop the prefix again in the map below, potentially removing contents of the comment?

Comment on lines +124 to +126
text.hasPrefix("/*\n")
? processMultilineBlockComment(text)
: processBlockComment(text, prefix: "/*", suffix: "*/")
Copy link
Member

Choose a reason for hiding this comment

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

I don’t think we need a general distinction here. Say if you have

  /* abc
      def
      ghi
   */

We should still be able to strip the indentation. We just need to exclude the first line from the indentation stripping.

@@ -42,6 +42,112 @@ public struct Trivia: Sendable {
pieces.isEmpty
}

/// The string contents of all the comment pieces with any comments tokens trimmed.
public var commentValue: String {
var comments = [String]()
Copy link
Member

Choose a reason for hiding this comment

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

Could this be an array of Substring instead of String to avoid intermediate memory allocations for the comment value pieces?

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.

2 participants