Skip to main content

format_expressions

Function format_expressions 

Source
pub fn format_expressions(source: &str) -> String
Expand description

Put every statement of an expressions source on its own line, in order.

The only edit is a line break after a statement-ending ; that has more code after it on the same line: the spaces and tabs after the ; become a line break plus that line’s own indentation. Nothing else moves. A ; followed (on its line) only by a // comment keeps it, so a trailing comment stays with its statement; blank lines, multi-line statements and comment lines are untouched, and the line-break style (\n or \r\n) follows the source’s.

Conservative by construction:

  • source that does not tokenize, or whose statements are not IDENT '=' <well-formed expr> (';' | end), is returned exactly as given (no evaluation: an unknown name or a missing configurator field is not a syntax error, and still formats);
  • the result is returned only when its token stream equals the source’s, so it evaluates to exactly the same variables and values;
  • it is idempotent: formatted text has no ; with code after it on its line, so formatting it again changes nothing.