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

feat: better format tool devex #1582

Merged
merged 1 commit into from
Aug 19, 2024
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
1 change: 1 addition & 0 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,6 @@ impl KclvmServiceImpl {
/// name = "Alice"
/// age = 18
/// }
///
/// "#.to_string();
/// let result = serv.format_code(&FormatCodeArgs {
/// source: source.clone(),
Expand Down
98 changes: 97 additions & 1 deletion kclvm/api/src/testdata/format-code.response.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,99 @@
{
"formatted": [115,99,104,101,109,97,32,80,101,114,115,111,110,58,10,32,32,32,32,110,97,109,101,58,32,115,116,114,10,32,32,32,32,97,103,101,58,32,105,110,116,10,10,112,101,114,115,111,110,32,61,32,80,101,114,115,111,110,32,123,10,32,32,32,32,110,97,109,101,32,61,32,34,65,108,105,99,101,34,10,32,32,32,32,97,103,101,32,61,32,49,56,10,125,10,10]
"formatted": [
115,
99,
104,
101,
109,
97,
32,
80,
101,
114,
115,
111,
110,
58,
10,
32,
32,
32,
32,
110,
97,
109,
101,
58,
32,
115,
116,
114,
10,
32,
32,
32,
32,
97,
103,
101,
58,
32,
105,
110,
116,
10,
10,
112,
101,
114,
115,
111,
110,
32,
61,
32,
80,
101,
114,
115,
111,
110,
32,
123,
10,
32,
32,
32,
32,
110,
97,
109,
101,
32,
61,
32,
34,
65,
108,
105,
99,
101,
34,
10,
32,
32,
32,
32,
97,
103,
101,
32,
61,
32,
49,
56,
10,
125,
10
]
}
4 changes: 2 additions & 2 deletions kclvm/api/src/testdata/list-variables.response.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"c": {
"variables": [
{
"value": "{\"a\": \"b\"}",
"value": "{\n \"a\": \"b\"\n}",
"type_name": "",
"op_sym": "=",
"list_items": [],
Expand Down Expand Up @@ -36,7 +36,7 @@
"b": {
"variables": [
{
"value": "[1, 2, 3]",
"value": "[\n 1\n 2\n 3\n]",
"type_name": "",
"op_sym": "=",
"list_items": [
Expand Down
1 change: 0 additions & 1 deletion kclvm/api/src/testdata/test.k
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@ schema Person:
age: int

alice = Person {age = 18}

9 changes: 5 additions & 4 deletions kclvm/ast_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,23 @@ pub struct Printer<'p> {
pub indent: usize,
pub cfg: Config,
/// Print comments,
pub last_ast_line: u64,
pub comments: VecDeque<ast::NodeRef<ast::Comment>>,
pub import_spec: IndexMap<String, String>,
pub hook: &'p (dyn PrinterHook + 'p),
/// Last AST expr/stmt line, default is 0.
last_ast_line: u64,
}

impl Default for Printer<'_> {
fn default() -> Self {
Self {
hook: &NoHook,
out: Default::default(),
indent: Default::default(),
cfg: Default::default(),
last_ast_line: Default::default(),
comments: Default::default(),
import_spec: Default::default(),
hook: &NoHook,
last_ast_line: Default::default(),
}
}
}
Expand All @@ -91,10 +92,10 @@ impl<'p> Printer<'p> {
out: "".to_string(),
indent: 0,
cfg,
last_ast_line: 0,
comments: VecDeque::default(),
import_spec: IndexMap::default(),
hook,
last_ast_line: 0,
}
}

Expand Down
41 changes: 30 additions & 11 deletions kclvm/ast_pretty/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,6 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
}
self.expr(&assign_stmt.value);
self.write_newline_without_fill();
if matches!(assign_stmt.value.node, ast::Expr::Schema(_)) {
self.write_newline_without_fill();
}
}

fn walk_aug_assign_stmt(&mut self, aug_assign_stmt: &'ctx ast::AugAssignStmt) -> Self::Result {
Expand Down Expand Up @@ -315,7 +312,7 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
}

fn walk_quant_expr(&mut self, quant_expr: &'ctx ast::QuantExpr) -> Self::Result {
let in_one_line = false;
let in_one_line = self.last_ast_line > 0 && quant_expr.test.line == self.last_ast_line;
let quant_op_string: String = quant_expr.op.clone().into();
self.write(&quant_op_string);
self.write_space();
Expand Down Expand Up @@ -451,11 +448,15 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
}

fn walk_list_expr(&mut self, list_expr: &'ctx ast::ListExpr) -> Self::Result {
let line_set = list_expr
let mut line_set = list_expr
.elts
.iter()
.map(|e| e.line)
.filter(|l| *l > 0)
.collect::<HashSet<u64>>();
if self.last_ast_line > 0 {
line_set.insert(self.last_ast_line);
}
// There are comments in the configuration block.
let has_comment = !list_expr.elts.is_empty()
&& list_expr
Expand Down Expand Up @@ -618,7 +619,15 @@ impl<'p, 'ctx> MutSelfTypedResultWalker<'ctx> for Printer<'p> {
}

fn walk_config_expr(&mut self, config_expr: &'ctx ast::ConfigExpr) -> Self::Result {
let line_set: HashSet<u64> = config_expr.items.iter().map(|item| item.line).collect();
let mut line_set: HashSet<u64> = config_expr
.items
.iter()
.map(|item| item.line)
.filter(|l| *l > 0)
.collect();
if self.last_ast_line > 0 {
line_set.insert(self.last_ast_line);
}
// There are comments in the configuration block.
let has_comment = !config_expr.items.is_empty()
&& config_expr
Expand Down Expand Up @@ -948,18 +957,28 @@ impl<'p> Printer<'p> {
}

pub fn stmts(&mut self, stmts: &[ast::NodeRef<ast::Stmt>]) {
let mut prev_stmt: Option<ast::Stmt> = None;
// Hold the prev statement pointer.
let mut prev_stmt: Option<&ast::NodeRef<ast::Stmt>> = None;
for stmt in stmts {
let import_stmt_alter = match (prev_stmt.as_ref(), stmt.as_ref().node.to_owned()) {
let import_stmt_alter = match (prev_stmt.map(|s| &s.node).as_ref(), &stmt.node) {
(Some(ast::Stmt::Import(_)), ast::Stmt::Import(_)) => false,
(Some(ast::Stmt::Import(_)), _) => true,
_ => false,
};
if import_stmt_alter {
self.write_newline();
// Do not format out user-reserved blank lines: which does not mean that to preserve all user-written blank lines.
// For situations where there are more than two blank lines, we only keep one blank line.
let need_newline = if let Some(prev_stmt) = prev_stmt {
stmt.line > 0
&& stmt.line >= prev_stmt.end_line + 2
&& !self.has_comments_on_node(stmt)
} else {
false
};
if import_stmt_alter || need_newline {
self.write_newline_without_fill();
}
self.stmt(stmt);
prev_stmt = Some(stmt.node.to_owned());
prev_stmt = Some(stmt);
}
}
}
36 changes: 36 additions & 0 deletions kclvm/ast_pretty/src/test_data/codelayout.input
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import math as alias_math
schema Person ( Base):
name:str
age:int

"attr": str
"attr-x": str
check :
Expand All @@ -17,6 +18,11 @@ person = Person{
}
if True:
a = 1

b = 2


c = 3
elif True:
b = 2
else:
Expand Down Expand Up @@ -108,3 +114,33 @@ a.b[1].c /= 1
a.b[c.d].e == 1
a.b[1 + 1].e = 1
a.b[f()].e = 1


a=1


b= 2


c =3



d = 4
e = 5
f = lambda {


a=1


b= 2


c =3



d = 4
e = 5
}
27 changes: 26 additions & 1 deletion kclvm/ast_pretty/src/test_data/codelayout.output
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import math as alias_math
schema Person(Base):
name: str
age: int

"attr": str
"attr-x": str

Expand All @@ -16,9 +17,12 @@ person = Person {
name: "Alice"
age: 18
}

if True:
a = 1

b = 2

c = 3
elif True:
b = 2
else:
Expand All @@ -43,12 +47,14 @@ _c = (a + b) * (a - b)
_b = 2
_c = 3
_d = 4

_value = (1 + 2 * 3)
_value = (1 + 2 * 3)
_value = 1 + -2 * ~3
_list = [1, 2, 3]
_list = [*_list, [4, 5, 6]]
_list = [*_list, [4, 5, 6]]

_dict = {**{"k": "v"}, **{"k": "v"}}
a = [1, 2, 3]
b = [
Expand Down Expand Up @@ -112,3 +118,22 @@ a.b[1].c /= 1
a.b[c.d].e == 1
a.b[1 + 1].e = 1
a.b[f()].e = 1

a = 1

b = 2

c = 3

d = 4
e = 5
f = lambda {
a = 1

b = 2

c = 3

d = 4
e = 5
}
2 changes: 2 additions & 0 deletions kclvm/ast_pretty/src/test_data/collection_if.input
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ data3 = {
else:
key4: "value4"
}


data4 = [
if True:
"value1"
Expand Down
Loading
Loading