-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9858846
commit 0dd25f8
Showing
1 changed file
with
42 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
); | ||
} | ||
} |