Skip to content

Commit 401d60c

Browse files
committed
Compute indent never relative to current column
1 parent e2892b0 commit 401d60c

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

src/algorithm.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub enum Breaks {
1515
#[derive(Clone, Copy)]
1616
pub struct BreakToken {
1717
pub offset: isize,
18-
pub blank_space: isize,
18+
pub blank_space: usize,
1919
}
2020

2121
#[derive(Clone, Copy)]
@@ -38,7 +38,7 @@ pub enum Token {
3838
#[derive(Copy, Clone)]
3939
enum PrintFrame {
4040
Fits,
41-
Broken(isize, Breaks),
41+
Broken(usize, Breaks),
4242
}
4343

4444
pub const SIZE_INFINITY: isize = 0xffff;
@@ -63,8 +63,10 @@ pub struct Printer {
6363
scan_stack: VecDeque<usize>,
6464
// Stack of blocks-in-progress being flushed by print
6565
print_stack: Vec<PrintFrame>,
66+
// Level of indentation of current line
67+
indent: usize,
6668
// Buffered indentation to avoid writing trailing whitespace
67-
pending_indentation: isize,
69+
pending_indentation: usize,
6870
}
6971

7072
#[derive(Clone)]
@@ -85,6 +87,7 @@ impl Printer {
8587
right_total: 0,
8688
scan_stack: VecDeque::new(),
8789
print_stack: Vec::new(),
90+
indent: 0,
8891
pending_indentation: 0,
8992
}
9093
}
@@ -135,7 +138,7 @@ impl Printer {
135138
size: -self.right_total,
136139
});
137140
self.scan_stack.push_back(right);
138-
self.right_total += token.blank_space;
141+
self.right_total += token.blank_space as isize;
139142
}
140143

141144
pub fn scan_string(&mut self, string: Cow<'static, str>) {
@@ -172,7 +175,7 @@ impl Printer {
172175
self.print_string(string);
173176
}
174177
Token::Break(token) => {
175-
self.left_total += token.blank_space;
178+
self.left_total += token.blank_space as isize;
176179
self.print_break(token, left.size);
177180
}
178181
Token::Begin(token) => self.print_begin(token, left.size),
@@ -220,42 +223,41 @@ impl Printer {
220223

221224
fn print_begin(&mut self, token: BeginToken, size: isize) {
222225
if size > self.space {
223-
let col = self.margin - self.space + token.offset as isize;
224-
self.print_stack.push(PrintFrame::Broken(col, token.breaks));
226+
self.print_stack
227+
.push(PrintFrame::Broken(self.indent, token.breaks));
228+
self.indent += token.offset;
225229
} else {
226230
self.print_stack.push(PrintFrame::Fits);
227231
}
228232
}
229233

230234
fn print_end(&mut self) {
231-
self.print_stack.pop().unwrap();
235+
if let PrintFrame::Broken(indent, ..) = self.print_stack.pop().unwrap() {
236+
self.indent = indent;
237+
}
232238
}
233239

234240
fn print_break(&mut self, token: BreakToken, size: isize) {
235-
if let Some(offset) = match self.get_top() {
236-
PrintFrame::Fits => None,
237-
PrintFrame::Broken(offset, Breaks::Consistent) => Some(offset),
238-
PrintFrame::Broken(offset, Breaks::Inconsistent) => {
239-
if size > self.space {
240-
Some(offset)
241-
} else {
242-
None
243-
}
244-
}
245-
} {
246-
self.out.push('\n');
247-
self.pending_indentation = offset + token.offset;
248-
self.space = self.margin - self.pending_indentation;
249-
} else {
241+
let fits = match self.get_top() {
242+
PrintFrame::Fits => true,
243+
PrintFrame::Broken(.., Breaks::Consistent) => false,
244+
PrintFrame::Broken(.., Breaks::Inconsistent) => size <= self.space,
245+
};
246+
if fits {
250247
self.pending_indentation += token.blank_space;
251-
self.space -= token.blank_space;
248+
self.space -= token.blank_space as isize;
249+
} else {
250+
self.out.push('\n');
251+
let indent = self.indent as isize + token.offset;
252+
self.pending_indentation = indent as usize;
253+
self.space = self.margin - indent;
252254
}
253255
}
254256

255257
fn print_string(&mut self, string: Cow<'static, str>) {
256-
self.out.reserve(self.pending_indentation as usize);
258+
self.out.reserve(self.pending_indentation);
257259
self.out
258-
.extend(iter::repeat(' ').take(self.pending_indentation as usize));
260+
.extend(iter::repeat(' ').take(self.pending_indentation));
259261
self.pending_indentation = 0;
260262

261263
self.out.push_str(&string);

src/convenience.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ impl Printer {
2525
pub fn break_offset(&mut self, n: usize, off: isize) {
2626
self.scan_break(BreakToken {
2727
offset: off,
28-
blank_space: n as isize,
28+
blank_space: n,
2929
});
3030
}
3131

@@ -61,7 +61,7 @@ impl Printer {
6161
pub fn hardbreak_tok_offset(off: isize) -> Token {
6262
Token::Break(BreakToken {
6363
offset: off,
64-
blank_space: algorithm::SIZE_INFINITY,
64+
blank_space: algorithm::SIZE_INFINITY as usize,
6565
})
6666
}
6767
}

0 commit comments

Comments
 (0)