-
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.
WIP: Initial attempt to split string formatting
- Loading branch information
1 parent
e043bd4
commit e54f469
Showing
16 changed files
with
319 additions
and
154 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
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
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
37 changes: 33 additions & 4 deletions
37
crates/ruff_python_formatter/src/expression/expr_bytes_literal.rs
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
40 changes: 34 additions & 6 deletions
40
crates/ruff_python_formatter/src/expression/expr_f_string.rs
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
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
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
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
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,24 @@ | ||
use ruff_python_ast::BytesLiteral; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::prelude::*; | ||
use crate::string::{Quoting, StringPart}; | ||
|
||
#[derive(Default)] | ||
pub struct FormatBytesLiteral; | ||
|
||
impl FormatNodeRule<BytesLiteral> for FormatBytesLiteral { | ||
fn fmt_fields(&self, _item: &BytesLiteral, _f: &mut PyFormatter) -> FormatResult<()> { | ||
unreachable!("Handled inside of `FormatExprBytesLiteral`"); | ||
fn fmt_fields(&self, item: &BytesLiteral, f: &mut PyFormatter) -> FormatResult<()> { | ||
let locator = f.context().locator(); | ||
let parent_docstring_quote_style = f.context().docstring(); | ||
|
||
StringPart::from_source(item.range(), &locator) | ||
.normalize( | ||
Quoting::CanChange, | ||
&locator, | ||
f.options().quote_style(), | ||
parent_docstring_quote_style, | ||
) | ||
.fmt(f) | ||
} | ||
} |
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,56 @@ | ||
use ruff_python_ast::FString; | ||
use ruff_python_ast::{FString, FStringElement, FStringExpressionElement}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::prelude::*; | ||
use crate::string::{Quoting, StringPart}; | ||
|
||
#[derive(Default)] | ||
pub struct FormatFString; | ||
|
||
impl FormatNodeRule<FString> for FormatFString { | ||
fn fmt_fields(&self, _item: &FString, _f: &mut PyFormatter) -> FormatResult<()> { | ||
unreachable!("Handled inside of `FormatExprFString`"); | ||
fn fmt_fields(&self, item: &FString, f: &mut PyFormatter) -> FormatResult<()> { | ||
let locator = f.context().locator(); | ||
let parent_docstring_quote_style = f.context().docstring(); | ||
|
||
let unprefixed = locator | ||
.slice(item.range()) | ||
.trim_start_matches(|c| c != '"' && c != '\''); | ||
let triple_quoted = unprefixed.starts_with(r#"""""#) || unprefixed.starts_with(r"'''"); | ||
let quoting = if item.elements.iter().any(|element| match element { | ||
FStringElement::Expression(FStringExpressionElement { range, .. }) => { | ||
let string_content = locator.slice(*range); | ||
if triple_quoted { | ||
string_content.contains(r#"""""#) || string_content.contains("'''") | ||
} else { | ||
string_content.contains(['"', '\'']) | ||
} | ||
} | ||
FStringElement::Literal(_) => false, | ||
}) { | ||
Quoting::Preserve | ||
} else { | ||
Quoting::CanChange | ||
}; | ||
|
||
let result = StringPart::from_source(item.range(), &locator) | ||
.normalize( | ||
quoting, | ||
&locator, | ||
f.options().quote_style(), | ||
parent_docstring_quote_style, | ||
) | ||
.fmt(f); | ||
|
||
// TODO(dhruvmanila): With PEP 701, comments can be inside f-strings. | ||
// This is to mark all of those comments as formatted but we need to | ||
// figure out how to handle them. Note that this needs to be done only | ||
// after the f-string is formatted, so only for all the non-formatted | ||
// comments. | ||
let comments = f.context().comments(); | ||
item.elements.iter().for_each(|value| { | ||
comments.mark_verbatim_node_comments_formatted(value.into()); | ||
}); | ||
|
||
result | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use ruff_formatter::{FormatOwnedWithRule, FormatRefWithRule}; | ||
use ruff_python_ast::FStringPart; | ||
|
||
use crate::prelude::*; | ||
|
||
#[derive(Default)] | ||
pub struct FormatFStringPart; | ||
|
||
impl FormatRule<FStringPart, PyFormatContext<'_>> for FormatFStringPart { | ||
fn fmt(&self, item: &FStringPart, f: &mut PyFormatter) -> FormatResult<()> { | ||
match item { | ||
FStringPart::Literal(string_literal) => string_literal.format().fmt(f), | ||
FStringPart::FString(f_string) => f_string.format().fmt(f), | ||
} | ||
} | ||
} | ||
|
||
impl<'ast> AsFormat<PyFormatContext<'ast>> for FStringPart { | ||
type Format<'a> = FormatRefWithRule<'a, FStringPart, FormatFStringPart, PyFormatContext<'ast>>; | ||
|
||
fn format(&self) -> Self::Format<'_> { | ||
FormatRefWithRule::new(self, FormatFStringPart) | ||
} | ||
} | ||
|
||
impl<'ast> IntoFormat<PyFormatContext<'ast>> for FStringPart { | ||
type Format = FormatOwnedWithRule<FStringPart, FormatFStringPart, PyFormatContext<'ast>>; | ||
|
||
fn into_format(self) -> Self::Format { | ||
FormatOwnedWithRule::new(self, FormatFStringPart) | ||
} | ||
} |
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
Oops, something went wrong.