keewis / blackdoc

run black on documentation code snippets
https://blackdoc.readthedocs.io
MIT License
47 stars 4 forks source link

don't remove the trailing newline for blocks #52

Closed keewis closed 3 years ago

keewis commented 3 years ago

To continue from https://github.com/pydata/xarray/pull/4510#discussion_r512293208:

I agree that if possible it would be good to keep the newline at the end of a block. Actually, I think doctests are just copied lines from the standard python REPL, so they should be as close to that as possible.

Consider this:

>>> 1 + 2
...

I would like to have blackdoc collapse that to a single line since it's impossible to get multiple lines in the REPL unless the line contains more opening that closing brackets or parens, or if it is the opening line of a block.

Thinking about this some more, maybe it's enough to check whether the first line of the code block matches

[^:]+:(\s+#.*)?

there are still a few corner cases this wouldn't match, though (lambda definitions, for example)

Edit: I can't seem to be able to get black to split the lambda definition after the colon, so I'll wait for bug reports before trying to modify the regular expression.

cc @max-sixty