From b05eefecee2badbdea95964d8a2478f96eae10c3 Mon Sep 17 00:00:00 2001 From: Peefy Date: Mon, 21 Oct 2024 15:08:49 +0800 Subject: [PATCH] fix: continue line with comment format (#1705) Signed-off-by: peefy --- kclvm/ast_pretty/src/lib.rs | 9 ++++++++- kclvm/ast_pretty/src/node.rs | 8 ++++---- kclvm/ast_pretty/src/test_data/codelayout.input | 6 +++++- kclvm/ast_pretty/src/test_data/codelayout.output | 3 +++ kclvm/ast_pretty/src/test_data/comment.input | 9 +++++++++ kclvm/ast_pretty/src/test_data/comment.output | 10 ++++++++++ 6 files changed, 39 insertions(+), 6 deletions(-) diff --git a/kclvm/ast_pretty/src/lib.rs b/kclvm/ast_pretty/src/lib.rs index c94e172d4..b4074971e 100644 --- a/kclvm/ast_pretty/src/lib.rs +++ b/kclvm/ast_pretty/src/lib.rs @@ -218,7 +218,14 @@ impl<'p> Printer<'p> { } /// Print ast comments. - pub fn write_ast_comments(&mut self, node: &ast::NodeRef) { + pub fn update_last_ast_line(&mut self, node: &ast::NodeRef) { + if node.line > self.last_ast_line { + self.last_ast_line = node.line; + } + } + + /// Print ast comments. + pub fn write_comments_before_node(&mut self, node: &ast::NodeRef) { if !self.cfg.write_comments { return; } diff --git a/kclvm/ast_pretty/src/node.rs b/kclvm/ast_pretty/src/node.rs index 30b8efbc9..7e061f0e0 100644 --- a/kclvm/ast_pretty/src/node.rs +++ b/kclvm/ast_pretty/src/node.rs @@ -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); @@ -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; @@ -940,7 +940,7 @@ impl<'p> Printer<'p> { pub fn expr(&mut self, expr: &ast::NodeRef) { 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)); } @@ -948,7 +948,7 @@ impl<'p> Printer<'p> { pub fn stmt(&mut self, stmt: &ast::NodeRef) { 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)); } diff --git a/kclvm/ast_pretty/src/test_data/codelayout.input b/kclvm/ast_pretty/src/test_data/codelayout.input index 674296717..54b6713b5 100644 --- a/kclvm/ast_pretty/src/test_data/codelayout.input +++ b/kclvm/ast_pretty/src/test_data/codelayout.input @@ -167,4 +167,8 @@ list_if_item = [ 3, 4 *[5, 6] if False: 2 -] \ No newline at end of file +] + +longString = "Too long expression " + \ + "Too long expression " + \ + "Too long expression " # recommended diff --git a/kclvm/ast_pretty/src/test_data/codelayout.output b/kclvm/ast_pretty/src/test_data/codelayout.output index 152b2e8bd..2fcf3afca 100644 --- a/kclvm/ast_pretty/src/test_data/codelayout.output +++ b/kclvm/ast_pretty/src/test_data/codelayout.output @@ -165,3 +165,6 @@ list_if_item = [ if False: 2 ] + +longString = "Too long expression " + "Too long expression " + "Too long expression " +# recommended diff --git a/kclvm/ast_pretty/src/test_data/comment.input b/kclvm/ast_pretty/src/test_data/comment.input index b2723ee4c..868786b77 100644 --- a/kclvm/ast_pretty/src/test_data/comment.input +++ b/kclvm/ast_pretty/src/test_data/comment.input @@ -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" +} diff --git a/kclvm/ast_pretty/src/test_data/comment.output b/kclvm/ast_pretty/src/test_data/comment.output index 15d59812d..46986d36e 100644 --- a/kclvm/ast_pretty/src/test_data/comment.output +++ b/kclvm/ast_pretty/src/test_data/comment.output @@ -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" +}