mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-01-23 17:30:04 +08:00
Improve python support (#3947)
This commit is contained in:
parent
0dc1b74647
commit
ae6052b9e0
@ -15,6 +15,22 @@
|
|||||||
" mode key function
|
" mode key function
|
||||||
" <
|
" <
|
||||||
|
|
||||||
|
|
||||||
|
if exists('s:enabled_linters')
|
||||||
|
finish
|
||||||
|
endif
|
||||||
|
|
||||||
|
let s:enabled_linters = ['python', 'flake8']
|
||||||
|
let s:format_on_save = 0
|
||||||
|
let s:python_file_head = [
|
||||||
|
\ '#!/usr/bin/env python',
|
||||||
|
\ '# -*- coding: utf-8 -*-',
|
||||||
|
\ '',
|
||||||
|
\ ''
|
||||||
|
\ ]
|
||||||
|
let s:enable_typeinfo = 0
|
||||||
|
let s:python_interpreter = 'python3'
|
||||||
|
|
||||||
function! SpaceVim#layers#lang#python#plugins() abort
|
function! SpaceVim#layers#lang#python#plugins() abort
|
||||||
let plugins = []
|
let plugins = []
|
||||||
" python
|
" python
|
||||||
@ -73,6 +89,8 @@ function! SpaceVim#layers#lang#python#config() abort
|
|||||||
elseif executable('python')
|
elseif executable('python')
|
||||||
call SpaceVim#plugins#repl#reg('python', ['python', '-i'])
|
call SpaceVim#plugins#repl#reg('python', ['python', '-i'])
|
||||||
endif
|
endif
|
||||||
|
let g:neomake_python_enabled_makers = ['python']
|
||||||
|
let g:neomake_python_python_exe = s:python_interpreter
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
function! s:language_specified_mappings() abort
|
function! s:language_specified_mappings() abort
|
||||||
@ -158,7 +176,7 @@ func! s:getexe() abort
|
|||||||
if line =~# '^#!'
|
if line =~# '^#!'
|
||||||
return s:Shebang_to_cmd(line)
|
return s:Shebang_to_cmd(line)
|
||||||
endif
|
endif
|
||||||
return ['python']
|
return [s:python_interpreter]
|
||||||
endf
|
endf
|
||||||
|
|
||||||
function! s:go_to_def() abort
|
function! s:go_to_def() abort
|
||||||
@ -169,16 +187,7 @@ function! s:go_to_def() abort
|
|||||||
endif
|
endif
|
||||||
endfunction
|
endfunction
|
||||||
|
|
||||||
let s:format_on_save = 0
|
|
||||||
let s:python_file_head = [
|
|
||||||
\ '#!/usr/bin/env python',
|
|
||||||
\ '# -*- coding: utf-8 -*-',
|
|
||||||
\ '',
|
|
||||||
\ ''
|
|
||||||
\ ]
|
|
||||||
let s:enable_typeinfo = 0
|
|
||||||
function! SpaceVim#layers#lang#python#set_variable(var) abort
|
function! SpaceVim#layers#lang#python#set_variable(var) abort
|
||||||
|
|
||||||
let s:format_on_save = get(a:var,
|
let s:format_on_save = get(a:var,
|
||||||
\ 'format_on_save',
|
\ 'format_on_save',
|
||||||
\ get(a:var,
|
\ get(a:var,
|
||||||
@ -193,4 +202,12 @@ function! SpaceVim#layers#lang#python#set_variable(var) abort
|
|||||||
\ 'enable_typeinfo',
|
\ 'enable_typeinfo',
|
||||||
\ s:enable_typeinfo
|
\ s:enable_typeinfo
|
||||||
\ )
|
\ )
|
||||||
|
let s:enabled_linters = get(a:var,
|
||||||
|
\ 'enabled_linters',
|
||||||
|
\ s:enabled_linters
|
||||||
|
\ )
|
||||||
|
let s:python_interpreter = get(a:var,
|
||||||
|
\ 'python_interpreter',
|
||||||
|
\ s:python_interpreter
|
||||||
|
\ )
|
||||||
endfunction
|
endfunction
|
||||||
|
@ -9,26 +9,35 @@ commentsID: "Use Vim as a Python IDE"
|
|||||||
|
|
||||||
# [Blogs](../blog/) >> Use Vim as a Python IDE
|
# [Blogs](../blog/) >> Use Vim as a Python IDE
|
||||||
|
|
||||||
This is a general guide for using SpaceVim as a Python IDE, including layer configuration and usage.
|
This tutorial introduces you to SpaceVim as a Python environment,
|
||||||
|
by using the `lang#python` layer, you make SpaceVim into a great lightweight Python IDE.
|
||||||
|
|
||||||
Each of the following sections will be covered:
|
Each of the following sections will be covered:
|
||||||
|
|
||||||
<!-- vim-markdown-toc GFM -->
|
<!-- vim-markdown-toc GFM -->
|
||||||
|
|
||||||
- [Enable language layer](#enable-language-layer)
|
- [Enable language layer](#enable-language-layer)
|
||||||
|
- [Select a Python interpreter](#select-a-python-interpreter)
|
||||||
- [Code completion](#code-completion)
|
- [Code completion](#code-completion)
|
||||||
- [Syntax linting](#syntax-linting)
|
- [Syntax linting](#syntax-linting)
|
||||||
- [Import packages](#import-packages)
|
|
||||||
- [Jump to test file](#jump-to-test-file)
|
|
||||||
- [running code](#running-code)
|
|
||||||
- [Code formatting](#code-formatting)
|
- [Code formatting](#code-formatting)
|
||||||
|
- [Import packages](#import-packages)
|
||||||
|
- [Jump between alternate files](#jump-between-alternate-files)
|
||||||
|
- [Code running](#code-running)
|
||||||
- [REPL](#repl)
|
- [REPL](#repl)
|
||||||
|
|
||||||
<!-- vim-markdown-toc -->
|
<!-- vim-markdown-toc -->
|
||||||
|
|
||||||
|
This tutorial is not intended to teach you Python itself.
|
||||||
|
|
||||||
|
If you have any problems, feel free to join the [SpaceVim gitter chatting room](https://gitter.im/SpaceVim/SpaceVim) for general discussion.
|
||||||
|
|
||||||
|
|
||||||
### Enable language layer
|
### Enable language layer
|
||||||
|
|
||||||
To add Python language support in SpaceVim, you need to enable the `lang#python` layer. Press `SPC f v d` to open
|
The python language support in SpaceVim is provided by `lang#python` layer, and it is not enabled by default.
|
||||||
SpaceVim configuration file, and add the following snippet to your configuration:
|
You need to enable it in SpaceVim configuration file. Press `SPC f v d` to open the SpaceVim configuration file,
|
||||||
|
and add following snippet to your configuration file.
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[[layers]]
|
[[layers]]
|
||||||
@ -37,24 +46,50 @@ SpaceVim configuration file, and add the following snippet to your configuration
|
|||||||
|
|
||||||
For more info, you can read the [lang#python](../layers/lang/python/) layer documentation.
|
For more info, you can read the [lang#python](../layers/lang/python/) layer documentation.
|
||||||
|
|
||||||
|
### Select a Python interpreter
|
||||||
|
|
||||||
|
Python is an interpreted language, and in order to run Python code and get semantic information,
|
||||||
|
you need to tell SpaceVim which interpreter to use. This can be set with `python_interpreter` layer
|
||||||
|
option. For example:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[layers]]
|
||||||
|
name = 'lang#python'
|
||||||
|
python_interpreter = 'D:\scoop\shims\python.exe'
|
||||||
|
```
|
||||||
|
|
||||||
|
This option will be applied to neomake's python maker and python code runner.
|
||||||
|
|
||||||
### Code completion
|
### Code completion
|
||||||
|
|
||||||
`lang#python` layer will load the jedi plugin automatically, unless overriden in your `init.toml`.
|
Code autocompletion is provided by `autocomplete` layer, which is loaded by default.
|
||||||
The completion menu will be opened as you type.
|
The language completion source is included in `lang#python` layer.
|
||||||
|
This layer includes `deoplete-jedi` for neovim.
|
||||||
|
|
||||||
![complete python code](https://user-images.githubusercontent.com/13142418/46339650-f5a49280-c665-11e8-86d4-20944ec23098.png)
|
![complete python code](https://user-images.githubusercontent.com/13142418/46339650-f5a49280-c665-11e8-86d4-20944ec23098.png)
|
||||||
|
|
||||||
### Syntax linting
|
### Syntax linting
|
||||||
|
|
||||||
The checkers layer is enabled by default. This layer provides asynchronous syntax linting via [neomake](https://github.com/neomake/neomake).
|
Code Linting is provided by [`checkers`](../layers/checkers/) layer, which is also enabled by default.
|
||||||
It will run flake asynchronously.
|
There are two syntax linters enabled by default,
|
||||||
|
python and [pylint](https://pylint.org/), both of them run asynchronously.
|
||||||
|
|
||||||
To install flake8, just run following command in terminal.
|
To install pylint, just run following command in terminal.
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
pip install --user flake8
|
pip install --user pylint
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Code formatting
|
||||||
|
|
||||||
|
Code formatting is provided by [`format`](../layers/format/) layer, this layer is also enabled by default.
|
||||||
|
And the key binding to format current buffer is `SPC b f`. The default formatter for python code is [yapf](https://github.com/google/yapf).
|
||||||
|
|
||||||
|
So, before using this feature, please install yapf.
|
||||||
|
|
||||||
|
```sh
|
||||||
|
pip install --user yapf
|
||||||
|
```
|
||||||
### Import packages
|
### Import packages
|
||||||
|
|
||||||
When edit Python file, you can import the package automatically, remove unused package and format package list.
|
When edit Python file, you can import the package automatically, remove unused package and format package list.
|
||||||
@ -63,10 +98,15 @@ When edit Python file, you can import the package automatically, remove unused p
|
|||||||
pip install --user isort
|
pip install --user isort
|
||||||
```
|
```
|
||||||
|
|
||||||
### Jump to test file
|
### Jump between alternate files
|
||||||
|
|
||||||
SpaceVim use built-in plugin to manager the files in a project,
|
The alternate files manager provides a command `:A`, with this command,
|
||||||
you can add a `.project_alt.json` to the root of your project with following content:
|
you can jump between alternate files within a project.
|
||||||
|
|
||||||
|
The alternate file structure can be definded in a `.project_alt.json`
|
||||||
|
file in the root of your project.
|
||||||
|
|
||||||
|
For example:
|
||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
@ -77,22 +117,15 @@ you can add a `.project_alt.json` to the root of your project with following con
|
|||||||
|
|
||||||
with this configuration, you can jump between the source code and test file via command `:A`.
|
with this configuration, you can jump between the source code and test file via command `:A`.
|
||||||
|
|
||||||
### running code
|
### Code running
|
||||||
|
|
||||||
To run current script, you can press `SPC l r`, and a split window
|
Code running is provided by builtin code runner. To run current script,
|
||||||
will open, the output of the script will be shown in this window.
|
you can press `SPC l r`, and a split window will open,
|
||||||
|
the output of the script will be shown in this window.
|
||||||
It is running asynchronously, and will not block your Vim.
|
It is running asynchronously, and will not block your Vim.
|
||||||
|
|
||||||
![code runner](https://user-images.githubusercontent.com/13142418/46293837-1c5fbc00-c5c7-11e8-9f3c-c11504e2e04a.png)
|
![code runner](https://user-images.githubusercontent.com/13142418/46293837-1c5fbc00-c5c7-11e8-9f3c-c11504e2e04a.png)
|
||||||
|
|
||||||
### Code formatting
|
|
||||||
|
|
||||||
The format layer is also enabled by default, with this layer you can use key binding `SPC b f` to format current buffer.
|
|
||||||
Before using this feature, please install yapf.
|
|
||||||
|
|
||||||
```sh
|
|
||||||
pip install --user yapf
|
|
||||||
```
|
|
||||||
|
|
||||||
### REPL
|
### REPL
|
||||||
|
|
||||||
|
@ -8,11 +8,10 @@ description: "This layer is for Python development, provide autocompletion, synt
|
|||||||
<!-- vim-markdown-toc GFM -->
|
<!-- vim-markdown-toc GFM -->
|
||||||
|
|
||||||
- [Description](#description)
|
- [Description](#description)
|
||||||
- [Features](#features)
|
- [Installation](#installation)
|
||||||
- [Install](#install)
|
- [Enable language layer](#enable-language-layer)
|
||||||
- [enable layer](#enable-layer)
|
- [Language tools](#language-tools)
|
||||||
- [language tools](#language-tools)
|
- [Layer options](#layer-options)
|
||||||
- [Configuration](#configuration)
|
|
||||||
- [Key bindings](#key-bindings)
|
- [Key bindings](#key-bindings)
|
||||||
- [Jump to definition](#jump-to-definition)
|
- [Jump to definition](#jump-to-definition)
|
||||||
- [Code generation](#code-generation)
|
- [Code generation](#code-generation)
|
||||||
@ -29,116 +28,119 @@ description: "This layer is for Python development, provide autocompletion, synt
|
|||||||
|
|
||||||
This layer is for Python development.
|
This layer is for Python development.
|
||||||
|
|
||||||
## Features
|
## Installation
|
||||||
|
|
||||||
- Auto-completion using [deoplete-jedi](https://github.com/zchee/deoplete-jedi) or [jedi-vim](https://github.com/davidhalter/jedi-vim)
|
### Enable language layer
|
||||||
- Documentation Lookup using [jedi-vim](https://github.com/davidhalter/jedi-vim)
|
|
||||||
|
|
||||||
## Install
|
The `lang#python` layer is not loaded by default, to use this layer,
|
||||||
|
you need to add following snippet into your spacevim configuration file.
|
||||||
### enable layer
|
|
||||||
|
|
||||||
To use this configuration layer, add following snippet to your custom configuration file.
|
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[[layers]]
|
[[layers]]
|
||||||
name = "lang#python"
|
name = "lang#python"
|
||||||
```
|
```
|
||||||
|
|
||||||
### language tools
|
### Language tools
|
||||||
|
|
||||||
**syntax checking:**
|
- **syntax checking:**
|
||||||
|
|
||||||
`checker` layer provide syntax checking feature, and for Python it uses `flake8` package:
|
`checker` layer provide syntax checking feature, and for Python it uses `pylint` package:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
pip install --user flake8
|
pip install --user pylint
|
||||||
```
|
```
|
||||||
|
|
||||||
**code formatting:**
|
- **code formatting:**
|
||||||
|
|
||||||
The default key binding for formatting buffer is `SPC b f`,
|
The default key binding for formatting buffer is `SPC b f`,
|
||||||
and you need to install `yapf`.
|
and you need to install `yapf`.
|
||||||
To enable automatic buffer formatting on save,
|
|
||||||
load this layer with setting `format_on_save` to `1`.
|
|
||||||
|
|
||||||
```toml
|
```sh
|
||||||
[[layers]]
|
pip install --user yapf
|
||||||
name = "lang#python"
|
```
|
||||||
format_on_save = 1
|
|
||||||
```
|
|
||||||
|
|
||||||
```sh
|
To use other tool as the format command, for example `black`,
|
||||||
pip install --user yapf
|
change the neoformat option in bootstrap function.
|
||||||
```
|
|
||||||
|
|
||||||
To use other tool as the format command, for example `black`, change the neoformat option in bootstrap
|
```viml
|
||||||
function.
|
let g:neoformat_python_black = {
|
||||||
|
\ 'exe': 'black',
|
||||||
|
\ 'stdin': 1,
|
||||||
|
\ 'args': ['-q', '-'],
|
||||||
|
\ }
|
||||||
|
let g:neoformat_enabled_python = ['black']
|
||||||
|
```
|
||||||
|
|
||||||
```viml
|
- **code formatting:**
|
||||||
let g:neoformat_python_black = {
|
|
||||||
\ 'exe': 'black',
|
|
||||||
\ 'stdin': 1,
|
|
||||||
\ 'args': ['-q', '-'],
|
|
||||||
\ }
|
|
||||||
let g:neoformat_enabled_python = ['black']
|
|
||||||
```
|
|
||||||
|
|
||||||
**format imports:**
|
The default formatter for python is [yapf](https://github.com/google/yapf).
|
||||||
|
|
||||||
To be able to suppress unused imports easily, install [autoflake](https://github.com/myint/autoflake):
|
```
|
||||||
|
pip install --user yapf
|
||||||
|
```
|
||||||
|
|
||||||
```sh
|
To be able to suppress unused imports easily, install [autoflake](https://github.com/myint/autoflake):
|
||||||
pip install --user autoflake
|
|
||||||
```
|
|
||||||
|
|
||||||
To be able to sort your imports, install [isort](https://github.com/timothycrosley/isort)
|
```
|
||||||
|
pip install --user autoflake
|
||||||
|
```
|
||||||
|
|
||||||
```sh
|
To be able to sort your imports, install [isort](https://github.com/timothycrosley/isort)
|
||||||
pip install --user isort
|
|
||||||
```
|
|
||||||
|
|
||||||
**code coverage:**
|
```
|
||||||
|
pip install --user isort
|
||||||
|
```
|
||||||
|
|
||||||
To be able to show code coverage, install coverage.py
|
- **code coverage:**
|
||||||
|
|
||||||
```sh
|
To be able to show code coverage, install coverage.py
|
||||||
pip install --user coverage
|
|
||||||
```
|
|
||||||
|
|
||||||
## Configuration
|
```
|
||||||
|
pip install --user coverage
|
||||||
|
```
|
||||||
|
|
||||||
By default, when create a new python file, SpaceVim will insert file head automatically.
|
## Layer options
|
||||||
to change the file head, use `python_file_head` option:
|
|
||||||
|
|
||||||
```toml
|
- `python_file_head`: Default file head when create new python file.
|
||||||
[[layers]]
|
|
||||||
name = "lang#python"
|
|
||||||
python_file_head = [
|
|
||||||
'#!/usr/bin/env python',
|
|
||||||
'# -*- coding: utf-8 -*-',
|
|
||||||
'',
|
|
||||||
''
|
|
||||||
]
|
|
||||||
```
|
|
||||||
|
|
||||||
When enable autocomplete layer, the symbol will be complete automatically. By default the type info
|
By default, when create a new python file, SpaceVim will insert file head automatically.
|
||||||
is disabled, because it is too slow. To enable type info:
|
to change the file head, use `python_file_head` option:
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[[layers]]
|
[[layers]]
|
||||||
name = "lang#python"
|
name = "lang#python"
|
||||||
enable_typeinfo = true
|
python_file_head = [
|
||||||
```
|
'#!/usr/bin/env python',
|
||||||
|
'# -*- coding: utf-8 -*-',
|
||||||
|
'',
|
||||||
|
''
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
By default, the python layer utilizes `neomake` for syntax checking, and the default python executable
|
When enable autocomplete layer, the symbol will be complete automatically. By default the type info
|
||||||
is simply `python`. Note that the python version is up to your system configuration. If the system
|
is disabled, because it is too slow. To enable type info:
|
||||||
(or environment) python version is 2, one can have the following configuration in the bootstrap function
|
|
||||||
for syntax checking on python3:
|
|
||||||
|
|
||||||
```vim
|
```toml
|
||||||
let g:neomake_python_python_exe = 'python3'
|
[[layers]]
|
||||||
```
|
name = "lang#python"
|
||||||
|
enable_typeinfo = true
|
||||||
|
```
|
||||||
|
|
||||||
|
- `format_on_save`: Enable/disabled file formatting when saving current python file. By default,
|
||||||
|
it is disabled, to enable it:
|
||||||
|
```toml
|
||||||
|
[[layers]]
|
||||||
|
name = 'lang#python'
|
||||||
|
format_on_save = true
|
||||||
|
```
|
||||||
|
|
||||||
|
* `python_interpreter`: Set the python interpreter, by default, it is `python3`. The value of this option will
|
||||||
|
be apply to `g:neomake_python_python_exe` and code runner.
|
||||||
|
```toml
|
||||||
|
[[layers]]
|
||||||
|
name = 'lang#python'
|
||||||
|
python_interpreter = 'D:\scoop\shims\python.exe'
|
||||||
|
```
|
||||||
|
|
||||||
## Key bindings
|
## Key bindings
|
||||||
|
|
||||||
@ -165,11 +167,17 @@ let g:neomake_python_python_exe = 'python3'
|
|||||||
|
|
||||||
### Text objects and motions
|
### Text objects and motions
|
||||||
|
|
||||||
This layer contains vim-pythonsense which provides text objects and motions for Python classes, methods, functions, and doc strings.
|
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 |
|
| Text Objects | Descriptions |
|
||||||
| ------------ | ------------------------ |
|
| ------------ | --------------------------- |
|
||||||
| `ac` | Outer class text object. |
|
| `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 |
|
||||||
|
|
||||||
### Inferior REPL process
|
### Inferior REPL process
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user