Skip to content

Add Format Code function + Format on Save (Qt frontend only)#87

Open
aik2mlj wants to merge 3 commits into
ccrma:mainfrom
aik2mlj:main
Open

Add Format Code function + Format on Save (Qt frontend only)#87
aik2mlj wants to merge 3 commits into
ccrma:mainfrom
aik2mlj:main

Conversation

@aik2mlj

@aik2mlj aik2mlj commented Apr 20, 2026

Copy link
Copy Markdown

Hi Ge, this PR is to integrate (and shamelessly promote) my chuckfmt util into miniAudicle.

This adds an Edit → Format Code menu item (Ctrl+Shift+I, which Qt auto-maps to Cmd+Shift+I on macOS) that pipes the active document through an external formatter via stdin → stdout and replaces the buffer with the result. The formatter command is configurable under Preferences → Editing and defaults to chuckfmt; any tool that honors the stdin → stdout contract will work. There's also an opt-in Format on Save checkbox (off by default) that runs the formatter before each save and save-as. If the user doesn't have chuckfmt, they will not run into issues unless they manually trigger formatting or turn on format-on-save.

The format itself is a single action that can be reverted by Ctrl+Z. If the formatter isn't on PATH, exits non-zero, or exceeds a 10-second timeout, the buffer is left untouched and the reason is shown on the status bar. Format-on-save follows the same rule: if formatting fails, the save still proceeds with the unformatted content.

This is the Qt frontend only, which covers the Linux and Windows builds plus make mac-qt for macOS. It does not cover the default Cocoa frontend for macOS yet. I'll implement it once I borrow a mac.

Testing

Install chuckfmt according to its document.

  • Format Code reformats a messy .ck; one Ctrl+Z reverts the entire change
  • Change formmatter to a nonexisting binary → status bar shows formatter not found: X, buffer unchanged
  • Formatter exits non-zero → status bar shows formatter failed: …
  • Formatter hangs → formatter timed out after ~10s
  • Two open tabs: trigger an error in each; no duplicated status messages
  • Enable Format on Save, save a messy file → written file is formatted; disable → plain save
  • Format on Save with a broken formatter still writes the file
  • Open an example (read-only), Ctrl+S then offers Save As, which writes the formatted content

aik2mlj added 3 commits April 19, 2026 20:31
Adds Edit -> Format Code (Ctrl+Shift+I, mapped to Cmd+Shift+I on macOS)
that pipes the active document through an external formatter and
replaces the buffer with the output. The formatter command is
configurable via Preferences -> Editing and defaults to `chuckfmt`; any
tool that accepts source on stdin and emits formatted source on stdout
will work.

- Full-buffer format only; single undoable action; cursor/scroll
  position preserved best-effort.
- Errors (command not found, non-zero exit, timeout) leave the buffer
  untouched and surface on the status bar via a new formatError signal.
- Qt frontend only
New checkbox in Preferences -> Editing (default off). When enabled,
save() and saveAs() invoke formatCode() before writing.
When the formatter preference is left at the default `chuckfmt` and it's
not found in PATH, point users to https://github.com/aik2mlj/chuckfmt
instead of the generic "formatter not found" message.
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.

1 participant