-
Notifications
You must be signed in to change notification settings - Fork 108
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
Avoid repositioning the buffer after formatting? #173
Comments
I might have found a workaround. But there are some edge cases that behaves weirdly though. (defun stable/format-all-buffer ()
"Auto-format whole buffer without repositioning the buffer."
(interactive)
(let ((windowstart (window-start)))
(format-all-buffer)
(set-window-start (selected-window) windowstart))) |
I found a better workaround based on your approach. I hope this approach will help the maintainer fix this bug. ;; remove hook initially assigned when autoload
(remove-hook 'before-save-hook 'format-all--buffer-from-hook t)
;; add hook with -format-all-buffer() again
(add-hook 'before-save-hook '-format-all-buffer))
(defun -format-all-buffer ()
"format-all-buffer without jumps of cursor"
(interactive)
(let ((point (point)) (wstart (window-start)))
(format-all-buffer)
(goto-char point)
(set-window-start (selected-window) wstart))) |
Thanks for the snippet!
Can you kindly explain this part a bit? And why it might fix the edge cases I'm experiencing? |
Nothing special. The problem was that your solution didn't work as well. And I tried replacing the hook ( I'm not an elisper unlike you :) |
I'm encountering this issue as well. My current solution based on the snipped above (probably very bad because i'm new to emacs/spacemacs):
Are there any plans to fix this bug? Thanks. |
i have the problem . but above solution not grace. the author why not fix it. the problem very suffer pain |
Sorry about the long delay. Does the above commit improve things? |
ye fixed it , but curror stay old position, why not keep stay old code line position?do u know my mean? do u have some choose ,people can config choose cursor pository mode |
the effect like prettier-js format html. after format, the curror return old code line star position.like below: 2022-12-11.01.59.36.mov |
Thank you for trying to improve this issue. As 'zcjava' is saying, it does save/restore the window position now but the cursor jumps to a new location. An easy way to reproduce this is to add some useless empty newlines above the cursor, scroll down so they are off screen, and then run the formatter to remove them. |
May be we can put a invisible text property at the cursor point before format, after format, move the cursor to the new position with the text property. |
Sorry, I'm very busy with other stuff and don't have time to look into this. If you can send pull requests which improve some aspect of the problem without breaking anything, I'll merge them. |
Bump because I am having the same problem |
As titled, I noticed that when I invoke format-all's command to format the current buffer, the buffer will reposition itself (most likely because there are bits of code 'outside' of the buffer that have been formatted), resulting in an overhead to try to find my cursor position again.
Is there a way for format-all to remember the buffer position and the cursor position and restore that after the buffer is formatted?
The text was updated successfully, but these errors were encountered: