diff --git a/kclvm/ast_pretty/src/lib.rs b/kclvm/ast_pretty/src/lib.rs index 980ba8550..ab5973b04 100644 --- a/kclvm/ast_pretty/src/lib.rs +++ b/kclvm/ast_pretty/src/lib.rs @@ -261,7 +261,7 @@ impl<'p> Printer<'p> { } } -/// Print AST to string +/// Print AST to string. The default format is according to the KCL code style defined here: https://kcl-lang.io/docs/reference/lang/spec/codestyle pub fn print_ast_module(module: &Module) -> String { let mut printer = Printer::default(); printer.write_module(module); diff --git a/kclvm/tools/src/format/test_data/format_data/different_stmts_line_breaks.golden b/kclvm/tools/src/format/test_data/format_data/different_stmts_line_breaks.golden new file mode 100644 index 000000000..1dc12573c --- /dev/null +++ b/kclvm/tools/src/format/test_data/format_data/different_stmts_line_breaks.golden @@ -0,0 +1,8 @@ +import a + +schema A: + name: str = a.name + +A {} + +# Break one blank line between different statements e.g., import, schema and expression statements. diff --git a/kclvm/tools/src/format/test_data/format_data/different_stmts_line_breaks.input b/kclvm/tools/src/format/test_data/format_data/different_stmts_line_breaks.input new file mode 100644 index 000000000..92b107a1e --- /dev/null +++ b/kclvm/tools/src/format/test_data/format_data/different_stmts_line_breaks.input @@ -0,0 +1,6 @@ +import a +schema A: + name: str = a.name +A{} + +# Break one blank line between different statements e.g., import, schema and expression statements. diff --git a/kclvm/tools/src/format/test_data/format_data/list_dict_schema_expr.golden b/kclvm/tools/src/format/test_data/format_data/list_dict_schema_expr.golden new file mode 100644 index 000000000..a71205411 --- /dev/null +++ b/kclvm/tools/src/format/test_data/format_data/list_dict_schema_expr.golden @@ -0,0 +1,20 @@ +dict = { + "key": "value" +} + +dict2 = {"key": "value"} + +list = [ + first + second +] + +list2 = [first, second] + +expr = Person { + name: "Alice" +} + +expr2 = Person {name: "Alice"} + +# It's both acceptable to write each entry in the list/dict/schema expr without line breaks, or separate each entry with one line break. diff --git a/kclvm/tools/src/format/test_data/format_data/list_dict_schema_expr.input b/kclvm/tools/src/format/test_data/format_data/list_dict_schema_expr.input new file mode 100644 index 000000000..a71205411 --- /dev/null +++ b/kclvm/tools/src/format/test_data/format_data/list_dict_schema_expr.input @@ -0,0 +1,20 @@ +dict = { + "key": "value" +} + +dict2 = {"key": "value"} + +list = [ + first + second +] + +list2 = [first, second] + +expr = Person { + name: "Alice" +} + +expr2 = Person {name: "Alice"} + +# It's both acceptable to write each entry in the list/dict/schema expr without line breaks, or separate each entry with one line break. diff --git a/kclvm/tools/src/format/test_data/format_data/union_types.golden b/kclvm/tools/src/format/test_data/format_data/union_types.golden new file mode 100644 index 000000000..86f5bc5d7 --- /dev/null +++ b/kclvm/tools/src/format/test_data/format_data/union_types.golden @@ -0,0 +1,5 @@ +workloadType: "Deployment" | "StatefulSet" = "Deployment" +ServiceType: "LoadBalance" | "ClusterIP" = "LoadBalance" +abc: A | B | C = A {} + +# In union types, there should be one and only one whitespace both before and after the union operator | diff --git a/kclvm/tools/src/format/test_data/format_data/union_types.input b/kclvm/tools/src/format/test_data/format_data/union_types.input new file mode 100644 index 000000000..0dd6de4f9 --- /dev/null +++ b/kclvm/tools/src/format/test_data/format_data/union_types.input @@ -0,0 +1,5 @@ +workloadType: "Deployment"|"StatefulSet" = "Deployment" +ServiceType: "LoadBalance" |"ClusterIP" = "LoadBalance" +abc: A|B|C = A{} + +# In union types, there should be one and only one whitespace both before and after the union operator | diff --git a/kclvm/tools/src/format/tests.rs b/kclvm/tools/src/format/tests.rs index d150f819a..0b483abfe 100644 --- a/kclvm/tools/src/format/tests.rs +++ b/kclvm/tools/src/format/tests.rs @@ -24,6 +24,9 @@ const TEST_CASES: &[&str; 18] = &[ "string", "type_alias", "unary", + // "union_types", + // "different_stmts_line_breaks", + // "list_dict_schema_expr", ]; fn read_data(data_name: &str) -> (String, String) {