1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-01-23 13:40:05 +08:00

Improve python support (#3947)

This commit is contained in:
Wang Shidong 2020-11-09 22:00:16 +08:00 committed by GitHub
parent 0dc1b74647
commit ae6052b9e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 181 additions and 123 deletions

View File

@ -15,6 +15,22 @@
" 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
let plugins = []
" python
@ -73,6 +89,8 @@ function! SpaceVim#layers#lang#python#config() abort
elseif executable('python')
call SpaceVim#plugins#repl#reg('python', ['python', '-i'])
endif
let g:neomake_python_enabled_makers = ['python']
let g:neomake_python_python_exe = s:python_interpreter
endfunction
function! s:language_specified_mappings() abort
@ -158,7 +176,7 @@ func! s:getexe() abort
if line =~# '^#!'
return s:Shebang_to_cmd(line)
endif
return ['python']
return [s:python_interpreter]
endf
function! s:go_to_def() abort
@ -169,16 +187,7 @@ function! s:go_to_def() abort
endif
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
let s:format_on_save = get(a:var,
\ 'format_on_save',
\ get(a:var,
@ -193,4 +202,12 @@ function! SpaceVim#layers#lang#python#set_variable(var) abort
\ '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

View File

@ -9,26 +9,35 @@ commentsID: "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:
<!-- vim-markdown-toc GFM -->
- [Enable language layer](#enable-language-layer)
- [Select a Python interpreter](#select-a-python-interpreter)
- [Code completion](#code-completion)
- [Syntax linting](#syntax-linting)
- [Import packages](#import-packages)
- [Jump to test file](#jump-to-test-file)
- [running code](#running-code)
- [Code formatting](#code-formatting)
- [Import packages](#import-packages)
- [Jump between alternate files](#jump-between-alternate-files)
- [Code running](#code-running)
- [REPL](#repl)
<!-- 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
To add Python language support in SpaceVim, you need to enable the `lang#python` layer. Press `SPC f v d` to open
SpaceVim configuration file, and add the following snippet to your configuration:
The python language support in SpaceVim is provided by `lang#python` layer, and it is not enabled by default.
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
[[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.
### 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
`lang#python` layer will load the jedi plugin automatically, unless overriden in your `init.toml`.
The completion menu will be opened as you type.
Code autocompletion is provided by `autocomplete` layer, which is loaded by default.
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)
### Syntax linting
The checkers layer is enabled by default. This layer provides asynchronous syntax linting via [neomake](https://github.com/neomake/neomake).
It will run flake asynchronously.
Code Linting is provided by [`checkers`](../layers/checkers/) layer, which is also enabled by default.
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
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
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
```
### Jump to test file
### Jump between alternate files
SpaceVim use built-in plugin to manager the files in a project,
you can add a `.project_alt.json` to the root of your project with following content:
The alternate files manager provides a command `:A`, with this command,
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
{
@ -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`.
### running code
### Code running
To run current script, you can press `SPC l r`, and a split window
will open, the output of the script will be shown in this window.
Code running is provided by builtin code runner. To run current script,
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.
![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

View File

@ -8,11 +8,10 @@ description: "This layer is for Python development, provide autocompletion, synt
<!-- vim-markdown-toc GFM -->
- [Description](#description)
- [Features](#features)
- [Install](#install)
- [enable layer](#enable-layer)
- [language tools](#language-tools)
- [Configuration](#configuration)
- [Installation](#installation)
- [Enable language layer](#enable-language-layer)
- [Language tools](#language-tools)
- [Layer options](#layer-options)
- [Key bindings](#key-bindings)
- [Jump to definition](#jump-to-definition)
- [Code generation](#code-generation)
@ -29,116 +28,119 @@ description: "This layer is for Python development, provide autocompletion, synt
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)
- Documentation Lookup using [jedi-vim](https://github.com/davidhalter/jedi-vim)
### Enable language layer
## Install
### enable layer
To use this configuration layer, add following snippet to your custom configuration file.
The `lang#python` layer is not loaded by default, to use this layer,
you need to add following snippet into your spacevim configuration file.
```toml
[[layers]]
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
pip install --user flake8
```
```sh
pip install --user pylint
```
**code formatting:**
- **code formatting:**
The default key binding for formatting buffer is `SPC b f`,
and you need to install `yapf`.
To enable automatic buffer formatting on save,
load this layer with setting `format_on_save` to `1`.
The default key binding for formatting buffer is `SPC b f`,
and you need to install `yapf`.
```toml
[[layers]]
name = "lang#python"
format_on_save = 1
```
```sh
pip install --user yapf
```
```sh
pip install --user yapf
```
To use other tool as the format command, for example `black`,
change the neoformat option in bootstrap function.
To use other tool as the format command, for example `black`, change the neoformat option in bootstrap
function.
```viml
let g:neoformat_python_black = {
\ 'exe': 'black',
\ 'stdin': 1,
\ 'args': ['-q', '-'],
\ }
let g:neoformat_enabled_python = ['black']
```
```viml
let g:neoformat_python_black = {
\ 'exe': 'black',
\ 'stdin': 1,
\ 'args': ['-q', '-'],
\ }
let g:neoformat_enabled_python = ['black']
```
- **code formatting:**
**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
pip install --user autoflake
```
To be able to suppress unused imports easily, install [autoflake](https://github.com/myint/autoflake):
To be able to sort your imports, install [isort](https://github.com/timothycrosley/isort)
```
pip install --user autoflake
```
```sh
pip install --user isort
```
To be able to sort your imports, install [isort](https://github.com/timothycrosley/isort)
**code coverage:**
```
pip install --user isort
```
To be able to show code coverage, install coverage.py
- **code coverage:**
```sh
pip install --user coverage
```
To be able to show code coverage, install coverage.py
## Configuration
```
pip install --user coverage
```
By default, when create a new python file, SpaceVim will insert file head automatically.
to change the file head, use `python_file_head` option:
## Layer options
```toml
[[layers]]
name = "lang#python"
python_file_head = [
'#!/usr/bin/env python',
'# -*- coding: utf-8 -*-',
'',
''
]
```
- `python_file_head`: Default file head when create new python file.
When enable autocomplete layer, the symbol will be complete automatically. By default the type info
is disabled, because it is too slow. To enable type info:
By default, when create a new python file, SpaceVim will insert file head automatically.
to change the file head, use `python_file_head` option:
```toml
[[layers]]
name = "lang#python"
enable_typeinfo = true
```
```toml
[[layers]]
name = "lang#python"
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
is simply `python`. Note that the python version is up to your system configuration. If the system
(or environment) python version is 2, one can have the following configuration in the bootstrap function
for syntax checking on python3:
When enable autocomplete layer, the symbol will be complete automatically. By default the type info
is disabled, because it is too slow. To enable type info:
```vim
let g:neomake_python_python_exe = 'python3'
```
```toml
[[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
@ -165,11 +167,17 @@ let g:neomake_python_python_exe = 'python3'
### 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 |
| ------------ | ------------------------ |
| `ac` | Outer class text object. |
| 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 |
### Inferior REPL process