Change pp indent to signed to allow negative indents

This commit is contained in:
David Tolnay 2022-01-21 14:15:14 -08:00
parent 0b7e1baa58
commit 8bdf08fbed
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
4 changed files with 9 additions and 10 deletions

View File

@ -392,7 +392,9 @@ impl Printer {
if size > self.space {
self.print_stack.push(PrintFrame::Broken { indent: self.indent, breaks: token.breaks });
self.indent = match token.indent {
IndentStyle::Block { offset } => (self.indent as isize + offset) as usize,
IndentStyle::Block { offset } => {
usize::try_from(self.indent as isize + offset).unwrap()
}
IndentStyle::Visual => (MARGIN - self.space) as usize,
};
} else {

View File

@ -3,20 +3,17 @@ use std::borrow::Cow;
impl Printer {
/// "raw box"
pub fn rbox(&mut self, indent: usize, breaks: Breaks) {
self.scan_begin(BeginToken {
indent: IndentStyle::Block { offset: indent as isize },
breaks,
})
pub fn rbox(&mut self, indent: isize, breaks: Breaks) {
self.scan_begin(BeginToken { indent: IndentStyle::Block { offset: indent }, breaks })
}
/// Inconsistent breaking box
pub fn ibox(&mut self, indent: usize) {
pub fn ibox(&mut self, indent: isize) {
self.rbox(indent, Breaks::Inconsistent)
}
/// Consistent breaking box
pub fn cbox(&mut self, indent: usize) {
pub fn cbox(&mut self, indent: isize) {
self.rbox(indent, Breaks::Consistent)
}

View File

@ -92,7 +92,7 @@ pub struct State<'a> {
ann: &'a (dyn PpAnn + 'a),
}
crate const INDENT_UNIT: usize = 4;
crate const INDENT_UNIT: isize = 4;
/// Requires you to pass an input filename and reader so that
/// it can scan the input text for comments to copy forward.

View File

@ -139,7 +139,7 @@ impl<'a> PrintState<'a> for State<'a> {
}
}
pub const INDENT_UNIT: usize = 4;
pub const INDENT_UNIT: isize = 4;
/// Requires you to pass an input filename and reader so that
/// it can scan the input text for comments to copy forward.