lassik / emacs-format-all-the-code

Auto-format source code in many languages with one command
https://melpa.org/#/format-all
MIT License
604 stars 105 forks source link

Fix format-all--save-line-number column logic #230

Closed tsilvap closed 1 year ago

tsilvap commented 1 year ago

The current code has a bug where given that:

  1. You're editing a file that uses tabs for indentation; and

  2. You run either format-all-buffer or format-all-region and the command exits with an error, with no changes to the buffer

Then point will be moved a little to the right (depending on your width of tab). Since there are no changes to the code, the expected result would be that point stays in same place.

The reason for that is the code adds the old value of current-column to point, but point and column are not equivalent, because column is based on the width of a given character. For example, when you move through a tab character, point increases by 1 but column will increase by 8 (or whichever is the configured width for tabs).

So I fixed that and added a note in the code.

minor: Changed point-at-eol to line-end-position and point-at-bol to line-beginning-position for consistency.

lassik commented 1 year ago

Thanks, and sorry about the bug! LGTM.

Perhaps the code should not attempt to move point unless thunk actually changed some text, but that's a separate issue.