ruff/crates/ruff_python_formatter/src
Andrew Gallant c48ba690eb
add support for formatting reStructuredText code snippets (#9003)
(This is not possible to actually use until
https://github.com/astral-sh/ruff/pull/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](https://github.com/adamchainz/blacken-docs/issues/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
2023-12-05 14:14:44 -05:00
..
comments remove several uses of unsafe (#8600) 2023-11-28 09:50:03 -05:00
expression add support for formatting reStructuredText code snippets (#9003) 2023-12-05 14:14:44 -05:00
module Attach dangling comments to the comprehension instead of the if or iter nodes (#7693) 2023-09-29 10:45:01 +01:00
other Create dedicated is_*_enabled functions for each preview style (#8988) 2023-12-04 05:38:54 +00:00
pattern New Singleton enum for PatternMatchSingleton node (#8063) 2023-10-30 05:48:53 +00:00
snapshots Remove exception-handler lexing from unused-bound-exception fix (#5851) 2023-07-18 18:27:46 +00:00
statement Inline trailing comments for type alias similar to assignments (#8941) 2023-12-04 05:27:04 +00:00
type_param Don't move type param opening parenthesis comment (#8163) 2023-10-24 12:02:27 +00:00
builders.rs Implement multiline dictionary and list hugging for preview style (#8293) 2023-11-30 21:11:14 -05:00
cli.rs Add --skip-magic-trailing-comma to formatter dev comment (#8689) 2023-11-15 09:23:46 +00:00
context.rs Create dedicated is_*_enabled functions for each preview style (#8988) 2023-12-04 05:38:54 +00:00
generated.rs Auto-generate formatter nodes for string parts (#8837) 2023-11-25 13:00:47 +00:00
lib.rs Create dedicated is_*_enabled functions for each preview style (#8988) 2023-12-04 05:38:54 +00:00
main.rs Formatter and parser refactoring (#7569) 2023-09-26 15:29:43 +02:00
options.rs Create dedicated is_*_enabled functions for each preview style (#8988) 2023-12-04 05:38:54 +00:00
prelude.rs Accept any Into<AnyNodeRef> as Comments arguments (#5205) 2023-06-20 16:49:21 +00:00
preview.rs Create dedicated is_*_enabled functions for each preview style (#8988) 2023-12-04 05:38:54 +00:00
shared_traits.rs ruff_python_formatter: copy and inline shared traits (#8656) 2023-11-13 12:16:04 -05:00
verbatim.rs Preserve trailing semicolons when using fmt: off (#8275) 2023-10-30 00:22:34 +00:00