Skip to content

Commit 8ced80f

Browse files
committed
chore: better way to fix else idempotent issue
1 parent 38eba48 commit 8ced80f

File tree

3 files changed

+32
-42
lines changed

3 files changed

+32
-42
lines changed

src/context.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,6 @@ impl Comment {
114114
self.metadata.has_empty_line_above
115115
}
116116

117-
pub fn has_prev_node_else_keyword(&self) -> bool {
118-
self.metadata.has_prev_node_else_keyword
119-
}
120-
121117
pub fn is_followed_by_bracket_composite_node(&self) -> bool {
122118
self.metadata.is_followed_by_bracket_composite_node
123119
}
@@ -167,7 +163,6 @@ pub struct CommentMetadata {
167163
has_empty_line_above: bool,
168164
has_empty_line_below: bool,
169165
has_prev_node: bool,
170-
has_prev_node_else_keyword: bool,
171166
is_followed_by_bracket_composite_node: bool,
172167
}
173168

@@ -176,9 +171,6 @@ impl CommentMetadata {
176171
let prev = node.prev_named_sibling();
177172
let has_prev_node = prev.is_some();
178173

179-
let prev_unnamed = node.prev_sibling();
180-
let has_prev_node_else_keyword = prev_unnamed.map(|n| n.kind() == "else").unwrap_or(false);
181-
182174
let has_leading_content = if let Some(prev_node) = prev {
183175
node.start_position().row == prev_node.end_position().row
184176
} else {
@@ -221,7 +213,6 @@ impl CommentMetadata {
221213
has_empty_line_above,
222214
has_empty_line_below,
223215
has_prev_node,
224-
has_prev_node_else_keyword,
225216
is_followed_by_bracket_composite_node,
226217
}
227218
}

src/data_model.rs

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1317,6 +1317,7 @@ pub struct IfStatement {
13171317
pub consequence: Statement,
13181318
pub alternative: Option<Statement>,
13191319
pub node_context: NodeContext,
1320+
pub has_else_and_followed_by_new_line_comment: bool, // https://github.com/xixiaofinland/afmt/issues/67
13201321
}
13211322

13221323
impl IfStatement {
@@ -1325,10 +1326,23 @@ impl IfStatement {
13251326

13261327
let alternative = node.try_c_by_n("alternative").map(|a| Statement::new(a));
13271328

1329+
let has_else_and_followed_by_new_line_comment = alternative.is_some()
1330+
&& node
1331+
.children(&mut node.walk())
1332+
.find(|n| n.kind() == "else")
1333+
.and_then(|else_node| {
1334+
else_node.next_sibling().map(|next_sibling| {
1335+
next_sibling.is_extra()
1336+
&& next_sibling.start_position().row != else_node.end_position().row
1337+
})
1338+
})
1339+
.unwrap_or(false);
1340+
13281341
Self {
13291342
condition: ParenthesizedExpression::new(node.c_by_n("condition")),
13301343
consequence: Statement::new(node.c_by_n("consequence")),
13311344
alternative,
1345+
has_else_and_followed_by_new_line_comment,
13321346
node_context: NodeContext::with_punctuation(&node),
13331347
}
13341348
}
@@ -1350,37 +1364,28 @@ impl<'a> DocBuild<'a> for IfStatement {
13501364

13511365
// Handle the 'else' part
13521366
if let Some(ref a) = self.alternative {
1353-
match a {
1354-
Statement::If(_) => {
1355-
if self.consequence.is_block() {
1356-
result.push(b.txt(" else "));
1357-
} else {
1358-
result.push(b.nl());
1359-
result.push(b.txt("else "));
1360-
}
1361-
result.push(a.build(b)); // Recursively build the nested 'else if' statement
1367+
if self.consequence.is_block() {
1368+
result.push(b.txt(" else"));
1369+
if self.has_else_and_followed_by_new_line_comment {
1370+
result.push(b.nl());
1371+
} else {
1372+
result.push(b.txt(" "));
13621373
}
1363-
Statement::Block(_) => {
1364-
if self.consequence.is_block() {
1365-
result.push(b.txt(" else "));
1366-
} else {
1367-
result.push(b.nl());
1368-
result.push(b.txt("else "));
1369-
}
1370-
result.push(a.build(b));
1374+
} else {
1375+
result.push(b.nl());
1376+
result.push(b.txt("else"));
1377+
if self.has_else_and_followed_by_new_line_comment {
1378+
result.push(b.nl());
13711379
}
1372-
// Handle "else" with a single statement
1373-
_ => {
1374-
if self.consequence.is_block() {
1375-
result.push(b.txt(" else "));
1376-
} else {
1377-
result.push(b.nl());
1378-
result.push(b.txt("else"));
1379-
result.push(b.indent(b.nl()));
1380-
}
1381-
result.push(a.build(b)); // Build the else statement
1380+
1381+
if !matches!(a, Statement::If(_) | Statement::Block(_)) {
1382+
result.push(b.indent(b.nl()));
1383+
} else {
1384+
result.push(b.txt(" "));
13821385
}
13831386
}
1387+
1388+
result.push(a.build(b));
13841389
}
13851390
});
13861391
}

src/utility.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,15 +355,9 @@ pub fn handle_pre_comments<'a>(
355355
docs.push(b.txt(" "));
356356
} else {
357357
// if it's in group(), then multi-line mode is selected in fits()
358-
// otherwise, do nothing
359358
docs.push(b.force_break());
360359

361360
// 1st element heading logic is handled in the preceding node;
362-
// except the preceding is an unnamed node like `else`
363-
if comment.has_prev_node_else_keyword() {
364-
docs.push(b.nl());
365-
}
366-
367361
if i != 0 {
368362
if comment.has_newline_above() {
369363
docs.push(b.empty_new_line());

0 commit comments

Comments
 (0)