zig-mode icon indicating copy to clipboard operation
zig-mode copied to clipboard

Proposal: use reformatter.el for `zig fmt`

Open joachimschmidt557 opened this issue 5 years ago • 5 comments

https://github.com/purcell/reformatter.el provides an easy interface to provide idiomatic formatting commands. Using this library would make it would be possible to move a lot of the code which deals with zig fmt from this package to reformatter.el. Of course, this means that this mode has one more Elisp package as a dependency.

joachimschmidt557 avatar Oct 06 '20 18:10 joachimschmidt557

Issues with current buffer reformatting which would be solved by using reformatter.el:

  • Files on remote servers (tramp) cannot be formatted and zig-mode will annoyingly tell this to the user every time a file is saved
  • Files which are not saved on disk (e.g.*scratch*) cannot be formatted
  • revert-buffer is run after every format (and thus after every save), which leads to unexpected behavior such as font size going back to the default, LSP mode is turned off (#49), etc. reformatter.el solves this behaviour by running the code through the formatter (via stdin, which is possible in zig fmt) before saving so no revert is necessary

Due to this, I would suggest that we consider this proposal and weigh the advantages with the disadvantage of having one additional dependency.

joachimschmidt557 avatar Dec 08 '20 19:12 joachimschmidt557

Most of the programming major modes in Emacs do not seem to have explicit code formatting functionality, but rather, packages such as https://github.com/lassik/emacs-format-all-the-code are used. Coupled with LSP support for formatting via zls, is there a need to have explicit formatting support in zig-mode?

GarbageHamburger avatar Dec 08 '20 19:12 GarbageHamburger

If the zig fmt is not used often by users of zig-mode, we can also consider removing that. But I personally use it all the time and the format on save functionality helps me a lot. zig-mode already supports compiling, running and testing zig source files, so formatting is not very far-fetched.

Seeing zig.vim also having formatting and format on save builtin, I support keeping formatting functionality and moving it to reformatter.el.

joachimschmidt557 avatar Dec 08 '20 19:12 joachimschmidt557

I see. I will look into adding this myself, I'm also looking into #13 as that significantly improves interop, but I am in no way an expert elisp hacker ^^.

GarbageHamburger avatar Dec 08 '20 19:12 GarbageHamburger

I want to add my zig-mode format experience here, given that it's very useful to have hints about my zig code, it doesn't play well with undo-hist, and cause it to prompt buffer-undo-list is not empty. Do you want to recover now? (y or n) every time zig formats on before save hook.

Inc0n avatar Jul 03 '22 14:07 Inc0n

Hi @joachimschmidt557 I try your forked version, https://github.com/ziglang/zig-mode/pull/51/files

it works amazing, since zls is not very stable now, so I suggest we merge it in master.

PS: I guess no one care one more package, besides its author is purcell, master of elisp.

jiacai2050 avatar Dec 11 '22 11:12 jiacai2050

I'm a long-time rust-format-buffer user, and there is no such logic in there

  • https://github.com/rust-lang/rust-mode/blob/master/rust-rustfmt.el#L42

Maybe we can follow what rust-mode does, pass stdin to zig fmt, and reinsert via replace-buffer-contents when succeed

jiacai2050 avatar Dec 11 '22 12:12 jiacai2050

See #75: I will merge this PR on the 1st of January 2023.

joachimschmidt557 avatar Dec 16 '22 22:12 joachimschmidt557