Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix format behavior on line break between import stmt and other stmts #792

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
}
1 change: 1 addition & 0 deletions kclvm/ast_pretty/src/test_data/codelayout.output
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Module documents
"""
import math as alias_math

schema Person(Base):
name: str
age: int
Expand Down
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
Loading