Skip to content

Commit

Permalink
fix: format behavior on line break between import stmt and other stmts
Browse files Browse the repository at this point in the history
Signed-off-by: xiarui.xr <[email protected]>
  • Loading branch information
amyXia1994 committed Oct 18, 2023
1 parent a4f6be2 commit ffdd980
Show file tree
Hide file tree
Showing 12 changed files with 32 additions and 4 deletions.
10 changes: 10 additions & 0 deletions kclvm/ast_pretty/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -904,8 +904,18 @@ impl<'p> Printer<'p> {
}

pub fn stmts(&mut self, stmts: &[ast::NodeRef<ast::Stmt>]) {
let mut prev_stmt: Option<ast::Stmt> = None;
for stmt in stmts {
let import_stmt_alter = match (prev_stmt.as_ref(), stmt.as_ref().node.to_owned()) {
(Some(ast::Stmt::Import(_)), ast::Stmt::Import(_)) => false,
(Some(ast::Stmt::Import(_)), _) => true,
_ => false,
};
if import_stmt_alter {
self.write_newline();
}
self.stmt(stmt);
prev_stmt = Some(stmt.node.to_owned());
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math

schema Base:
name: str

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math as alias_math

schema Person(Base):
# inline comment
name: str
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ schema A:
name: str = a.name

A {}

# Break one blank line between different statements e.g., import, schema and expression statements.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ import a
schema A:
name: str = a.name
A{}

# Break one blank line between different statements e.g., import, schema and expression statements.
1 change: 1 addition & 0 deletions kclvm/tools/src/format/test_data/format_data/import.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ import b
import c
import d
import e as e

a = 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import math
import abc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import math
import abc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import a.b.c
import d

schema A:
name: str

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import a.b.c
import d
schema A:
name: str
1 change: 1 addition & 0 deletions kclvm/tools/src/format/test_data/format_data/schema.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import math

mixin XXMixin:
nameVar: str

Expand Down
6 changes: 4 additions & 2 deletions kclvm/tools/src/format/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use walkdir::WalkDir;

const FILE_INPUT_SUFFIX: &str = ".input";
const FILE_OUTPUT_SUFFIX: &str = ".golden";
const TEST_CASES: &[&str; 19] = &[
const TEST_CASES: &[&str; 22] = &[
"assert",
"check",
"blankline",
Expand All @@ -16,6 +16,7 @@ const TEST_CASES: &[&str; 19] = &[
"comp_for",
"empty",
"import",
"import_only",
"indent",
"inline_comment",
"lambda",
Expand All @@ -25,7 +26,8 @@ const TEST_CASES: &[&str; 19] = &[
"type_alias",
"unary",
"union_types",
// "different_stmts_line_breaks",
"layout_import_stmt",
"different_stmts_line_breaks",
// "list_dict_schema_expr",
];

Expand Down

0 comments on commit ffdd980

Please sign in to comment.