Skip to content

create new undo block for formatting (fix #30) #55

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

Merged
merged 4 commits into from
Jan 6, 2017
Merged

create new undo block for formatting (fix #30) #55

merged 4 commits into from
Jan 6, 2017

Conversation

0xC0FFEE
Copy link
Contributor

@0xC0FFEE 0xC0FFEE commented Jan 4, 2017

This PR creates a new undo block to separate formatting related changes from the last undo block. The proposed solution in #30 (to simply remove the undojoin) does not respect the old cursor position. Thus a new undo block is created to store the current cursor position.

This creates a new undo block to only contain the changes introduced by
clang format but still preserves the original cursor position.
@rhysd
Copy link
Owner

rhysd commented Jan 4, 2017

test failed in visual mode: https://travis-ci.org/rhysd/vim-clang-format/jobs/188839298

Could you confirm this works in visual mode?

@0xC0FFEE
Copy link
Contributor Author

0xC0FFEE commented Jan 4, 2017

There was indeed a problem in visual mode when the cursor was at the first line of the buffer. Should be fixed now.

Edit: Should I squash the commits?

@rhysd
Copy link
Owner

rhysd commented Jan 4, 2017

silent execute "normal! ii<esc>x"

Current implementation looks very hacky and not easy to maintain. Instead of considering normal mode and visual mode at once, can we use mode() as below?

let mode = mode()
if mode ==? 'v'
  " normal! for visual (or visual block) mode
else
  " otherwise
endif

==? is comparison ignoring cases.

Edit: Should I squash the commits?

You need not squash manually. I can do it on GitHub when mergin PR 👍

@0xC0FFEE
Copy link
Contributor Author

0xC0FFEE commented Jan 4, 2017

I would gladly like a more maintainable solution, but unfortunately I don't know how to realize it.

The current implementation just performs a 'dummy edit' in order to create a new undo block. Due to the special handling of the first line in a buffer this dummy edit must be reverted. So AFAICT a special handling for visual mode won't help. Or is there an easier solution I am missing? Sadly I could not find a way to directly add an empty undo block to vim's history without actually modifying the buffer.

Edit: After a couple of tests it seems that mode() is never equal to 'v' or 'V' at this point in code.

Reminder to myself: the text yanked by x should be redirected to the black hole register.

@rhysd
Copy link
Owner

rhysd commented Jan 5, 2017

@0xC0FFEE

I would gladly like a more maintainable solution, but unfortunately I don't know how to realize it.

np. I think this is good start point to improve undo. I'll merge this after your tiny fix and then try improving it later.

Reminder to myself: the text yanked by x should be redirected to the black hole register.

yeah, and entering insert mode fires InsertEnter and InsertLeave events unintentionally. So below is better:

silent execute "noautocmd normal! ii\<esc>\"_x"

noautocmd suppresses autocmd events while the execute command.

@rhysd
Copy link
Owner

rhysd commented Jan 6, 2017

Thanks.

@rhysd rhysd merged commit 4475797 into rhysd:master Jan 6, 2017
@oblitum
Copy link

oblitum commented Jan 7, 2017

I think this solves issue #8. The expectation at the time of that issue I guess was not to have an extra undo point for formatting, but since that can't be easily circumvented or the choice is to keep this undo point regardless, then the only thing left from that issue regards positioning, which seems fixed here.

@rhysd
Copy link
Owner

rhysd commented Jan 8, 2017

On undo, cursor moves to the top of buffer. So I think #8 is not solved yet. vim-go resolves it by creating a new file formatted with gofmt and replace current file with it. But I'm afraid of it making a side effect...

@0xC0FFEE 0xC0FFEE deleted the fix-undo branch January 8, 2017 08:34
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