Skip to content

Add commentValue property to Trivia for cleaned comments #2966

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

Open
wants to merge 5 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.

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.

Copy link
Author

Choose a reason for hiding this comment

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

But even if we exclude the first line from indentation stripping, wouldn't the minimum indentation still be 3 due to the last line? That would result in:

abc
   def
   ghi

Maybe we should consider excluding the last line from indentation stripping as well?

@zyadtaha
Copy link
Author

Hey @ahoppen, I've pushed my updates. For your last two comments, I've added responses—let me know your feedback when you get a chance. Thanks!

@zyadtaha zyadtaha force-pushed the trivia-piece-comment-value branch from 8df001d to 52fdc83 Compare April 19, 2025 15:19
@zyadtaha zyadtaha requested a review from ahoppen April 19, 2025 15:21
}
}

return (Swift.min(currentMin.0, spaceCount), Swift.min(currentMin.1, tabCount))
Copy link
Member

Choose a reason for hiding this comment

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

I think this doesn’t work if you have one line that contains a space as indentation and another one that only contains a tab as indentation.

Copy link
Author

Choose a reason for hiding this comment

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

In this case:

/* comment
\tcomment*/

The minIndentation would be 1 because it only starts checking from the second line, which contains a tab. Since the first line is ignored during trimming if it contains the /*, the output would be:

 comment
comment

But in this case:

/* 
 comment
\tcomment*/

The minIndentation would be 0, as expected, and the output would be:

 comment
\tcomment

If we want both cases to produce consistent output, maybe we could insert a \n after the /* if it's not already present, to ensure that minIndentation is 0. What do you think?

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