mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-24 05:25:17 +00:00
Format ExprName
(#4803)
This commit is contained in:
parent
d6daa61563
commit
2c41c54e0c
1 changed files with 42 additions and 3 deletions
|
@ -1,5 +1,6 @@
|
||||||
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
|
use crate::prelude::*;
|
||||||
use ruff_formatter::{write, Buffer, FormatResult};
|
use crate::FormatNodeRule;
|
||||||
|
use ruff_formatter::{write, FormatContext};
|
||||||
use rustpython_parser::ast::ExprName;
|
use rustpython_parser::ast::ExprName;
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -7,6 +8,44 @@ pub struct FormatExprName;
|
||||||
|
|
||||||
impl FormatNodeRule<ExprName> for FormatExprName {
|
impl FormatNodeRule<ExprName> for FormatExprName {
|
||||||
fn fmt_fields(&self, item: &ExprName, f: &mut PyFormatter) -> FormatResult<()> {
|
fn fmt_fields(&self, item: &ExprName, f: &mut PyFormatter) -> FormatResult<()> {
|
||||||
write!(f, [verbatim_text(item.range)])
|
let ExprName { id, range, ctx: _ } = item;
|
||||||
|
|
||||||
|
debug_assert_eq!(
|
||||||
|
id.as_str(),
|
||||||
|
f.context()
|
||||||
|
.source_code()
|
||||||
|
.slice(*range)
|
||||||
|
.text(f.context().source_code())
|
||||||
|
);
|
||||||
|
|
||||||
|
write!(f, [source_text_slice(*range, ContainsNewlines::No)])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use ruff_text_size::{TextRange, TextSize};
|
||||||
|
use rustpython_parser::ast::{ModModule, Ranged};
|
||||||
|
use rustpython_parser::Parse;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn name_range_with_comments() {
|
||||||
|
let source = ModModule::parse("a # comment", "file.py").unwrap();
|
||||||
|
|
||||||
|
let expression_statement = source
|
||||||
|
.body
|
||||||
|
.first()
|
||||||
|
.expect("Expected non-empty body")
|
||||||
|
.as_expr_stmt()
|
||||||
|
.unwrap();
|
||||||
|
let name = expression_statement
|
||||||
|
.value
|
||||||
|
.as_name_expr()
|
||||||
|
.expect("Expected name expression");
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
name.range(),
|
||||||
|
TextRange::at(TextSize::new(0), TextSize::new(1))
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue