From 0cab3f84373eb823920f2451c1b789c0509d257a Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 26 Jan 2023 16:09:35 -0500 Subject: [PATCH] Preserve indentation when fixing via LibCST (#2223) --- src/rules/flake8_simplify/rules/fix_if.rs | 1 + src/rules/flake8_simplify/rules/fix_with.rs | 1 + src/source_code/stylist.rs | 8 +++++++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/rules/flake8_simplify/rules/fix_if.rs b/src/rules/flake8_simplify/rules/fix_if.rs index 48c20bd83f..d17da52c08 100644 --- a/src/rules/flake8_simplify/rules/fix_if.rs +++ b/src/rules/flake8_simplify/rules/fix_if.rs @@ -114,6 +114,7 @@ pub(crate) fn fix_nested_if_statements( let mut state = CodegenState { default_newline: stylist.line_ending(), + default_indent: stylist.indentation(), ..Default::default() }; tree.codegen(&mut state); diff --git a/src/rules/flake8_simplify/rules/fix_with.rs b/src/rules/flake8_simplify/rules/fix_with.rs index a947cacf27..5dc431d94f 100644 --- a/src/rules/flake8_simplify/rules/fix_with.rs +++ b/src/rules/flake8_simplify/rules/fix_with.rs @@ -78,6 +78,7 @@ pub(crate) fn fix_multiple_with_statements( let mut state = CodegenState { default_newline: stylist.line_ending(), + default_indent: stylist.indentation(), ..Default::default() }; tree.codegen(&mut state); diff --git a/src/source_code/stylist.rs b/src/source_code/stylist.rs index 8aec4fc115..cd30473d19 100644 --- a/src/source_code/stylist.rs +++ b/src/source_code/stylist.rs @@ -108,11 +108,17 @@ impl Default for Indentation { } } +impl Indentation { + pub fn as_str(&self) -> &str { + self.0.as_str() + } +} + impl Deref for Indentation { type Target = str; fn deref(&self) -> &Self::Target { - &self.0 + self.as_str() } }