rustc_ast_pretty/pp/
convenience.rs

1use std::borrow::Cow;
2
3use crate::pp::{
4    BeginToken, BoxMarker, BreakToken, Breaks, IndentStyle, Printer, SIZE_INFINITY, Token,
5};
6
7impl Printer {
8    /// "raw box"
9    pub fn rbox(&mut self, indent: isize, breaks: Breaks) -> BoxMarker {
10        self.scan_begin(BeginToken { indent: IndentStyle::Block { offset: indent }, breaks })
11    }
12
13    /// Inconsistent breaking box
14    pub fn ibox(&mut self, indent: isize) -> BoxMarker {
15        self.rbox(indent, Breaks::Inconsistent)
16    }
17
18    /// Consistent breaking box
19    pub fn cbox(&mut self, indent: isize) -> BoxMarker {
20        self.rbox(indent, Breaks::Consistent)
21    }
22
23    pub fn visual_align(&mut self) -> BoxMarker {
24        self.scan_begin(BeginToken { indent: IndentStyle::Visual, breaks: Breaks::Consistent })
25    }
26
27    pub fn break_offset(&mut self, n: usize, off: isize) {
28        self.scan_break(BreakToken {
29            offset: off,
30            blank_space: n as isize,
31            ..BreakToken::default()
32        });
33    }
34
35    pub fn end(&mut self, b: BoxMarker) {
36        self.scan_end(b)
37    }
38
39    pub fn eof(mut self) -> String {
40        self.scan_eof();
41        self.out
42    }
43
44    pub fn word<S: Into<Cow<'static, str>>>(&mut self, wrd: S) {
45        let string = wrd.into();
46        self.scan_string(string)
47    }
48
49    fn spaces(&mut self, n: usize) {
50        self.break_offset(n, 0)
51    }
52
53    pub fn zerobreak(&mut self) {
54        self.spaces(0)
55    }
56
57    pub fn space(&mut self) {
58        self.spaces(1)
59    }
60
61    pub fn hardbreak(&mut self) {
62        self.spaces(SIZE_INFINITY as usize)
63    }
64
65    pub fn is_beginning_of_line(&self) -> bool {
66        match self.last_token() {
67            Some(last_token) => last_token.is_hardbreak_tok(),
68            None => true,
69        }
70    }
71
72    pub(crate) fn hardbreak_tok_offset(off: isize) -> Token {
73        Token::Break(BreakToken {
74            offset: off,
75            blank_space: SIZE_INFINITY,
76            ..BreakToken::default()
77        })
78    }
79
80    pub fn trailing_comma(&mut self) {
81        self.scan_break(BreakToken { pre_break: Some(','), ..BreakToken::default() });
82    }
83
84    pub fn trailing_comma_or_space(&mut self) {
85        self.scan_break(BreakToken {
86            blank_space: 1,
87            pre_break: Some(','),
88            ..BreakToken::default()
89        });
90    }
91}
92
93impl Token {
94    pub(crate) fn is_hardbreak_tok(&self) -> bool {
95        *self == Printer::hardbreak_tok_offset(0)
96    }
97}