noonat / sublime-gofmt

Sublime plugin to run gofmt (or goimports, or whatever you want).
MIT License
28 stars 6 forks source link

Better view patching that preserves cursor position #17

Open mitranim opened 4 years ago

mitranim commented 4 years ago

Currently the plugin calls view.replace on the entire buffer contents and then restores the scroll position. This preserves the scroll position, but not the cursor position.

There's a different method we can borrow from DisposaBoy/GoSublime: diff the two buffers, and call view.replace only on the changed regions on the view. This tends to preserve both the scroll and the cursor position. I've tested this in mitranim/sublime-rust-fmt and mitranim/sublime-gofmt (my simplified alternative to this plugin), and it seems to work well. Performance seems fine; I tested this on a 5k LoC file and couldn't feel the difference.

Both of my plugins provide this as an option. If this method causes issues for some users, they can opt out. Anyway if you like the idea, I can make a PR.