Skip to content

Commit

Permalink
fix: continue line with comment format (#1705)
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy authored Oct 21, 2024
1 parent dbb5cb0 commit b05eefe
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
9 changes: 8 additions & 1 deletion kclvm/ast_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,14 @@ impl<'p> Printer<'p> {
}

/// Print ast comments.
pub fn write_ast_comments<T>(&mut self, node: &ast::NodeRef<T>) {
pub fn update_last_ast_line<T>(&mut self, node: &ast::NodeRef<T>) {
if node.line > self.last_ast_line {
self.last_ast_line = node.line;
}
}

/// Print ast comments.
pub fn write_comments_before_node<T>(&mut self, node: &ast::NodeRef<T>) {
if !self.cfg.write_comments {
return;
}
Expand Down
8 changes: 4 additions & 4 deletions kclvm/ast_pretty/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
}
if let Some(index_signature) = &schema_stmt.index_signature {
self.fill("");
self.write_ast_comments(index_signature);
self.write_comments_before_node(index_signature);
self.write_token(TokenKind::OpenDelim(DelimToken::Bracket));
if index_signature.node.any_other {
self.write_token(TokenKind::DotDotDot);
Expand Down Expand Up @@ -893,7 +893,7 @@ impl<'p> Printer<'p> {
match &key.node {
ast::Expr::Identifier(identifier) => {
self.hook.pre(self, super::ASTNode::Expr(key));
self.write_ast_comments(key);
self.write_comments_before_node(key);
// Judge contains string or dot identifier, e.g., "x-y-z" and "a.b.c"
let names = &identifier.names;

Expand Down Expand Up @@ -940,15 +940,15 @@ impl<'p> Printer<'p> {

pub fn expr(&mut self, expr: &ast::NodeRef<ast::Expr>) {
self.hook.pre(self, super::ASTNode::Expr(expr));
self.write_ast_comments(expr);
self.update_last_ast_line(expr);
self.walk_expr(&expr.node);
self.hook.post(self, super::ASTNode::Expr(expr));
}

pub fn stmt(&mut self, stmt: &ast::NodeRef<ast::Stmt>) {
self.hook.pre(self, super::ASTNode::Stmt(stmt));
self.fill("");
self.write_ast_comments(stmt);
self.write_comments_before_node(stmt);
self.walk_stmt(&stmt.node);
self.hook.post(self, super::ASTNode::Stmt(stmt));
}
Expand Down
6 changes: 5 additions & 1 deletion kclvm/ast_pretty/src/test_data/codelayout.input
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,8 @@ list_if_item = [
3, 4
*[5, 6]
if False: 2
]
]

longString = "Too long expression " + \
"Too long expression " + \
"Too long expression " # recommended
3 changes: 3 additions & 0 deletions kclvm/ast_pretty/src/test_data/codelayout.output
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,6 @@ list_if_item = [
if False:
2
]

longString = "Too long expression " + "Too long expression " + "Too long expression "
# recommended
9 changes: 9 additions & 0 deletions kclvm/ast_pretty/src/test_data/comment.input
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ schema Foo:
[k: str]: int
# Comment for `x` field
x: int

config = { # Comment One
# Comment Two
key1 = "value1" # Comment Three
# Comment Four
key2 = \
"value2" # Comment Five
key3 = "value3"
}
10 changes: 10 additions & 0 deletions kclvm/ast_pretty/src/test_data/comment.output
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ schema Foo:
# Comment for `x` field
x: int

# Comment One
config = {
# Comment Two
# Comment Three
key1 = "value1"
# Comment Four
key2 = "value2"
# Comment Five
key3 = "value3"
}

0 comments on commit b05eefe

Please sign in to comment.