spacify 0.3.0__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.4.0](https://github.com/avanserv/spacify/compare/v0.3.0...v0.4.0) (2026-11-16)
4
+
5
+
6
+ ### Features
7
+
8
+ * Keep paragraphs between simple statements ([#19](https://github.com/avanserv/spacify/issues/19)) ([90ad405](https://github.com/avanserv/spacify/commit/90ad40534c6024e97624acb3c9b500f044d0aa66))
9
+
3
10
  ## [0.3.0](https://github.com/avanserv/spacify/compare/v0.2.1...v0.3.0) (2026-10-27)
4
11
 
5
12
 
@@ -229,9 +229,9 @@ dependencies = [
229
229
 
230
230
  [[package]]
231
231
  name = "globset"
232
- version = "0.4.20"
232
+ version = "0.4.21"
233
233
  source = "registry+https://github.com/rust-lang/crates.io-index"
234
- checksum = "07c34a9410465b45bd9787443bc7370f37735bad04b0f0cd57ff1a3186c98988"
234
+ checksum = "f98b0d85522dc0a824cfebbc9594ff4a9b289c568bea0370319dfbdf20ccdd2d"
235
235
  dependencies = [
236
236
  "aho-corasick",
237
237
  "bstr",
@@ -254,9 +254,9 @@ checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
254
254
 
255
255
  [[package]]
256
256
  name = "ignore"
257
- version = "0.4.33"
257
+ version = "0.4.34"
258
258
  source = "registry+https://github.com/rust-lang/crates.io-index"
259
- checksum = "00b69833ed729dc5aa7d19541d96d6cf8e9137194207a04916d658e43168402f"
259
+ checksum = "484be5657551d7868d67587df967fb4fcbe9d266a761cc855aaa148fda0b3cc7"
260
260
  dependencies = [
261
261
  "crossbeam-deque",
262
262
  "globset",
@@ -493,16 +493,16 @@ checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba"
493
493
 
494
494
  [[package]]
495
495
  name = "similar"
496
- version = "3.2.0"
496
+ version = "3.2.1"
497
497
  source = "registry+https://github.com/rust-lang/crates.io-index"
498
- checksum = "4f66ca1f7aca2474dc10c942eb22feffc897735f54cd1db90138c2fddb490987"
498
+ checksum = "d2f8bec09ae8fae0f4f265a4559a9334be3929d8504e9b536df0a41302bd1733"
499
499
  dependencies = [
500
500
  "bstr",
501
501
  ]
502
502
 
503
503
  [[package]]
504
504
  name = "spacify"
505
- version = "0.3.0"
505
+ version = "0.4.0"
506
506
  dependencies = [
507
507
  "anyhow",
508
508
  "clap",
@@ -558,9 +558,9 @@ dependencies = [
558
558
 
559
559
  [[package]]
560
560
  name = "toml"
561
- version = "1.1.8+spec-1.1.0"
561
+ version = "1.2.0+spec-1.1.0"
562
562
  source = "registry+https://github.com/rust-lang/crates.io-index"
563
- checksum = "20489e00e4d8741d6be680764cc12e270655e375a20d1011e844a9c3379e678d"
563
+ checksum = "c5ffcdf8f50436dbbfd675c1fe1b8e0854f42b7defea8584bdebe1aee900e561"
564
564
  dependencies = [
565
565
  "indexmap",
566
566
  "serde_core",
@@ -591,9 +591,9 @@ dependencies = [
591
591
 
592
592
  [[package]]
593
593
  name = "toml_writer"
594
- version = "1.1.3+spec-1.1.0"
594
+ version = "1.2.0+spec-1.1.0"
595
595
  source = "registry+https://github.com/rust-lang/crates.io-index"
596
- checksum = "06bdbd8cfc056b8d2e2e85f29b56a3bdbecb527cef81eb39e3e7b98af4652770"
596
+ checksum = "009dbb7700f11c55ef83aebbe4c0e9cf1f1ac0844bd8d6f00dc38f302026e13a"
597
597
 
598
598
  [[package]]
599
599
  name = "tree-sitter"
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "spacify"
3
- version = "0.3.0"
3
+ version = "0.4.0"
4
4
  edition = "2024"
5
5
  rust-version = "1.85"
6
6
  description = "A fast formatter for the vertical rhythm of Python and Rust code, complementary to ruff and rustfmt"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: spacify
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Classifier: Development Status :: 3 - Alpha
5
5
  Classifier: Environment :: Console
6
6
  Classifier: Intended Audience :: Developers
@@ -36,7 +36,6 @@ def load(paths):
36
36
  continue
37
37
  seen.add(path)
38
38
  total = len(seen)
39
-
40
39
  count = 0
41
40
  log(total)
42
41
  return total
@@ -136,18 +135,19 @@ Each rule can be turned on or off. They all apply to sibling statements in the s
136
135
  | `blank-before-block` | At least one blank line before a block statement when another statement comes before it. |
137
136
  | `blank-after-block` | At least one blank line after a block statement, before the next statement. |
138
137
  | `blank-before-exit` | At least one blank line before an exit, in blocks of `exit-min-block-statements` (default 3) or more statements. |
139
- | `collapse-groups` | No blank line between two single-line statements of the same kind. |
138
+ | `keep-paragraphs` | Exactly one blank line between two simple statements the author separated with any. |
139
+ | `collapse-groups` | No blank line between two single-line statements of the same kind. Acts only with `keep-paragraphs` off. |
140
140
 
141
- The added blank lines take priority over `collapse-groups`. Blank lines that have a comment in the
142
- gap are kept, because a comment marks a paragraph you meant. When a rule does add a blank line, it
143
- goes above the comments and attributes that belong to the next statement. What each term means
141
+ The added blank lines take priority over the other two rules, and `keep-paragraphs` over
142
+ `collapse-groups`. Even with `keep-paragraphs` off, blank lines that have a comment in the gap are
143
+ kept, because a comment marks a paragraph you meant. When a rule does add a blank line, it goes above the comments and attributes that belong to the next statement. What each term means
144
144
  depends on the language:
145
145
 
146
146
  | | Python | Rust |
147
147
  | --- | --- | --- |
148
148
  | Block statements | `if`, `for`, `while`, `try`, `with`, `match`, and their `async` forms | `if`, `match`, `for`, `while`, `loop`, and bare, `unsafe`, `async` and `const` blocks |
149
149
  | Exits | `return`, `raise`, `break`, `continue`, a bare `yield` | `return`, `break`, `continue`, and a function body's tail expression |
150
- | Kinds that collapse | assignments, augmented assignments, calls, `del`, `assert`, `global`/`nonlocal`, module constants | `let` and assignments, compound assignments, calls (macros, `.await` and `?` included) |
150
+ | Simple statements, by kind | assignments, augmented assignments, calls, `del`, `assert`, `global`/`nonlocal`, module constants | `let` and assignments, compound assignments, calls (macros, `.await` and `?` included) |
151
151
  | Never touched | gaps next to a `def` or `class`, after an `import`, after a docstring | gaps next to an item (`fn`, `struct`, `use`, `const`, ...) |
152
152
  | Switched off by | `# fmt: off`/`# fmt: on`, `# spacify: off`/`# spacify: on`, `# fmt: skip` | `// spacify: off`/`// spacify: on`, `#[rustfmt::skip]` |
153
153
  | Most blank lines in a row | 2 at module level (1 in a `.pyi` stub), 1 in a nested block | 1 |
@@ -175,6 +175,7 @@ blank-before-block = true
175
175
  blank-after-block = true
176
176
  blank-before-exit = true
177
177
  exit-min-block-statements = 3
178
+ keep-paragraphs = true
178
179
  collapse-groups = true
179
180
 
180
181
  [tool.spacify.rust.rules] # overrides the shared rules for Rust only
@@ -18,7 +18,6 @@ def load(paths):
18
18
  continue
19
19
  seen.add(path)
20
20
  total = len(seen)
21
-
22
21
  count = 0
23
22
  log(total)
24
23
  return total
@@ -118,18 +117,19 @@ Each rule can be turned on or off. They all apply to sibling statements in the s
118
117
  | `blank-before-block` | At least one blank line before a block statement when another statement comes before it. |
119
118
  | `blank-after-block` | At least one blank line after a block statement, before the next statement. |
120
119
  | `blank-before-exit` | At least one blank line before an exit, in blocks of `exit-min-block-statements` (default 3) or more statements. |
121
- | `collapse-groups` | No blank line between two single-line statements of the same kind. |
120
+ | `keep-paragraphs` | Exactly one blank line between two simple statements the author separated with any. |
121
+ | `collapse-groups` | No blank line between two single-line statements of the same kind. Acts only with `keep-paragraphs` off. |
122
122
 
123
- The added blank lines take priority over `collapse-groups`. Blank lines that have a comment in the
124
- gap are kept, because a comment marks a paragraph you meant. When a rule does add a blank line, it
125
- goes above the comments and attributes that belong to the next statement. What each term means
123
+ The added blank lines take priority over the other two rules, and `keep-paragraphs` over
124
+ `collapse-groups`. Even with `keep-paragraphs` off, blank lines that have a comment in the gap are
125
+ kept, because a comment marks a paragraph you meant. When a rule does add a blank line, it goes above the comments and attributes that belong to the next statement. What each term means
126
126
  depends on the language:
127
127
 
128
128
  | | Python | Rust |
129
129
  | --- | --- | --- |
130
130
  | Block statements | `if`, `for`, `while`, `try`, `with`, `match`, and their `async` forms | `if`, `match`, `for`, `while`, `loop`, and bare, `unsafe`, `async` and `const` blocks |
131
131
  | Exits | `return`, `raise`, `break`, `continue`, a bare `yield` | `return`, `break`, `continue`, and a function body's tail expression |
132
- | Kinds that collapse | assignments, augmented assignments, calls, `del`, `assert`, `global`/`nonlocal`, module constants | `let` and assignments, compound assignments, calls (macros, `.await` and `?` included) |
132
+ | Simple statements, by kind | assignments, augmented assignments, calls, `del`, `assert`, `global`/`nonlocal`, module constants | `let` and assignments, compound assignments, calls (macros, `.await` and `?` included) |
133
133
  | Never touched | gaps next to a `def` or `class`, after an `import`, after a docstring | gaps next to an item (`fn`, `struct`, `use`, `const`, ...) |
134
134
  | Switched off by | `# fmt: off`/`# fmt: on`, `# spacify: off`/`# spacify: on`, `# fmt: skip` | `// spacify: off`/`// spacify: on`, `#[rustfmt::skip]` |
135
135
  | Most blank lines in a row | 2 at module level (1 in a `.pyi` stub), 1 in a nested block | 1 |
@@ -157,6 +157,7 @@ blank-before-block = true
157
157
  blank-after-block = true
158
158
  blank-before-exit = true
159
159
  exit-min-block-statements = 3
160
+ keep-paragraphs = true
160
161
  collapse-groups = true
161
162
 
162
163
  [tool.spacify.rust.rules] # overrides the shared rules for Rust only
@@ -43,6 +43,8 @@ pub struct Rules {
43
43
  pub blank_before_exit: bool,
44
44
  /// A block shorter than this keeps its exit statement tight against the line before it.
45
45
  pub exit_min_block_statements: usize,
46
+ /// Wins over `collapse_groups`, which only acts when this is off.
47
+ pub keep_paragraphs: bool,
46
48
  pub collapse_groups: bool,
47
49
  }
48
50
 
@@ -53,6 +55,7 @@ impl Default for Rules {
53
55
  blank_after_block: true,
54
56
  blank_before_exit: true,
55
57
  exit_min_block_statements: 3,
58
+ keep_paragraphs: true,
56
59
  collapse_groups: true,
57
60
  }
58
61
  }
@@ -66,6 +69,7 @@ pub struct RuleOverrides {
66
69
  pub blank_after_block: Option<bool>,
67
70
  pub blank_before_exit: Option<bool>,
68
71
  pub exit_min_block_statements: Option<usize>,
72
+ pub keep_paragraphs: Option<bool>,
69
73
  pub collapse_groups: Option<bool>,
70
74
  }
71
75
 
@@ -182,6 +186,7 @@ impl Config {
182
186
  exit_min_block_statements: overrides
183
187
  .exit_min_block_statements
184
188
  .unwrap_or(shared.exit_min_block_statements),
189
+ keep_paragraphs: overrides.keep_paragraphs.unwrap_or(shared.keep_paragraphs),
185
190
  collapse_groups: overrides.collapse_groups.unwrap_or(shared.collapse_groups),
186
191
  }
187
192
  }
@@ -213,13 +218,15 @@ mod tests {
213
218
  fn a_language_override_wins_over_the_shared_rule() {
214
219
  let config: Config = toml::from_str(
215
220
  "[rules]\nexit-min-block-statements = 4\ncollapse-groups = false\n\n\
216
- [python.rules]\nexit-min-block-statements = 2\n",
221
+ [python.rules]\nexit-min-block-statements = 2\nkeep-paragraphs = false\n",
217
222
  )
218
223
  .unwrap();
219
224
  let rules = config.rules_for(Language::Python);
220
225
  assert_eq!(rules.exit_min_block_statements, 2);
221
226
  assert!(!rules.collapse_groups);
227
+ assert!(!rules.keep_paragraphs);
222
228
  assert!(rules.blank_before_block);
229
+ assert!(config.rules_for(Language::Rust).keep_paragraphs);
223
230
  }
224
231
 
225
232
  #[test]
@@ -44,12 +44,17 @@ pub struct Gap {
44
44
  }
45
45
 
46
46
  /// The blank-line count `gap` should have, or `None` when no rule has an opinion on it. Rules that
47
- /// add a line win over the one that removes lines, and nothing ever exceeds ruff's ceiling.
47
+ /// add a line win over the others, and `keep_paragraphs` covers every gap `collapse_groups` would
48
+ /// empty, so the latter only acts when the former is off. Nothing ever exceeds ruff's ceiling.
48
49
  pub fn desired(gap: &Gap, rules: &Rules) -> Option<usize> {
49
50
  if blank_before_block(gap, rules) || blank_after_block(gap, rules) || blank_before_exit(gap, rules) {
50
51
  return Some(gap.blank.clamp(1, gap.max));
51
52
  }
52
53
 
54
+ if keep_paragraphs(gap, rules) {
55
+ return Some(1);
56
+ }
57
+
53
58
  if collapse_groups(gap, rules) {
54
59
  return Some(0);
55
60
  }
@@ -69,6 +74,11 @@ fn blank_before_exit(gap: &Gap, rules: &Rules) -> bool {
69
74
  rules.blank_before_exit && gap.next == Kind::Exit && gap.block_len >= rules.exit_min_block_statements
70
75
  }
71
76
 
77
+ /// A blank line the author put between two simple statements marks a paragraph: it stays, as one.
78
+ fn keep_paragraphs(gap: &Gap, rules: &Rules) -> bool {
79
+ rules.keep_paragraphs && gap.blank > 0 && matches!((gap.prev, gap.next), (Kind::Simple(_), Kind::Simple(_)))
80
+ }
81
+
72
82
  /// A comment in the gap marks a paragraph the author meant, and a multi-line statement reads
73
83
  /// better with air around it, so both keep their blank line.
74
84
  fn collapse_groups(gap: &Gap, rules: &Rules) -> bool {
@@ -101,6 +111,36 @@ mod tests {
101
111
  desired(gap, &Rules::default())
102
112
  }
103
113
 
114
+ fn desired_collapsing(gap: &Gap) -> Option<usize> {
115
+ let rules = Rules {
116
+ keep_paragraphs: false,
117
+ ..Rules::default()
118
+ };
119
+ desired(gap, &rules)
120
+ }
121
+
122
+ #[test]
123
+ fn paragraphs_between_simple_statements_keep_one_line() {
124
+ let paragraph = |blank, max| desired_default(&Gap { blank, max, ..gap() });
125
+ assert_eq!(paragraph(0, 2), None);
126
+ assert_eq!(paragraph(1, 2), Some(1));
127
+ assert_eq!(paragraph(2, 2), Some(1));
128
+ assert_eq!(paragraph(3, 1), Some(1));
129
+
130
+ let mixed = Gap {
131
+ next: Kind::Simple(Group::Call),
132
+ has_comment: true,
133
+ both_single_line: false,
134
+ ..gap()
135
+ };
136
+ assert_eq!(desired_default(&mixed), Some(1));
137
+ let exit = Gap {
138
+ next: Kind::Other,
139
+ ..gap()
140
+ };
141
+ assert_eq!(desired_default(&exit), None);
142
+ }
143
+
104
144
  #[test]
105
145
  fn added_lines_stay_between_one_and_the_ceiling() {
106
146
  let before_block = |blank, max| {
@@ -145,24 +185,24 @@ mod tests {
145
185
 
146
186
  #[test]
147
187
  fn collapse_needs_every_condition() {
148
- assert_eq!(desired_default(&gap()), Some(0));
149
- assert_eq!(desired_default(&Gap { blank: 0, ..gap() }), None);
188
+ assert_eq!(desired_collapsing(&gap()), Some(0));
189
+ assert_eq!(desired_collapsing(&Gap { blank: 0, ..gap() }), None);
150
190
  assert_eq!(
151
- desired_default(&Gap {
191
+ desired_collapsing(&Gap {
152
192
  has_comment: true,
153
193
  ..gap()
154
194
  }),
155
195
  None
156
196
  );
157
197
  assert_eq!(
158
- desired_default(&Gap {
198
+ desired_collapsing(&Gap {
159
199
  both_single_line: false,
160
200
  ..gap()
161
201
  }),
162
202
  None
163
203
  );
164
204
  assert_eq!(
165
- desired_default(&Gap {
205
+ desired_collapsing(&Gap {
166
206
  next: Kind::Simple(Group::Call),
167
207
  ..gap()
168
208
  }),
@@ -176,13 +216,13 @@ mod tests {
176
216
  prev: Kind::Simple(Group::Constant),
177
217
  ..gap()
178
218
  };
179
- assert_eq!(desired_default(&constant_then_assign), None);
219
+ assert_eq!(desired_collapsing(&constant_then_assign), None);
180
220
  let constants = Gap {
181
221
  prev: Kind::Simple(Group::Constant),
182
222
  next: Kind::Simple(Group::Constant),
183
223
  ..gap()
184
224
  };
185
- assert_eq!(desired_default(&constants), Some(0));
225
+ assert_eq!(desired_collapsing(&constants), Some(0));
186
226
  }
187
227
 
188
228
  #[test]
@@ -217,8 +257,8 @@ mod tests {
217
257
  blank: 0,
218
258
  ..gap()
219
259
  };
220
- let collapse = gap();
221
- let all = [before_block, after_block, before_exit, collapse];
260
+ let paragraph = gap();
261
+ let all = [before_block, after_block, before_exit, paragraph];
222
262
  let toggles: [fn(&mut Rules); 4] = [
223
263
  |rules| rules.blank_before_block = false,
224
264
  |rules| rules.blank_after_block = false,
@@ -226,8 +266,12 @@ mod tests {
226
266
  |rules| rules.collapse_groups = false,
227
267
  ];
228
268
 
269
+ // With paragraphs kept, `collapse_groups` never acts, so the matrix runs without it.
229
270
  for (off, toggle) in toggles.iter().enumerate() {
230
- let mut rules = Rules::default();
271
+ let mut rules = Rules {
272
+ keep_paragraphs: false,
273
+ ..Rules::default()
274
+ };
231
275
  toggle(&mut rules);
232
276
  for (index, gap) in all.iter().enumerate() {
233
277
  assert_eq!(
@@ -86,6 +86,7 @@ fn each_rule_can_be_switched_off() {
86
86
  blank_before_block: false,
87
87
  blank_after_block: false,
88
88
  blank_before_exit: false,
89
+ keep_paragraphs: false,
89
90
  collapse_groups: false,
90
91
  ..Rules::default()
91
92
  };
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes