1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 13:10:04 +08:00
SpaceVim/docs/layers/lang/python.md

260 lines
7.2 KiB
Markdown
Raw Permalink Normal View History

Update lang#python layer (#779) * plugin: json-vim: update to newer, forked version Updated the lang layer to use elzr/vim-json's fork or the old plugin (leshill/vim-json). Now the actual used plugin is consistent with the documentation, which pointed to elzr/vim-json before * Update lang#python layer * Remove plugin that does not exist * Use fork instead * Remove g:delimitMate_matchpairs setting. Let `)`(delimitMate mapping) can work. Remove '<:>' mapping, let the `less then` operator can work. Fixes #772 * Call complete_parameter#pre_complete only pumvisible. If pumvisible is false, call "\<Plug>delimitMate(" * Fix mapping to ( * Update statusline for ctrlp buffer * remove stray chars from documentation * Update version * Add mapping g<c-]> for jump to tag under cursor close #785 * fix typo * Move context from readme * Update readme * Update intr * Update readme * Bug: can not override layer options. Fix #793 * Add help desc * Update fild founder cmd * Update autocomplete layer * Add gq for format Nmove text * Update autocomplete layer options * Update travis config * Fix ag command in windows * Fix #803 * Update chinese doc * Update doc * Add fonts doc * Update doc * copy doc to zh_cn_doc * Fix command documentation * Add weixin & zhifubao info * Update img * Update features * Add Julia Support * Update markdown layer setting * Update markdown layer doc * Add vim-repeat to edit layer This is a really nice plugin which adds a user pluggable "." command. It integrates seamlessly with several plugins, notably tpope/vim-surround and makes the "." command capable of repeating on the entire surround command, rather than just a part of the command. ysaw" becomes easily repeatable! * Update doc * Update config doc * Update statusline doc * Fix git head * Update doc for statusline * Update doc for tabline * Update doc * Update remark config * Update markdown layer * Update config for remark * Add layer for Pony programming language * Fix remark setting * Add airline/tabline theme for nord * Update doc for getting help * Fix slash * Upate toc * Remove title * Update markdown plugin * Fix doc generation * Update jumping doc * Fix shell layer * Add key binding desc for SPC ' * Add key binding for clear saved buffers * Merge dev branch * Add SPC a q for QQ in vim * Note for chinese doc * Update doc for file manager * Set img size * Update img size * Update img size * Update lang#python layer
2017-09-10 15:13:03 +08:00
---
title: "SpaceVim lang#python layer"
2021-06-21 20:39:29 +08:00
description: "This layer is for Python development, provides autocompletion, syntax checking, and code formatting for Python files."
Update lang#python layer (#779) * plugin: json-vim: update to newer, forked version Updated the lang layer to use elzr/vim-json's fork or the old plugin (leshill/vim-json). Now the actual used plugin is consistent with the documentation, which pointed to elzr/vim-json before * Update lang#python layer * Remove plugin that does not exist * Use fork instead * Remove g:delimitMate_matchpairs setting. Let `)`(delimitMate mapping) can work. Remove '<:>' mapping, let the `less then` operator can work. Fixes #772 * Call complete_parameter#pre_complete only pumvisible. If pumvisible is false, call "\<Plug>delimitMate(" * Fix mapping to ( * Update statusline for ctrlp buffer * remove stray chars from documentation * Update version * Add mapping g<c-]> for jump to tag under cursor close #785 * fix typo * Move context from readme * Update readme * Update intr * Update readme * Bug: can not override layer options. Fix #793 * Add help desc * Update fild founder cmd * Update autocomplete layer * Add gq for format Nmove text * Update autocomplete layer options * Update travis config * Fix ag command in windows * Fix #803 * Update chinese doc * Update doc * Add fonts doc * Update doc * copy doc to zh_cn_doc * Fix command documentation * Add weixin & zhifubao info * Update img * Update features * Add Julia Support * Update markdown layer setting * Update markdown layer doc * Add vim-repeat to edit layer This is a really nice plugin which adds a user pluggable "." command. It integrates seamlessly with several plugins, notably tpope/vim-surround and makes the "." command capable of repeating on the entire surround command, rather than just a part of the command. ysaw" becomes easily repeatable! * Update doc * Update config doc * Update statusline doc * Fix git head * Update doc for statusline * Update doc for tabline * Update doc * Update remark config * Update markdown layer * Update config for remark * Add layer for Pony programming language * Fix remark setting * Add airline/tabline theme for nord * Update doc for getting help * Fix slash * Upate toc * Remove title * Update markdown plugin * Fix doc generation * Update jumping doc * Fix shell layer * Add key binding desc for SPC ' * Add key binding for clear saved buffers * Merge dev branch * Add SPC a q for QQ in vim * Note for chinese doc * Update doc for file manager * Set img size * Update img size * Update img size * Update lang#python layer
2017-09-10 15:13:03 +08:00
---
2018-06-23 14:37:41 +08:00
# [Available Layers](../../) >> lang#python
Update lang#python layer (#779) * plugin: json-vim: update to newer, forked version Updated the lang layer to use elzr/vim-json's fork or the old plugin (leshill/vim-json). Now the actual used plugin is consistent with the documentation, which pointed to elzr/vim-json before * Update lang#python layer * Remove plugin that does not exist * Use fork instead * Remove g:delimitMate_matchpairs setting. Let `)`(delimitMate mapping) can work. Remove '<:>' mapping, let the `less then` operator can work. Fixes #772 * Call complete_parameter#pre_complete only pumvisible. If pumvisible is false, call "\<Plug>delimitMate(" * Fix mapping to ( * Update statusline for ctrlp buffer * remove stray chars from documentation * Update version * Add mapping g<c-]> for jump to tag under cursor close #785 * fix typo * Move context from readme * Update readme * Update intr * Update readme * Bug: can not override layer options. Fix #793 * Add help desc * Update fild founder cmd * Update autocomplete layer * Add gq for format Nmove text * Update autocomplete layer options * Update travis config * Fix ag command in windows * Fix #803 * Update chinese doc * Update doc * Add fonts doc * Update doc * copy doc to zh_cn_doc * Fix command documentation * Add weixin & zhifubao info * Update img * Update features * Add Julia Support * Update markdown layer setting * Update markdown layer doc * Add vim-repeat to edit layer This is a really nice plugin which adds a user pluggable "." command. It integrates seamlessly with several plugins, notably tpope/vim-surround and makes the "." command capable of repeating on the entire surround command, rather than just a part of the command. ysaw" becomes easily repeatable! * Update doc * Update config doc * Update statusline doc * Fix git head * Update doc for statusline * Update doc for tabline * Update doc * Update remark config * Update markdown layer * Update config for remark * Add layer for Pony programming language * Fix remark setting * Add airline/tabline theme for nord * Update doc for getting help * Fix slash * Upate toc * Remove title * Update markdown plugin * Fix doc generation * Update jumping doc * Fix shell layer * Add key binding desc for SPC ' * Add key binding for clear saved buffers * Merge dev branch * Add SPC a q for QQ in vim * Note for chinese doc * Update doc for file manager * Set img size * Update img size * Update img size * Update lang#python layer
2017-09-10 15:13:03 +08:00
<!-- vim-markdown-toc GFM -->
2017-12-02 23:23:58 +08:00
- [Description](#description)
2020-11-09 22:00:16 +08:00
- [Installation](#installation)
- [Enable language layer](#enable-language-layer)
- [Language tools](#language-tools)
- [Layer options](#layer-options)
2017-12-11 19:55:53 +08:00
- [Key bindings](#key-bindings)
2018-06-23 14:37:41 +08:00
- [Jump to definition](#jump-to-definition)
- [Code generation](#code-generation)
- [Code Coverage](#code-coverage)
2019-04-24 22:54:44 +08:00
- [Text objects and motions](#text-objects-and-motions)
2017-12-11 19:55:53 +08:00
- [Inferior REPL process](#inferior-repl-process)
- [Running current script](#running-current-script)
- [Testing](#testing)
- [Refactoring](#refactoring)
- [LSP key Bindings](#lsp-key-bindings)
Update lang#python layer (#779) * plugin: json-vim: update to newer, forked version Updated the lang layer to use elzr/vim-json's fork or the old plugin (leshill/vim-json). Now the actual used plugin is consistent with the documentation, which pointed to elzr/vim-json before * Update lang#python layer * Remove plugin that does not exist * Use fork instead * Remove g:delimitMate_matchpairs setting. Let `)`(delimitMate mapping) can work. Remove '<:>' mapping, let the `less then` operator can work. Fixes #772 * Call complete_parameter#pre_complete only pumvisible. If pumvisible is false, call "\<Plug>delimitMate(" * Fix mapping to ( * Update statusline for ctrlp buffer * remove stray chars from documentation * Update version * Add mapping g<c-]> for jump to tag under cursor close #785 * fix typo * Move context from readme * Update readme * Update intr * Update readme * Bug: can not override layer options. Fix #793 * Add help desc * Update fild founder cmd * Update autocomplete layer * Add gq for format Nmove text * Update autocomplete layer options * Update travis config * Fix ag command in windows * Fix #803 * Update chinese doc * Update doc * Add fonts doc * Update doc * copy doc to zh_cn_doc * Fix command documentation * Add weixin & zhifubao info * Update img * Update features * Add Julia Support * Update markdown layer setting * Update markdown layer doc * Add vim-repeat to edit layer This is a really nice plugin which adds a user pluggable "." command. It integrates seamlessly with several plugins, notably tpope/vim-surround and makes the "." command capable of repeating on the entire surround command, rather than just a part of the command. ysaw" becomes easily repeatable! * Update doc * Update config doc * Update statusline doc * Fix git head * Update doc for statusline * Update doc for tabline * Update doc * Update remark config * Update markdown layer * Update config for remark * Add layer for Pony programming language * Fix remark setting * Add airline/tabline theme for nord * Update doc for getting help * Fix slash * Upate toc * Remove title * Update markdown plugin * Fix doc generation * Update jumping doc * Fix shell layer * Add key binding desc for SPC ' * Add key binding for clear saved buffers * Merge dev branch * Add SPC a q for QQ in vim * Note for chinese doc * Update doc for file manager * Set img size * Update img size * Update img size * Update lang#python layer
2017-09-10 15:13:03 +08:00
<!-- vim-markdown-toc -->
## Description
This layer is for Python development.
2020-11-09 22:00:16 +08:00
## Installation
Update lang#python layer (#779) * plugin: json-vim: update to newer, forked version Updated the lang layer to use elzr/vim-json's fork or the old plugin (leshill/vim-json). Now the actual used plugin is consistent with the documentation, which pointed to elzr/vim-json before * Update lang#python layer * Remove plugin that does not exist * Use fork instead * Remove g:delimitMate_matchpairs setting. Let `)`(delimitMate mapping) can work. Remove '<:>' mapping, let the `less then` operator can work. Fixes #772 * Call complete_parameter#pre_complete only pumvisible. If pumvisible is false, call "\<Plug>delimitMate(" * Fix mapping to ( * Update statusline for ctrlp buffer * remove stray chars from documentation * Update version * Add mapping g<c-]> for jump to tag under cursor close #785 * fix typo * Move context from readme * Update readme * Update intr * Update readme * Bug: can not override layer options. Fix #793 * Add help desc * Update fild founder cmd * Update autocomplete layer * Add gq for format Nmove text * Update autocomplete layer options * Update travis config * Fix ag command in windows * Fix #803 * Update chinese doc * Update doc * Add fonts doc * Update doc * copy doc to zh_cn_doc * Fix command documentation * Add weixin & zhifubao info * Update img * Update features * Add Julia Support * Update markdown layer setting * Update markdown layer doc * Add vim-repeat to edit layer This is a really nice plugin which adds a user pluggable "." command. It integrates seamlessly with several plugins, notably tpope/vim-surround and makes the "." command capable of repeating on the entire surround command, rather than just a part of the command. ysaw" becomes easily repeatable! * Update doc * Update config doc * Update statusline doc * Fix git head * Update doc for statusline * Update doc for tabline * Update doc * Update remark config * Update markdown layer * Update config for remark * Add layer for Pony programming language * Fix remark setting * Add airline/tabline theme for nord * Update doc for getting help * Fix slash * Upate toc * Remove title * Update markdown plugin * Fix doc generation * Update jumping doc * Fix shell layer * Add key binding desc for SPC ' * Add key binding for clear saved buffers * Merge dev branch * Add SPC a q for QQ in vim * Note for chinese doc * Update doc for file manager * Set img size * Update img size * Update img size * Update lang#python layer
2017-09-10 15:13:03 +08:00
2020-11-09 22:00:16 +08:00
### Enable language layer
2017-12-11 19:55:53 +08:00
2020-11-09 22:00:16 +08:00
The `lang#python` layer is not loaded by default, to use this layer,
you need to add following snippet into your spacevim configuration file.
2018-06-23 14:37:41 +08:00
```toml
[[layers]]
name = "lang#python"
```
2017-12-11 19:55:53 +08:00
2020-11-09 22:00:16 +08:00
### Language tools
2020-11-09 22:00:16 +08:00
- **syntax checking:**
2017-12-11 19:55:53 +08:00
2021-06-21 20:39:29 +08:00
`checker` layer provides syntax checking feature, and for Python it uses the `pylint` package:
2017-12-11 19:55:53 +08:00
2020-11-09 22:00:16 +08:00
```sh
pip install --user pylint
```
2017-12-11 19:55:53 +08:00
2020-11-09 22:00:16 +08:00
- **code formatting:**
2017-12-11 19:55:53 +08:00
2021-06-21 20:39:29 +08:00
The default key binding for formatting the current buffer is `SPC b f`,
2020-11-09 22:00:16 +08:00
and you need to install `yapf`.
2017-12-11 19:55:53 +08:00
2020-11-09 22:00:16 +08:00
```sh
pip install --user yapf
```
2021-06-21 20:39:29 +08:00
To use another tool as the format command, for example `black`,
2020-11-09 22:00:16 +08:00
change the neoformat option in bootstrap function.
2017-12-11 19:55:53 +08:00
2020-11-09 22:00:16 +08:00
```viml
let g:neoformat_python_black = {
\ 'exe': 'black',
\ 'stdin': 1,
\ 'args': ['-q', '-'],
\ }
let g:neoformat_enabled_python = ['black']
```
2020-11-09 22:00:16 +08:00
- **code formatting:**
2020-11-09 22:00:16 +08:00
The default formatter for python is [yapf](https://github.com/google/yapf).
2017-12-11 19:55:53 +08:00
2020-11-09 22:00:16 +08:00
```
pip install --user yapf
```
2017-12-11 19:55:53 +08:00
2020-11-09 22:00:16 +08:00
To be able to suppress unused imports easily, install [autoflake](https://github.com/myint/autoflake):
2017-12-11 19:55:53 +08:00
2020-11-09 22:00:16 +08:00
```
pip install --user autoflake
```
2017-12-11 19:55:53 +08:00
2020-11-09 22:00:16 +08:00
To be able to sort your imports, install [isort](https://github.com/timothycrosley/isort)
2017-12-11 19:55:53 +08:00
2020-11-09 22:00:16 +08:00
```
pip install --user isort
```
2020-11-09 22:00:16 +08:00
- **code coverage:**
2020-11-09 22:00:16 +08:00
To be able to show code coverage, install coverage.py
2020-11-09 22:00:16 +08:00
```
pip install --user coverage
```
2019-09-06 00:34:36 +08:00
- **language server**
2022-06-07 23:50:28 +08:00
To enable python support of `lsp` layer. You may need to install `pyright` or `python-lsp-server`:
```
npm install -g pyright
```
2022-06-07 23:50:28 +08:00
or
```
pip install python-lsp-server
```
2022-06-07 23:50:28 +08:00
Also you need enable `lsp` layer with `pyright` client:
```
[[layers]]
name = 'lsp'
enabled_clients = ['pyright']
```
2022-06-07 23:50:28 +08:00
If you want to use `python-lsp-server`, use following config:
```
[[layers]]
name = 'lsp'
enabled_clients = ['pylsp']
```
2020-11-09 22:00:16 +08:00
## Layer options
2019-09-06 00:34:36 +08:00
2020-11-09 22:00:16 +08:00
- `python_file_head`: Default file head when create new python file.
2019-09-06 00:34:36 +08:00
2020-11-09 22:00:16 +08:00
By default, when create a new python file, SpaceVim will insert file head automatically.
to change the file head, use `python_file_head` option:
2020-11-09 22:00:16 +08:00
```toml
[[layers]]
name = "lang#python"
python_file_head = [
'#!/usr/bin/env python',
'# -*- coding: utf-8 -*-',
'',
''
]
```
2021-06-21 20:39:29 +08:00
When the autocomplete layer is enabled, the symbol will be completed automatically.
By default the type info is disabled, because it is too slow. To enable type info add the following to your configuration file:
2020-11-09 22:00:16 +08:00
```toml
[[layers]]
name = "lang#python"
enable_typeinfo = true
```
2021-06-21 20:39:29 +08:00
- `format_on_save`: Enable/disable file formatting when saving current python file. By default,
2020-11-09 22:00:16 +08:00
it is disabled, to enable it:
2020-11-09 22:00:16 +08:00
```toml
[[layers]]
name = 'lang#python'
format_on_save = true
```
2021-03-31 21:35:26 +08:00
- `python_interpreter`: Set the python interpreter, by default, it is `python3`. The value of this option will
2021-06-21 20:39:29 +08:00
be applied to `g:neomake_python_python_exe` and code runner.
2020-11-09 22:00:16 +08:00
```toml
[[layers]]
name = 'lang#python'
python_interpreter = 'D:\scoop\shims\python.exe'
```
2021-04-03 22:07:15 +08:00
- `enabled_linters`: Set the default linters for python language, by default it is `['python']`. You can change
2021-03-31 21:35:26 +08:00
it to `['python', 'pylint']`.
```toml
[[layers]]
name = 'lang#python'
enabled_linters = ['python', 'pylint']
```
2017-12-11 19:55:53 +08:00
## Key bindings
2018-06-23 14:37:41 +08:00
### Jump to definition
2021-06-21 20:39:29 +08:00
| Mode | Key Bindings | Description |
| ------ | ------------ | ---------------------------------------------------------- |
| normal | `g d` | Jump to the definition position of the symbol under cursor |
2018-06-23 14:37:41 +08:00
### Code generation
2018-06-23 14:37:41 +08:00
| Mode | Key Binding | Description |
| ------ | ----------- | ------------------ |
| normal | `SPC l g d` | Generate docstring |
### Code Coverage
| Mode | Key Binding | Description |
| ------ | ----------- | ---------------- |
| normal | `SPC l c r` | coverage report |
| normal | `SPC l c s` | coverage show |
| normal | `SPC l c e` | coverage session |
| normal | `SPC l c f` | coverage refresh |
2019-04-24 22:54:44 +08:00
### Text objects and motions
2020-11-09 22:00:16 +08:00
This layer contains [vim-pythonsense](https://github.com/jeetsukumaran/vim-pythonsense)
which provides text objects and motions for Python classes, methods, functions, and doc strings.
| Text Objects | Descriptions |
| ------------ | --------------------------- |
| `ac` | Outer class text object |
| `ic` | Inner class text object |
| `af` | Inner function text object |
| `if` | Inner function text object |
| `ad` | Inner docstring text object |
| `id` | Inner docstring text object |
2019-04-24 22:54:44 +08:00
2017-12-11 19:55:53 +08:00
### Inferior REPL process
2019-02-03 20:51:01 +08:00
Start a Python or iPython inferior REPL process with `SPC l s i`. If `ipython` is available in system executable search paths, `ipython` will be used to launch Python shell; otherwise, default `python` interpreter will be used. You may change your system executable search path by activating a virtual environment.
2017-12-11 19:55:53 +08:00
Send code to inferior process commands:
2019-02-03 20:51:01 +08:00
| Key Bindings | Descriptions |
| ------------ | ------------------------------------------------ |
| `SPC l s b` | send buffer and keep code buffer focused |
| `SPC l s l` | send line and keep code buffer focused |
| `SPC l s s` | send selection text and keep code buffer focused |
2017-12-11 19:55:53 +08:00
### Running current script
2021-06-21 20:39:29 +08:00
To run a Python script, you can press `SPC l r` to run the current file without losing focus, and the result will be shown in a runner buffer.
2017-12-11 19:55:53 +08:00
### Testing
### Refactoring
2019-02-03 20:51:01 +08:00
| Key Bindings | Descriptions |
| ------------ | ------------------------------------ |
| `SPC l i r` | remove unused imports with autoflake |
| `SPC l i s` | sort imports with isort |
### LSP key Bindings
If the lsp layer is enabled for python, the following key bindings can
be used:
| key binding | Description |
| ----------- | ----------------------- |
| `g D` | jump to type definition |
| `SPC l e` | rename symbol |
| `SPC l x` | show references |
| `SPC l s` | show line diagnostics |
| `SPC l d` | show document |
| `K` | show document |
| `SPC l w l` | list workspace folder |
| `SPC l w a` | add workspace folder |
| `SPC l w r` | remove workspace folder |