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
|
@ -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]
|
/// - [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
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
@ -863,7 +863,7 @@ where
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ## Tab indention
|
/// ## Tab indentation
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use std::num::NonZeroU8;
|
/// 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 printer indents the function's `}` by two spaces because it is inside of an `align`.
|
||||||
/// - the block `console.log` gets indented by two tabs.
|
/// - 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`.
|
/// 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;
|
/// 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`.
|
/// The printing of `align` differs if using spaces as indentation sequence *and* it contains an `indent`.
|
||||||
/// You can see the difference when comparing the indention of the `console.log(...)` expression to the previous example:
|
/// 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.
|
/// - tab indentation: Printer indents the expression with two tabs because the `align` increases the indentation level.
|
||||||
/// - space indention: Printer indents the expression by 4 spaces (one indention level) **and** 2 spaces for the align.
|
/// - 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>
|
pub fn align<Content, Context>(count: u8, content: &Content) -> Align<Context>
|
||||||
where
|
where
|
||||||
Content: Format<Context>,
|
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
|
/// Block indents indent a block of code, such as in a function body, and therefore insert a line
|
||||||
/// break before and after the content.
|
/// 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
|
/// # 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
|
/// 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.
|
/// Doesn't change the formatting if the enclosing group fits on a single line.
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # 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`].
|
/// 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
|
/// 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
|
/// # Examples
|
||||||
///
|
///
|
||||||
|
|
|
@ -20,7 +20,7 @@ pub enum Tag {
|
||||||
StartAlign(Align),
|
StartAlign(Align),
|
||||||
EndAlign,
|
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.
|
/// Reverse operation of `Indent` and can be used to *undo* an `Align` for nested content.
|
||||||
StartDedent(DedentMode),
|
StartDedent(DedentMode),
|
||||||
EndDedent,
|
EndDedent,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
use crate::format_element::tag::TagKind;
|
use crate::format_element::tag::TagKind;
|
||||||
use crate::format_element::PrintMode;
|
use crate::format_element::PrintMode;
|
||||||
use crate::printer::stack::{Stack, StackedStack};
|
use crate::printer::stack::{Stack, StackedStack};
|
||||||
use crate::printer::{Indention, MeasureMode};
|
use crate::printer::{Indentation, MeasureMode};
|
||||||
use crate::{IndentStyle, InvalidDocumentError, PrintError, PrintResult};
|
use crate::{IndentStyle, InvalidDocumentError, PrintError, PrintResult};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::num::NonZeroU8;
|
use std::num::NonZeroU8;
|
||||||
|
@ -26,13 +26,13 @@ pub(super) struct StackFrame {
|
||||||
/// data structures. Such structures should be stored on the [`PrinterState`] instead.
|
/// data structures. Such structures should be stored on the [`PrinterState`] instead.
|
||||||
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
pub(super) struct PrintElementArgs {
|
pub(super) struct PrintElementArgs {
|
||||||
indent: Indention,
|
indent: Indentation,
|
||||||
mode: PrintMode,
|
mode: PrintMode,
|
||||||
measure_mode: MeasureMode,
|
measure_mode: MeasureMode,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PrintElementArgs {
|
impl PrintElementArgs {
|
||||||
pub(crate) fn new(indent: Indention) -> Self {
|
pub(crate) fn new(indent: Indentation) -> Self {
|
||||||
Self {
|
Self {
|
||||||
indent,
|
indent,
|
||||||
..Self::default()
|
..Self::default()
|
||||||
|
@ -47,7 +47,7 @@ impl PrintElementArgs {
|
||||||
self.measure_mode
|
self.measure_mode
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(super) fn indention(self) -> Indention {
|
pub(super) fn indentation(self) -> Indentation {
|
||||||
self.indent
|
self.indent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ impl PrintElementArgs {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn reset_indent(mut self) -> Self {
|
pub(crate) fn reset_indent(mut self) -> Self {
|
||||||
self.indent = Indention::default();
|
self.indent = Indentation::default();
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -85,7 +85,7 @@ impl PrintElementArgs {
|
||||||
impl Default for PrintElementArgs {
|
impl Default for PrintElementArgs {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
indent: Indention::Level(0),
|
indent: Indentation::Level(0),
|
||||||
mode: PrintMode::Expanded,
|
mode: PrintMode::Expanded,
|
||||||
measure_mode: MeasureMode::FirstLine,
|
measure_mode: MeasureMode::FirstLine,
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ impl<'a> Printer<'a> {
|
||||||
document: &'a Document,
|
document: &'a Document,
|
||||||
indent: u16,
|
indent: u16,
|
||||||
) -> PrintResult<Printed> {
|
) -> PrintResult<Printed> {
|
||||||
let indentation = Indention::Level(indent);
|
let indentation = Indentation::Level(indent);
|
||||||
self.state.pending_indent = indentation;
|
self.state.pending_indent = indentation;
|
||||||
|
|
||||||
let mut stack = PrintCallStack::new(PrintElementArgs::new(indentation));
|
let mut stack = PrintCallStack::new(PrintElementArgs::new(indentation));
|
||||||
|
@ -135,7 +135,7 @@ impl<'a> Printer<'a> {
|
||||||
self.print_char('\n');
|
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>,
|
pending_source_position: Option<TextSize>,
|
||||||
|
|
||||||
/// The current indentation that should be written before the next text.
|
/// 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.
|
/// 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.
|
/// This is used to avoid re-measuring the same content multiple times.
|
||||||
|
@ -943,37 +943,37 @@ impl GroupModes {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
|
||||||
enum Indention {
|
enum Indentation {
|
||||||
/// Indent the content by `count` levels by using the indention sequence specified by the printer options.
|
/// Indent the content by `count` levels by using the indentation sequence specified by the printer options.
|
||||||
Level(u16),
|
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 },
|
Align { level: u16, align: NonZeroU8 },
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Indention {
|
impl Indentation {
|
||||||
const fn is_empty(self) -> bool {
|
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 {
|
const fn new() -> Self {
|
||||||
Indention::Level(0)
|
Indentation::Level(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the indention level
|
/// Returns the indentation level
|
||||||
fn level(self) -> u16 {
|
fn level(self) -> u16 {
|
||||||
match self {
|
match self {
|
||||||
Indention::Level(count) => count,
|
Indentation::Level(count) => count,
|
||||||
Indention::Align { level: indent, .. } => indent,
|
Indentation::Align { level: indent, .. } => indent,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of trailing align spaces or 0 if none
|
/// Returns the number of trailing align spaces or 0 if none
|
||||||
fn align(self) -> u8 {
|
fn align(self) -> u8 {
|
||||||
match self {
|
match self {
|
||||||
Indention::Level(_) => 0,
|
Indentation::Level(_) => 0,
|
||||||
Indention::Align { align, .. } => align.into(),
|
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.
|
/// Keeps any the current value is [`Indent::Align`] and increments the level by one.
|
||||||
fn increment_level(self, indent_style: IndentStyle) -> Self {
|
fn increment_level(self, indent_style: IndentStyle) -> Self {
|
||||||
match 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
|
// Increase the indent AND convert the align to an indent
|
||||||
Indention::Align { level, .. } if indent_style.is_tab() => Indention::Level(level + 2),
|
Indentation::Align { level, .. } if indent_style.is_tab() => {
|
||||||
Indention::Align {
|
Indentation::Level(level + 2)
|
||||||
|
}
|
||||||
|
Indentation::Align {
|
||||||
level: indent,
|
level: indent,
|
||||||
align,
|
align,
|
||||||
} => Indention::Align {
|
} => Indentation::Align {
|
||||||
level: indent + 1,
|
level: indent + 1,
|
||||||
align,
|
align,
|
||||||
},
|
},
|
||||||
|
@ -1005,23 +1007,23 @@ impl Indention {
|
||||||
/// No-op if the level is already zero.
|
/// No-op if the level is already zero.
|
||||||
fn decrement(self) -> Self {
|
fn decrement(self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Indention::Level(level) => Indention::Level(level.saturating_sub(1)),
|
Indentation::Level(level) => Indentation::Level(level.saturating_sub(1)),
|
||||||
Indention::Align { level, .. } => Indention::Level(level),
|
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`].
|
/// It increments the `level` value if the current value is [`Indent::IndentAlign`].
|
||||||
fn set_align(self, count: NonZeroU8) -> Self {
|
fn set_align(self, count: NonZeroU8) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Indention::Level(indent_count) => Indention::Align {
|
Indentation::Level(indent_count) => Indentation::Align {
|
||||||
level: indent_count,
|
level: indent_count,
|
||||||
align: count,
|
align: count,
|
||||||
},
|
},
|
||||||
|
|
||||||
// Convert the existing align to an indent
|
// Convert the existing align to an indent
|
||||||
Indention::Align { level: indent, .. } => Indention::Align {
|
Indentation::Align { level: indent, .. } => Indentation::Align {
|
||||||
level: indent + 1,
|
level: indent + 1,
|
||||||
align: count,
|
align: count,
|
||||||
},
|
},
|
||||||
|
@ -1029,9 +1031,9 @@ impl Indention {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Indention {
|
impl Default for Indentation {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Indention::new()
|
Indentation::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1191,7 +1193,7 @@ impl<'a, 'print> FitsMeasurer<'a, 'print> {
|
||||||
MeasureMode::AllLines | MeasureMode::AllLinesAllowTextOverflow => {
|
MeasureMode::AllLines | MeasureMode::AllLinesAllowTextOverflow => {
|
||||||
// Continue measuring on the next line
|
// Continue measuring on the next line
|
||||||
self.state.line_width = 0;
|
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)
|
// to ensure any trailing comments (that, unfortunately, are attached to the statement and not the expression)
|
||||||
// fit too.
|
// fit too.
|
||||||
self.state.line_width = 0;
|
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));
|
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
|
/// State used when measuring if a group fits on a single line
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct FitsState {
|
struct FitsState {
|
||||||
pending_indent: Indention,
|
pending_indent: Indentation,
|
||||||
has_line_suffix: bool,
|
has_line_suffix: bool,
|
||||||
line_width: u32,
|
line_width: u32,
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ impl<'a> Stylist<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_tokens(tokens: &Tokens, locator: &'a Locator<'a>) -> Self {
|
pub fn from_tokens(tokens: &Tokens, locator: &'a Locator<'a>) -> Self {
|
||||||
let indentation = detect_indention(tokens, locator);
|
let indentation = detect_indentation(tokens, locator);
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
locator,
|
locator,
|
||||||
|
@ -60,7 +60,7 @@ fn detect_quote(tokens: &[Token]) -> Quote {
|
||||||
Quote::default()
|
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| {
|
let indent_range = tokens.iter().find_map(|token| {
|
||||||
if matches!(token.kind(), TokenKind::Indent) {
|
if matches!(token.kind(), TokenKind::Indent) {
|
||||||
Some(token.range())
|
Some(token.range())
|
||||||
|
@ -204,7 +204,7 @@ x = (
|
||||||
let stylist = Stylist::from_tokens(parsed.tokens(), &locator);
|
let stylist = Stylist::from_tokens(parsed.tokens(), &locator);
|
||||||
assert_eq!(stylist.indentation(), &Indentation(" ".to_string()));
|
assert_eq!(stylist.indentation(), &Indentation(" ".to_string()));
|
||||||
|
|
||||||
// formfeed indent, see `detect_indention` comment.
|
// formfeed indent, see `detect_indentation` comment.
|
||||||
let contents = r"
|
let contents = r"
|
||||||
class FormFeedIndent:
|
class FormFeedIndent:
|
||||||
def __init__(self, a=[]):
|
def __init__(self, a=[]):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue