From c582a14d9583c52c41979e453a53a4f0cdb7f347 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Obr=C3=A9jan?= Date: Mon, 21 Mar 2022 14:49:42 +0100 Subject: [PATCH] Fix empty line handling --- CHANGELOG.md | 9 ++++++++- src/format.rs | 13 ++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 762ce92..611e19d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,15 +7,22 @@ The format is based on [Keep a Changelog], and this project adheres to ## [Unreleased] +### Fixed + +- Empty lines are now correctly handled when computing template indentation. + ## [0.4.1] - 2022-02-14 ## [0.4.0] - 2022-02-10 ### Changed +- Block indentation width is now computed and adjusted according to the configuration. + +### Fixed + - `indentTemplate` now works correctly when the content couldn't be formatted through dprint. -- Block indentation width is now computed and adjusted according to the configuration. ## [0.3.2] - 2022-02-03 diff --git a/src/format.rs b/src/format.rs index b49626d..2b4cc05 100644 --- a/src/format.rs +++ b/src/format.rs @@ -72,7 +72,14 @@ fn format_block<'a>( let indent_width = pretty .lines() - .map(|line| line.len() - line.trim_start().len()) + .filter_map(|line| { + let trimed_line = line.trim_start(); + if trimed_line.is_empty() { + None + } else { + Some(line.len() - trimed_line.len()) + } + }) .min() .unwrap_or(0); @@ -178,12 +185,12 @@ mod test { assert_eq!( format( Path::new("file.vue"), - "", + "", &config, |_, raw, _| Ok(raw) ) .unwrap(), - "" + "" ); } }