Skip to content

Commit

Permalink
Format ExprName
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaReiser committed Jun 2, 2023
1 parent 9858846 commit 0dd25f8
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions crates/ruff_python_formatter/src/expression/expr_name.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
use crate::{verbatim_text, FormatNodeRule, PyFormatter};
use ruff_formatter::{write, Buffer, FormatResult};
use crate::prelude::*;
use crate::FormatNodeRule;
use ruff_formatter::{write, FormatContext};
use rustpython_parser::ast::ExprName;

#[derive(Default)]
pub struct FormatExprName;

impl FormatNodeRule<ExprName> for FormatExprName {
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))
);
}
}

0 comments on commit 0dd25f8

Please sign in to comment.