Skip to content

Commit

Permalink
Implement prettyprinting of attributes (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
BinderDavid authored Mar 24, 2024
1 parent 2da2839 commit ff6db57
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions lang/printer/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions lang/printer/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
19 changes: 16 additions & 3 deletions lang/printer/src/ust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit ff6db57

Please sign in to comment.