Skip to content

Commit

Permalink
Support passing --preview through to Black (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz committed Aug 16, 2023
1 parent 4eb4e4c commit 8f0ed18
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.rst
Expand Up @@ -16,6 +16,8 @@ Changelog

Thanks to Julianus Pfeuffer for the report in `Issue #218 <https://github.com/adamchainz/blacken-docs/issues/218>`__.

* Support passing the ``--preview`` option through to Black, to select the future style.

* Remove ``language_version`` from ``.pre-commit-hooks.yaml``.
This change allows ``default_language_version`` in ``.pre-commit-config.yaml` to take precedence.
Expand Down
22 changes: 19 additions & 3 deletions README.rst
Expand Up @@ -94,9 +94,25 @@ __ https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core
blacken-docs currently passes the following options through to Black:

* ``-l`` / ``--line-length``
* ``-t`` / ``--target-version``
* ``-S`` / ``--skip-string-normalization``
* |-l / --line-length|__

.. |-l / --line-length| replace:: ``-l`` / ``--line-length``
__ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#l-line-length

* |--preview|__

.. |--preview| replace:: ``--preview``
__ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#preview

* |-S / --skip-string-normalization|__

.. |-S / --skip-string-normalization| replace:: ``-S`` / ``--skip-string-normalization``
__ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#s-skip-string-normalization

* |-t / --target-version|__

.. |-t / --target-version| replace:: ``-t`` / ``--target-version``
__ https://black.readthedocs.io/en/stable/usage_and_configuration/the_basics.html#t-target-version

It also has the below extra options:

Expand Down
14 changes: 8 additions & 6 deletions src/blacken_docs/__init__.py
Expand Up @@ -261,6 +261,12 @@ def main(argv: Sequence[str] | None = None) -> int:
type=int,
default=DEFAULT_LINE_LENGTH,
)
parser.add_argument("--preview", action="store_true")
parser.add_argument(
"-S",
"--skip-string-normalization",
action="store_true",
)
parser.add_argument(
"-t",
"--target-version",
Expand All @@ -270,11 +276,6 @@ def main(argv: Sequence[str] | None = None) -> int:
help=f"choices: {[v.name.lower() for v in TargetVersion]}",
dest="target_versions",
)
parser.add_argument(
"-S",
"--skip-string-normalization",
action="store_true",
)
parser.add_argument("-E", "--skip-errors", action="store_true")
parser.add_argument(
"--rst-literal-blocks",
Expand All @@ -284,9 +285,10 @@ def main(argv: Sequence[str] | None = None) -> int:
args = parser.parse_args(argv)

black_mode = black.FileMode(
target_versions=set(args.target_versions),
line_length=args.line_length,
preview=args.preview,
string_normalization=not args.skip_string_normalization,
target_versions=set(args.target_versions),
)

retv = 0
Expand Down
29 changes: 27 additions & 2 deletions tests/test_blacken_docs.py
Expand Up @@ -470,9 +470,10 @@ def test_integration_line_length(tmp_path):
)

result = blacken_docs.main((str(f), "--line-length=80"))

assert result == 0
assert blacken_docs.main((str(f), "--line-length=50"))

result2 = blacken_docs.main((str(f), "--line-length=50"))
assert result2 == 1
assert f.read_text() == (
"```python\n"
"foo(\n"
Expand All @@ -483,6 +484,30 @@ def test_integration_line_length(tmp_path):
)


def test_integration_preview(tmp_path):
f = tmp_path / "f.md"
f.write_text(
dedent(
"""\
```python
x = 'a' 'b'
```
"""
)
)

result = blacken_docs.main((str(f), "--preview"))

assert result == 1
assert f.read_text() == dedent(
"""\
```python
x = "ab"
```
"""
)


def test_integration_py36(tmp_path):
f = tmp_path / "f.md"
f.write_text(
Expand Down

0 comments on commit 8f0ed18

Please sign in to comment.