mirror of
https://github.com/SpaceVim/SpaceVim.git
synced 2025-04-14 23:49:19 +08:00
Add post for rust (#3199)
This commit is contained in:
parent
68f821be40
commit
4938183571
@ -68,13 +68,17 @@ function! SpaceVim#layers#lang#rust#plugins() abort
|
||||
endfunction
|
||||
|
||||
function! SpaceVim#layers#lang#rust#config() abort
|
||||
call SpaceVim#plugins#runner#reg_runner('rust', [
|
||||
\ 'rustc %s -o #TEMP#',
|
||||
\ '#TEMP#'])
|
||||
call SpaceVim#plugins#repl#reg('rust', 'rustup run nightly-2016-08-01 ~/.cargo/bin/rusti')
|
||||
let g:racer_experimental_completer = 1
|
||||
let g:racer_cmd = s:racer_cmd ==# ''
|
||||
\ ? get(g:, 'racer_cmd', $HOME . '/.cargo/bin/racer')
|
||||
\ : 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
|
||||
\ ? 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
|
||||
|
||||
@ -100,6 +104,7 @@ function! SpaceVim#layers#lang#rust#set_variable(var) abort
|
||||
endfunction
|
||||
|
||||
function! s:language_specified_mappings() abort
|
||||
call SpaceVim#mapping#space#langSPC('nmap', ['l', 'r'], 'call SpaceVim#plugins#runner#open()', 'execute current file', 1)
|
||||
call SpaceVim#mapping#space#langSPC('nmap', ['l', 's'],
|
||||
\ '<Plug>(rust-def-split)', 'rust-def-split', 0)
|
||||
call SpaceVim#mapping#space#langSPC('nmap', ['l', 'x'],
|
||||
@ -144,9 +149,19 @@ function! s:language_specified_mappings() abort
|
||||
call SpaceVim#mapping#space#langSPC('nmap', ['l', 'd'],
|
||||
\ '<Plug>(rust-doc)', 'show documentation', 1)
|
||||
endif
|
||||
|
||||
call SpaceVim#mapping#space#langSPC('nnoremap', ['l', 'f'],
|
||||
\ 'call rustfmt#Format()' ,'format file', 1)
|
||||
let g:_spacevim_mappings_space.l.s = {'name' : '+Send'}
|
||||
call SpaceVim#mapping#space#langSPC('nmap', ['l','s', 'i'],
|
||||
\ 'call SpaceVim#plugins#repl#start("rust")',
|
||||
\ 'start REPL process', 1)
|
||||
call SpaceVim#mapping#space#langSPC('nmap', ['l','s', 'l'],
|
||||
\ 'call SpaceVim#plugins#repl#send("line")',
|
||||
\ 'send line and keep code buffer focused', 1)
|
||||
call SpaceVim#mapping#space#langSPC('nmap', ['l','s', 'b'],
|
||||
\ 'call SpaceVim#plugins#repl#send("buffer")',
|
||||
\ 'send buffer and keep code buffer focused', 1)
|
||||
call SpaceVim#mapping#space#langSPC('nmap', ['l','s', 's'],
|
||||
\ 'call SpaceVim#plugins#repl#send("selection")',
|
||||
\ 'send selection and keep code buffer focused', 1)
|
||||
endfunction
|
||||
|
||||
function! s:gotodef() abort
|
||||
@ -161,4 +176,13 @@ function! s:execCMD(cmd) abort
|
||||
call SpaceVim#plugins#runner#open(a:cmd)
|
||||
endfunction
|
||||
|
||||
" vim:set et sw=2 cc=80:
|
||||
"
|
||||
"#用于更新 toolchain
|
||||
"
|
||||
" set RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
|
||||
"
|
||||
" #用于更新 rustup
|
||||
"
|
||||
" set RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup
|
||||
"
|
||||
" vim:set et sw=2 cc=80
|
||||
|
87
docs/_posts/2019-10-25-use-vim-as-a-rust-ide.md
Normal file
87
docs/_posts/2019-10-25-use-vim-as-a-rust-ide.md
Normal file
@ -0,0 +1,87 @@
|
||||
---
|
||||
title: "Use Vim as a Rust IDE"
|
||||
categories: [tutorials, blog]
|
||||
image: https://user-images.githubusercontent.com/13142418/75607213-9afbb880-5b2f-11ea-8569-5f39142f134b.png
|
||||
excerpt: "A general guide for using SpaceVim as Rust IDE, including layer configuration, requiems installation and usage."
|
||||
type: BlogPosting
|
||||
comments: true
|
||||
commentsID: "Use Vim as a Rust IDE"
|
||||
---
|
||||
|
||||
# [Blogs](../blog/) >> Use Vim as a Rust IDE
|
||||
|
||||
This is a general guide for using SpaceVim as a Rust IDE, including layer configuration and usage.
|
||||
Each of the following sections will be covered:
|
||||
|
||||
|
||||
<!-- vim-markdown-toc GFM -->
|
||||
|
||||
- [Enable language layer](#enable-language-layer)
|
||||
- [Code completion](#code-completion)
|
||||
- [Syntax linting](#syntax-linting)
|
||||
- [Jump to test file](#jump-to-test-file)
|
||||
- [running code](#running-code)
|
||||
- [Code formatting](#code-formatting)
|
||||
- [REPL support](#repl-support)
|
||||
|
||||
<!-- vim-markdown-toc -->
|
||||
|
||||
### Enable language layer
|
||||
|
||||
To add rust language support in SpaceVim, you need to enable the `lang#rust` layer. Press `SPC f v d` to open
|
||||
SpaceVim configuration file, and add the following snippet:
|
||||
|
||||
```toml
|
||||
[[layers]]
|
||||
name = "lang#rust"
|
||||
```
|
||||
|
||||
For more info, you can read the [lang#rust](../layers/lang/rust/) layer documentation.
|
||||
|
||||
### Code completion
|
||||
|
||||
`lang#rust` layer will load the rust plugin automatically, unless it's overriden in your `init.toml`.
|
||||
The completion menu will be opened as you type.
|
||||
|
||||
### 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 rustc asynchronously.
|
||||
|
||||
### Jump to test file
|
||||
|
||||
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 the following content:
|
||||
|
||||
```json
|
||||
{
|
||||
"src/*.rs": {"alternate": "test/test_{}.rs"},
|
||||
"test/test_*.rs": {"alternate": "src/{}.rs"}
|
||||
}
|
||||
```
|
||||
|
||||
With this configuration, you can jump between the source code and test file via command `:A`
|
||||
|
||||
### running code
|
||||
|
||||
To run current script, you can press `SPC l r`, and a split window
|
||||
will be openen, the output of the script will be shown in this window.
|
||||
It is running asynchronously, and will not block your Vim.
|
||||
|
||||

|
||||
|
||||
### 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 rustfmt:
|
||||
|
||||
```sh
|
||||
rustup component add rustfmt
|
||||
```
|
||||
|
||||
### REPL support
|
||||
|
||||
Start a `rusti` inferior REPL process with `SPC l s i`. After the REPL process being started, you can
|
||||
send code to inferior process. All key bindings prefix with `SPC l s`, including sending line, sending selection or even
|
||||
send whole buffer.
|
||||
|
116
docs/_posts/2020-02-26-use-vim-as-a-rust-ide.md
Normal file
116
docs/_posts/2020-02-26-use-vim-as-a-rust-ide.md
Normal file
@ -0,0 +1,116 @@
|
||||
---
|
||||
title: "使用 Vim 搭建 Rust 开发环境"
|
||||
categories: [tutorials_cn, blog_cn]
|
||||
image: https://user-images.githubusercontent.com/13142418/75607213-9afbb880-5b2f-11ea-8569-5f39142f134b.png
|
||||
excerpt: "这篇文章主要介绍如何使用 SpaceVim 搭建 Rust 的开发环境,简介 lang#rust 模块所支持的功能特性以及使用技巧"
|
||||
permalink: /cn/:title/
|
||||
lang: zh
|
||||
type: BlogPosting
|
||||
comments: true
|
||||
commentsID: "使用 Vim 搭建 Rust 开发环境"
|
||||
---
|
||||
|
||||
# [Blogs](../blog/) >> 使用 Vim 搭建 Rust 开发环境
|
||||
|
||||
SpaceVim 是一个模块化的 Vim IDE,针对 Rust 这一语言的支持主要依靠 `lang#rust` 模块以及与之相关的其它模块。
|
||||
的这篇文章主要介绍如何使用 SpaceVim 搭建 Rust 的开发环境,侧重介绍跟 Rust 开发相关使用技巧。
|
||||
在阅读这篇文章之前,可以先阅读《[使用 Vim 搭建基础的开发环境](../use-vim-as-ide/)》,对语言相关以外的功能有一个大致的了解。
|
||||
|
||||
<!-- vim-markdown-toc GFM -->
|
||||
|
||||
- [安装模块](#安装模块)
|
||||
- [代码自动补全](#代码自动补全)
|
||||
- [语法检查](#语法检查)
|
||||
- [工程文件跳转](#工程文件跳转)
|
||||
- [代码格式化](#代码格式化)
|
||||
- [快速运行](#快速运行)
|
||||
- [交互式编程](#交互式编程)
|
||||
|
||||
<!-- vim-markdown-toc -->
|
||||
|
||||
### 安装模块
|
||||
|
||||
SpaceVim 初次安装时默认并未启用相关语言模块。首先需要启用
|
||||
`lang#rust` 模块,通过快捷键 `SPC f v d` 打开配置文件,添加如下片断:
|
||||
|
||||
```toml
|
||||
[[layers]]
|
||||
name = "lang#rust"
|
||||
```
|
||||
|
||||
启用 `lang#rust` 模块后,在打开 Rust 文件时,就可以使用语言专属快捷键,这些快捷键都是以 `SPC l` 为前缀的。
|
||||
|
||||
### 代码自动补全
|
||||
|
||||
`autocomplete` 模块为 SpaceVim 提供了自动补全功能,目前针对 Rust 而言,比较好的补全方案是配合使用 lsp 模块:
|
||||
|
||||
```toml
|
||||
[[layers]]
|
||||
name = "lsp"
|
||||
```
|
||||
|
||||
lsp 模块默认使用 [rls](https://github.com/rust-lang/rls) 作为 Rust 的语言服务器后台命令,使用如下命令安装rls:
|
||||
|
||||
```sh
|
||||
rustup component add rls rust-analysis rust-src
|
||||
```
|
||||
|
||||
在配置文件中添加如下内容即可为 Rust 启用语言服务器:
|
||||
|
||||
```toml
|
||||
[[layers]]
|
||||
name = "lsp"
|
||||
filetypes = [
|
||||
"rust"
|
||||
]
|
||||
[layers.override_cmd]
|
||||
php = ["rls"]
|
||||
```
|
||||
|
||||
### 语法检查
|
||||
|
||||
`checkers` 模块为 SpaceVim 提供了语法检查的功能,该模块默认已经载入。该模块默认使用 [neomake](https://github.com/neomake/neomake)
|
||||
这一异步语法检查工具。对于 Rust 的支持,是通过异步调用 rustc。
|
||||
|
||||
### 工程文件跳转
|
||||
|
||||
SpaceVim 自带工程管理插件,可以识别项目根目录,自动跳转 alternate 文件。需要在项目根目录添加工程文件 `.project_alt.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"src/*.rs": {"alternate": "test/{}.rs"},
|
||||
"test/*.rs": {"alternate": "src/{}.rs"}
|
||||
}
|
||||
```
|
||||
|
||||
通过以上的配置,就可以使用命令 `:A` 在源文件和测试文件之间进行跳转。
|
||||
|
||||
|
||||
### 代码格式化
|
||||
|
||||
Rust 代码格式化,主要依赖 `format` 模块,同时需要安装相关的后台命令 [rustfmt](https://github.com/rust-lang/rustfmt),
|
||||
默认快捷键为 `SPC b f` :
|
||||
|
||||
```toml
|
||||
[[layers]]
|
||||
name = "format"
|
||||
```
|
||||
|
||||
使用 rustup 安装 rustfmt:
|
||||
|
||||
```sh
|
||||
rustup component add rustfmt
|
||||
```
|
||||
|
||||
|
||||
### 快速运行
|
||||
|
||||
在编辑 Rust 文件时,可以快速运行当前文件,默认的快捷键是 `SPC l r` 。按下后,
|
||||
会在屏幕下方打开一个插件窗口,运行的结果会被展示在窗口内。于此同时,光标并不会跳到该插件窗口,避免影响编辑。在这里需要说明下,
|
||||
|
||||

|
||||
|
||||
### 交互式编程
|
||||
|
||||
在编辑 Rust 文件时,可通过快捷键 `SPC l s i` 启动 `rusti` 交互窗口,
|
||||
之后使用快捷键将代码发送至解释器。默认快捷键都以 `SPC l s` 为前缀。
|
Loading…
x
Reference in New Issue
Block a user