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

Rust layer enhancement (#3336)

This commit is contained in:
JHZheng 2020-02-06 13:31:07 +08:00 committed by GitHub
parent e29f4b5d33
commit bf427c94e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 79 additions and 35 deletions

4
.gitignore vendored
View File

@ -33,3 +33,7 @@ build/
.gtm/
.metadata/
jdt.ls-java-project/
# generated by rustfmt.vim
# https://github.com/rust-lang/rust.vim/blob/master/autoload/rustfmt.vim#L110
view/

View File

@ -44,9 +44,19 @@
" -----------------------------------------------
" normal gd rust-definition
" normal SPC l d rust-doc
" normal SPC l r execute current file
" normal SPC l s rust-def-split
" normal SPC l x rust-def-vertical
" normal SPC l f rustfmt-format
" normal SPC l e rls-rename-symbol
" normal SPC l u rls-show-references
" normal SPC l c b cargo-build
" normal SPC l c c cargo-clean
" normal SPC l c f cargo-fmt
" normal SPC l c t cargo-test
" normal SPC l c u cargo-update
" normal SPC l c B cargo-bench
" normal SPC l c D cargo-docs
" normal SPC l c r cargo-run
" <
function! SpaceVim#layers#lang#rust#plugins() abort
@ -62,7 +72,11 @@ function! SpaceVim#layers#lang#rust#config() abort
let g:racer_cmd = s:racer_cmd ==# ''
\ ? get(g:, 'racer_cmd', $HOME . '/.cargo/bin/racer')
\ : s:racer_cmd
let g:rustfmt_cmd = s:rustfmt_cmd ==# ''
\ ? get(g:, 'rustfmt_cmd', $HOME . '/.cargo/bin/rustfmt')
\ : s:rustfmt_cmd
let g:rust_recommended_style = s:recommended_style
let g:rustfmt_autosave = s:format_autosave
call SpaceVim#mapping#space#regesit_lang_mappings('rust', function('s:language_specified_mappings'))
call add(g:spacevim_project_rooter_patterns, 'Cargo.toml')
@ -72,24 +86,25 @@ function! SpaceVim#layers#lang#rust#config() abort
else
call SpaceVim#mapping#gd#add('rust', function('s:gotodef'))
endif
endfunction
let s:recommended_style = 0
let s:format_autosave = 0
let s:racer_cmd = ''
let s:rustfmt_cmd = ''
function! SpaceVim#layers#lang#rust#set_variable(var) abort
let s:recommended_style = get(a:var, 'recommended-style', s:recommended_style)
let s:racer_cmd = get(a:var, 'racer_cmd', s:racer_cmd)
let s:format_autosave = get(a:var, 'format-autosave', s:format_autosave)
let s:racer_cmd = get(a:var, 'racer-cmd', s:racer_cmd)
let s:rustfmt_cmd = get(a:var, 'rustfmt-cmd', s:rustfmt_cmd)
endfunction
function! s:language_specified_mappings() abort
call SpaceVim#mapping#space#langSPC('nnoremap', ['l', 's'],
call SpaceVim#mapping#space#langSPC('nmap', ['l', 's'],
\ '<Plug>(rust-def-split)', 'rust-def-split', 0)
call SpaceVim#mapping#space#langSPC('nnoremap', ['l', 'x'],
call SpaceVim#mapping#space#langSPC('nmap', ['l', 'x'],
\ '<Plug>(rust-def-vertical)', 'rust-def-vertical', 0)
let g:_spacevim_mappings_space.l.c = {'name' : '+Cargo'}
call SpaceVim#mapping#space#langSPC('nnoremap', ['l','c', 'r'], 'call call('
\ . string(function('s:execCMD')) . ', ["cargo run"])',
@ -112,7 +127,9 @@ function! s:language_specified_mappings() abort
call SpaceVim#mapping#space#langSPC('nnoremap', ['l','c', 'D'], 'call call('
\ . string(function('s:execCMD')) . ', ["cargo doc"])',
\ 'build-docs', 1)
call SpaceVim#mapping#space#langSPC('nnoremap', ['l','c', 'f'], 'call call('
\ . string(function('s:execCMD')) . ', ["cargo fmt"])',
\ 'format project files', 1)
if SpaceVim#layers#lsp#check_filetype('rust')
nnoremap <silent><buffer> K :call SpaceVim#lsp#show_doc()<CR>
@ -128,14 +145,16 @@ function! s:language_specified_mappings() abort
\ '<Plug>(rust-doc)', 'show documentation', 1)
endif
call SpaceVim#mapping#space#langSPC('nnoremap', ['l', 'f'],
\ 'call rustfmt#Format()' ,'format file', 1)
endfunction
function! s:gotodef() abort
if exists('*racer#GoToDefinition')
try
call racer#GoToDefinition()
else
catch
exec 'normal! gd'
endif
endtry
endfunction
function! s:execCMD(cmd) abort

View File

