Skip to content

Commit

Permalink
Restore a visual alignment mode for block comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Jan 31, 2022
1 parent 402f322 commit 125c729
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 7 deletions.
32 changes: 29 additions & 3 deletions compiler/rustc_ast_pretty/src/pp.rs
Expand Up @@ -146,6 +146,22 @@ pub enum Breaks {
Inconsistent,
}

#[derive(Clone, Copy)]
enum IndentStyle {
/// Vertically aligned under whatever column this block begins at.
///
/// fn demo(arg1: usize,
/// arg2: usize);
Visual,
/// Indented relative to the indentation level of the previous line.
///
/// fn demo(
/// arg1: usize,
/// arg2: usize,
/// );
Block { offset: isize },
}

#[derive(Clone, Copy)]
pub struct BreakToken {
offset: isize,
Expand All @@ -154,7 +170,7 @@ pub struct BreakToken {

#[derive(Clone, Copy)]
pub struct BeginToken {
offset: isize,
indent: IndentStyle,
breaks: Breaks,
}

Expand Down Expand Up @@ -377,7 +393,10 @@ impl Printer {
fn print_begin(&mut self, token: BeginToken, size: isize) {
if size > self.space {
self.print_stack.push(PrintFrame::Broken { indent: self.indent, breaks: token.breaks });
self.indent = (self.indent as isize + token.offset) as usize;
self.indent = match token.indent {
IndentStyle::Block { offset } => (self.indent as isize + offset) as usize,
IndentStyle::Visual => (self.margin - self.space) as usize,
};
} else {
self.print_stack.push(PrintFrame::Fits);
}
Expand Down Expand Up @@ -425,7 +444,10 @@ impl Printer {

/// "raw box"
pub fn rbox(&mut self, indent: usize, breaks: Breaks) {
self.scan_begin(BeginToken { offset: indent as isize, breaks })
self.scan_begin(BeginToken {
indent: IndentStyle::Block { offset: indent as isize },
breaks,
})
}

/// Inconsistent breaking box
Expand All @@ -438,6 +460,10 @@ impl Printer {
self.rbox(indent, Breaks::Consistent)
}

pub fn visual_align(&mut self) {
self.scan_begin(BeginToken { indent: IndentStyle::Visual, breaks: Breaks::Consistent });
}

pub fn break_offset(&mut self, n: usize, off: isize) {
self.scan_break(BreakToken { offset: off, blank_space: n as isize })
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast_pretty/src/pprust/state.rs
Expand Up @@ -315,7 +315,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.word(cmnt.lines[0].clone());
self.hardbreak()
} else {
self.ibox(0);
self.visual_align();
for line in &cmnt.lines {
if !line.is_empty() {
self.word(line.clone());
Expand Down
6 changes: 3 additions & 3 deletions src/test/pretty/block-comment-trailing-whitespace2.rs
Expand Up @@ -2,7 +2,7 @@

// pp-exact
fn f() {} /*
The next line should not be indented.
The next line should not be indented.
That one. It shouldn't have been indented.
*/
That one. It shouldn't have been indented.
*/

0 comments on commit 125c729

Please sign in to comment.