-
Notifications
You must be signed in to change notification settings - Fork 7
Description
brioche fmt and the LSP server can re-format a project-- implemented by running .bri files through Prettier. Of course, this can re-indent lines. But, Prettier preserves indentations and whitespace when formatting tagged template literals.
This is obviously the correct behavior because whitespace can be meaningful in general. But we have the tagged template literal std.indoc and its derivatives like std.runBash, std.bashRunnable, etc., which explicitly de-indent a string. So brioche fmt can cause indentation like this (from brioche-dev/brioche-packages#1594):
function() {
return recipe
.pipe((source) =>
// Fix absolute paths in the script freetype-config
std.runBash`
sed -i 's| prefix=".*"| prefix=\`dirname "\$0"\`/..|' "$BRIOCHE_OUTPUT/bin/freetype-config"
sed -i 's| exec_prefix=".*"| exec_prefix=\${prefix}|' "$BRIOCHE_OUTPUT/bin/freetype-config"
sed -i 's| libdir=".*"| libdir=\${prefix}/lib|' "$BRIOCHE_OUTPUT/bin/freetype-config"
sed -i 's| includedir=".*"| includedir=\${prefix}/include|' "$BRIOCHE_OUTPUT/bin/freetype-config"
`
.outputScaffold(source)
.toDirectory(),
)
}...where we'd really want it to look like this after formatting:
function() {
return recipe
.pipe((source) =>
// Fix absolute paths in the script freetype-config
std.runBash`
sed -i 's| prefix=".*"| prefix=\`dirname "\$0"\`/..|' "$BRIOCHE_OUTPUT/bin/freetype-config"
sed -i 's| exec_prefix=".*"| exec_prefix=\${prefix}|' "$BRIOCHE_OUTPUT/bin/freetype-config"
sed -i 's| libdir=".*"| libdir=\${prefix}/lib|' "$BRIOCHE_OUTPUT/bin/freetype-config"
sed -i 's| includedir=".*"| includedir=\${prefix}/include|' "$BRIOCHE_OUTPUT/bin/freetype-config"
`
.outputScaffold(source)
.toDirectory(),
)
}(2 extra spaces removed per line within the std.runBash tagged template literal)
Implementation-wise, it's not too hard to write a Prettier plugin or post-processing step to re-indent tagged template literals, but the hard part is deciding which tagged template literals are safe to re-indent. Hard-coding a list would be do-able, but would lead to churn if we add new ones. It'd be best if we could somehow mark the tagged template literals that are safe to reindent (maybe via a magic value within a doc comment?)