Skip to content

Commit

Permalink
enhance: enhance fmt tool. (#1850)
Browse files Browse the repository at this point in the history
* enhance: enhance fmt tool. save empty line between comments, fix logic of empty line between stmt

Signed-off-by: he1pa <[email protected]>

* fix ut

Signed-off-by: he1pa <[email protected]>

---------

Signed-off-by: he1pa <[email protected]>
  • Loading branch information
He1pa authored Feb 7, 2025
1 parent d8964b2 commit 763724d
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
8 changes: 8 additions & 0 deletions kclvm/ast_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,14 @@ impl<'p> Printer<'p> {
match self.comments.pop_front() {
Some(comment) => {
self.writeln(&comment.node.text);
match self.comments.front() {
Some(next_comment) => {
if next_comment.line >= comment.line + 2 && count > 0 {
self.write_newline();
}
}
None => {}
}
}
None => break,
}
Expand Down
11 changes: 8 additions & 3 deletions kclvm/ast_pretty/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
if let Some(doc) = &module.doc {
self.write(&doc.node);
self.write_newline();
self.write_newline();
}

self.stmts(&module.body);
Expand Down Expand Up @@ -993,9 +994,13 @@ impl<'p> Printer<'p> {
// Do not format out user-reserved blank lines: which does not mean that to preserve all user-written blank lines.
// For situations where there are more than two blank lines, we only keep one blank line.
let need_newline = if let Some(prev_stmt) = prev_stmt {
stmt.line > 0
&& stmt.line >= prev_stmt.end_line + 2
&& !self.has_comments_on_node(stmt)
if stmt.line > prev_stmt.end_line + 2 {
true
} else if stmt.line == prev_stmt.end_line + 2 {
stmt.line > 0 && !self.has_comments_on_node(stmt)
} else {
false
}
} else {
false
};
Expand Down
2 changes: 2 additions & 0 deletions kclvm/ast_pretty/src/test_data/codelayout.output
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Module documents
"""

import math as alias_math

schema Person(Base):
Expand Down Expand Up @@ -111,6 +112,7 @@ joined_data_3 = '''\
joined_data_4 = '''\
\${CC}
'''

# Member access and index assign targets
a[0].b -= 1
a.b[0] += 1
Expand Down
8 changes: 7 additions & 1 deletion kclvm/ast_pretty/src/test_data/comment.input
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,10 @@ data = [ # Comment One
2 # Comment Five
# Comment Six
*[3, 4] # Comment Seven
]
]

# This is a comment
foo = "bar"

# This is another comment
fizz = "bazz"
9 changes: 9 additions & 0 deletions kclvm/ast_pretty/src/test_data/comment.output
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ appConfiguration = AppConfiguration {
name: "kusion_override"
}
# Comment Seven

# Comment Eight
overQuota: True
}

# Comment Nine

# Deprecated
@Deprecated()
schema Foo:
Expand Down Expand Up @@ -91,3 +94,9 @@ data = [
# Comment Seven
*[3, 4]
]

# This is a comment
foo = "bar"

# This is another comment
fizz = "bazz"
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Inline comment 2
# Inline comment 3
a = 1

# Inline comment 4
# Inline comment 5
#
Expand All @@ -11,6 +12,7 @@ a = 1
#
# Inline comment 8
b = 2

# Same inline comment
# Same inline comment
# Same inline comment
Expand Down

0 comments on commit 763724d

Please sign in to comment.