Skip to content

Commit c48ba69

Browse files
authored
add support for formatting reStructuredText code snippets (#9003)
(This is not possible to actually use until #8854 is merged.) ruff_python_formatter: add reStructuredText docstring formatting support This commit makes use of the refactoring done in prior commits to slot in reStructuredText support. Essentially, we add a new type of code example and look for *both* literal blocks and code block directives. Literal blocks are treated as Python by default because it seems to be a [common practice](adamchainz/blacken-docs#195). That is, literal blocks like this: ``` def example(): """ Here's an example:: foo( 1 ) All done. """ pass ``` Will get reformatted. And code blocks (via reStructuredText directives) will also get reformatted: ``` def example(): """ Here's an example: .. code-block:: python foo( 1 ) All done. """ pass ``` When looking for a code block, it is possible for it to become invalid. In which case, we back out of looking for a code example and print the lines out as they are. As with doctest formatting, if reformatting the code would result in invalid Python or if the code collected from the block is invalid, then formatting is also skipped. A number of tests have been added to check both the formatting and resetting behavior. Mixed indentation is also tested a fair bit, since one of my initial attempts at dealing with mixed indentation ended up not working. I recommend working through this PR commit-by-commit. There is in particular a somewhat gnarly refactoring before reST support is added. Closes #8859
1 parent fd49fb9 commit c48ba69

File tree

5 files changed

+7121
-1566
lines changed

5 files changed

+7121
-1566
lines changed

crates/ruff_python_formatter/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ countme = "3.0.1"
2828
itertools = { workspace = true }
2929
memchr = { workspace = true }
3030
once_cell = { workspace = true }
31+
regex = { workspace = true }
3132
rustc-hash = { workspace = true }
3233
serde = { workspace = true, optional = true }
3334
schemars = { workspace = true, optional = true }

0 commit comments

Comments
 (0)