Skip to main content

format_doc

Function format_doc 

Source
pub fn format_doc<K: TokenKind + Ord>(
    tree: &Node<K>,
    source: &str,
    style: &Style<K>,
    keep: &[Span],
) -> Result<Doc, FormatError>
Expand description

Builds the Doc that format_keeping (or, with no keep regions, format()) renders, for callers that render it themselves: into an existing buffer or writer, or at several widths.

§Errors

As for format().

§Examples

use fmt_lang::syntax_lang::{Element, Node, Span, Token, TokenKind};
use fmt_lang::{format_doc, Style};

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Word;
impl TokenKind for Word {}

let tree = Node::new(Word, vec![Element::Token(Token::new(Word, Span::new(0, 5)))]);
let doc = format_doc(&tree, "hello", &Style::default(), &[])?;
let mut out = String::new();
doc.render_into(80, &mut out)?;
assert_eq!(out, "hello\n");