Skip to content

Commit b05eefe

Browse files
authored
fix: continue line with comment format (#1705)
Signed-off-by: peefy <xpf6677@163.com>
1 parent dbb5cb0 commit b05eefe

6 files changed

Lines changed: 39 additions & 6 deletions

File tree

kclvm/ast_pretty/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,14 @@ impl<'p> Printer<'p> {
218218
}
219219

220220
/// Print ast comments.
221-
pub fn write_ast_comments<T>(&mut self, node: &ast::NodeRef<T>) {
221+
pub fn update_last_ast_line<T>(&mut self, node: &ast::NodeRef<T>) {
222+
if node.line > self.last_ast_line {
223+
self.last_ast_line = node.line;
224+
}
225+
}
226+
227+
/// Print ast comments.
228+
pub fn write_comments_before_node<T>(&mut self, node: &ast::NodeRef<T>) {
222229
if !self.cfg.write_comments {
223230
return;
224231
}

kclvm/ast_pretty/src/node.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
222222
}
223223
if let Some(index_signature) = &schema_stmt.index_signature {
224224
self.fill("");
225-
self.write_ast_comments(index_signature);
225+
self.write_comments_before_node(index_signature);
226226
self.write_token(TokenKind::OpenDelim(DelimToken::Bracket));
227227
if index_signature.node.any_other {
228228
self.write_token(TokenKind::DotDotDot);
@@ -893,7 +893,7 @@ impl<'p> Printer<'p> {
893893
match &key.node {
894894
ast::Expr::Identifier(identifier) => {
895895
self.hook.pre(self, super::ASTNode::Expr(key));
896-
self.write_ast_comments(key);
896+
self.write_comments_before_node(key);
897897
// Judge contains string or dot identifier, e.g., "x-y-z" and "a.b.c"
898898
let names = &identifier.names;
899899

@@ -940,15 +940,15 @@ impl<'p> Printer<'p> {
940940

941941
pub fn expr(&mut self, expr: &ast::NodeRef<ast::Expr>) {
942942
self.hook.pre(self, super::ASTNode::Expr(expr));
943-
self.write_ast_comments(expr);
943+
self.update_last_ast_line(expr);
944944
self.walk_expr(&expr.node);
945945
self.hook.post(self, super::ASTNode::Expr(expr));
946946
}
947947

948948
pub fn stmt(&mut self, stmt: &ast::NodeRef<ast::Stmt>) {
949949
self.hook.pre(self, super::ASTNode::Stmt(stmt));
950950
self.fill("");
951-
self.write_ast_comments(stmt);
951+
self.write_comments_before_node(stmt);
952952
self.walk_stmt(&stmt.node);
953953
self.hook.post(self, super::ASTNode::Stmt(stmt));
954954
}

kclvm/ast_pretty/src/test_data/codelayout.input

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,8 @@ list_if_item = [
167167
3, 4
168168
*[5, 6]
169169
if False: 2
170-
]
170+
]
171+
172+
longString = "Too long expression " + \
173+
"Too long expression " + \
174+
"Too long expression " # recommended

kclvm/ast_pretty/src/test_data/codelayout.output

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,6 @@ list_if_item = [
165165
if False:
166166
2
167167
]
168+
169+
longString = "Too long expression " + "Too long expression " + "Too long expression "
170+
# recommended

kclvm/ast_pretty/src/test_data/comment.input

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,12 @@ schema Foo:
4343
[k: str]: int
4444
# Comment for `x` field
4545
x: int
46+
47+
config = { # Comment One
48+
# Comment Two
49+
key1 = "value1" # Comment Three
50+
# Comment Four
51+
key2 = \
52+
"value2" # Comment Five
53+
key3 = "value3"
54+
}

kclvm/ast_pretty/src/test_data/comment.output

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,13 @@ schema Foo:
4444
# Comment for `x` field
4545
x: int
4646

47+
# Comment One
48+
config = {
49+
# Comment Two
50+
# Comment Three
51+
key1 = "value1"
52+
# Comment Four
53+
key2 = "value2"
54+
# Comment Five
55+
key3 = "value3"
56+
}

0 commit comments

Comments
 (0)