Skip to content

Commit 2cb6a30

Browse files
committed
fix regression on multiline lambdas with whitespace before them
close #219
1 parent 4606423 commit 2cb6a30

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/formatter.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ impl Formatter {
159159
/// to clean up/balance out the output.
160160
#[inline(always)]
161161
fn postprocess(&mut self) -> &mut Self {
162+
// Re-parse once here to sync self.tree with the content after Topiary
163+
// formatted it.
164+
//
165+
// Topiary replaces self.content entirely but does not update self.tree.
166+
// NOTE: Nathan: maybe I missed a way to do that but for now I'm
167+
// reparsing to sync.
168+
self.tree = self.parser.parse(&self.content, None).unwrap();
169+
162170
self.add_newlines_after_extends_statement()
163171
.fix_dangling_semicolons()
164172
.fix_dangling_commas()
@@ -310,9 +318,10 @@ impl Formatter {
310318
while let Some(node) = stack.pop() {
311319
if node.kind() == "lambda"
312320
&& let Some(body) = node.child_by_field_name("body")
313-
&& body.end_position().row > node.start_position().row {
314-
captures.push((node, body));
315-
}
321+
&& body.end_position().row > node.start_position().row
322+
{
323+
captures.push((node, body));
324+
}
316325

317326
let mut cursor = node.walk();
318327
for child in node.children(&mut cursor) {
@@ -478,6 +487,16 @@ impl Formatter {
478487
/// This function runs postprocess passes that uses tree-sitter.
479488
#[inline(always)]
480489
fn postprocess_tree_sitter(&mut self) -> &mut Self {
490+
// FIXME: Nathan: A full re-parse is required here for now because the
491+
// regex-based steps that run before this (fix_dangling_semicolons(),
492+
// fix_dangling_commas(), etc.) can remove or reposition tokens, making
493+
// the AST stale. handle_two_blank_line() runs tree-sitter queries
494+
// against this tree, so it must reflect the actual content or it will
495+
// compute wrong insertion points.
496+
//
497+
// If we're going to keep working on this, we should get rid of regexes
498+
// and start moving everything to a visitor pattern that modifies the
499+
// AST incrementally for formatting rules.
481500
self.tree = self.parser.parse(&self.content, None).unwrap();
482501

483502
self.handle_two_blank_line()
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func f9():
2+
connect(
3+
func():
4+
print("THIS LINE IS CORRECT")
5+
6+
print("THIS LINE IS INCORRECT")
7+
)
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
3+
func f9():
4+
connect(
5+
func():
6+
print("THIS LINE IS CORRECT")
7+
8+
print("THIS LINE IS INCORRECT")
9+
)

0 commit comments

Comments
 (0)