ctrl+shift+p filters: :st2 :st3 :win :osx :linux
Browse

python-black

by thep0y ST4

Black formatter for Sublime Text

Labels formatter

Details

Installs

  • Total 7K
  • Win 3K
  • Mac 2K
  • Linux 2K
Dec 9 Dec 8 Dec 7 Dec 6 Dec 5 Dec 4 Dec 3 Dec 2 Dec 1 Nov 30 Nov 29 Nov 28 Nov 27 Nov 26 Nov 25 Nov 24 Nov 23 Nov 22 Nov 21 Nov 20 Nov 19 Nov 18 Nov 17 Nov 16 Nov 15 Nov 14 Nov 13 Nov 12 Nov 11 Nov 10 Nov 9 Nov 8 Nov 7 Nov 6 Nov 5 Nov 4 Nov 3 Nov 2 Nov 1 Oct 31 Oct 30 Oct 29 Oct 28 Oct 27 Oct 26 Oct 25
Windows 1 4 5 8 4 4 8 8 8 10 6 7 4 6 8 6 7 5 9 2 5 7 9 8 6 3 6 8 7 12 9 9 2 6 3 7 8 7 5 4 9 1 8 5 9 3
Mac 2 12 6 9 7 4 1 5 9 7 4 5 1 1 6 3 5 5 4 1 3 5 7 6 2 5 2 5 6 10 4 5 7 3 5 9 3 18 4 5 2 3 2 3 5 10
Linux 3 10 1 9 4 4 1 4 10 6 4 4 4 5 2 5 7 6 6 6 5 8 9 3 8 7 7 1 1 4 6 6 6 1 3 3 3 3 4 4 6 7 3 6 2 2

Readme

Source
raw.​githubusercontent.​com

python-black

Black formatter for Sublime Text.

It is recommended to use with LSP-pyright.

Installation

There is no need to install black, but if you install it, it will not affect this package.

You can install python-black with package control:

  1. Open your command pallete and type Package Control: Install Package.
  2. Find this project python-black and press Enter.

Local installation

This package has been uploaded to packagecontrol.io, so you do not need to choose local installation:

git clone https://github.com/thep0y/python-black.git

Copy or move the python-black folder to the packages directory of Sublime Text 4.

Usage

1 Key Binding

You can create custom key binding based on samplePreferences - Package Settings - Python Black - Key Bindings, such as:

{
     "keys": [
         "ctrl+super+l"
     ],
     "command": "black",
     "args": {
        "use_selection": true
     }
}

The optional use_selection boolean (defaults to true) controls whether to format the selected region, or the entire file.

:warning:Note: Do not duplicate the key binding of other packages

2 Settings

There is only one modifiable property in settings:

{
   // Whether to automatically format the entire document when saving.
   // There are three modes:
   //    - true (deprecated)
   //    - false (deprecated)
   //    - "on"
   //    - "off"
   //    - "smart": Automatic formatting is only enabled if there is a `black` section in the project's `pyproject.toml`
   "format_on_save": "on"
}

This can also be toggled via Preferences > Package Settings > Python Black > Format On Save.

3 Create Black Configuration File

You can quickly generate a black configuration file for the current project.

Command Description
python-black: Create Black Configuration File Creates a pyproject.toml file in the root of the project with basic options. Opens the configuration file if it already exists.

:warning: If you don't want to generate a pyproject.toml for each project, then you need to create a black global configuration file.

Refer to Black Documentation.

If you want to disable format_on_save in a project that does not use black code style #14, you need to add the configuration to *.sublime-project: json { ... "settings": { ... "python-black": { "format_on_save": false } } }

Development

If you want to fix bugs or add features, you can read log:

  • Colorful: in python-black.log in the [SublimeText]/Log directory.

  • Colorless: in sublime text console.

You can also add logs where you think they should be logged as follows:

from .log import child_logger

logger = child_logger(__name__)


# ...
logger.debug("...")
logger.info("...")
logger.warning("...")
logger.error("...")
# ...

Discussion and creation of PRs are welcome.

TODO

  • [ ] format all python files in the current project

If someone likes or gives feedback, some features may be added in the future.