Skip to content

Commit 761705e

Browse files
authored
Rollup merge of #93515 - dtolnay:convenience, r=davidtwco
Factor convenience functions out of main printer implementation The pretty printer in rustc_ast_pretty has a section of methods commented "Convenience functions to talk to the printer". This PR pulls those out to a separate module. This leaves pp.rs with only the minimal API that is core to the pretty printing algorithm. I found this separation to be helpful in https://github.com/dtolnay/prettyplease because it makes clear when changes are adding some fundamental new capability to the pretty printer algorithm vs just making it more convenient to call some already existing functionality.
2 parents 2fe9f76 + 2d7ffbb commit 761705e

File tree

2 files changed

+78
-75
lines changed

2 files changed

+78
-75
lines changed

compiler/rustc_ast_pretty/src/pp.rs

+1-75
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
//! methods called `Printer::scan_*`, and the 'PRINT' process is the
133133
//! method called `Printer::print`.
134134
135+
mod convenience;
135136
mod ring;
136137

137138
use ring::RingBuffer;
@@ -186,12 +187,6 @@ pub enum Token {
186187
End,
187188
}
188189

189-
impl Token {
190-
pub fn is_hardbreak_tok(&self) -> bool {
191-
matches!(self, Token::Break(BreakToken { offset: 0, blank_space: SIZE_INFINITY }))
192-
}
193-
}
194-
195190
#[derive(Copy, Clone)]
196191
enum PrintFrame {
197192
Fits,
@@ -441,73 +436,4 @@ impl Printer {
441436
self.out.push_str(string);
442437
self.space -= string.len() as isize;
443438
}
444-
445-
// Convenience functions to talk to the printer.
446-
447-
/// "raw box"
448-
pub fn rbox(&mut self, indent: usize, breaks: Breaks) {
449-
self.scan_begin(BeginToken {
450-
indent: IndentStyle::Block { offset: indent as isize },
451-
breaks,
452-
})
453-
}
454-
455-
/// Inconsistent breaking box
456-
pub fn ibox(&mut self, indent: usize) {
457-
self.rbox(indent, Breaks::Inconsistent)
458-
}
459-
460-
/// Consistent breaking box
461-
pub fn cbox(&mut self, indent: usize) {
462-
self.rbox(indent, Breaks::Consistent)
463-
}
464-
465-
pub fn visual_align(&mut self) {
466-
self.scan_begin(BeginToken { indent: IndentStyle::Visual, breaks: Breaks::Consistent });
467-
}
468-
469-
pub fn break_offset(&mut self, n: usize, off: isize) {
470-
self.scan_break(BreakToken { offset: off, blank_space: n as isize })
471-
}
472-
473-
pub fn end(&mut self) {
474-
self.scan_end()
475-
}
476-
477-
pub fn eof(mut self) -> String {
478-
self.scan_eof();
479-
self.out
480-
}
481-
482-
pub fn word<S: Into<Cow<'static, str>>>(&mut self, wrd: S) {
483-
let string = wrd.into();
484-
self.scan_string(string)
485-
}
486-
487-
fn spaces(&mut self, n: usize) {
488-
self.break_offset(n, 0)
489-
}
490-
491-
pub fn zerobreak(&mut self) {
492-
self.spaces(0)
493-
}
494-
495-
pub fn space(&mut self) {
496-
self.spaces(1)
497-
}
498-
499-
pub fn hardbreak(&mut self) {
500-
self.spaces(SIZE_INFINITY as usize)
501-
}
502-
503-
pub fn is_beginning_of_line(&self) -> bool {
504-
match self.last_token() {
505-
Some(last_token) => last_token.is_hardbreak_tok(),
506-
None => true,
507-
}
508-
}
509-
510-
pub fn hardbreak_tok_offset(off: isize) -> Token {
511-
Token::Break(BreakToken { offset: off, blank_space: SIZE_INFINITY })
512-
}
513439
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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: usize, breaks: Breaks) {
7+
self.scan_begin(BeginToken {
8+
indent: IndentStyle::Block { offset: indent as isize },
9+
breaks,
10+
})
11+
}
12+
13+
/// Inconsistent breaking box
14+
pub fn ibox(&mut self, indent: usize) {
15+
self.rbox(indent, Breaks::Inconsistent)
16+
}
17+
18+
/// Consistent breaking box
19+
pub fn cbox(&mut self, indent: usize) {
20+
self.rbox(indent, Breaks::Consistent)
21+
}
22+
23+
pub fn visual_align(&mut self) {
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 { offset: off, blank_space: n as isize })
29+
}
30+
31+
pub fn end(&mut self) {
32+
self.scan_end()
33+
}
34+
35+
pub fn eof(mut self) -> String {
36+
self.scan_eof();
37+
self.out
38+
}
39+
40+
pub fn word<S: Into<Cow<'static, str>>>(&mut self, wrd: S) {
41+
let string = wrd.into();
42+
self.scan_string(string)
43+
}
44+
45+
fn spaces(&mut self, n: usize) {
46+
self.break_offset(n, 0)
47+
}
48+
49+
pub fn zerobreak(&mut self) {
50+
self.spaces(0)
51+
}
52+
53+
pub fn space(&mut self) {
54+
self.spaces(1)
55+
}
56+
57+
pub fn hardbreak(&mut self) {
58+
self.spaces(SIZE_INFINITY as usize)
59+
}
60+
61+
pub fn is_beginning_of_line(&self) -> bool {
62+
match self.last_token() {
63+
Some(last_token) => last_token.is_hardbreak_tok(),
64+
None => true,
65+
}
66+
}
67+
68+
pub fn hardbreak_tok_offset(off: isize) -> Token {
69+
Token::Break(BreakToken { offset: off, blank_space: SIZE_INFINITY })
70+
}
71+
}
72+
73+
impl Token {
74+
pub fn is_hardbreak_tok(&self) -> bool {
75+
matches!(self, Token::Break(BreakToken { offset: 0, blank_space: SIZE_INFINITY }))
76+
}
77+
}

0 commit comments

Comments
 (0)