From dcbc76b967f4c16fbb08a3b103abc1ed2951af7e Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Sun, 9 Jun 2024 12:00:43 +0800 Subject: [PATCH 1/3] internal: better print style for hir --- crates/hir-def/src/body/pretty.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/hir-def/src/body/pretty.rs b/crates/hir-def/src/body/pretty.rs index cbb5ca887f..9aff712f4c 100644 --- a/crates/hir-def/src/body/pretty.rs +++ b/crates/hir-def/src/body/pretty.rs @@ -48,22 +48,36 @@ pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBo let mut p = Printer { db, body, buf: header, indent_level: 0, needs_indent: false }; if let DefWithBodyId::FunctionId(it) = owner { p.buf.push('('); - let params = &db.function_data(it).params; - let mut params = params.iter(); + let function_data = &db.function_data(it); + let (mut params, ret_type) = (function_data.params.iter(), &function_data.ret_type); if let Some(self_param) = body.self_param { p.print_binding(self_param); p.buf.push(':'); + p.buf.push(' '); if let Some(ty) = params.next() { p.print_type_ref(ty); + p.buf.push(','); + p.buf.push(' '); } } body.params.iter().zip(params).for_each(|(¶m, ty)| { p.print_pat(param); p.buf.push(':'); + p.buf.push(' '); p.print_type_ref(ty); + p.buf.push(','); + p.buf.push(' '); }); + // remove the last ", " in param list + p.buf.truncate(p.buf.len() - 2); p.buf.push(')'); p.buf.push(' '); + // return type + p.buf.push('-'); + p.buf.push('>'); + p.buf.push(' '); + p.print_type_ref(ret_type); + p.buf.push(' '); } p.print_expr(body.body_expr); if matches!(owner, DefWithBodyId::StaticId(_) | DefWithBodyId::ConstId(_)) { From e8941dae46e241a4d809c5fdaf1a72d0e9f93fdc Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Sun, 9 Jun 2024 20:55:40 +0800 Subject: [PATCH 2/3] internal: tweak test case --- crates/hir-def/src/body/pretty.rs | 4 +++- crates/hir-def/src/body/tests.rs | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/crates/hir-def/src/body/pretty.rs b/crates/hir-def/src/body/pretty.rs index 9aff712f4c..16e409dfe6 100644 --- a/crates/hir-def/src/body/pretty.rs +++ b/crates/hir-def/src/body/pretty.rs @@ -69,7 +69,9 @@ pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBo p.buf.push(' '); }); // remove the last ", " in param list - p.buf.truncate(p.buf.len() - 2); + if body.params.len() > 0 { + p.buf.truncate(p.buf.len() - 2); + } p.buf.push(')'); p.buf.push(' '); // return type diff --git a/crates/hir-def/src/body/tests.rs b/crates/hir-def/src/body/tests.rs index e8b26d5373..0011d3a20c 100644 --- a/crates/hir-def/src/body/tests.rs +++ b/crates/hir-def/src/body/tests.rs @@ -156,7 +156,7 @@ fn main() { ); expect![[r#" - fn main() { + fn main() -> () { let are = "are"; let count = 10; builtin#lang(Arguments::new_v1_formatted)( @@ -258,7 +258,7 @@ impl SsrError { assert_eq!(db.body_with_source_map(def).1.diagnostics(), &[]); expect![[r#" - fn main() { + fn main() -> () { _ = $crate::error::SsrError::new( builtin#lang(Arguments::new_v1_formatted)( &[ @@ -303,7 +303,7 @@ macro_rules! m { }; } -fn f() { +fn f(a: i32, b: u32) -> String { m!(); } "#, @@ -317,7 +317,7 @@ fn f() { } expect![[r#" - fn f() { + fn f(a: i32, b: u32) -> String { { $crate::panicking::panic_fmt( builtin#lang(Arguments::new_v1_formatted)( From 6403bdb9302c55baf80e7047ef243e7818cd2854 Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Sun, 9 Jun 2024 22:31:55 +0800 Subject: [PATCH 3/3] minor: use push_str instead --- crates/hir-def/src/body/pretty.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/crates/hir-def/src/body/pretty.rs b/crates/hir-def/src/body/pretty.rs index 16e409dfe6..c48d16d053 100644 --- a/crates/hir-def/src/body/pretty.rs +++ b/crates/hir-def/src/body/pretty.rs @@ -52,32 +52,25 @@ pub(super) fn print_body_hir(db: &dyn DefDatabase, body: &Body, owner: DefWithBo let (mut params, ret_type) = (function_data.params.iter(), &function_data.ret_type); if let Some(self_param) = body.self_param { p.print_binding(self_param); - p.buf.push(':'); - p.buf.push(' '); + p.buf.push_str(": "); if let Some(ty) = params.next() { p.print_type_ref(ty); - p.buf.push(','); - p.buf.push(' '); + p.buf.push_str(", "); } } body.params.iter().zip(params).for_each(|(¶m, ty)| { p.print_pat(param); - p.buf.push(':'); - p.buf.push(' '); + p.buf.push_str(": "); p.print_type_ref(ty); - p.buf.push(','); - p.buf.push(' '); + p.buf.push_str(", "); }); // remove the last ", " in param list if body.params.len() > 0 { p.buf.truncate(p.buf.len() - 2); } p.buf.push(')'); - p.buf.push(' '); // return type - p.buf.push('-'); - p.buf.push('>'); - p.buf.push(' '); + p.buf.push_str(" -> "); p.print_type_ref(ret_type); p.buf.push(' '); }