mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +00:00
Use indentation
consistently (#12293)
This commit is contained in:
parent
90e9aae3f4
commit
b6545ce5d6
5 changed files with 56 additions and 54 deletions
|
@ -727,11 +727,11 @@ impl<Context> std::fmt::Debug for Indent<'_, Context> {
|
|||
}
|
||||
}
|
||||
|
||||
/// It reduces the indention for the given content depending on the closest [indent] or [align] parent element.
|
||||
/// It reduces the indentation for the given content depending on the closest [indent] or [align] parent element.
|
||||
/// - [align] Undoes the spaces added by [align]
|
||||
/// - [indent] Reduces the indention level by one
|
||||
/// - [indent] Reduces the indentation level by one
|
||||
///
|
||||
/// This is a No-op if the indention level is zero.
|
||||
/// This is a No-op if the indentation level is zero.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -863,7 +863,7 @@ where
|
|||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ## Tab indention
|
||||
/// ## Tab indentation
|
||||
///
|
||||
/// ```
|
||||
/// use std::num::NonZeroU8;
|
||||
|
@ -904,11 +904,11 @@ where
|
|||
///
|
||||
/// - the printer indents the function's `}` by two spaces because it is inside of an `align`.
|
||||
/// - the block `console.log` gets indented by two tabs.
|
||||
/// This is because `align` increases the indention level by one (same as `indent`)
|
||||
/// This is because `align` increases the indentation level by one (same as `indent`)
|
||||
/// if you nest an `indent` inside an `align`.
|
||||
/// Meaning that, `align > ... > indent` results in the same indention as `indent > ... > indent`.
|
||||
/// Meaning that, `align > ... > indent` results in the same indentation as `indent > ... > indent`.
|
||||
///
|
||||
/// ## Spaces indention
|
||||
/// ## Spaces indentation
|
||||
///
|
||||
/// ```
|
||||
/// use std::num::NonZeroU8;
|
||||
|
@ -952,11 +952,11 @@ where
|
|||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// The printing of `align` differs if using spaces as indention sequence *and* it contains an `indent`.
|
||||
/// You can see the difference when comparing the indention of the `console.log(...)` expression to the previous example:
|
||||
/// The printing of `align` differs if using spaces as indentation sequence *and* it contains an `indent`.
|
||||
/// You can see the difference when comparing the indentation of the `console.log(...)` expression to the previous example:
|
||||
///
|
||||
/// - tab indention: Printer indents the expression with two tabs because the `align` increases the indention level.
|
||||
/// - space indention: Printer indents the expression by 4 spaces (one indention level) **and** 2 spaces for the align.
|
||||
/// - tab indentation: Printer indents the expression with two tabs because the `align` increases the indentation level.
|
||||
/// - space indentation: Printer indents the expression by 4 spaces (one indentation level) **and** 2 spaces for the align.
|
||||
pub fn align<Content, Context>(count: u8, content: &Content) -> Align<Context>
|
||||
where
|
||||
Content: Format<Context>,
|
||||
|
@ -992,12 +992,12 @@ impl<Context> std::fmt::Debug for Align<'_, Context> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Inserts a hard line break before and after the content and increases the indention level for the content by one.
|
||||
/// Inserts a hard line break before and after the content and increases the indentation level for the content by one.
|
||||
///
|
||||
/// Block indents indent a block of code, such as in a function body, and therefore insert a line
|
||||
/// break before and after the content.
|
||||
///
|
||||
/// Doesn't create an indention if the passed in content is [`FormatElement.is_empty`].
|
||||
/// Doesn't create an indentation if the passed in content is [`FormatElement.is_empty`].
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -1035,7 +1035,7 @@ pub fn block_indent<Context>(content: &impl Format<Context>) -> BlockIndent<Cont
|
|||
}
|
||||
|
||||
/// Indents the content by inserting a line break before and after the content and increasing
|
||||
/// the indention level for the content by one if the enclosing group doesn't fit on a single line.
|
||||
/// the indentation level for the content by one if the enclosing group doesn't fit on a single line.
|
||||
/// Doesn't change the formatting if the enclosing group fits on a single line.
|
||||
///
|
||||
/// # Examples
|
||||
|
@ -2057,7 +2057,7 @@ impl<Context> std::fmt::Debug for IfGroupBreaks<'_, Context> {
|
|||
/// If you want to indent some content if the enclosing group breaks, use [`indent`].
|
||||
///
|
||||
/// Use [`if_group_breaks`] or [`if_group_fits_on_line`] if the fitting and breaking content differs more than just the
|
||||
/// indention level.
|
||||
/// indentation level.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
|
|
@ -20,7 +20,7 @@ pub enum Tag {
|
|||
StartAlign(Align),
|
||||
EndAlign,
|
||||
|
||||
/// Reduces the indention of the specified content either by one level or to the root, depending on the mode.
|
||||
/// Reduces the indentation of the specified content either by one level or to the root, depending on the mode.
|
||||
/// Reverse operation of `Indent` and can be used to *undo* an `Align` for nested content.
|
||||
StartDedent(DedentMode),
|
||||
EndDedent,
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
|
|
@ -36,7 +36,7 @@ impl<'a> Stylist<'a> {
|
|||
}
|
||||
|
||||
pub fn from_tokens(tokens: &Tokens, locator: &'a Locator<'a>) -> Self {
|
||||
let indentation = detect_indention(tokens, locator);
|
||||
let indentation = detect_indentation(tokens, locator);
|
||||
|
||||
Self {
|
||||
locator,
|
||||
|
@ -60,7 +60,7 @@ fn detect_quote(tokens: &[Token]) -> Quote {
|
|||
Quote::default()
|
||||
}
|
||||
|
||||
fn detect_indention(tokens: &[Token], locator: &Locator) -> Indentation {
|
||||
fn detect_indentation(tokens: &[Token], locator: &Locator) -> Indentation {
|
||||
let indent_range = tokens.iter().find_map(|token| {
|
||||
if matches!(token.kind(), TokenKind::Indent) {
|
||||
Some(token.range())
|
||||
|
@ -204,7 +204,7 @@ x = (
|
|||
let stylist = Stylist::from_tokens(parsed.tokens(), &locator);
|
||||
assert_eq!(stylist.indentation(), &Indentation(" ".to_string()));
|
||||
|
||||
// formfeed indent, see `detect_indention` comment.
|
||||
// formfeed indent, see `detect_indentation` comment.
|
||||
let contents = r"
|
||||
class FormFeedIndent:
|
||||
def __init__(self, a=[]):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue