From ff6db57f0931457738aaf11bedad36ea81d88003 Mon Sep 17 00:00:00 2001 From: David Binder Date: Sun, 24 Mar 2024 11:04:59 +0100 Subject: [PATCH] Implement prettyprinting of attributes (#169) --- lang/printer/src/tokens.rs | 1 + lang/printer/src/types.rs | 6 ++++++ lang/printer/src/ust.rs | 19 ++++++++++++++++--- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/lang/printer/src/tokens.rs b/lang/printer/src/tokens.rs index 0c76a45dfa..0917e66c63 100644 --- a/lang/printer/src/tokens.rs +++ b/lang/printer/src/tokens.rs @@ -7,6 +7,7 @@ pub const COLON: &str = ":"; pub const DOT: &str = "."; pub const AT: &str = "@"; pub const HOLE: &str = "?"; +pub const HASH: &str = "#"; // Keywords diff --git a/lang/printer/src/types.rs b/lang/printer/src/types.rs index e64e0d4f19..38ca0fe64b 100644 --- a/lang/printer/src/types.rs +++ b/lang/printer/src/types.rs @@ -39,6 +39,12 @@ pub trait Print<'a> { } } +impl<'a> Print<'a> for String { + fn print(&'a self, _cfg: &PrintCfg, alloc: &'a Alloc<'a>) -> Builder<'a> { + alloc.text(self) + } +} + pub trait PrintInCtx<'a> { type Ctx; diff --git a/lang/printer/src/ust.rs b/lang/printer/src/ust.rs index 9e7058c37e..741117d63a 100644 --- a/lang/printer/src/ust.rs +++ b/lang/printer/src/ust.rs @@ -96,6 +96,7 @@ impl<'a> PrintInCtx<'a> for Data { let head = doc .print(cfg, alloc) + .append(attr.print(cfg, alloc)) .append(alloc.keyword(DATA)) .append(alloc.space()) .append(alloc.typ(name)) @@ -140,6 +141,7 @@ impl<'a> PrintInCtx<'a> for Codata { let head = doc .print(cfg, alloc) + .append(attr.print(cfg, alloc)) .append(alloc.keyword(CODATA)) .append(alloc.space()) .append(alloc.typ(name)) @@ -175,7 +177,7 @@ impl<'a> Print<'a> for Def { return alloc.nil(); } - let doc = doc.print(cfg, alloc); + let doc = doc.print(cfg, alloc).append(attr.print(cfg, alloc)); let head = alloc .keyword(DEF) @@ -200,7 +202,7 @@ impl<'a> Print<'a> for Codef { return alloc.nil(); } - let doc = doc.print(cfg, alloc); + let doc = doc.print(cfg, alloc).append(attr.print(cfg, alloc)); let head = alloc .keyword(CODEF) @@ -223,7 +225,7 @@ impl<'a> Print<'a> for Let { return alloc.nil(); } - let doc = doc.print(cfg, alloc); + let doc = doc.print(cfg, alloc).append(attr.print(cfg, alloc)); let head = alloc .keyword(LET) @@ -534,6 +536,17 @@ impl<'a> Print<'a> for Motive { } } +impl<'a> Print<'a> for Attribute { + fn print(&'a self, cfg: &PrintCfg, alloc: &'a Alloc<'a>) -> Builder<'a> { + if self.attrs.is_empty() { + alloc.nil() + } else { + let p = print_comma_separated(&self.attrs, cfg, alloc); + alloc.text(HASH).append(p.brackets()).append(alloc.hardline()) + } + } +} + impl<'a> Print<'a> for DocComment { fn print(&'a self, _cfg: &PrintCfg, alloc: &'a Alloc<'a>) -> Builder<'a> { let DocComment { docs } = self;