pub struct DocBuilder<'a, D>(pub &'a D, pub BuildDoc<'a, <D as DocAllocator<'a>>::Doc>)
where
D: DocAllocator<'a> + ?Sized;
Expand description
The DocBuilder
type allows for convenient appending of documents even for arena allocated
documents by storing the arena inline.
Tuple Fields§
§0: &'a D
§1: BuildDoc<'a, <D as DocAllocator<'a>>::Doc>
Implementations§
Source§impl<'a, D> DocBuilder<'a, D>where
D: DocAllocator<'a> + ?Sized,
impl<'a, D> DocBuilder<'a, D>where
D: DocAllocator<'a> + ?Sized,
pub fn into_doc(self) -> <D as DocAllocator<'a>>::Doc
pub fn into_ref(self) -> DocBuilder<'a, D>
Sourcepub fn append<E>(self, that: E) -> DocBuilder<'a, D>where
E: Pretty<'a, D>,
pub fn append<E>(self, that: E) -> DocBuilder<'a, D>where
E: Pretty<'a, D>,
Append the given document after this document.
Sourcepub fn repeat(self, n: usize) -> DocBuilder<'a, D>
pub fn repeat(self, n: usize) -> DocBuilder<'a, D>
Repeats self
n
times, appending each repetition.
use prettyless::{Arena, DocAllocator};
let arena = Arena::new();
let doc = arena.text("[]");
assert_eq!(doc.clone().repeat(0).print(100).to_string(), "");
assert_eq!(arena.len(), 0); // +0
assert_eq!(doc.clone().repeat(1).print(100).to_string(), "[]");
assert_eq!(arena.len(), 0); // +0
assert_eq!(doc.clone().repeat(2).print(100).to_string(), "[][]");
assert_eq!(arena.len(), 1); // +1
assert_eq!(doc.clone().repeat(5).print(100).to_string(), "[][][][][]");
assert_eq!(arena.len(), 5); // +4
Sourcepub fn as_line_suffix(self) -> DocBuilder<'a, D>
pub fn as_line_suffix(self) -> DocBuilder<'a, D>
Make self
a line suffix.
use prettyless::{Arena, DocAllocator};
let arena = Arena::new();
let doc = arena.text(" // comment").as_line_suffix() + arena.text("x");
assert_eq!(doc.print(80).to_string(), "x // comment");
Sourcepub fn flat_alt<E>(self, that: E) -> DocBuilder<'a, D>where
E: Pretty<'a, D>,
pub fn flat_alt<E>(self, that: E) -> DocBuilder<'a, D>where
E: Pretty<'a, D>,
Acts as self
when laid out on multiple lines and acts as that
when laid out on a single line.
use prettyless::{Arena, DocAllocator};
let arena = Arena::new();
let body = arena.line().append("x");
let doc = arena.text("let")
.append(arena.line())
.append("x")
.group()
.append(
body.clone()
.flat_alt(
arena.line()
.append("in")
.append(body)
)
)
.group();
assert_eq!(doc.print(100).to_string(), "let x in x");
assert_eq!(doc.print(8).to_string(), "let x\nx");
Sourcepub fn when_group_flat(self) -> DocBuilder<'a, D>
pub fn when_group_flat(self) -> DocBuilder<'a, D>
Equivalent to nil.flat_alt(self)
use prettyless::{Arena, DocAllocator};
let arena = Arena::new();
let doc = arena.text("flat-only").when_group_flat().group();
assert_eq!(doc.print(0).to_string(), "");
assert_eq!(doc.print(10).to_string(), "flat-only");
Sourcepub fn when_group_break(self) -> DocBuilder<'a, D>
pub fn when_group_break(self) -> DocBuilder<'a, D>
Equivalent to self.flat_alt(nil)
use prettyless::{Arena, DocAllocator};
let arena = Arena::new();
let doc = arena.text("a")
.append(arena.text(",").when_group_break())
.enclose(arena.line_(), arena.line_())
.parens()
.group();
assert_eq!(doc.print(1).to_string(), "(\na,\n)");
assert_eq!(doc.print(10).to_string(), "(a)");
Sourcepub fn flatten(self) -> DocBuilder<'a, D>
pub fn flatten(self) -> DocBuilder<'a, D>
Flatten inner docs. Hard line will turn to failure.
Sourcepub fn group(self) -> DocBuilder<'a, D>
pub fn group(self) -> DocBuilder<'a, D>
Mark this document as a group.
Groups are layed out on a single line if possible. Within a group, all basic documents with several possible layouts are assigned the same layout, that is, they are all layed out horizontally and combined into a one single line, or they are each layed out on their own line.
Sourcepub fn nest(self, offset: isize) -> DocBuilder<'a, D>
pub fn nest(self, offset: isize) -> DocBuilder<'a, D>
Increase the indentation level of this document.
Sourcepub fn indent(self, offset: usize) -> DocBuilder<'a, D>
pub fn indent(self, offset: usize) -> DocBuilder<'a, D>
Increases the indentation level of this document by the given number of spaces.
This is equivalent to calling nest(offset as isize)
.
Sourcepub fn dedent(self, offset: usize) -> DocBuilder<'a, D>
pub fn dedent(self, offset: usize) -> DocBuilder<'a, D>
Decreases the indentation level of this document by the given number of spaces.
This is equivalent to calling nest(-(offset as isize))
.
Sourcepub fn dedent_to_root(self) -> DocBuilder<'a, D>
pub fn dedent_to_root(self) -> DocBuilder<'a, D>
Dedents to the root level, which is always 0.
use prettyless::{Arena, DocAllocator};
let arena = Arena::new();
let doc = (
arena.text("a")
+ (arena.text("b") + arena.hardline() + arena.text("c")).dedent_to_root()
+ arena.hardline()
+ arena.text("e")
).indent(4);
assert_eq!(doc.print(10).to_string(), "ab\nc\n e");
Sourcepub fn align(self) -> DocBuilder<'a, D>
pub fn align(self) -> DocBuilder<'a, D>
Lays out self
so with the nesting level set to the current column
use prettyless::{docs, DocAllocator};
let arena = &prettyless::Arena::new();
let doc = docs![
arena,
"lorem",
" ",
arena.intersperse(["ipsum", "dolor"].iter().cloned(), arena.line_()).align(),
arena.hardline(),
"next",
];
assert_eq!(
doc.print(80).to_string(),
"
lorem ipsum
dolor
next".trim_start()
);
Sourcepub fn union<E>(self, other: E) -> DocBuilder<'a, D>
pub fn union<E>(self, other: E) -> DocBuilder<'a, D>
use prettyless::{Arena, DocAllocator};
let arena = Arena::new();
let doc = (arena.text("short") + arena.hardline() + arena.text("long long long"))
.union(arena.text("short") + arena.hardline() + arena.text("short"));
assert_eq!(doc.print(10).to_string(), "short\nshort");
Sourcepub fn partial_union<E>(self, other: E) -> DocBuilder<'a, D>
pub fn partial_union<E>(self, other: E) -> DocBuilder<'a, D>
Like union
, but it only ensures fitting on the first line.
use prettyless::{Arena, DocAllocator};
let arena = Arena::new();
let doc = (arena.text("short") + arena.hardline() + arena.text("long long long"))
.partial_union(arena.text("short") + arena.hardline() + arena.text("short"));
assert_eq!(doc.print(10).to_string(), "short\nlong long long");
Sourcepub fn enclose<E, F>(self, before: E, after: F) -> DocBuilder<'a, D>
pub fn enclose<E, F>(self, before: E, after: F) -> DocBuilder<'a, D>
Puts self
between before
and after
pub fn single_quotes(self) -> DocBuilder<'a, D>
pub fn double_quotes(self) -> DocBuilder<'a, D>
pub fn parens(self) -> DocBuilder<'a, D>
pub fn angles(self) -> DocBuilder<'a, D>
pub fn braces(self) -> DocBuilder<'a, D>
pub fn brackets(self) -> DocBuilder<'a, D>
Methods from Deref<Target = Doc<'a, <D as DocAllocator<'a>>::Doc>>§
pub fn is_nil(&self) -> bool
Sourcepub fn render<W>(&self, width: usize, out: &mut W) -> Result<(), Error>
pub fn render<W>(&self, width: usize, out: &mut W) -> Result<(), Error>
Writes a rendered document to a std::io::Write
object.
Sourcepub fn render_fmt<W>(&self, width: usize, out: &mut W) -> Result<(), Error>
pub fn render_fmt<W>(&self, width: usize, out: &mut W) -> Result<(), Error>
Writes a rendered document to a std::fmt::Write
object.
Sourcepub fn render_raw<W>(
&self,
width: usize,
out: &mut W,
) -> Result<(), <W as Render>::Error>
pub fn render_raw<W>( &self, width: usize, out: &mut W, ) -> Result<(), <W as Render>::Error>
Writes a rendered document to a RenderAnnotated<A>
object.
Sourcepub fn print<'d>(&'d self, width: usize) -> PrettyFmt<'a, 'd, T>
pub fn print<'d>(&'d self, width: usize) -> PrettyFmt<'a, 'd, T>
Returns a value which implements std::fmt::Display
use prettyless::{Doc, BoxDoc};
let doc = BoxDoc::group(
BoxDoc::text("hello").append(Doc::line()).append(Doc::text("world"))
);
assert_eq!(format!("{}", doc.print(80)), "hello world");
Trait Implementations§
Source§impl<'a, D, P> Add<P> for DocBuilder<'a, D>
impl<'a, D, P> Add<P> for DocBuilder<'a, D>
Source§impl<'a, D, P> AddAssign<P> for DocBuilder<'a, D>
impl<'a, D, P> AddAssign<P> for DocBuilder<'a, D>
Source§fn add_assign(&mut self, other: P)
fn add_assign(&mut self, other: P)
+=
operation. Read moreSource§impl<'a, D> Clone for DocBuilder<'a, D>
impl<'a, D> Clone for DocBuilder<'a, D>
Source§fn clone(&self) -> DocBuilder<'a, D>
fn clone(&self) -> DocBuilder<'a, D>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<'a, D> Debug for DocBuilder<'a, D>
impl<'a, D> Debug for DocBuilder<'a, D>
Source§impl<'a, D> Deref for DocBuilder<'a, D>where
D: DocAllocator<'a> + ?Sized,
impl<'a, D> Deref for DocBuilder<'a, D>where
D: DocAllocator<'a> + ?Sized,
Source§impl<'a, D> Pretty<'a, D> for DocBuilder<'a, D>where
D: DocAllocator<'a> + ?Sized,
impl<'a, D> Pretty<'a, D> for DocBuilder<'a, D>where
D: DocAllocator<'a> + ?Sized,
Source§fn pretty(self, _: &'a D) -> DocBuilder<'a, D>
fn pretty(self, _: &'a D) -> DocBuilder<'a, D>
self
into a documentAuto Trait Implementations§
impl<'a, D> Freeze for DocBuilder<'a, D>
impl<'a, D> RefUnwindSafe for DocBuilder<'a, D>
impl<'a, D> Send for DocBuilder<'a, D>
impl<'a, D> Sync for DocBuilder<'a, D>
impl<'a, D> Unpin for DocBuilder<'a, D>
impl<'a, D> UnwindSafe for DocBuilder<'a, D>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more