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

Fix trailing formatting edit when range ends mid-token #50082

Merged
merged 1 commit into from
Jul 28, 2022

Conversation

andrewbranch
Copy link
Member

Fixes #50076

@andrewbranch andrewbranch marked this pull request as ready for review July 28, 2022 18:16
@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Jul 28, 2022
// with a selection from the beginning of the line to the space inside the curly braces,
// inclusive. We would expect a format-selection would delete the space (if rules apply),
// but in order to do that, we need to process the pair ["{", "}"], but we stopped processing
// just before getting there. This block handles this trailing edit.
Copy link
Member Author

Choose a reason for hiding this comment

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

This comment should have been included with #46875.

// produce bogus edits if we try to `processPair`. Recall that the point of this logic is
// to perform a trailing edit at the end of the selection range: but there can be no valid
// edit in the middle of a token where the range ended, so if we have a non-contiguous
// pair here, we're already done and we can ignore it.
Copy link
Member Author

Choose a reason for hiding this comment

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

This comment describes the fix for this bug.

Copy link
Member

@jakebailey jakebailey left a comment

Choose a reason for hiding this comment

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

LGTM, I'm always suspicious of the ! but they're at least consistent with the other side-effecty previous variables, so...

@andrewbranch
Copy link
Member Author

Yeah, it’s correlated with the existence of previousRange.

Comment on lines +451 to +452
// but in order to do that, we need to process the pair ["{", "}"], but we stopped processing
// just before getting there. This block handles this trailing edit.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// but in order to do that, we need to process the pair ["{", "}"], but we stopped processing
// just before getting there. This block handles this trailing edit.
// but in order to do that, we need to process the pair ["{", "}"].
// This block handles this trailing edit by ensuring we continue processing.
// the pair of tokens that the range intersects.

const tokenInfo =
formattingScanner.isOnEOF() ? formattingScanner.readEOFTokenRange() :
formattingScanner.isOnToken() ? formattingScanner.readTokenInfo(enclosingNode).token :
undefined;

if (tokenInfo) {
if (tokenInfo && tokenInfo.pos === previousRangeTriviaEnd!) {
// We need to check that tokenInfo and previousRange are contiguous: the `originalRange`
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// We need to check that tokenInfo and previousRange are contiguous: the `originalRange`
// We need to check that previousRange and tokenInfo are contiguous since the `originalRange`

@andrewbranch andrewbranch merged commit b7355e3 into microsoft:main Jul 28, 2022
@andrewbranch andrewbranch deleted the bug/50076 branch July 28, 2022 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team 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 selection (and thus format-on-paste) causes destructive edits
4 participants