mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-03 15:15:33 +00:00
Use indentation
consistently (#12293)
This commit is contained in:
parent
90e9aae3f4
commit
b6545ce5d6
5 changed files with 56 additions and 54 deletions
|
@ -1,7 +1,7 @@
|
|||
use crate::format_element::tag::TagKind;
|
||||
use crate::format_element::PrintMode;
|
||||
use crate::printer::stack::{Stack, StackedStack};
|
||||
use crate::printer::{Indention, MeasureMode};
|
||||
use crate::printer::{Indentation, MeasureMode};
|
||||
use crate::{IndentStyle, InvalidDocumentError, PrintError, PrintResult};
|
||||
use std::fmt::Debug;
|
||||
use std::num::NonZeroU8;
|
||||
|
@ -26,13 +26,13 @@ pub(super) struct StackFrame {
|
|||
/// data structures. Such structures should be stored on the [`PrinterState`] instead.
|
||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||
pub(super) struct PrintElementArgs {
|
||||
indent: Indention,
|
||||
indent: Indentation,
|
||||
mode: PrintMode,
|
||||
measure_mode: MeasureMode,
|
||||
}
|
||||
|
||||
impl PrintElementArgs {
|
||||
pub(crate) fn new(indent: Indention) -> Self {
|
||||
pub(crate) fn new(indent: Indentation) -> Self {
|
||||
Self {
|
||||
indent,
|
||||
..Self::default()
|
||||
|
@ -47,7 +47,7 @@ impl PrintElementArgs {
|
|||
self.measure_mode
|
||||
}
|
||||
|
||||
pub(super) fn indention(self) -> Indention {
|
||||
pub(super) fn indentation(self) -> Indentation {
|
||||
self.indent
|
||||
}
|
||||
|
||||
|
@ -62,7 +62,7 @@ impl PrintElementArgs {
|
|||
}
|
||||
|
||||
pub(crate) fn reset_indent(mut self) -> Self {
|
||||
self.indent = Indention::default();
|
||||
self.indent = Indentation::default();
|
||||
self
|
||||
}
|
||||
|
||||
|
@ -85,7 +85,7 @@ impl PrintElementArgs {
|
|||
impl Default for PrintElementArgs {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
indent: Indention::Level(0),
|
||||
indent: Indentation::Level(0),
|
||||
mode: PrintMode::Expanded,
|
||||
measure_mode: MeasureMode::FirstLine,
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ impl<'a> Printer<'a> {
|
|||
document: &'a Document,
|
||||
indent: u16,
|
||||
) -> PrintResult<Printed> {
|
||||
let indentation = Indention::Level(indent);
|
||||
let indentation = Indentation::Level(indent);
|
||||
self.state.pending_indent = indentation;
|
||||
|
||||
let mut stack = PrintCallStack::new(PrintElementArgs::new(indentation));
|
||||
|
@ -135,7 +135,7 @@ impl<'a> Printer<'a> {
|
|||
self.print_char('\n');
|
||||
}
|
||||
|
||||
self.state.pending_indent = args.indention();
|
||||
self.state.pending_indent = args.indentation();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -883,7 +883,7 @@ struct PrinterState<'a> {
|
|||
pending_source_position: Option<TextSize>,
|
||||
|
||||
/// The current indentation that should be written before the next text.
|
||||
pending_indent: Indention,
|
||||
pending_indent: Indentation,
|
||||
|
||||
/// Caches if the code up to the next newline has been measured to fit on a single line.
|
||||
/// This is used to avoid re-measuring the same content multiple times.
|
||||
|
@ -943,37 +943,37 @@ impl GroupModes {
|
|||
}
|
||||
|
||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||
enum Indention {
|
||||
/// Indent the content by `count` levels by using the indention sequence specified by the printer options.
|
||||
enum Indentation {
|
||||
/// Indent the content by `count` levels by using the indentation sequence specified by the printer options.
|
||||
Level(u16),
|
||||
|
||||
/// Indent the content by n-`level`s using the indention sequence specified by the printer options and `align` spaces.
|
||||
/// Indent the content by n-`level`s using the indentation sequence specified by the printer options and `align` spaces.
|
||||
Align { level: u16, align: NonZeroU8 },
|
||||
}
|
||||
|
||||
impl Indention {
|
||||
impl Indentation {
|
||||
const fn is_empty(self) -> bool {
|
||||
matches!(self, Indention::Level(0))
|
||||
matches!(self, Indentation::Level(0))
|
||||
}
|
||||
|
||||
/// Creates a new indention level with a zero-indent.
|
||||
/// Creates a new indentation level with a zero-indent.
|
||||
const fn new() -> Self {
|
||||
Indention::Level(0)
|
||||
Indentation::Level(0)
|
||||
}
|
||||
|
||||
/// Returns the indention level
|
||||
/// Returns the indentation level
|
||||
fn level(self) -> u16 {
|
||||
match self {
|
||||
Indention::Level(count) => count,
|
||||
Indention::Align { level: indent, .. } => indent,
|
||||
Indentation::Level(count) => count,
|
||||
Indentation::Align { level: indent, .. } => indent,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the number of trailing align spaces or 0 if none
|
||||
fn align(self) -> u8 {
|
||||
match self {
|
||||
Indention::Level(_) => 0,
|
||||
Indention::Align { align, .. } => align.into(),
|
||||
Indentation::Level(_) => 0,
|
||||
Indentation::Align { align, .. } => align.into(),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -985,13 +985,15 @@ impl Indention {
|
|||
/// Keeps any the current value is [`Indent::Align`] and increments the level by one.
|
||||
fn increment_level(self, indent_style: IndentStyle) -> Self {
|
||||
match self {
|
||||
Indention::Level(count) => Indention::Level(count + 1),
|
||||
Indentation::Level(count) => Indentation::Level(count + 1),
|
||||
// Increase the indent AND convert the align to an indent
|
||||
Indention::Align { level, .. } if indent_style.is_tab() => Indention::Level(level + 2),
|
||||
Indention::Align {
|
||||
Indentation::Align { level, .. } if indent_style.is_tab() => {
|
||||
Indentation::Level(level + 2)
|
||||
}
|
||||
Indentation::Align {
|
||||
level: indent,
|
||||
align,
|
||||
} => Indention::Align {
|
||||
} => Indentation::Align {
|
||||
level: indent + 1,
|
||||
align,
|
||||
},
|
||||
|
@ -1005,23 +1007,23 @@ impl Indention {
|
|||
/// No-op if the level is already zero.
|
||||
fn decrement(self) -> Self {
|
||||
match self {
|
||||
Indention::Level(level) => Indention::Level(level.saturating_sub(1)),
|
||||
Indention::Align { level, .. } => Indention::Level(level),
|
||||
Indentation::Level(level) => Indentation::Level(level.saturating_sub(1)),
|
||||
Indentation::Align { level, .. } => Indentation::Level(level),
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds an `align` of `count` spaces to the current indention.
|
||||
/// Adds an `align` of `count` spaces to the current indentation.
|
||||
///
|
||||
/// It increments the `level` value if the current value is [`Indent::IndentAlign`].
|
||||
fn set_align(self, count: NonZeroU8) -> Self {
|
||||
match self {
|
||||
Indention::Level(indent_count) => Indention::Align {
|
||||
Indentation::Level(indent_count) => Indentation::Align {
|
||||
level: indent_count,
|
||||
align: count,
|
||||
},
|
||||
|
||||
// Convert the existing align to an indent
|
||||
Indention::Align { level: indent, .. } => Indention::Align {
|
||||
Indentation::Align { level: indent, .. } => Indentation::Align {
|
||||
level: indent + 1,
|
||||
align: count,
|
||||
},
|
||||
|
@ -1029,9 +1031,9 @@ impl Indention {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for Indention {
|
||||
impl Default for Indentation {
|
||||
fn default() -> Self {
|
||||
Indention::new()
|
||||
Indentation::new()
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1191,7 +1193,7 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
|
|||
MeasureMode::AllLines | MeasureMode::AllLinesAllowTextOverflow => {
|
||||
// Continue measuring on the next line
|
||||
self.state.line_width = 0;
|
||||
self.state.pending_indent = args.indention();
|
||||
self.state.pending_indent = args.indentation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1302,7 +1304,7 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
|
|||
// to ensure any trailing comments (that, unfortunately, are attached to the statement and not the expression)
|
||||
// fit too.
|
||||
self.state.line_width = 0;
|
||||
self.state.pending_indent = unindented.indention();
|
||||
self.state.pending_indent = unindented.indentation();
|
||||
|
||||
return Ok(self.fits_text(Text::Token(")"), unindented));
|
||||
}
|
||||
|
@ -1615,7 +1617,7 @@ impl From<bool> for Fits {
|
|||
/// State used when measuring if a group fits on a single line
|
||||
#[derive(Debug)]
|
||||
struct FitsState {
|
||||
pending_indent: Indention,
|
||||
pending_indent: Indentation,
|
||||
has_line_suffix: bool,
|
||||
line_width: u32,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue