Skip to content

Instantly share code, notes, and snippets.

@jeffomatic
Last active February 5, 2023 04:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffomatic/fa443170b856b1af5ca3aa67e0ea3e65 to your computer and use it in GitHub Desktop.
Save jeffomatic/fa443170b856b1af5ca3aa67e0ea3e65 to your computer and use it in GitHub Desktop.
rustfmt --skip-children emulation script
#!/bin/bash
#
# This script filters rustfmt output for format-on-save workflows in text
# editors.
#
# Usage:
#
# rustfmt-skip-children /path/to/source
#
# In particular, it:
#
# 1. Removes output for all but the first file in the output. rustfmt prior to
# v2.0 will not only emit formatted source for the provided file, but also
# for local modules that it depends on. This is not very useful if you're
# doing single-file formatting, e.g., for format-on-save.
# 2. Removes some metadata output that precedes the actual formatting output,
# such as the filename and the following blank line. You can also do this
# simply by passing --quiet to rustfmt, but the metadata is necessary for
# achieving item 1 above.
#
# Note that this script is not necessary for rustfmt 2.0 and onward. Read more
# at https://github.com/rust-lang/rustfmt/issues/3893#issuecomment-546747980
set -euo pipefail
~/.cargo/bin/rustfmt --emit stdout "$1" | awk "
BEGIN {print_ok = 0}
{
if (\$0 == \"$(realpath $1):\") {
print_ok = 1;
# rustfmt includes a blank line after the filename
getline;
next;
}
if (/^(\/[^/]+)*\/[^/]+:/) {
print_ok = 0;
next;
}
if (print_ok == 1) {print}
}
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment