forge_fmt/pp/
helpers.rs

1use super::{Printer, Token};
2use std::borrow::Cow;
3
4impl Printer {
5    pub fn word_space(&mut self, w: impl Into<Cow<'static, str>>) {
6        self.word(w);
7        self.space();
8    }
9
10    /// Adds a new hardbreak if not at the beginning of the line.
11    /// If there was a buffered break token, replaces it (ensures hardbreak) keeping the offset.
12    pub fn hardbreak_if_not_bol(&mut self) {
13        if !self.is_bol_or_only_ind() {
14            if let Some(Token::Break(last)) = self.last_token_still_buffered()
15                && last.offset != 0
16            {
17                self.replace_last_token_still_buffered(Self::hardbreak_tok_offset(last.offset));
18                return;
19            }
20            self.hardbreak();
21        }
22    }
23
24    pub fn space_if_not_bol(&mut self) {
25        if !self.is_bol_or_only_ind() {
26            self.space();
27        }
28    }
29
30    pub fn nbsp(&mut self) {
31        self.word(" ");
32    }
33
34    pub fn space_or_nbsp(&mut self, breaks: bool) {
35        if breaks {
36            self.space();
37        } else {
38            self.nbsp();
39        }
40    }
41
42    pub fn word_nbsp(&mut self, w: impl Into<Cow<'static, str>>) {
43        self.word(w);
44        self.nbsp();
45    }
46}