about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorDavid Tolnay <dtolnay@gmail.com>2022-01-21 01:26:00 -0800
committerDavid Tolnay <dtolnay@gmail.com>2022-01-30 19:50:54 -0800
commit125c729e05e563b1e20b52c1b7203e3ac4feab7c (patch)
tree6f1b059ae6d86777f1c32bafd230b54da97df096 /compiler
parent402f322940caddcf80ee5fb8a5006be50d26ad15 (diff)
downloadrust-125c729e05e563b1e20b52c1b7203e3ac4feab7c.tar.gz
rust-125c729e05e563b1e20b52c1b7203e3ac4feab7c.zip
Restore a visual alignment mode for block comments
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_ast_pretty/src/pp.rs32
-rw-r--r--compiler/rustc_ast_pretty/src/pprust/state.rs2
2 files changed, 30 insertions, 4 deletions
diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs
index 78b51965164..e1f43cb20dc 100644
--- a/compiler/rustc_ast_pretty/src/pp.rs
+++ b/compiler/rustc_ast_pretty/src/pp.rs
@@ -147,6 +147,22 @@ pub enum Breaks {
 }
 
 #[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,
     blank_space: isize,
@@ -154,7 +170,7 @@ pub struct BreakToken {
 
 #[derive(Clone, Copy)]
 pub struct BeginToken {
-    offset: isize,
+    indent: IndentStyle,
     breaks: Breaks,
 }
 
@@ -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);
         }
@@ -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
@@ -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 })
     }
diff --git a/compiler/rustc_ast_pretty/src/pprust/state.rs b/compiler/rustc_ast_pretty/src/pprust/state.rs
index dff85f52b15..b575dc21961 100644
--- a/compiler/rustc_ast_pretty/src/pprust/state.rs
+++ b/compiler/rustc_ast_pretty/src/pprust/state.rs
@@ -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());