mirror of
				https://github.com/astral-sh/ruff.git
				synced 2025-11-03 21:23:45 +00:00 
			
		
		
		
	Remove collapsing space behaviour from Printer (#4782)
				
					
				
			This commit is contained in:
		
							parent
							
								
									5f4bce6d2b
								
							
						
					
					
						commit
						28aad95414
					
				
					 2 changed files with 12 additions and 31 deletions
				
			
		| 
						 | 
					@ -144,6 +144,12 @@ impl std::fmt::Display for DisplayDocument<'_> {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					impl std::fmt::Debug for DisplayDocument<'_> {
 | 
				
			||||||
 | 
					    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
 | 
				
			||||||
 | 
					        std::fmt::Display::fmt(self, f)
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Clone, Debug)]
 | 
					#[derive(Clone, Debug)]
 | 
				
			||||||
struct IrFormatContext<'a> {
 | 
					struct IrFormatContext<'a> {
 | 
				
			||||||
    /// The interned elements that have been printed to this point
 | 
					    /// The interned elements that have been printed to this point
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -91,12 +91,7 @@ impl<'a> Printer<'a> {
 | 
				
			||||||
        let args = stack.top();
 | 
					        let args = stack.top();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        match element {
 | 
					        match element {
 | 
				
			||||||
            FormatElement::Space => {
 | 
					            FormatElement::Space => self.print_text(" ", None),
 | 
				
			||||||
                if self.state.line_width > 0 {
 | 
					 | 
				
			||||||
                    self.state.pending_space = true;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            FormatElement::StaticText { text } => self.print_text(text, None),
 | 
					            FormatElement::StaticText { text } => self.print_text(text, None),
 | 
				
			||||||
            FormatElement::DynamicText { text } => self.print_text(text, None),
 | 
					            FormatElement::DynamicText { text } => self.print_text(text, None),
 | 
				
			||||||
            FormatElement::SourceCodeSlice { slice, .. } => {
 | 
					            FormatElement::SourceCodeSlice { slice, .. } => {
 | 
				
			||||||
| 
						 | 
					@ -107,8 +102,8 @@ impl<'a> Printer<'a> {
 | 
				
			||||||
                if args.mode().is_flat()
 | 
					                if args.mode().is_flat()
 | 
				
			||||||
                    && matches!(line_mode, LineMode::Soft | LineMode::SoftOrSpace)
 | 
					                    && matches!(line_mode, LineMode::Soft | LineMode::SoftOrSpace)
 | 
				
			||||||
                {
 | 
					                {
 | 
				
			||||||
                    if line_mode == &LineMode::SoftOrSpace && self.state.line_width > 0 {
 | 
					                    if line_mode == &LineMode::SoftOrSpace {
 | 
				
			||||||
                        self.state.pending_space = true;
 | 
					                        self.print_text(" ", None);
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                } else if self.state.line_suffixes.has_pending() {
 | 
					                } else if self.state.line_suffixes.has_pending() {
 | 
				
			||||||
                    self.flush_line_suffixes(queue, stack, Some(element));
 | 
					                    self.flush_line_suffixes(queue, stack, Some(element));
 | 
				
			||||||
| 
						 | 
					@ -304,12 +299,6 @@ impl<'a> Printer<'a> {
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Print pending spaces
 | 
					 | 
				
			||||||
        if self.state.pending_space {
 | 
					 | 
				
			||||||
            self.print_str(" ");
 | 
					 | 
				
			||||||
            self.state.pending_space = false;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // Insert source map markers before and after the token
 | 
					        // Insert source map markers before and after the token
 | 
				
			||||||
        //
 | 
					        //
 | 
				
			||||||
        // If the token has source position information the start marker
 | 
					        // If the token has source position information the start marker
 | 
				
			||||||
| 
						 | 
					@ -879,7 +868,6 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        let fits_state = FitsState {
 | 
					        let fits_state = FitsState {
 | 
				
			||||||
            pending_indent: printer.state.pending_indent,
 | 
					            pending_indent: printer.state.pending_indent,
 | 
				
			||||||
            pending_space: printer.state.pending_space,
 | 
					 | 
				
			||||||
            line_width: printer.state.line_width,
 | 
					            line_width: printer.state.line_width,
 | 
				
			||||||
            has_line_suffix: printer.state.line_suffixes.has_pending(),
 | 
					            has_line_suffix: printer.state.line_suffixes.has_pending(),
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
| 
						 | 
					@ -968,18 +956,12 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
 | 
				
			||||||
        let args = self.stack.top();
 | 
					        let args = self.stack.top();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        match element {
 | 
					        match element {
 | 
				
			||||||
            FormatElement::Space => {
 | 
					            FormatElement::Space => return Ok(self.fits_text(" ")),
 | 
				
			||||||
                if self.state.line_width > 0 {
 | 
					 | 
				
			||||||
                    self.state.pending_space = true;
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            FormatElement::Line(line_mode) => {
 | 
					            FormatElement::Line(line_mode) => {
 | 
				
			||||||
                if args.mode().is_flat() {
 | 
					                if args.mode().is_flat() {
 | 
				
			||||||
                    match line_mode {
 | 
					                    match line_mode {
 | 
				
			||||||
                        LineMode::SoftOrSpace => {
 | 
					                        LineMode::SoftOrSpace => return Ok(self.fits_text(" ")),
 | 
				
			||||||
                            self.state.pending_space = true;
 | 
					 | 
				
			||||||
                        }
 | 
					 | 
				
			||||||
                        LineMode::Soft => {}
 | 
					                        LineMode::Soft => {}
 | 
				
			||||||
                        LineMode::Hard | LineMode::Empty => {
 | 
					                        LineMode::Hard | LineMode::Empty => {
 | 
				
			||||||
                            return Ok(if self.must_be_flat {
 | 
					                            return Ok(if self.must_be_flat {
 | 
				
			||||||
| 
						 | 
					@ -1145,10 +1127,6 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
 | 
				
			||||||
        self.state.line_width += indent.level() as usize * self.options().indent_width() as usize
 | 
					        self.state.line_width += indent.level() as usize * self.options().indent_width() as usize
 | 
				
			||||||
            + indent.align() as usize;
 | 
					            + indent.align() as usize;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if self.state.pending_space {
 | 
					 | 
				
			||||||
            self.state.line_width += 1;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for c in text.chars() {
 | 
					        for c in text.chars() {
 | 
				
			||||||
            let char_width = match c {
 | 
					            let char_width = match c {
 | 
				
			||||||
                '\t' => self.options().tab_width as usize,
 | 
					                '\t' => self.options().tab_width as usize,
 | 
				
			||||||
| 
						 | 
					@ -1168,8 +1146,6 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
 | 
				
			||||||
            return Fits::No;
 | 
					            return Fits::No;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.state.pending_space = false;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        Fits::Maybe
 | 
					        Fits::Maybe
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1254,7 +1230,6 @@ impl From<bool> for Fits {
 | 
				
			||||||
#[derive(Debug)]
 | 
					#[derive(Debug)]
 | 
				
			||||||
struct FitsState {
 | 
					struct FitsState {
 | 
				
			||||||
    pending_indent: Indention,
 | 
					    pending_indent: Indention,
 | 
				
			||||||
    pending_space: bool,
 | 
					 | 
				
			||||||
    has_line_suffix: bool,
 | 
					    has_line_suffix: bool,
 | 
				
			||||||
    line_width: usize,
 | 
					    line_width: usize,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -1555,7 +1530,7 @@ two lines`,
 | 
				
			||||||
                text("]")
 | 
					                text("]")
 | 
				
			||||||
            ]),
 | 
					            ]),
 | 
				
			||||||
            text(";"),
 | 
					            text(";"),
 | 
				
			||||||
            &line_suffix(&format_args![space(), text("// trailing"), space()])
 | 
					            &line_suffix(&format_args![space(), text("// trailing")])
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        assert_eq!(printed.as_code(), "[1, 2, 3]; // trailing")
 | 
					        assert_eq!(printed.as_code(), "[1, 2, 3]; // trailing")
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue