Add basic Constant formatting (#4954)

This commit is contained in:
Micha Reiser 2023-06-08 13:42:44 +02:00 committed by GitHub
parent 83cf6d6e2f
commit c1cc6f3be1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
47 changed files with 549 additions and 624 deletions

View file

@ -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)
}
}
}
}