perf(pycodestyle): Initialize Stylist from tokens (#3757)

This commit is contained in:
Micha Reiser 2023-03-28 11:53:35 +02:00 committed by GitHub
parent 000394f428
commit f68c26a506
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 200 additions and 172 deletions

View file

@ -63,7 +63,7 @@ pub struct Generator<'a> {
/// The indentation style to use.
indent: &'a Indentation,
/// The quote style to use for string literals.
quote: &'a Quote,
quote: Quote,
/// The line ending to use.
line_ending: &'a LineEnding,
buffer: String,
@ -87,11 +87,7 @@ impl<'a> From<&'a Stylist<'a>> for Generator<'a> {
}
impl<'a> Generator<'a> {
pub const fn new(
indent: &'a Indentation,
quote: &'a Quote,
line_ending: &'a LineEnding,
) -> Self {
pub const fn new(indent: &'a Indentation, quote: Quote, line_ending: &'a LineEnding) -> Self {
Self {
// Style preferences.
indent,
@ -1229,8 +1225,8 @@ impl<'a> Generator<'a> {
let mut generator = Generator::new(
self.indent,
match self.quote {
Quote::Single => &Quote::Double,
Quote::Double => &Quote::Single,
Quote::Single => Quote::Double,
Quote::Double => Quote::Single,
},
self.line_ending,
);
@ -1270,14 +1266,14 @@ mod tests {
let line_ending = LineEnding::default();
let program = parser::parse_program(contents, "<filename>").unwrap();
let stmt = program.first().unwrap();
let mut generator = Generator::new(&indentation, &quote, &line_ending);
let mut generator = Generator::new(&indentation, quote, &line_ending);
generator.unparse_stmt(stmt);
generator.generate()
}
fn round_trip_with(
indentation: &Indentation,
quote: &Quote,
quote: Quote,
line_ending: &LineEnding,
contents: &str,
) -> String {
@ -1452,7 +1448,7 @@ if True:
assert_eq!(
round_trip_with(
&Indentation::default(),
&Quote::Double,
Quote::Double,
&LineEnding::default(),
r#""hello""#
),
@ -1461,7 +1457,7 @@ if True:
assert_eq!(
round_trip_with(
&Indentation::default(),
&Quote::Single,
Quote::Single,
&LineEnding::default(),
r#""hello""#
),
@ -1470,7 +1466,7 @@ if True:
assert_eq!(
round_trip_with(
&Indentation::default(),
&Quote::Double,
Quote::Double,
&LineEnding::default(),
r#"'hello'"#
),
@ -1479,7 +1475,7 @@ if True:
assert_eq!(
round_trip_with(
&Indentation::default(),
&Quote::Single,
Quote::Single,
&LineEnding::default(),
r#"'hello'"#
),
@ -1492,7 +1488,7 @@ if True:
assert_eq!(
round_trip_with(
&Indentation::new(" ".to_string()),
&Quote::default(),
Quote::default(),
&LineEnding::default(),
r#"
if True:
@ -1510,7 +1506,7 @@ if True:
assert_eq!(
round_trip_with(
&Indentation::new(" ".to_string()),
&Quote::default(),
Quote::default(),
&LineEnding::default(),
r#"
if True:
@ -1528,7 +1524,7 @@ if True:
assert_eq!(
round_trip_with(
&Indentation::new("\t".to_string()),
&Quote::default(),
Quote::default(),
&LineEnding::default(),
r#"
if True:
@ -1550,7 +1546,7 @@ if True:
assert_eq!(
round_trip_with(
&Indentation::default(),
&Quote::default(),
Quote::default(),
&LineEnding::Lf,
"if True:\n print(42)",
),
@ -1560,7 +1556,7 @@ if True:
assert_eq!(
round_trip_with(
&Indentation::default(),
&Quote::default(),
Quote::default(),
&LineEnding::CrLf,
"if True:\n print(42)",
),
@ -1570,7 +1566,7 @@ if True:
assert_eq!(
round_trip_with(
&Indentation::default(),
&Quote::default(),
Quote::default(),
&LineEnding::Cr,
"if True:\n print(42)",
),