Skip to content

Commit 763724d

Browse files
authored
enhance: enhance fmt tool. (#1850)
* enhance: enhance fmt tool. save empty line between comments, fix logic of empty line between stmt Signed-off-by: he1pa <[email protected]> * fix ut Signed-off-by: he1pa <[email protected]> --------- Signed-off-by: he1pa <[email protected]>
1 parent d8964b2 commit 763724d

File tree

6 files changed

+36
-4
lines changed

6 files changed

+36
-4
lines changed

kclvm/ast_pretty/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,14 @@ impl<'p> Printer<'p> {
245245
match self.comments.pop_front() {
246246
Some(comment) => {
247247
self.writeln(&comment.node.text);
248+
match self.comments.front() {
249+
Some(next_comment) => {
250+
if next_comment.line >= comment.line + 2 && count > 0 {
251+
self.write_newline();
252+
}
253+
}
254+
None => {}
255+
}
248256
}
249257
None => break,
250258
}

kclvm/ast_pretty/src/node.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
3939
if let Some(doc) = &module.doc {
4040
self.write(&doc.node);
4141
self.write_newline();
42+
self.write_newline();
4243
}
4344

4445
self.stmts(&module.body);
@@ -993,9 +994,13 @@ impl<'p> Printer<'p> {
993994
// Do not format out user-reserved blank lines: which does not mean that to preserve all user-written blank lines.
994995
// For situations where there are more than two blank lines, we only keep one blank line.
995996
let need_newline = if let Some(prev_stmt) = prev_stmt {
996-
stmt.line > 0
997-
&& stmt.line >= prev_stmt.end_line + 2
998-
&& !self.has_comments_on_node(stmt)
997+
if stmt.line > prev_stmt.end_line + 2 {
998+
true
999+
} else if stmt.line == prev_stmt.end_line + 2 {
1000+
stmt.line > 0 && !self.has_comments_on_node(stmt)
1001+
} else {
1002+
false
1003+
}
9991004
} else {
10001005
false
10011006
};

kclvm/ast_pretty/src/test_data/codelayout.output

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
22
Module documents
33
"""
4+
45
import math as alias_math
56

67
schema Person(Base):
@@ -111,6 +112,7 @@ joined_data_3 = '''\
111112
joined_data_4 = '''\
112113
\${CC}
113114
'''
115+
114116
# Member access and index assign targets
115117
a[0].b -= 1
116118
a.b[0] += 1

kclvm/ast_pretty/src/test_data/comment.input

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,10 @@ data = [ # Comment One
7777
2 # Comment Five
7878
# Comment Six
7979
*[3, 4] # Comment Seven
80-
]
80+
]
81+
82+
# This is a comment
83+
foo = "bar"
84+
85+
# This is another comment
86+
fizz = "bazz"

kclvm/ast_pretty/src/test_data/comment.output

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,13 @@ appConfiguration = AppConfiguration {
3434
name: "kusion_override"
3535
}
3636
# Comment Seven
37+
3738
# Comment Eight
3839
overQuota: True
3940
}
41+
4042
# Comment Nine
43+
4144
# Deprecated
4245
@Deprecated()
4346
schema Foo:
@@ -91,3 +94,9 @@ data = [
9194
# Comment Seven
9295
*[3, 4]
9396
]
97+
98+
# This is a comment
99+
foo = "bar"
100+
101+
# This is another comment
102+
fizz = "bazz"

kclvm/tools/src/format/test_data/format_data/inline_comment.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
# Inline comment 2
33
# Inline comment 3
44
a = 1
5+
56
# Inline comment 4
67
# Inline comment 5
78
#
@@ -11,6 +12,7 @@ a = 1
1112
#
1213
# Inline comment 8
1314
b = 2
15+
1416
# Same inline comment
1517
# Same inline comment
1618
# Same inline comment

0 commit comments

Comments
 (0)