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

Fixed trimming comments on the remaining range #45807

Merged
merged 2 commits into from
Sep 17, 2021

Conversation

armanio123
Copy link
Member

Fixes #45526

The bug above happens because the "remaining range" contains all comments that are last in the range. This have already been trimmed in indentTriviaItems creating duplicate edits.

This PR fixes that by excluding from the remaining range the comment trivia.

@typescript-bot typescript-bot added the For Milestone Bug PRs that fix a bug with a specific milestone label Sep 9, 2021
Copy link
Member

@andrewbranch andrewbranch left a comment

Choose a reason for hiding this comment

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

I don't understand this very deeply, so it would be nice to get another reviewer

@@ -432,13 432,12 @@ namespace ts.formatting {
if (leadingTrivia) {
indentTriviaItems(leadingTrivia, indentation, /*indentNextTokenOrTrivia*/ false,
Copy link
Member

Choose a reason for hiding this comment

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

how does indentTriviaItems already get rid of trailing whitespace for trivia?

Copy link
Member Author

Choose a reason for hiding this comment

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

If there's leading trivia on the node, we first parse those in indentTriviaItems. This method pretty much apply rules for trivia only (i.e. indenting multiline/single line). The later trimTrailingWhitespacesForRemainingRange, used to ignore this trivia and cause some changes to conflict between them.

@@ -432,13 432,12 @@ namespace ts.formatting {
if (leadingTrivia) {
indentTriviaItems(leadingTrivia, indentation, /*indentNextTokenOrTrivia*/ false,
item => processRange(item, sourceFile.getLineAndCharacterOfPosition(item.pos), enclosingNode, enclosingNode, /*dynamicIndentation*/ undefined!));
if (options.trimTrailingWhitespace !== false) {
trimTrailingWhitespacesForRemainingRange(leadingTrivia);
Copy link
Member

Choose a reason for hiding this comment

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

why don't we need to call trimTrailingWhitespacesForRemainingRange in the above case when isOnToken() is true?

Copy link
Member Author

Choose a reason for hiding this comment

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

trimTrailingWhitespacesForRemainingRange() should only be use for leading trivia I believe. By the time isOnToken() is false that means we're at the end of the file or at trivia after processing the previous nodes or outside the original range; otherwise we'll require to process the node like we're doing in on the previous lines.

Hope this clarifies it.

Copy link
Member

@gabritto gabritto left a comment

Choose a reason for hiding this comment

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

Since I'm not very familiar with this, I left some questions so that I can understand a few things before I can fully review it.

Copy link
Member

@gabritto gabritto 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 explanations!

@armanio123 armanio123 merged commit 8346143 into microsoft:main Sep 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Format Document doesn't work when .ts file has {tab}{tab}/*{tab} in it
4 participants