mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-26 11:59:35 +00:00
Format implicit string continuation (#5328)
This commit is contained in:
parent
313711aaf9
commit
49cabca3e7
9 changed files with 443 additions and 60 deletions
|
@ -1,10 +1,35 @@
|
|||
use crate::context::NodeLevel;
|
||||
use crate::prelude::*;
|
||||
use crate::trivia::{first_non_trivia_token, lines_after, skip_trailing_trivia, Token, TokenKind};
|
||||
use ruff_formatter::write;
|
||||
use ruff_formatter::{format_args, write, Argument, Arguments};
|
||||
use ruff_text_size::TextSize;
|
||||
use rustpython_parser::ast::Ranged;
|
||||
|
||||
/// Adds parentheses and indents `content` if it doesn't fit on a line.
|
||||
pub(crate) fn optional_parentheses<'ast, T>(content: &T) -> OptionalParentheses<'_, 'ast>
|
||||
where
|
||||
T: Format<PyFormatContext<'ast>>,
|
||||
{
|
||||
OptionalParentheses {
|
||||
inner: Argument::new(content),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct OptionalParentheses<'a, 'ast> {
|
||||
inner: Argument<'a, PyFormatContext<'ast>>,
|
||||
}
|
||||
|
||||
impl<'ast> Format<PyFormatContext<'ast>> for OptionalParentheses<'_, 'ast> {
|
||||
fn fmt(&self, f: &mut Formatter<PyFormatContext<'ast>>) -> FormatResult<()> {
|
||||
group(&format_args![
|
||||
if_group_breaks(&text("(")),
|
||||
soft_block_indent(&Arguments::from(&self.inner)),
|
||||
if_group_breaks(&text(")"))
|
||||
])
|
||||
.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
/// Provides Python specific extensions to [`Formatter`].
|
||||
pub(crate) trait PyFormatterExtensions<'ast, 'buf> {
|
||||
/// Creates a joiner that inserts the appropriate number of empty lines between two nodes, depending on the
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue