Skip to main content

Apply Black formatting only in regions changed since last commit

Project description

What?

This is a small utility built on top of the black Python code formatter to enable formatting of only regions which have changed since the last Git commit.

Why?

Python code should be black, just like outer space. However, sometimes people insist on staying on the Earth. Since there’s no complete blackness on the Earth, you eventually settle for just a little darker as a compromise.

In other words, you want to use black for the code you write, but for some reason you don’t want to convert the whole files, e.g. when contributing to upstream codebases that are not under your complete control.

However, partial formatting is not supported by black itself, for various good reasons, and it won’t be implemented either (134, 142, 245, 370, 511, 830).

This is where darker enters the stage. This tool is for those who want to do partial formatting anyway.

Note that this tool is a stopgap measure, and you should avoid using it if you can.

How?

To install, use:

pip install darker

The darker <myfile.py> command reads the original file, formats it using black, combines original and formatted regions based on edits, and writes back over the original file.

Alternatively, you can invoke the module directly through the python executable, which may be preferable depending on your setup. Use python -m darker instead of darker in that case.

Example:

$ mkdir test && cd test && git init
Initialized empty Git repository in /tmp/test/.git/
$ echo "if True: print('hi')\n\nif False: print('there')" | tee test.py
if True: print('hi')

if False: print('there')
$ git add test.py && git commit -m "Initial commit"
[master (root-commit) a0c7c32] Initial commit
 1 file changed, 3 insertions(+)
 create mode 100644 test.py
$ echo "if True: print('changed')\n\nif False: print('there')" | tee test.py
if True: print('changed')

if False: print('there')
$ darker test.py && cat test.py
if True:
    print("changed")

if False: print('there')

Editor integration

Many editors have plugins or recipes for integrating black. You may be able to adapt them to be used with darker. See editor integration in the black documentation.

PyCharm/IntelliJ IDEA

  1. Install darker:

    $ pip install darker
  2. Locate your darker installation folder.

    On macOS / Linux / BSD:

    $ which darker
    /usr/local/bin/darker  # possible location

    On Windows:

    $ where darker
    %LocalAppData%\Programs\Python\Python36-32\Scripts\darker.exe  # possible location
  3. Open External tools in PyCharm/IntelliJ IDEA

    On macOS:

    PyCharm -> Preferences -> Tools -> External Tools

    On Windows / Linux / BSD:

    File -> Settings -> Tools -> External Tools

  4. Click the + icon to add a new external tool with the following values:

    • Name: Darker

    • Description: Use Black to auto-format regions changed since the last git commit.

    • Program: <install_location_from_step_2>

    • Arguments: "$FilePath$"

  5. Format the currently opened file by selecting Tools -> External Tools -> Darker.

    • Alternatively, you can set a keyboard shortcut by navigating to Preferences or Settings -> Keymap -> External Tools -> External Tools - Darker

  6. Optionally, run darker on every file save:

    1. Make sure you have the File Watcher plugin installed.

    2. Go to Preferences or Settings -> Tools -> File Watchers and click + to add a new watcher:

      • Name: Darker

      • File type: Python

      • Scope: Project Files

      • Program: <install_location_from_step_2>

      • Arguments: $FilePath$

      • Output paths to refresh: $FilePath$

      • Working directory: $ProjectFileDir$

    3. Uncheck “Auto-save edited files to trigger the watcher”

License

BSD. See LICENSE.rst.

Prior art

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

darker-0.2.0.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

darker-0.2.0-py3-none-any.whl (22.0 kB view details)

Uploaded Python 3

File details

Details for the file darker-0.2.0.tar.gz.

File metadata

  • Download URL: darker-0.2.0.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.6

File hashes

Hashes for darker-0.2.0.tar.gz
Algorithm Hash digest
SHA256 14bf2fd5d3978518090e8a586569aa5113c9a90967d5db6dc556dfba7dd1f383
MD5 79f3a14d42a82799374a90b908ff9672
BLAKE2b-256 9dfdade175132fa0c5a8b96a0743a653ae43c293e1c8d6b7e9eefa8c23de77f6

See more details on using hashes here.

File details

Details for the file darker-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: darker-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 22.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.6

File hashes

Hashes for darker-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d1b456f56a7cae25ddfbe2a5871e60421de5bd6886fc5bcefc32e22ac3f8e979
MD5 854950f60b3bb5558c7fc4e2b247575d
BLAKE2b-256 efbbe649bdfd9b1ec77b76dd5bb8f253e822e2d3e8c1e7713a3fc40fb3ede008

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page