Skip to content

Commit

Permalink
fix: ast expr comment format
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <xpf6677@163.com>
  • Loading branch information
Peefy committed Oct 25, 2024
1 parent 88dbe98 commit 0a4ef41
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 6 deletions.
34 changes: 28 additions & 6 deletions kclvm/ast_pretty/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
interleave!(
|| self.write_newline(),
|expr: &ast::NodeRef<CallExpr>| {
self.write_comments_before_node(&expr);
self.write("@");
self.walk_call_expr(&expr.node);
},
Expand Down Expand Up @@ -212,7 +213,10 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
self.write(",");
self.write_newline();
},
|mixin_name: &ast::NodeRef<ast::Identifier>| self.walk_identifier(&mixin_name.node),
|mixin_name: &ast::NodeRef<ast::Identifier>| {
self.write_comments_before_node(&mixin_name);
self.walk_identifier(&mixin_name.node);
},
schema_stmt.mixins
);
self.write_indentation(Indentation::Dedent);
Expand Down Expand Up @@ -249,7 +253,10 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
self.write_indentation(Indentation::IndentWithNewline);
interleave!(
|| self.write_newline(),
|check_expr: &ast::NodeRef<ast::CheckExpr>| self.walk_check_expr(&check_expr.node),
|check_expr: &ast::NodeRef<ast::CheckExpr>| {
self.write_comments_before_node(&check_expr);
self.walk_check_expr(&check_expr.node);
},
schema_stmt.checks
);
self.write_newline_without_fill();
Expand Down Expand Up @@ -303,7 +310,10 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
if !rule_stmt.checks.is_empty() {
interleave!(
|| self.write_newline(),
|check_expr: &ast::NodeRef<ast::CheckExpr>| self.walk_check_expr(&check_expr.node),
|check_expr: &ast::NodeRef<ast::CheckExpr>| {
self.write_comments_before_node(&check_expr);
self.walk_check_expr(&check_expr.node);
},
rule_stmt.checks
);
self.write_newline_without_fill();
Expand Down Expand Up @@ -343,6 +353,7 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
interleave!(
|| self.write_newline(),
|expr: &ast::NodeRef<CallExpr>| {
self.write_comments_before_node(&expr);
self.write("@");
self.walk_call_expr(&expr.node)
},
Expand Down Expand Up @@ -482,7 +493,10 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
} else {
self.write_newline();
},
|elt| self.expr(elt),
|elt| {
self.write_comments_before_node(elt);
self.expr(elt);
},
list_expr.elts
);
if !in_one_line {
Expand Down Expand Up @@ -510,7 +524,10 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
self.write_indentation(Indentation::IndentWithNewline);
interleave!(
|| self.write_newline(),
|expr| self.expr(expr),
|expr| {
self.write_comments_before_node(expr);
self.expr(expr);
},
list_if_item_expr.exprs
);
self.write_indentation(Indentation::Dedent);
Expand All @@ -522,7 +539,10 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
self.write_indentation(Indentation::IndentWithNewline);
interleave!(
|| self.write_newline(),
|expr| self.expr(expr),
|expr| {
self.write_comments_before_node(expr);
self.expr(expr);
},
list_expr.elts
);
self.write_indentation(Indentation::Dedent);
Expand Down Expand Up @@ -881,6 +901,7 @@ impl<'p> Printer<'p> {
self.write(&"}".repeat(print_right_brace_count));
}
None => {
self.write_comments_before_node(&item);
if !matches!(&item.node.value.node, ast::Expr::ConfigIfEntry(_)) {
self.write("**");
}
Expand Down Expand Up @@ -916,6 +937,7 @@ impl<'p> Printer<'p> {
count
}
_ => {
self.write_comments_before_node(key);
self.expr(key);
0
}
Expand Down
26 changes: 26 additions & 0 deletions kclvm/ast_pretty/src/test_data/comment.input
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,22 @@ appConfiguration = AppConfiguration {
}
# Comment Nine

@Deprecated # Deprecated
schema Foo:
mixin [
AMixin, # AMixin
# BMixin
BMixin
]
# Comment for index signature
[k: str]: int
# Comment for `x` field
x: int

check:
x > 0 # x > 0
# x < 0
x < 0

config = { # Comment One
# Comment Two
Expand All @@ -51,4 +62,19 @@ config = { # Comment One
key2 = \
"value2" # Comment Five
key3 = "value3"
# Comment Six
"key4" = "value4"
# Comment Seven
key5.v = "value5"
**key6 # Comment Eight
if True: # Comment Nine
key7 = "value7" # Comment Ten
}
data = [ # Comment One
# Comment Two
1 # Comment Three
if True: # Comment Four
2 # Comment Five
# Comment Six
*[3, 4] # Comment Seven
]
37 changes: 37 additions & 0 deletions kclvm/ast_pretty/src/test_data/comment.output
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,26 @@ appConfiguration = AppConfiguration {
overQuota: True
}
# Comment Nine
# Deprecated
@Deprecated()
schema Foo:
mixin [
# AMixin
AMixin,
# BMixin
BMixin
]
# Comment for index signature
[k: str]: int
# Comment for `x` field
x: int

check:
# x > 0
x > 0
# x < 0
x < 0

# Comment One
config = {
# Comment Two
Expand All @@ -53,4 +67,27 @@ config = {
key2 = "value2"
# Comment Five
key3 = "value3"
# Comment Six
"key4" = "value4"
# Comment Seven
key5.v = "value5"
# Comment Eight
**key6
# Comment Nine
if True:
# Comment Ten
key7 = "value7"
}
# Comment One
data = [
# Comment Two
# Comment Three
1
# Comment Four
if True:
# Comment Five
2
# Comment Six
# Comment Seven
*[3, 4]
]

0 comments on commit 0a4ef41

Please sign in to comment.