Proposal: use reformatter.el for `zig fmt`
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.
Issues with current buffer reformatting which would be solved by using reformatter.el:
- Files on remote servers (tramp) cannot be formatted and
zig-modewill 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-bufferis 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.elsolves this behaviour by running the code through the formatter (via stdin, which is possible inzig 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.
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?
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.
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 ^^.
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.
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.
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
See #75: I will merge this PR on the 1st of January 2023.