Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance: enhance fmt tool. #1850

Merged
merged 2 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading