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