Skip to content

Commit 125c729

Browse files
committed
Restore a visual alignment mode for block comments
1 parent 402f322 commit 125c729

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

compiler/rustc_ast_pretty/src/pp.rs

+29-3
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ pub enum Breaks {
146146
Inconsistent,
147147
}
148148

149+
#[derive(Clone, Copy)]
150+
enum IndentStyle {
151+
/// Vertically aligned under whatever column this block begins at.
152+
///
153+
/// fn demo(arg1: usize,
154+
/// arg2: usize);
155+
Visual,
156+
/// Indented relative to the indentation level of the previous line.
157+
///
158+
/// fn demo(
159+
/// arg1: usize,
160+
/// arg2: usize,
161+
/// );
162+
Block { offset: isize },
163+
}
164+
149165
#[derive(Clone, Copy)]
150166
pub struct BreakToken {
151167
offset: isize,
@@ -154,7 +170,7 @@ pub struct BreakToken {
154170

155171
#[derive(Clone, Copy)]
156172
pub struct BeginToken {
157-
offset: isize,
173+
indent: IndentStyle,
158174
breaks: Breaks,
159175
}
160176

@@ -377,7 +393,10 @@ impl Printer {
377393
fn print_begin(&mut self, token: BeginToken, size: isize) {
378394
if size > self.space {
379395
self.print_stack.push(PrintFrame::Broken { indent: self.indent, breaks: token.breaks });
380-
self.indent = (self.indent as isize + token.offset) as usize;
396+
self.indent = match token.indent {
397+
IndentStyle::Block { offset } => (self.indent as isize + offset) as usize,
398+
IndentStyle::Visual => (self.margin - self.space) as usize,
399+
};
381400
} else {
382401
self.print_stack.push(PrintFrame::Fits);
383402
}
@@ -425,7 +444,10 @@ impl Printer {
425444

426445
/// "raw box"
427446
pub fn rbox(&mut self, indent: usize, breaks: Breaks) {
428-
self.scan_begin(BeginToken { offset: indent as isize, breaks })
447+
self.scan_begin(BeginToken {
448+
indent: IndentStyle::Block { offset: indent as isize },
449+
breaks,
450+
})
429451
}
430452

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

463+
pub fn visual_align(&mut self) {
464+
self.scan_begin(BeginToken { indent: IndentStyle::Visual, breaks: Breaks::Consistent });
465+
}
466+
441467
pub fn break_offset(&mut self, n: usize, off: isize) {
442468
self.scan_break(BreakToken { offset: off, blank_space: n as isize })
443469
}

compiler/rustc_ast_pretty/src/pprust/state.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
315315
self.word(cmnt.lines[0].clone());
316316
self.hardbreak()
317317
} else {
318-
self.ibox(0);
318+
self.visual_align();
319319
for line in &cmnt.lines {
320320
if !line.is_empty() {
321321
self.word(line.clone());

src/test/pretty/block-comment-trailing-whitespace2.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
// pp-exact
44
fn f() {} /*
5-
The next line should not be indented.
5+
The next line should not be indented.
66
7-
That one. It shouldn't have been indented.
8-
*/
7+
That one. It shouldn't have been indented.
8+
*/

0 commit comments

Comments
 (0)