Skip to content

Commit

Permalink
Fix empty line handling
Browse files Browse the repository at this point in the history
  • Loading branch information
malobre committed Mar 21, 2022
1 parent 0ea06e8 commit c582a14
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 10 additions & 3 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -178,12 +185,12 @@ mod test {
assert_eq!(
format(
Path::new("file.vue"),
"<template>\n <div></div>\n <div></div>\n</template>",
"<template>\n <div></div>\n\n <div></div>\n</template>",
&config,
|_, raw, _| Ok(raw)
)
.unwrap(),
"<template>\n <div></div>\n <div></div>\n</template>"
"<template>\n <div></div>\n\n <div></div>\n</template>"
);
}
}

0 comments on commit c582a14

Please sign in to comment.