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

Use replace-buffer-contents to update formatting #45

Closed
wants to merge 1 commit into from
Closed

Use replace-buffer-contents to update formatting #45

wants to merge 1 commit into from

Conversation

ljos
Copy link

@ljos ljos commented Oct 18, 2021

blacken.el (blacken-buffer): Use replace-buffer-contents to update
formatting after running black. This is potentially slower than using
copy-to-buffer, but it keeps the position of point etc (if it can).

blacken.el (blacken-replace-buffer-contents-max-secs): Add new
variable to control how long to spend calculating
replac-buffer-contents.

Fixes #33
Fixes #15

blacken.el (blacken-buffer): Use replace-buffer-contents to update
formatting after running black. This is potentially slower than using
copy-to-buffer, but it keeps the position of point etc (if it can).

blacken.el (blacken-replace-buffer-contents-max-secs): Add new
variable to control how long to spend calculating
replac-buffer-contents.
@ljos
Copy link
Author

ljos commented Oct 18, 2021

I set the blacken-replace-buffer-contents-max-secs to nil as setting it can lead to unexpected behaviour if the timeout is reached sometimes, but not others. Then the buffer is replaced in its entirety and we lose position again.

The other issue is that replace-buffer-contents was introduced in emacs 26.1, so this change drops support for versions previous to that.

@dakra
Copy link
Member

dakra commented Oct 18, 2021

Personally I don't use blacken but replace-buffer-contents only got added Emacs 26 and the MAX-SEC parameter only Emacs 27.

I guess we could drop Emacs 25 support? But depending on Emacs 27 is surely too new.

Also I read about a couple of problems already with replace-buffer-contents.
https://github.com/purcell/emacs-reformatter/blob/b57f5d480003ab7b0880e0059dcc51747fb2e088/reformatter.el#L296

@ljos
Copy link
Author

ljos commented Oct 18, 2021

I wasn't aware of reformatter. I would suggest it is probably more robust using that. I see that there is already an alternative using it on GitHub: https://github.com/wbolster/emacs-python-black

@ljos ljos closed this Oct 18, 2021
@dakra
Copy link
Member

dakra commented Oct 18, 2021

Yes. I guess emacs-python-black is better maintained than this repository here (which is on life support only ;)

Personally I use https://github.com/raxod502/apheleia and are happy with 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.

Blacken mode does not save excursion Apply patch instead of replacing whole buffer
2 participants