Skip to content

Commit

Permalink
refactor formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
aqrln committed Jan 11, 2025
1 parent 86885d8 commit 9f44246
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 174 deletions.
178 changes: 4 additions & 174 deletions query-engine/core/src/compiler/expression.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use pretty::{
termcolor::{Color, ColorSpec},
DocAllocator, DocBuilder,
};
use query_structure::PrismaValue;
use serde::Serialize;

mod format;

#[derive(Debug, Serialize)]
pub struct Binding {
pub name: String,
Expand Down Expand Up @@ -102,7 +100,8 @@ pub enum PrettyPrintError {
impl Expression {
pub fn pretty_print(&self, color: bool, width: usize) -> Result<String, PrettyPrintError> {
let arena = pretty::Arena::new();
let doc = self.to_doc(&arena);
let builder = format::PrettyPrinter::new(&arena);
let doc = builder.expression(self);

let mut buf = if color {
pretty::termcolor::Buffer::ansi()
Expand All @@ -113,175 +112,6 @@ impl Expression {
doc.render_colored(width, &mut buf)?;
Ok(String::from_utf8(buf.into_inner())?)
}

fn to_doc<'a, D>(&'a self, d: &'a D) -> DocBuilder<'a, D, ColorSpec>
where
D: DocAllocator<'a, ColorSpec>,
D::Doc: Clone,
{
let color_kw = || ColorSpec::new().set_fg(Some(Color::Blue)).clone();
let color_fn = || ColorSpec::new().set_underline(true).clone();
let color_var = || ColorSpec::new().set_bold(true).clone();
let color_lit = || ColorSpec::new().set_italic(true).set_fg(Some(Color::Green)).clone();

let format_query = |tag: &'static str, db_query: &'a DbQuery| {
d.text(tag)
.annotate(color_kw())
.append(d.softline())
.append(
d.reflow(&db_query.query)
.align()
.enclose("«", "»")
.annotate(color_lit()),
)
.append(d.line())
.append(d.text("with params").annotate(color_kw()))
.append(d.space())
.append(
d.intersperse(
db_query.params.iter().map(|param| match param {
PrismaValue::Placeholder { name, r#type } => d.text("var").annotate(color_kw()).append(
d.text(name)
.annotate(color_var())
.append(d.space())
.append(d.text("as").annotate(color_kw()))
.append(d.space())
.append(match r#type {
query_structure::PlaceholderType::Array(inner) => format!("{inner:?}[]"),
_ => format!("{type:?}"),
})
.parens(),
),
_ => d
.text("const")
.annotate(color_kw())
.append(d.text(format!("{param:?}")).annotate(color_lit()).parens()),
}),
d.text(",").append(d.softline()),
)
.align()
.brackets(),
)
.align()
};

let format_function = |name: &'static str, args: &'a [Expression]| {
d.text(name).annotate(color_fn()).append(d.space()).append(
d.intersperse(args.iter().map(|expr| expr.to_doc(d)), d.space())
.parens(),
)
};

let format_unary_function = |name: &'static str, arg: &'a Expression| {
d.text(name)
.annotate(color_fn())
.append(d.space())
.append(arg.to_doc(d).parens())
};

match self {
Expression::Seq(vec) => d.intersperse(vec.iter().map(|expr| expr.to_doc(d)), d.text(";").append(d.line())),

Expression::Get { name } => d
.text("get")
.annotate(color_kw())
.append(d.space())
.append(d.text(name).annotate(color_var())),

Expression::Let { bindings, expr } => d
.text("let")
.annotate(color_kw())
.append(d.softline())
.append(
d.intersperse(
bindings.iter().map(|binding| {
d.text(&binding.name)
.annotate(color_var())
.append(d.space())
.append("=")
.append(d.softline())
.append(binding.expr.to_doc(d))
}),
d.text(";").append(d.line()),
)
.align(),
)
.append(d.line())
.append(d.text("in").annotate(color_kw()))
.append(d.softline())
.append(expr.to_doc(d).align()),

Expression::GetFirstNonEmpty { names } => d
.text("getFirstNonEmpty")
.annotate(color_fn())
.append(d.intersperse(names.iter().map(|name| d.text(name).annotate(color_var())), d.space())),

Expression::Query(db_query) => format_query("query", db_query),

Expression::Execute(db_query) => format_query("execute", db_query),

Expression::Reverse(expression) => format_unary_function("reverse", expression),

Expression::Sum(vec) => format_function("sum", vec),

Expression::Concat(vec) => format_function("concat", vec),

Expression::Unique(expression) => format_unary_function("unique", expression),

Expression::Required(expression) => format_unary_function("required", expression),

Expression::Join { parent, children } => d
.text("join")
.annotate(color_kw())
.append(d.space())
.append(parent.to_doc(d).parens())
.append(d.line())
.append(d.text("with").annotate(color_kw()))
.append(d.space())
.append(
d.intersperse(
children.iter().map(|join| {
join.child
.to_doc(d)
.parens()
.append(d.space())
.append(d.text("on").annotate(color_kw()))
.append(d.space())
.append(d.intersperse(
join.on.iter().map(|(l, r)| {
d.text("left")
.annotate(color_kw())
.append(".")
.append(d.text(l).annotate(color_var()))
.parens()
.append(d.space())
.append("=")
.append(d.space())
.append(
d.text("right")
.annotate(color_kw())
.append(".")
.append(d.text(r).annotate(color_var()))
.parens(),
)
}),
d.text(", "),
))
}),
d.text(",").append(d.line()),
)
.align(),
),

Expression::MapField { field, records } => d
.text("mapField")
.annotate(color_fn())
.append(d.space())
.append(d.text(field).double_quotes().annotate(color_lit()))
.append(d.space())
.append(records.to_doc(d).parens()),
}
}
}

impl std::fmt::Display for Expression {
Expand Down
Loading

0 comments on commit 9f44246

Please sign in to comment.