lassik / emacs-format-all-the-code

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

How can we implement a `format-modified-region` with `format-all-region` #242

Open xeechou opened 1 year ago

xeechou commented 1 year ago

Hi, it is nice to have format-all-region that only format one region of the buffer. However I am not sure how useful it is. I tried to archive formatting only the modified regions (as it is already available feature in vscode, neovim, etc) for large code base with legacy code.

I try to implement this feature with (after-change-functions) hook and format-all-region but it is not possible since format-all-region modifies the entire buffer anyway.

I am wondering how we can archive the similar feature as in https://github.com/microsoft/vscode/pull/104994 ?

lassik commented 1 year ago

it is not possible since format-all-region modifies the entire buffer anyway.

It should not modify the entire buffer. That sounds like a bug. Which formatter did you test it with?

xeechou commented 1 year ago

it is not possible since format-all-region modifies the entire buffer anyway.

It should not modify the entire buffer. That sounds like a bug. Which formatter did you test it with?

I don't meant it changed actual "content" of the entire buffer. If you turned on highlight-changes-mode and run a format-all-région you will see. I am using clang-format

xeechou commented 1 year ago

I found the only reliable way to format only changed lines is relying on output from diff. I created a small fmo-mode to apply format-all-region on only changed lines.