ruff/crates
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
..
flake8_to_ruff Bump version to v0.1.7 (#8999) 2023-12-04 16:28:23 -05:00
ruff_benchmark Bump ureq from 2.8.0 to 2.9.1 (#8993) 2023-12-04 09:53:25 -06:00
ruff_cache Update to Rust 1.74 and use new clippy lints table (#8722) 2023-11-16 18:12:46 -05:00
ruff_cli Bump version to v0.1.7 (#8999) 2023-12-04 16:28:23 -05:00
ruff_dev Update ruff-dev to use SourceKind (#8878) 2023-11-28 14:27:35 -06:00
ruff_diagnostics Update applicability messages for clarity in tests (#8541) 2023-11-07 16:11:43 +00:00
ruff_formatter remove several uses of unsafe (#8600) 2023-11-28 09:50:03 -05:00
ruff_index Update to Rust 1.74 and use new clippy lints table (#8722) 2023-11-16 18:12:46 -05:00
ruff_linter Fix example for PLR0203 (#9011) 2023-12-05 13:55:15 -05:00
ruff_macros remove several uses of unsafe (#8600) 2023-11-28 09:50:03 -05:00
ruff_notebook Update E402 to work at cell level for notebooks (#8872) 2023-11-29 00:32:35 +00:00
ruff_python_ast Move ParenthesizedExpr to ruff_python_parser (#8987) 2023-12-04 05:36:28 +00:00
ruff_python_codegen Rename as_str to to_str (#8886) 2023-11-28 18:50:42 -06:00
ruff_python_formatter add support for formatting reStructuredText code snippets (#9003) 2023-12-05 14:14:44 -05:00
ruff_python_index Update to Rust 1.74 and use new clippy lints table (#8722) 2023-11-16 18:12:46 -05:00
ruff_python_literal remove several uses of unsafe (#8600) 2023-11-28 09:50:03 -05:00
ruff_python_parser Move ParenthesizedExpr to ruff_python_parser (#8987) 2023-12-04 05:36:28 +00:00
ruff_python_resolver Update to Rust 1.74 and use new clippy lints table (#8722) 2023-11-16 18:12:46 -05:00
ruff_python_semantic Rename semantic model flag LITERAL to TYPING_LITERAL (#8997) 2023-12-04 11:28:09 -06:00
ruff_python_stdlib Update to Rust 1.74 and use new clippy lints table (#8722) 2023-11-16 18:12:46 -05:00
ruff_python_trivia Update to Rust 1.74 and use new clippy lints table (#8722) 2023-11-16 18:12:46 -05:00
ruff_shrinking Bump version to v0.1.7 (#8999) 2023-12-04 16:28:23 -05:00
ruff_source_file remove several uses of unsafe (#8600) 2023-11-28 09:50:03 -05:00
ruff_text_size Update to Rust 1.74 and use new clippy lints table (#8722) 2023-11-16 18:12:46 -05:00
ruff_wasm Bump js-sys from 0.3.65 to 0.3.66 (#8992) 2023-12-04 19:50:08 +09:00
ruff_workspace Default max-positional-args to max-args (#8998) 2023-12-04 19:02:10 +00:00