mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-15 23:06:15 +00:00
Add basic Constant formatting (#4954)
This commit is contained in:
parent
83cf6d6e2f
commit
c1cc6f3be1
47 changed files with 549 additions and 624 deletions
|
@ -1,24 +1,38 @@
|
|||
use crate::expression::parentheses::{
|
||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
||||
};
|
||||
use crate::{not_yet_implemented_custom_text, AsFormat, FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::prelude::text;
|
||||
use ruff_formatter::{write, Buffer, FormatResult};
|
||||
use rustpython_parser::ast::ExprAttribute;
|
||||
use crate::prelude::*;
|
||||
use crate::{not_yet_implemented_custom_text, FormatNodeRule};
|
||||
use ruff_formatter::write;
|
||||
use rustpython_parser::ast::{Constant, Expr, ExprAttribute, ExprConstant};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatExprAttribute;
|
||||
|
||||
impl FormatNodeRule<ExprAttribute> for FormatExprAttribute {
|
||||
fn fmt_fields(&self, item: &ExprAttribute, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
// We need to write the value - which is also a dummy - already because power op spacing
|
||||
// depends on it
|
||||
let ExprAttribute {
|
||||
value,
|
||||
range: _,
|
||||
attr: _,
|
||||
ctx: _,
|
||||
} = item;
|
||||
|
||||
let requires_space = matches!(
|
||||
value.as_ref(),
|
||||
Expr::Constant(ExprConstant {
|
||||
value: Constant::Int(_) | Constant::Float(_),
|
||||
..
|
||||
})
|
||||
);
|
||||
|
||||
write!(
|
||||
f,
|
||||
[
|
||||
item.value.format(),
|
||||
requires_space.then_some(space()),
|
||||
text("."),
|
||||
not_yet_implemented_custom_text(item, "NOT_IMPLEMENTED_attr")
|
||||
not_yet_implemented_custom_text("NOT_IMPLEMENTED_attr")
|
||||
]
|
||||
)
|
||||
}
|
||||
|
|
|
@ -9,11 +9,10 @@ use rustpython_parser::ast::ExprBoolOp;
|
|||
pub struct FormatExprBoolOp;
|
||||
|
||||
impl FormatNodeRule<ExprBoolOp> for FormatExprBoolOp {
|
||||
fn fmt_fields(&self, item: &ExprBoolOp, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_fields(&self, _item: &ExprBoolOp, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(
|
||||
f,
|
||||
[not_yet_implemented_custom_text(
|
||||
item,
|
||||
"NOT_IMPLEMENTED_bool_op1 and NOT_IMPLEMENTED_bool_op2"
|
||||
)]
|
||||
)
|
||||
|
|
|
@ -9,13 +9,10 @@ use rustpython_parser::ast::ExprCall;
|
|||
pub struct FormatExprCall;
|
||||
|
||||
impl FormatNodeRule<ExprCall> for FormatExprCall {
|
||||
fn fmt_fields(&self, item: &ExprCall, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_fields(&self, _item: &ExprCall, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(
|
||||
f,
|
||||
[not_yet_implemented_custom_text(
|
||||
item,
|
||||
"NOT_IMPLEMENTED_call()"
|
||||
)]
|
||||
[not_yet_implemented_custom_text("NOT_IMPLEMENTED_call()")]
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,11 +10,10 @@ use rustpython_parser::ast::ExprCompare;
|
|||
pub struct FormatExprCompare;
|
||||
|
||||
impl FormatNodeRule<ExprCompare> for FormatExprCompare {
|
||||
fn fmt_fields(&self, item: &ExprCompare, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_fields(&self, _item: &ExprCompare, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(
|
||||
f,
|
||||
[not_yet_implemented_custom_text(
|
||||
item,
|
||||
"NOT_IMPLEMENTED_left < NOT_IMPLEMENTED_right"
|
||||
)]
|
||||
)
|
||||
|
|
|
@ -1,16 +1,42 @@
|
|||
use crate::expression::parentheses::{
|
||||
default_expression_needs_parentheses, NeedsParentheses, Parentheses, Parenthesize,
|
||||
};
|
||||
use crate::{not_yet_implemented_custom_text, FormatNodeRule, PyFormatter};
|
||||
use ruff_formatter::{write, Buffer, FormatResult};
|
||||
use rustpython_parser::ast::ExprConstant;
|
||||
use crate::prelude::*;
|
||||
use crate::{not_yet_implemented_custom_text, verbatim_text, FormatNodeRule};
|
||||
use ruff_formatter::write;
|
||||
use rustpython_parser::ast::{Constant, ExprConstant};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FormatExprConstant;
|
||||
|
||||
impl FormatNodeRule<ExprConstant> for FormatExprConstant {
|
||||
fn fmt_fields(&self, item: &ExprConstant, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(f, [not_yet_implemented_custom_text(item, "0x42")])
|
||||
let ExprConstant {
|
||||
range: _,
|
||||
value,
|
||||
kind: _,
|
||||
} = item;
|
||||
|
||||
match value {
|
||||
Constant::Ellipsis => text("...").fmt(f),
|
||||
Constant::None => text("None").fmt(f),
|
||||
Constant::Bool(value) => match value {
|
||||
true => text("True").fmt(f),
|
||||
false => text("False").fmt(f),
|
||||
},
|
||||
Constant::Int(_) | Constant::Float(_) | Constant::Complex { .. } => {
|
||||
write!(f, [verbatim_text(item)])
|
||||
}
|
||||
Constant::Str(_) => {
|
||||
not_yet_implemented_custom_text(r#""NOT_YET_IMPLEMENTED_STRING""#).fmt(f)
|
||||
}
|
||||
Constant::Bytes(_) => {
|
||||
not_yet_implemented_custom_text(r#"b"NOT_YET_IMPLEMENTED_BYTE_STRING""#).fmt(f)
|
||||
}
|
||||
Constant::Tuple(_) => {
|
||||
not_yet_implemented_custom_text("(NOT_YET_IMPLEMENTED_TUPLE,)").fmt(f)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,11 +9,10 @@ use rustpython_parser::ast::ExprDict;
|
|||
pub struct FormatExprDict;
|
||||
|
||||
impl FormatNodeRule<ExprDict> for FormatExprDict {
|
||||
fn fmt_fields(&self, item: &ExprDict, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_fields(&self, _item: &ExprDict, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(
|
||||
f,
|
||||
[not_yet_implemented_custom_text(
|
||||
item,
|
||||
"{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value}"
|
||||
)]
|
||||
)
|
||||
|
|
|
@ -9,11 +9,10 @@ use rustpython_parser::ast::ExprDictComp;
|
|||
pub struct FormatExprDictComp;
|
||||
|
||||
impl FormatNodeRule<ExprDictComp> for FormatExprDictComp {
|
||||
fn fmt_fields(&self, item: &ExprDictComp, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_fields(&self, _item: &ExprDictComp, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(
|
||||
f,
|
||||
[not_yet_implemented_custom_text(
|
||||
item,
|
||||
"{NOT_IMPLEMENTED_dict_key: NOT_IMPLEMENTED_dict_value for key, value in NOT_IMPLEMENTED_dict}"
|
||||
)]
|
||||
)
|
||||
|
|
|
@ -9,11 +9,8 @@ use rustpython_parser::ast::ExprGeneratorExp;
|
|||
pub struct FormatExprGeneratorExp;
|
||||
|
||||
impl FormatNodeRule<ExprGeneratorExp> for FormatExprGeneratorExp {
|
||||
fn fmt_fields(&self, item: &ExprGeneratorExp, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(
|
||||
f,
|
||||
[not_yet_implemented_custom_text(item, "(i for i in [])")]
|
||||
)
|
||||
fn fmt_fields(&self, _item: &ExprGeneratorExp, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(f, [not_yet_implemented_custom_text("(i for i in [])")])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,11 +9,10 @@ use rustpython_parser::ast::ExprIfExp;
|
|||
pub struct FormatExprIfExp;
|
||||
|
||||
impl FormatNodeRule<ExprIfExp> for FormatExprIfExp {
|
||||
fn fmt_fields(&self, item: &ExprIfExp, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_fields(&self, _item: &ExprIfExp, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(
|
||||
f,
|
||||
[not_yet_implemented_custom_text(
|
||||
item,
|
||||
"NOT_IMPLEMENTED_true if NOT_IMPLEMENTED_cond else NOT_IMPLEMENTED_false"
|
||||
)]
|
||||
)
|
||||
|
|
|
@ -9,8 +9,8 @@ use rustpython_parser::ast::ExprLambda;
|
|||
pub struct FormatExprLambda;
|
||||
|
||||
impl FormatNodeRule<ExprLambda> for FormatExprLambda {
|
||||
fn fmt_fields(&self, item: &ExprLambda, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(f, [not_yet_implemented_custom_text(item, "lambda x: True")])
|
||||
fn fmt_fields(&self, _item: &ExprLambda, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(f, [not_yet_implemented_custom_text("lambda x: True")])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -9,11 +9,8 @@ use rustpython_parser::ast::ExprListComp;
|
|||
pub struct FormatExprListComp;
|
||||
|
||||
impl FormatNodeRule<ExprListComp> for FormatExprListComp {
|
||||
fn fmt_fields(&self, item: &ExprListComp, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(
|
||||
f,
|
||||
[not_yet_implemented_custom_text(item, "[i for i in []]")]
|
||||
)
|
||||
fn fmt_fields(&self, _item: &ExprListComp, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(f, [not_yet_implemented_custom_text("[i for i in []]")])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -10,11 +10,10 @@ use rustpython_parser::ast::ExprSlice;
|
|||
pub struct FormatExprSlice;
|
||||
|
||||
impl FormatNodeRule<ExprSlice> for FormatExprSlice {
|
||||
fn fmt_fields(&self, item: &ExprSlice, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_fields(&self, _item: &ExprSlice, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(
|
||||
f,
|
||||
[not_yet_implemented_custom_text(
|
||||
item,
|
||||
"NOT_IMPLEMENTED_start:NOT_IMPLEMENTED_end"
|
||||
)]
|
||||
)
|
||||
|
|
|
@ -10,11 +10,10 @@ use rustpython_parser::ast::ExprSubscript;
|
|||
pub struct FormatExprSubscript;
|
||||
|
||||
impl FormatNodeRule<ExprSubscript> for FormatExprSubscript {
|
||||
fn fmt_fields(&self, item: &ExprSubscript, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
fn fmt_fields(&self, _item: &ExprSubscript, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(
|
||||
f,
|
||||
[not_yet_implemented_custom_text(
|
||||
item,
|
||||
"NOT_IMPLEMENTED_value[NOT_IMPLEMENTED_key]"
|
||||
)]
|
||||
)
|
||||
|
|
|
@ -9,8 +9,8 @@ use rustpython_parser::ast::ExprTuple;
|
|||
pub struct FormatExprTuple;
|
||||
|
||||
impl FormatNodeRule<ExprTuple> for FormatExprTuple {
|
||||
fn fmt_fields(&self, item: &ExprTuple, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(f, [not_yet_implemented_custom_text(item, "(1, 2)")])
|
||||
fn fmt_fields(&self, _item: &ExprTuple, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
write!(f, [not_yet_implemented_custom_text("(1, 2)")])
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue