Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RST: Detect :: signifying default language code-block. #195

Closed
carltongibson opened this issue Nov 30, 2022 · 12 comments · Fixed by #196
Closed

RST: Detect :: signifying default language code-block. #195

carltongibson opened this issue Nov 30, 2022 · 12 comments · Fixed by #196

Comments

@carltongibson
Copy link
Contributor

blacken-docs will do this:

- call ``reset_queries()``, like this::
+ call ``reset_queries()``, like this:
+ 
+  .. code-block:: python

But it's an old-as-time RST convention that the :: means the following code block is the default language (i.e. Python)

Not respecting that is quite a lot of noise. Is there a way to control it? Could there be?

Thanks!

Refs django/django#16261

@carltongibson
Copy link
Contributor Author

carltongibson commented Nov 30, 2022

Ah, I've missed the point there: the change was added by hand in order to get blacken-docs to register the code block. Ref

...so it could be argued that blacken-docs should be able to know about the default and lint blocks that are unmarked?

We have to add the explicit code-block to get the linting.

Could the :: be detected correctly to avoid that? 🤔

@carltongibson carltongibson changed the title Skip verbose RST .. code-block annotation for default (Python) language. RST: Detect :: signifying default language code-block. Nov 30, 2022
@benjaoming
Copy link
Contributor

benjaoming commented Nov 30, 2022

Could the :: be detected correctly to avoid that?

A reason to support this is quite simply to have a way for a large documentation project like Django to introduce blacken-docs without also having a huge changeset related to refactoring :: into .. code-block:: directives.

But I'm not sure, @carltongibson - given the already quite huge changeset - if it matters much. It's just huge changeset vs. also huge changeset 😄

@carltongibson
Copy link
Contributor Author

It's not just the change set. It's the more verbose code style throughout the project, forever more. 😳

@benjaoming
Copy link
Contributor

Working on several Sphinx projects made me unable to remember what language the default :: will highlight, so I always use .. code-block::. And someone who doesn't know Sphinx by heart can probably guess what a code-block is, while :: is a bit weird. So I'm pretty much 👍 on the more verbose style.

@carltongibson
Copy link
Contributor Author

That's OK. Disagreement on such is within the bounds of reason 😜

@benjaoming
Copy link
Contributor

haha, indeed, I couldn't imagine a more classic way for developers to have different experiences than this ❤️ It's why there always exists -f and --force :)

@asottile
Copy link
Contributor

#21 for previous discussion

@carltongibson
Copy link
Contributor Author

Thanks for the link back @asottile

I don't think that's a particularly common case…

@benjaoming
Copy link
Contributor

:: could be a quirk to avoid linting but retain Python highlighting. This would be a work-around for #193

Is this approach of interest @carltongibson - or too quirky yirky dirky? 😵‍💫

@carltongibson
Copy link
Contributor Author

It's not just up to me 😄

Personally, I do want to keep the :: usage, but we can always adapt if the benefits are worth it.

I still need to investigate all the changes but, I'm more concerned by the required Oh, it always has to be valid code changes.

The trouble with skips is we no longer have an auto-formatter.

What we need is a black-ish that will hand wave over the invalid bits. (Don't think that's on the table)

You've been looking at this for while. I only just came to it. I also need to confer with Mariusz.

(This seems more a discussion for the Django PR than here... 😬)

@adamchainz
Copy link
Owner

I’m not in favour of this because :: could be any programming language.

I’ve always preferred using explicit code-block directives. It lets other non-Sphinx tools render the blocks correctly too, like the rST previews on GitHub.

@carltongibson
Copy link
Contributor Author

carltongibson commented Dec 1, 2022

...:: could be any programming language.

It could, but if you're using Sphinx it isn't, it's Python. Projects created by sphinx-quickstart use this, Django uses this, CPython uses this… — it's got to be a million projects. It's a bit rich saying the whole ecosystem is wrong.

It lets other non-Sphinx tools render the blocks correctly too

Sure. 👍 Hence it should clearly need a flag, like --use-sphinx-default say. (But names as ever.)

BurntSushi added a commit to astral-sh/ruff that referenced this issue Dec 5, 2023
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[1].

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.

Closes #8859

[1]: adamchainz/blacken-docs#195
BurntSushi added a commit to astral-sh/ruff that referenced this issue Dec 5, 2023
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[1].

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.

Closes #8859

[1]: adamchainz/blacken-docs#195
BurntSushi added a commit to astral-sh/ruff that referenced this issue Dec 5, 2023
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[1].

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.

Closes #8859

[1]: adamchainz/blacken-docs#195
BurntSushi added a commit to astral-sh/ruff that referenced this issue Dec 5, 2023
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[1].

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.

Closes #8859

[1]: adamchainz/blacken-docs#195
BurntSushi added a commit to astral-sh/ruff that referenced this issue Dec 5, 2023
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[1].

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.

Closes #8859

[1]: adamchainz/blacken-docs#195
BurntSushi added a commit to astral-sh/ruff that referenced this issue Dec 5, 2023
(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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants