Vim plugin: allow using system black rather than virtualenv (#3309)

Provide a configuration parameter to the Vim plugin which will allow the
plugin to skip setting up a virtualenv. This is useful when there is a
system installation of black (e.g. from a Linux distribution) which the
user prefers to use.

Using a virtualenv remains the default.

- Fixes #3308
This commit is contained in:
Corey Hickey 2022-10-27 16:55:33 -07:00 committed by GitHub
parent d338de7f68
commit 4bb6e4f64a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 29 additions and 1 deletions

View File

@ -50,6 +50,9 @@
<!-- For example, Docker, GitHub Actions, pre-commit, editors --> <!-- For example, Docker, GitHub Actions, pre-commit, editors -->
- Vim plugin: Optionally allow using the system installation of Black via
`let g:black_use_virtualenv = 0`(#3309)
### Documentation ### Documentation
<!-- Major changes to documentation and policies. Small docs changes <!-- Major changes to documentation and policies. Small docs changes

View File

@ -56,6 +56,16 @@ def _get_virtualenv_site_packages(venv_path, pyver):
return venv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages' return venv_path / 'lib' / f'python{pyver[0]}.{pyver[1]}' / 'site-packages'
def _initialize_black_env(upgrade=False): def _initialize_black_env(upgrade=False):
if vim.eval("g:black_use_virtualenv ? 'true' : 'false'") == "false":
if upgrade:
print("Upgrade disabled due to g:black_use_virtualenv being disabled.")
print("Either use your system package manager (or pip) to upgrade black separately,")
print("or modify your vimrc to have 'let g:black_use_virtualenv = 1'.")
return False
else:
# Nothing needed to be done.
return True
pyver = sys.version_info[:3] pyver = sys.version_info[:3]
if pyver < (3, 7): if pyver < (3, 7):
print("Sorry, Black requires Python 3.7+ to run.") print("Sorry, Black requires Python 3.7+ to run.")

View File

@ -104,7 +104,7 @@ Commands and shortcuts:
- you can optionally pass `target_version=<version>` with the same values as in the - you can optionally pass `target_version=<version>` with the same values as in the
command line. command line.
- `:BlackUpgrade` to upgrade _Black_ inside the virtualenv; - `:BlackUpgrade` to upgrade _Black_ inside the virtualenv;
- `:BlackVersion` to get the current version of _Black_ inside the virtualenv. - `:BlackVersion` to get the current version of _Black_ in use.
Configuration: Configuration:
@ -160,6 +160,18 @@ If you need to do anything special to make your virtualenv work and install _Bla
example you want to run a version from main), create a virtualenv manually and point example you want to run a version from main), create a virtualenv manually and point
`g:black_virtualenv` to it. The plugin will use it. `g:black_virtualenv` to it. The plugin will use it.
If you would prefer to use the system installation of _Black_ rather than a virtualenv,
then add this to your vimrc:
```
let g:black_use_virtualenv = 0
```
Note that the `:BlackUpgrade` command is only usable and useful with a virtualenv, so
when the virtualenv is not in use, `:BlackUpgrade` is disabled. If you need to upgrade
the system installation of _Black_, then use your system package manager or pip--
whatever tool you used to install _Black_ originally.
To run _Black_ on save, add the following lines to `.vimrc` or `init.vim`: To run _Black_ on save, add the following lines to `.vimrc` or `init.vim`:
``` ```

View File

@ -63,6 +63,9 @@ endif
if !exists("g:black_target_version") if !exists("g:black_target_version")
let g:black_target_version = "" let g:black_target_version = ""
endif endif
if !exists("g:black_use_virtualenv")
let g:black_use_virtualenv = 1
endif
if !exists("g:black_preview") if !exists("g:black_preview")
let g:black_preview = 0 let g:black_preview = 0
endif endif