Apply Black formatting only in regions changed since last commit
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.
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.
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.
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.
$ 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')
$ pip install darker
Locate your darker installation folder.
On macOS / Linux / BSD:
$ which darker /usr/local/bin/darker # possible location
$ where darker %LocalAppData%\Programs\Python\Python36-32\Scripts\darker.exe # possible location
Open External tools in PyCharm/IntelliJ IDEA
PyCharm -> Preferences -> Tools -> External Tools
On Windows / Linux / BSD:
File -> Settings -> Tools -> External Tools
Click the + icon to add a new external tool with the following values:
Description: Use Black to auto-format regions changed since the last git commit.
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
Optionally, run darker on every file save:
Make sure you have the File Watcher plugin installed.
Go to Preferences or Settings -> Tools -> File Watchers and click + to add a new watcher:
File type: Python
Scope: Project Files
Output paths to refresh: $FilePath$
Working directory: $ProjectFileDir$
Uncheck “Auto-save edited files to trigger the watcher”
BSD. See LICENSE.rst.
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.