@ -13,7 +13,6 @@ lang: zh
- [启用模块](#启用模块)
- [模块选项](#模块选项)
- [快捷键](#快捷键)
- [运行当前脚本](#运行当前脚本)
<!-- vim-markdown-toc -->
@ -40,18 +39,28 @@ lang: zh
## 模块选项
- `recommended-style`: 启用/禁用 rust 推荐的代码规范,该选项默认已禁用。
- `recommended-style`: 1/0 (启用/禁用) rust 推荐的代码规范,该选项默认已禁用。
- `format-autosave`: 1/0 (启动/禁用) 保存文件修改后自动格式化,该选项默认已禁用。
- `racer-cmd`: 可执行文件 `racer` 的路径,该选项默认为 `$HOME/.cargo/bin/racer`
- `rustfmt-cmd`: 可执行文件 `rustfmt` 的路径,该选项默认为 `$HOME/.cargo/bin/rustfmt`
## 快捷键
| 快捷键 | 功能描述 |
| --------------- | --------------------------------------- |
| `SPC l d` / `K` | 展示光标函数或变量相关文档 |
| `SPC l e` | 重命名光标函数或变量(需要 `lsp` 模块) |
| `g d` | 跳至函数或变量定义处 |
| `SPC l s` | 跳至函数或变量定义处 (split) |
| `SPC l x` | 跳至函数或变量定义处 (vertical) |
| 快捷键 | 功能描述 |
| --------------- | ---------------------------------------------- |
| `g d` | 跳至函数或变量定义处 |
| `SPC l d` / `K` | 展示光标函数或变量相关文档 |
| `SPC l s` | 跳至函数或变量定义处 (split) |
| `SPC l x` | 跳至函数或变量定义处 (vertical) |
| `SPC l f` | 格式化当前文件代码 |
| `SPC l e` | 重命名光标函数或变量(需要 `lsp` 模块) |
| `SPC l u` | 显示光标函数或变量的所有引用 (需要 `lsp` 模块) |
| `SPC l c b` | 运行 `cargo build` |
| `SPC l c c` | 运行 `cargo clean` |
| `SPC l c f` | 运行 `cargo fmt` |
| `SPC l c t` | 运行 `cargo test` |
| `SPC l c u` | 运行 `cargo update` |
| `SPC l c B` | 运行 `cargo bench` |
| `SPC l c D` | 运行 `cargo doc` |
| `SPC l c r` | 运行 `cargo run` |
### 运行当前脚本
在编辑 Rust 文件时,可通过快捷键 `SPC l r` 快速异步运行当前文件,运行结果会展示在一个独立的执行窗口内。

View File

@ -13,7 +13,6 @@ description: "This layer is for Rust development, provide autocompletion, syntax
- [Layer](#layer)
- [Layer options](#layer-options)
- [Key bindings](#key-bindings)
- [Code runner](#code-runner)
<!-- vim-markdown-toc -->
@ -29,6 +28,9 @@ This layer is for Rust development.
- Documentation lookup
- Jump to the definition.
- Find references
- Rename symbol
- Cargo integration
- Code formatting
SpaceVim also provides code runner and Language Server protocol support for Rust. To enable language server protocol
for Rust, you need to load `lsp` layer for Rust.
@ -46,18 +48,28 @@ To use this configuration layer, update custom configuration file with:
## Layer options
- `recommended-style`: Enable/Disable recommended code style for rust. This option is disabled by default.
- `recommended-style`: 1/0 (Enable/Disable) recommended code style for rust. This option is disabled by default.
- `format-autosave`: 1/0 (Enable/Disable) format current buffer after saved. This option is disabled by default.
- `racer-cmd`: the directory of `racer` binary. This option is `$HOME/.cargo/bin/racer` by default.
- `rustfmt-cmd`: the directory of `rustfmt` binary. This option is `$HOME/.cargo/bin/rustfmt` by default.
## Key bindings
| Key bindings | Descriptions |
| --------------- | -------------------------------- |
| `SPC l d` / `K` | Show doc of cursor symbol |
| `SPC l e` | Rename symbol (need `lsp` layer) |
| `g d` | Jump to definition |
| `SPC l s` | Jump to definition (split) |
| `SPC l x` | Jump to definition (vertical) |
| Key bindings | Descriptions |
| --------------- | ---------------------------------- |
| `g d` | Jump to definition |
| `SPC l d` / `K` | Show doc of cursor symbol |
| `SPC l s` | Jump to definition (split) |
| `SPC l x` | Jump to definition (vertical) |
| `SPC l f` | Format current buffer |
| `SPC l e` | Rename symbol (need `lsp` layer) |
| `SPC l u` | Show references (need `lsp` layer) |
| `SPC l c b` | Run `cargo build` |
| `SPC l c c` | Run `cargo clean` |
| `SPC l c f` | Run `cargo fmt` |
| `SPC l c t` | Run `cargo test` |
| `SPC l c u` | Run `cargo update` |
| `SPC l c B` | Run `cargo bench` |
| `SPC l c D` | Run `cargo doc` |
| `SPC l c r` | Run `cargo run` |
### Code runner
To running current script, you can press `SPC l r` to run current file without loss focus, and the result will be shown in a runner buffer.