Skip to content

Commit 68f1aa9

Browse files
authored
Fix empty lines placed within a sorted table (#63)
1 parent 38d0e0a commit 68f1aa9

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

common/src/table.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,13 @@ pub fn reorder_table_keys(table: &mut RefMut<Vec<SyntaxElement>>, order: &[&str]
187187
}
188188

189189
fn load_keys(table: &[SyntaxElement]) -> (HashMap<String, usize>, Vec<Vec<SyntaxElement>>) {
190+
let table_clone = if table.last().unwrap().kind() == NEWLINE {
191+
// drop the final element if it is a new line, multiple new lines are handled together and add unwanted
192+
// empty lines within the table when reordered
193+
&table[..table.len() - 1]
194+
} else {
195+
table
196+
};
190197
let mut key_to_pos = HashMap::<String, usize>::new();
191198
let mut key_set = Vec::<Vec<SyntaxElement>>::new();
192199
let entry_set = RefCell::new(Vec::<SyntaxElement>::new());
@@ -200,7 +207,7 @@ fn load_keys(table: &[SyntaxElement]) -> (HashMap<String, usize>, Vec<Vec<Syntax
200207
};
201208
let mut key = String::new();
202209
let mut cutoff = false;
203-
for element in table {
210+
for element in table_clone {
204211
let kind = element.kind();
205212
if kind == ENTRY {
206213
if cutoff {

pyproject-fmt/rust/src/tests/dependency_groups_tests.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,28 @@ fn evaluate(start: &str, keep_full_version: bool) -> String {
9696
"#},
9797
false,
9898
)]
99+
#[case::multiple_groups_and_extra_line(
100+
indoc ! {r#"
101+
[dependency-groups]
102+
example = [ "c<1" ]
103+
coverage = ["b<2"]
104+
type = [ "a>1" ]
105+
106+
"#},
107+
indoc ! {r#"
108+
[dependency-groups]
109+
type = [
110+
"a>1",
111+
]
112+
example = [
113+
"c<1",
114+
]
115+
coverage = [
116+
"b<2",
117+
]
118+
"#},
119+
false,
120+
)]
99121
#[case::include_single_group(
100122
indoc ! {r#"
101123
[dependency-groups]

0 commit comments

Comments
 (0)