Eliminate offset number from Fits frames

PrintStackElems with pbreak=PrintStackBreak::Fits always carried a
meaningless value offset=0. We can combine the two types PrintStackElem
+ PrintStackBreak into one PrintFrame enum that stores offset only for
Broken frames.
This commit is contained in:
David Tolnay 2022-01-19 18:51:07 -08:00
parent 65dd67096e
commit 9e794d7de3
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -176,15 +176,9 @@ impl Token {
} }
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
enum PrintStackBreak { enum PrintFrame {
Fits, Fits,
Broken(Breaks), Broken { offset: isize, breaks: Breaks },
}
#[derive(Copy, Clone)]
struct PrintStackElem {
offset: isize,
pbreak: PrintStackBreak,
} }
const SIZE_INFINITY: isize = 0xffff; const SIZE_INFINITY: isize = 0xffff;
@ -209,7 +203,7 @@ pub struct Printer {
/// advancing. /// advancing.
scan_stack: VecDeque<usize>, scan_stack: VecDeque<usize>,
/// Stack of blocks-in-progress being flushed by print /// Stack of blocks-in-progress being flushed by print
print_stack: Vec<PrintStackElem>, print_stack: Vec<PrintFrame>,
/// Buffered indentation to avoid writing trailing whitespace /// Buffered indentation to avoid writing trailing whitespace
pending_indentation: isize, pending_indentation: isize,
/// The token most recently popped from the left boundary of the /// The token most recently popped from the left boundary of the
@ -380,21 +374,19 @@ impl Printer {
self.pending_indentation += amount; self.pending_indentation += amount;
} }
fn get_top(&self) -> PrintStackElem { fn get_top(&self) -> PrintFrame {
*self.print_stack.last().unwrap_or({ *self
&PrintStackElem { offset: 0, pbreak: PrintStackBreak::Broken(Breaks::Inconsistent) } .print_stack
}) .last()
.unwrap_or(&PrintFrame::Broken { offset: 0, breaks: Breaks::Inconsistent })
} }
fn print_begin(&mut self, token: BeginToken, size: isize) { fn print_begin(&mut self, token: BeginToken, size: isize) {
if size > self.space { if size > self.space {
let col = self.margin - self.space + token.offset; let col = self.margin - self.space + token.offset;
self.print_stack.push(PrintStackElem { self.print_stack.push(PrintFrame::Broken { offset: col, breaks: token.breaks });
offset: col,
pbreak: PrintStackBreak::Broken(token.breaks),
});
} else { } else {
self.print_stack.push(PrintStackElem { offset: 0, pbreak: PrintStackBreak::Fits }); self.print_stack.push(PrintFrame::Fits);
} }
} }
@ -403,20 +395,19 @@ impl Printer {
} }
fn print_break(&mut self, token: BreakToken, size: isize) { fn print_break(&mut self, token: BreakToken, size: isize) {
let top = self.get_top(); match self.get_top() {
match top.pbreak { PrintFrame::Fits => {
PrintStackBreak::Fits => {
self.space -= token.blank_space;
self.indent(token.blank_space); self.indent(token.blank_space);
self.space -= token.blank_space;
} }
PrintStackBreak::Broken(Breaks::Consistent) => { PrintFrame::Broken { offset, breaks: Breaks::Consistent } => {
self.print_newline(top.offset + token.offset); self.print_newline(offset + token.offset);
self.space = self.margin - (top.offset + token.offset); self.space = self.margin - (offset + token.offset);
} }
PrintStackBreak::Broken(Breaks::Inconsistent) => { PrintFrame::Broken { offset, breaks: Breaks::Inconsistent } => {
if size > self.space { if size > self.space {
self.print_newline(top.offset + token.offset); self.print_newline(offset + token.offset);
self.space = self.margin - (top.offset + token.offset); self.space = self.margin - (offset + token.offset);
} else { } else {
self.indent(token.blank_space); self.indent(token.blank_space);
self.space -= token.blank_space; self.space -= token.blank_space;