Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Sample:
"""Hello "World\""""

def __init__(self):
pass
11 changes: 7 additions & 4 deletions crates/ruff_python_formatter/src/string/docstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1597,11 +1597,14 @@ fn docstring_format_source(
}

/// If the last line of the docstring is `content" """` or `content\ """`, we need a chaperone space
/// that avoids `content""""` and `content\"""`. This does only applies to un-escaped backslashes,
/// so `content\\ """` doesn't need a space while `content\\\ """` does.
/// that avoids `content""""` and `content\"""`. This does only applies to un-escaped or escaping
/// backslashes, so `content\\ """` or `content\""""` don't need a space while `content\\\ """` does.
pub(super) fn needs_chaperone_space(flags: AnyStringFlags, trim_end: &str) -> bool {
if trim_end.chars().rev().take_while(|c| *c == '\\').count() % 2 == 1 {
true
if (flags.is_triple_quoted()
&& trim_end.ends_with(&std::format!("\\{}", flags.quote_style().as_char())))
Comment thread
MichaReiser marked this conversation as resolved.
Outdated
|| trim_end.ends_with('\\')
{
trim_end.chars().rev().take_while(|c| *c == '\\').count() % 2 == 1
} else {
flags.is_triple_quoted() && trim_end.ends_with(flags.quote_style().as_char())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/ruff_python_formatter/tests/fixtures.rs
input_file: crates/ruff_python_formatter/resources/test/fixtures/ruff/docstring_ends_on_escaped_space.py
---
## Input
```python
class Sample:
"""Hello "World\""""

def __init__(self):
pass
```

## Output
```python
class Sample:
"""Hello "World\""""

def __init__(self):
pass
```
Loading