Skip to content
This repository has been archived by the owner on Sep 11, 2022. It is now read-only.
/ vscode-ufmt-autoflake Public archive
forked from omnilib/vscode-ufmt

VSCode Extension for Formatting and Sorting / Removing Imports for Python. (black & µsort & autoflake)

License

Notifications You must be signed in to change notification settings

34j/vscode-ufmt-autoflake

 
 

Repository files navigation

µfmt-autoflake

GitHub Visual Studio Marketplace Installs Visual Studio Marketplace Version

µfmt-autoflake is a code formatter and import sorter / remover for Python and Visual Studio Code, built on top of black and µsort and autoflake:

Black makes code review faster by producing the smallest diffs possible. Blackened code looks the same regardless of the project you’re reading.

μsort is a safe, minimal import sorter. Its primary goal is to make no “dangerous” changes to code, and to make no changes on code style.

µfmt formats files in-memory, first with µsort and then with black, before writing any changes back to disk. This enables a combined, atomic formatting steps in VS Code, without any chance of conflict or intermediate changes between the import sorter and the code formatter.

autoflake removes unused imports and unused variables as reported by pyflakes.

Install Now

Example GIF

Note:

  • This extension requires Python version 3.7 or newer.
  • The extension comes bundled with µfmt 2.0.0b1, autoflake 1.5.2. Older versions of µfmt or autoflake may work, but are not supported.
  • This extension is a fork of omnilib/vscode-ufmt

Usage

Once installed, "µfmt-autoflake" will be available as a formatter for Python files (extension id: mikoz.ufmt-autoflake). µfmt-autoflake can be set as the default formatter by adding the following to your settings:

"[python]": {
    "editor.defaultFormatter": "mikoz.ufmt-autoflake"
}

or through the command pallete option Format Document With...:

Be sure to disable the legacy Python formatter, if enabled:

"python.formatting.provider": "none"

Format on Save

VS Code can automatically format your Python files when saving by adding the following to your settings:

"[python]": {
    "editor.defaultFormatter": "mikoz.ufmt-autoflake",
    "editor.formatOnSave": true
}

Resources

For Developers

  • Run the following command to init

For details, see microsoft/vscode-python-tools-extension-template

git init https://github.com/34j/vscode-ufmt-autoflake
cd ./vscode-ufmt-autoflake
py -3.7 -m venv .venv
"./.venv/scripts/activate"
pip install -U nox
nox --session setup
npm install
  • To debug, open VSCode and press F5
  • A messagebox with text Errors exist after running preLaunchTask 'npm: watch' will appear. Just Debug Anyway. Microsoft is 100% blame to this, it just took me 6 hours to figure out that it could be simply ignored. Just Debug Anyway.
  • To change the behavior, change codes in server.py and runner.py. Other codes are untouched and does not need to be touched generally.
  • To publish package,
vsce publish
  • The difference between the original omnilib/vscode-ufmt and this project is
    • server.py around line 303
    • runner.py around line 64
    • requirements.in (add autoflake)
    • icon.png
    • package.json
    • LICENSE.txt README.md

About

VSCode Extension for Formatting and Sorting / Removing Imports for Python. (black & µsort & autoflake)

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 68.2%
  • TypeScript 29.3%
  • JavaScript 2.5%