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

Restore cursor to same line of code, not same line of buffer #989

Merged
merged 1 commit into from
Oct 20, 2019
Merged

Restore cursor to same line of code, not same line of buffer #989

merged 1 commit into from
Oct 20, 2019

Conversation

chmreid
Copy link
Contributor

@chmreid chmreid commented Aug 17, 2019

This updates the way that the new cursor location in the buffer is determined. It is a two-step process. First, all code ahead of the cursor is run through the Black formatter, and the new length of the code ahead of the cursor is determined. This sets the new location of the cursor. Second, the entire buffer is run through Black, as it was previously.

The end result is that the user can call the Black formatter and have the cursor remain on the same line of code as it was before and after the Black formatting process.

To make sure the code ahead of the cursor will pass through the Black formatter okay, we check if the last line ends with : and if so we tack pass onto the end.

This pull request is not very robust to corner cases (yet). For example, if the cursor is located in the middle of a long, multi-line list of arguments.

@ambv
Copy link
Collaborator

ambv commented Oct 20, 2019

Thanks! ✨ 🍰 ✨

@ahrex
Copy link
Contributor

ahrex commented Oct 21, 2019

Hi @chmreid, I think your PR introduced a few issues:

I've given it some thought, and I don't know the best way of restoring to the current line without multiple passes.

I also know that trying to reformat half of a python file could result in errors, as demonstrated above. Maybe we can back this out and give it a second try?

@ambv
Copy link
Collaborator

ambv commented Oct 21, 2019

Right, I was too hasty in merging this. Let me back this out so @ahrex can land his thing first. Then we can work out the edge cases.

ambv added a commit that referenced this pull request Oct 21, 2019
…989)"

This reverts commit 65c5a0d.

Edge cases were discovered on the pull request post merge.
@chmreid
Copy link
Contributor Author

chmreid commented Oct 22, 2019

This pull request is not very robust to corner cases (yet). For example, if the cursor is located in the middle of a long, multi-line list of arguments.

Making this bulletproof/robust to corner cases would, I think, become a quite complicated exercise in parsing Python syntax and I'm not convinced it is worth it

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.

3 participants