On the Python projects that I work on, our convention is one name per line with a trailing comma in imports, just as the example linked to, for any imports of more than one name from a module. It makes it much easier to view diffs, as adding or removing an import only affects that one line, and you donât run into issues with having to reformat the whole thing when adding something into the middle of it, or having to switch between one line and multi-line formats when it gets too long.
What I would generally prefer from a tool, however, is to leave line breaking decisions as-is unless required to wrap to the desired length. If Iâm working on a module that has:
use syntax::parse::lexer::{StringReader, TokenAndSpan, Reader};
and I want to run a formatter on it to fix up some other style problems later on, I donât want it to introduce the change to this format:
use syntax::parse::lexer::{
StringReader,
TokenAndSpan,
Reader,
};
As far as formatting goes, there are basically some rules that I consider mandatory (line length, spaces being necessary in certain places and forbidden in others, etc), and some that are more like suggestions and a matter of judgement, such as precisely whether to split a line that is not over the limit, or where to split it. For example, if you have more than a couple of positional arguments to a function, it can be nice to just split it so each is on a separate line, even if it wouldnât go over the length limit; I wouldnât want the formatter to put it back on one line for me. Likewise, in this case, while I prefer the one import per line style, I donât want the formatter introducing that change (unless the line exceeds the maximum length).
Overall, rustformat is looking like a pretty good start. Noticed one problem when I tried it; it added trailing whitespace after commas when I ran it on itself:
Modified src/token_handling.rs
diff --git a/src/token_handling.rs b/src/token_handling.rs
index 97bfa70..409c32f 100644
--- a/src/token_handling.rs
+++ b/src/token_handling.rs
@@ -179,12 +179,12 @@ impl Word {
token::Float(c) => c.as_str().to_string(),
token::Integer(c) => c.as_str().to_string(),
token::Str_(s) => format!("\"{}\"", s.as_str()),
- token::StrRaw(s, n) => format!("r{delim}\"{string}\"{delim}",
- delim = repeat("#", n),
+ token::StrRaw(s, n) => format!("r{delim}\"{string}\"{delim}",
+ delim = repeat("#", n),
string = s.as_str()),
token::Binary(v) => format!("b\"{}\"", v.as_str()),
- token::BinaryRaw(s, n) => format!("br{delim}\"{string}\"{delim}",
- delim = repeat("#", n),
+ token::BinaryRaw(s, n) => format!("br{delim}\"{string}\"{delim}",
+ delim = repeat("#", n),
string = s.as_str()),
};
if let Some(s) = suf {