mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-23 04:55:09 +00:00
Implement complex literal formatting (#3186)
This commit is contained in:
parent
ac79bf4ee9
commit
32d165b7ad
3 changed files with 48 additions and 12 deletions
|
@ -13,7 +13,7 @@ use crate::cst::{
|
||||||
Arguments, Boolop, Cmpop, Comprehension, Expr, ExprKind, Keyword, Operator, Unaryop,
|
Arguments, Boolop, Cmpop, Comprehension, Expr, ExprKind, Keyword, Operator, Unaryop,
|
||||||
};
|
};
|
||||||
use crate::format::helpers::{is_self_closing, is_simple_power, is_simple_slice};
|
use crate::format::helpers::{is_self_closing, is_simple_power, is_simple_slice};
|
||||||
use crate::format::numbers::{float_literal, int_literal};
|
use crate::format::numbers::{complex_literal, float_literal, int_literal};
|
||||||
use crate::format::strings::string_literal;
|
use crate::format::strings::string_literal;
|
||||||
use crate::shared_traits::AsFormat;
|
use crate::shared_traits::AsFormat;
|
||||||
use crate::trivia::{Parenthesize, Relationship, TriviaKind};
|
use crate::trivia::{Parenthesize, Relationship, TriviaKind};
|
||||||
|
@ -645,6 +645,7 @@ fn format_constant(
|
||||||
_kind: Option<&str>,
|
_kind: Option<&str>,
|
||||||
) -> FormatResult<()> {
|
) -> FormatResult<()> {
|
||||||
match constant {
|
match constant {
|
||||||
|
Constant::Ellipsis => write!(f, [text("...")])?,
|
||||||
Constant::None => write!(f, [text("None")])?,
|
Constant::None => write!(f, [text("None")])?,
|
||||||
Constant::Bool(value) => {
|
Constant::Bool(value) => {
|
||||||
if *value {
|
if *value {
|
||||||
|
@ -655,8 +656,10 @@ fn format_constant(
|
||||||
}
|
}
|
||||||
Constant::Int(_) => write!(f, [int_literal(Range::from_located(expr))])?,
|
Constant::Int(_) => write!(f, [int_literal(Range::from_located(expr))])?,
|
||||||
Constant::Float(_) => write!(f, [float_literal(Range::from_located(expr))])?,
|
Constant::Float(_) => write!(f, [float_literal(Range::from_located(expr))])?,
|
||||||
Constant::Str(_) | Constant::Bytes(_) => write!(f, [string_literal(expr)])?,
|
Constant::Str(_) => write!(f, [string_literal(expr)])?,
|
||||||
_ => write!(f, [literal(Range::from_located(expr))])?,
|
Constant::Bytes(_) => write!(f, [string_literal(expr)])?,
|
||||||
|
Constant::Complex { .. } => write!(f, [complex_literal(Range::from_located(expr))])?,
|
||||||
|
Constant::Tuple(_) => unreachable!("Constant::Tuple should be handled by format_tuple"),
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -162,3 +162,39 @@ impl Format<ASTFormatContext<'_>> for IntLiteral {
|
||||||
pub const fn int_literal(range: Range) -> IntLiteral {
|
pub const fn int_literal(range: Range) -> IntLiteral {
|
||||||
IntLiteral { range }
|
IntLiteral { range }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
|
||||||
|
pub struct ComplexLiteral {
|
||||||
|
range: Range,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Format<ASTFormatContext<'_>> for ComplexLiteral {
|
||||||
|
fn fmt(&self, f: &mut Formatter<ASTFormatContext<'_>>) -> FormatResult<()> {
|
||||||
|
let (source, start, end) = f.context().locator().slice(self.range);
|
||||||
|
|
||||||
|
if source[start..end].ends_with('j') {
|
||||||
|
write!(f, [literal(self.range)])?;
|
||||||
|
} else if source[start..end].ends_with('J') {
|
||||||
|
write!(
|
||||||
|
f,
|
||||||
|
[literal(Range::new(
|
||||||
|
self.range.location,
|
||||||
|
Location::new(
|
||||||
|
self.range.end_location.row(),
|
||||||
|
self.range.end_location.column() - 1
|
||||||
|
),
|
||||||
|
))]
|
||||||
|
)?;
|
||||||
|
write!(f, [text("j")])?;
|
||||||
|
} else {
|
||||||
|
unreachable!("expected complex literal to end with j or J");
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub const fn complex_literal(range: Range) -> ComplexLiteral {
|
||||||
|
ComplexLiteral { range }
|
||||||
|
}
|
||||||
|
|
|
@ -46,8 +46,6 @@ y = 100(no)
|
||||||
-x = (123456789.123456789).hex()
|
-x = (123456789.123456789).hex()
|
||||||
-x = (123456789.123456789e123456789).real
|
-x = (123456789.123456789e123456789).real
|
||||||
-x = (123456789e123456789).conjugate()
|
-x = (123456789e123456789).conjugate()
|
||||||
-x = 123456789j.real
|
|
||||||
-x = 123456789.123456789j.__add__(0b1011.bit_length())
|
|
||||||
+x = 0.1.is_integer()
|
+x = 0.1.is_integer()
|
||||||
+x = 1.0.imag
|
+x = 1.0.imag
|
||||||
+x = 1e1.imag
|
+x = 1e1.imag
|
||||||
|
@ -55,15 +53,14 @@ y = 100(no)
|
||||||
+x = 123456789.123456789.hex()
|
+x = 123456789.123456789.hex()
|
||||||
+x = 123456789.123456789e123456789.real
|
+x = 123456789.123456789e123456789.real
|
||||||
+x = 123456789e123456789.conjugate()
|
+x = 123456789e123456789.conjugate()
|
||||||
+x = 123456789J.real
|
x = 123456789j.real
|
||||||
+x = 123456789.123456789J.__add__(0b1011.bit_length())
|
x = 123456789.123456789j.__add__(0b1011.bit_length())
|
||||||
x = 0xB1ACC.conjugate()
|
x = 0xB1ACC.conjugate()
|
||||||
x = 0b1011.conjugate()
|
x = 0b1011.conjugate()
|
||||||
x = 0o777.real
|
x = 0o777.real
|
||||||
-x = (0.000000006).hex()
|
-x = (0.000000006).hex()
|
||||||
-x = -100.0000j
|
|
||||||
+x = 0.000000006.hex()
|
+x = 0.000000006.hex()
|
||||||
+x = -100.0000J
|
x = -100.0000j
|
||||||
|
|
||||||
-if (10).real:
|
-if (10).real:
|
||||||
+if 10.real:
|
+if 10.real:
|
||||||
|
@ -86,13 +83,13 @@ x = 1e-1.real
|
||||||
x = 123456789.123456789.hex()
|
x = 123456789.123456789.hex()
|
||||||
x = 123456789.123456789e123456789.real
|
x = 123456789.123456789e123456789.real
|
||||||
x = 123456789e123456789.conjugate()
|
x = 123456789e123456789.conjugate()
|
||||||
x = 123456789J.real
|
x = 123456789j.real
|
||||||
x = 123456789.123456789J.__add__(0b1011.bit_length())
|
x = 123456789.123456789j.__add__(0b1011.bit_length())
|
||||||
x = 0xB1ACC.conjugate()
|
x = 0xB1ACC.conjugate()
|
||||||
x = 0b1011.conjugate()
|
x = 0b1011.conjugate()
|
||||||
x = 0o777.real
|
x = 0o777.real
|
||||||
x = 0.000000006.hex()
|
x = 0.000000006.hex()
|
||||||
x = -100.0000J
|
x = -100.0000j
|
||||||
|
|
||||||
if 10.real:
|
if 10.real:
|
||||||
...
|
...
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue