Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir-def/src/body/pretty.rs')
-rw-r--r--crates/hir-def/src/body/pretty.rs125
1 files changed, 106 insertions, 19 deletions
diff --git a/crates/hir-def/src/body/pretty.rs b/crates/hir-def/src/body/pretty.rs
index 55740a68ac..37167fcb81 100644
--- a/crates/hir-def/src/body/pretty.rs
+++ b/crates/hir-def/src/body/pretty.rs
@@ -16,6 +16,13 @@ use crate::{
use super::*;
+#[derive(Debug, Clone, Copy, PartialEq, Eq)]
+pub(super) enum LineFormat {
+ Oneline,
+ Newline,
+ Indentation,
+}
+
pub(super) fn print_body_hir(
db: &dyn DefDatabase,
body: &Body,
@@ -52,7 +59,14 @@ pub(super) fn print_body_hir(
}
};
- let mut p = Printer { db, body, buf: header, indent_level: 0, needs_indent: false, edition };
+ let mut p = Printer {
+ db,
+ body,
+ buf: header,
+ indent_level: 0,
+ line_format: LineFormat::Newline,
+ edition,
+ };
if let DefWithBodyId::FunctionId(it) = owner {
p.buf.push('(');
let function_data = &db.function_data(it);
@@ -95,12 +109,38 @@ pub(super) fn print_expr_hir(
expr: ExprId,
edition: Edition,
) -> String {
- let mut p =
- Printer { db, body, buf: String::new(), indent_level: 0, needs_indent: false, edition };
+ let mut p = Printer {
+ db,
+ body,
+ buf: String::new(),
+ indent_level: 0,
+ line_format: LineFormat::Newline,
+ edition,
+ };
p.print_expr(expr);
p.buf
}
+pub(super) fn print_pat_hir(
+ db: &dyn DefDatabase,
+ body: &Body,
+ _owner: DefWithBodyId,
+ pat: PatId,
+ oneline: bool,
+ edition: Edition,
+) -> String {
+ let mut p = Printer {
+ db,
+ body,
+ buf: String::new(),
+ indent_level: 0,
+ line_format: if oneline { LineFormat::Oneline } else { LineFormat::Newline },
+ edition,
+ };
+ p.print_pat(pat);
+ p.buf
+}
+
macro_rules! w {
($dst:expr, $($arg:tt)*) => {
{ let _ = write!($dst, $($arg)*); }
@@ -109,10 +149,10 @@ macro_rules! w {
macro_rules! wln {
($dst:expr) => {
- { let _ = writeln!($dst); }
+ { $dst.newline(); }
};
($dst:expr, $($arg:tt)*) => {
- { let _ = writeln!($dst, $($arg)*); }
+ { let _ = w!($dst, $($arg)*); $dst.newline(); }
};
}
@@ -121,24 +161,30 @@ struct Printer<'a> {
body: &'a Body,
buf: String,
indent_level: usize,
- needs_indent: bool,
+ line_format: LineFormat,
edition: Edition,
}
impl Write for Printer<'_> {
fn write_str(&mut self, s: &str) -> fmt::Result {
for line in s.split_inclusive('\n') {
- if self.needs_indent {
+ if matches!(self.line_format, LineFormat::Indentation) {
match self.buf.chars().rev().find(|ch| *ch != ' ') {
Some('\n') | None => {}
_ => self.buf.push('\n'),
}
self.buf.push_str(&" ".repeat(self.indent_level));
- self.needs_indent = false;
}
self.buf.push_str(line);
- self.needs_indent = line.ends_with('\n');
+
+ if matches!(self.line_format, LineFormat::Newline | LineFormat::Indentation) {
+ self.line_format = if line.ends_with('\n') {
+ LineFormat::Indentation
+ } else {
+ LineFormat::Newline
+ };
+ }
}
Ok(())
@@ -161,14 +207,28 @@ impl Printer<'_> {
}
}
+ // Add a newline if the current line is not empty.
+ // If the current line is empty, add a space instead.
+ //
+ // Do not use [`writeln!()`] or [`wln!()`] here, which will result in
+ // infinite recursive calls to this function.
fn newline(&mut self) {
- match self.buf.chars().rev().find_position(|ch| *ch != ' ') {
- Some((_, '\n')) | None => {}
- Some((idx, _)) => {
- if idx != 0 {
- self.buf.drain(self.buf.len() - idx..);
+ if matches!(self.line_format, LineFormat::Oneline) {
+ match self.buf.chars().last() {
+ Some(' ') | None => {}
+ Some(_) => {
+ w!(self, " ");
+ }
+ }
+ } else {
+ match self.buf.chars().rev().find_position(|ch| *ch != ' ') {
+ Some((_, '\n')) | None => {}
+ Some((idx, _)) => {
+ if idx != 0 {
+ self.buf.drain(self.buf.len() - idx..);
+ }
+ w!(self, "\n");
}
- writeln!(self).unwrap()
}
}
}
@@ -539,12 +599,14 @@ impl Printer<'_> {
w!(self, ")");
}
Pat::Or(pats) => {
+ w!(self, "(");
for (i, pat) in pats.iter().enumerate() {
if i != 0 {
w!(self, " | ");
}
self.print_pat(*pat);
}
+ w!(self, ")");
}
Pat::Record { path, args, ellipsis } => {
match path {
@@ -554,12 +616,37 @@ impl Printer<'_> {
w!(self, " {{");
let edition = self.edition;
+ let oneline = matches!(self.line_format, LineFormat::Oneline);
self.indented(|p| {
- for arg in args.iter() {
- w!(p, "{}: ", arg.name.display(self.db.upcast(), edition));
- p.print_pat(arg.pat);
- wln!(p, ",");
+ for (idx, arg) in args.iter().enumerate() {
+ let field_name = arg.name.display(self.db.upcast(), edition).to_string();
+
+ let mut same_name = false;
+ if let Pat::Bind { id, subpat: None } = &self.body[arg.pat] {
+ if let Binding { name, mode: BindingAnnotation::Unannotated, .. } =
+ &self.body.bindings[*id]
+ {
+ if name.as_str() == field_name {
+ same_name = true;
+ }
+ }
+ }
+
+ w!(p, "{}", field_name);
+
+ if !same_name {
+ w!(p, ": ");
+ p.print_pat(arg.pat);
+ }
+
+ // Do not print the extra comma if the line format is oneline
+ if oneline && idx == args.len() - 1 {
+ w!(p, " ");
+ } else {
+ wln!(p, ",");
+ }
}
+
if *ellipsis {
wln!(p, "..");
}