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

Avoid repositioning the buffer after formatting? #173

Open
ianyepan opened this issue Jan 31, 2022 · 13 comments
Open

Avoid repositioning the buffer after formatting? #173

ianyepan opened this issue Jan 31, 2022 · 13 comments
Labels
pull request wanted Waiting for someone to send a PR to solve this problem

Comments

@ianyepan
Copy link

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?

@ianyepan
Copy link
Author

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)))

@thyeem
Copy link

thyeem commented Feb 8, 2022

I found a better workaround based on your approach.
I'm sure this will work. The point is to remove and add the hook again.

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)))

@ianyepan
Copy link
Author

ianyepan commented Feb 8, 2022

Thanks for the snippet!

The point is to remove and add the hook again.

Can you kindly explain this part a bit? And why it might fix the edge cases I'm experiencing?

@thyeem
Copy link

thyeem commented Feb 9, 2022

Nothing special. The problem was that your solution didn't work as well.
I assumed that there was something wrong with the hook. (Obviously, no other reasons to come up with)

And I tried replacing the hook (remove and add), it worked well.
I'm suspicious of conflicts (if any) around the hook.

I'm not an elisper unlike you :)
Please take a closer look based on this assumption.

@rappie
Copy link

rappie commented Mar 28, 2022

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):

  ;; Set up 'format-all' to format on save
  (use-package format-all
    :init
    (add-hook 'solidity-mode-hook
              (lambda ()
                (format-all-ensure-formatter)
                (add-hook
                 'before-save-hook
                 `format-all-buffer--no-bufferjump)))
    )

  ;; Custom 'format' function with fixed buffer jumping
  (defun format-all-buffer--no-bufferjump ()
    "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)))

Are there any plans to fix this bug? Thanks.

@zcjava
Copy link

zcjava commented Dec 9, 2022

i have the problem . but above solution not grace. the author why not fix it. the problem very suffer pain

lassik added a commit that referenced this issue Dec 10, 2022
@lassik
Copy link
Owner

lassik commented Dec 10, 2022

Sorry about the long delay. Does the above commit improve things?

@zcjava
Copy link

zcjava commented Dec 10, 2022

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

@zcjava
Copy link

zcjava commented Dec 10, 2022

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

@rappie
Copy link

rappie commented Dec 12, 2022

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.

@tangxinfa
Copy link

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.

@lassik
Copy link
Owner

lassik commented Jan 29, 2023

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.

@lassik lassik added the pull request wanted Waiting for someone to send a PR to solve this problem label Jan 29, 2023
@yaaama
Copy link

yaaama commented Jul 15, 2023

Bump because I am having the same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pull request wanted Waiting for someone to send a PR to solve this problem
Projects
None yet
Development

No branches or pull requests

7 participants