mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-28 21:05:08 +00:00
Extract LineIndex
independent methods from Locator
(#13938)
This commit is contained in:
parent
f8eb547fb4
commit
9f3a38d408
171 changed files with 1348 additions and 1284 deletions
|
@ -1,6 +1,7 @@
|
|||
use ruff_formatter::write;
|
||||
use ruff_python_ast::{AnyStringFlags, FString, StringFlags};
|
||||
use ruff_source_file::Locator;
|
||||
use ruff_source_file::LineRanges;
|
||||
use ruff_text_size::Ranged;
|
||||
|
||||
use crate::prelude::*;
|
||||
use crate::preview::is_f_string_formatting_enabled;
|
||||
|
@ -27,8 +28,6 @@ impl<'a> FormatFString<'a> {
|
|||
|
||||
impl Format<PyFormatContext<'_>> for FormatFString<'_> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let locator = f.context().locator();
|
||||
|
||||
// If the preview style is enabled, make the decision on what quotes to use locally for each
|
||||
// f-string instead of globally for the entire f-string expression.
|
||||
let quoting = if is_f_string_formatting_enabled(f.context()) {
|
||||
|
@ -66,7 +65,7 @@ impl Format<PyFormatContext<'_>> for FormatFString<'_> {
|
|||
|
||||
let context = FStringContext::new(
|
||||
string_kind,
|
||||
FStringLayout::from_f_string(self.value, &locator),
|
||||
FStringLayout::from_f_string(self.value, f.context().source()),
|
||||
);
|
||||
|
||||
// Starting prefix and quote
|
||||
|
@ -117,7 +116,7 @@ pub(crate) enum FStringLayout {
|
|||
}
|
||||
|
||||
impl FStringLayout {
|
||||
pub(crate) fn from_f_string(f_string: &FString, locator: &Locator) -> Self {
|
||||
pub(crate) fn from_f_string(f_string: &FString, source: &str) -> Self {
|
||||
// Heuristic: Allow breaking the f-string expressions across multiple lines
|
||||
// only if there already is at least one multiline expression. This puts the
|
||||
// control in the hands of the user to decide if they want to break the
|
||||
|
@ -133,7 +132,7 @@ impl FStringLayout {
|
|||
if f_string
|
||||
.elements
|
||||
.expressions()
|
||||
.any(|expr| memchr::memchr2(b'\n', b'\r', locator.slice(expr).as_bytes()).is_some())
|
||||
.any(|expr| source.contains_line_break(expr.range()))
|
||||
{
|
||||
Self::Multiline
|
||||
} else {
|
||||
|
|
|
@ -5,7 +5,7 @@ use ruff_python_ast::{
|
|||
AnyStringFlags, ConversionFlag, Expr, FStringElement, FStringExpressionElement,
|
||||
FStringLiteralElement, StringFlags,
|
||||
};
|
||||
use ruff_text_size::Ranged;
|
||||
use ruff_text_size::{Ranged, TextSlice};
|
||||
|
||||
use crate::comments::{dangling_open_parenthesis_comments, trailing_comments};
|
||||
use crate::context::{FStringState, NodeLevel, WithFStringState, WithNodeLevel};
|
||||
|
@ -60,7 +60,7 @@ impl<'a> FormatFStringLiteralElement<'a> {
|
|||
|
||||
impl Format<PyFormatContext<'_>> for FormatFStringLiteralElement<'_> {
|
||||
fn fmt(&self, f: &mut PyFormatter) -> FormatResult<()> {
|
||||
let literal_content = f.context().locator().slice(self.element.range());
|
||||
let literal_content = f.context().source().slice(self.element);
|
||||
let normalized =
|
||||
normalize_string(literal_content, 0, self.fstring_flags, false, false, true);
|
||||
match &normalized {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue