Format multi-line string literal arguments without extra lines

891 Views Asked by At

I would like to call macros with a single multi-line string argument, formatted like so:

css!(r"
    background: grey;
    color: white;
");

However, Rustfmt insists on putting the string literal on its own line, which is ugly and takes up more space:

css!(
    r"
    background: grey;
    color: white;
"
);

Is there a way I can tell Rustfmt to not put a single string literal on its own line, even when it has multiple lines?

I am aware that Rustfmt can be configured, but I couldn't find an option for this and I don't really know what to search for.

1

There are 1 best solutions below

0
On

You can use #[rustfmt::skip] to tell rustfmt to not format code:

#[rustfmt::skip]
css!(r"
background: grey;
color: white;
");

rustfmt also skips braced macros, so you can do:

css! { r"
background: grey;
color: white;
" };

You can also use #[rustfmt::skip::macros(...)] to always skip this macro:

#[rustfmt::skip::macros(css)]
fn foo() { ... }

To apply it whole-file (see Is there a stable way to tell Rustfmt to skip an entire file), you can either apply it to the module declaration, or use an inner attribute, but that's unstable:

#![feature(custom_inner_attributes)]
#![rustfmt::skip::macros(css)]