Extract LineIndex independent methods from Locator (#13938)

This commit is contained in:
Micha Reiser 2024-10-28 08:53:41 +01:00 committed by GitHub
parent f8eb547fb4
commit 9f3a38d408
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
171 changed files with 1348 additions and 1284 deletions

View file

@ -1,6 +1,5 @@
use ruff_python_ast::{AnyNodeRef, ExprFString, StringLike};
use ruff_source_file::Locator;
use ruff_text_size::Ranged;
use ruff_text_size::TextSlice;
use crate::expression::parentheses::{
in_parentheses_only_group, NeedsParentheses, OptionalParentheses,
@ -18,11 +17,8 @@ impl FormatNodeRule<ExprFString> for FormatExprFString {
let ExprFString { value, .. } = item;
if let [f_string_part] = value.as_slice() {
FormatFStringPart::new(
f_string_part,
f_string_quoting(item, &f.context().locator()),
)
.fmt(f)
FormatFStringPart::new(f_string_part, f_string_quoting(item, f.context().source()))
.fmt(f)
} else {
// Always join fstrings that aren't parenthesized and thus, are always on a single line.
if !f.context().node_level().is_parenthesized() {
@ -73,9 +69,9 @@ impl NeedsParentheses for ExprFString {
}
}
pub(crate) fn f_string_quoting(f_string: &ExprFString, locator: &Locator) -> Quoting {
let unprefixed = locator
.slice(f_string.range())
pub(crate) fn f_string_quoting(f_string: &ExprFString, source: &str) -> Quoting {
let unprefixed = source
.slice(f_string)
.trim_start_matches(|c| c != '"' && c != '\'');
let triple_quoted = unprefixed.starts_with(r#"""""#) || unprefixed.starts_with(r"'''");
@ -84,7 +80,7 @@ pub(crate) fn f_string_quoting(f_string: &ExprFString, locator: &Locator) -> Quo
.elements()
.filter_map(|element| element.as_expression())
.any(|expression| {
let string_content = locator.slice(expression.range());
let string_content = source.slice(expression);
if triple_quoted {
string_content.contains(r#"""""#) || string_content.contains("'''")
} else {

View file

@ -2,7 +2,7 @@ use std::borrow::Cow;
use ruff_python_ast::AnyNodeRef;
use ruff_python_ast::{ExprNumberLiteral, Number};
use ruff_text_size::{Ranged, TextSize};
use ruff_text_size::{Ranged, TextSize, TextSlice};
use crate::expression::parentheses::{NeedsParentheses, OptionalParentheses};
use crate::prelude::*;
@ -14,28 +14,26 @@ impl FormatNodeRule<ExprNumberLiteral> for FormatExprNumberLiteral {
fn fmt_fields(&self, item: &ExprNumberLiteral, f: &mut PyFormatter) -> FormatResult<()> {
match item.value {
Number::Int(_) => {
let range = item.range();
let content = f.context().locator().slice(range);
let content = f.context().source().slice(item);
let normalized = normalize_integer(content);
match normalized {
Cow::Borrowed(_) => source_text_slice(range).fmt(f),
Cow::Borrowed(_) => source_text_slice(item.range()).fmt(f),
Cow::Owned(normalized) => text(&normalized).fmt(f),
}
}
Number::Float(_) => {
let range = item.range();
let content = f.context().locator().slice(range);
let content = f.context().source().slice(item);
let normalized = normalize_floating_number(content);
match normalized {
Cow::Borrowed(_) => source_text_slice(range).fmt(f),
Cow::Borrowed(_) => source_text_slice(item.range()).fmt(f),
Cow::Owned(normalized) => text(&normalized).fmt(f),
}
}
Number::Complex { .. } => {
let range = item.range();
let content = f.context().locator().slice(range);
let content = f.context().source().slice(item);
let normalized = normalize_floating_number(content.trim_end_matches(['j', 'J']));
match normalized {