ft_rust

ft_rust.txt Rust 的文件类型插件 译者: Willis

内 容 rust

1. 简介 rust-intro 2. 设置 rust-settings 3. 命令 rust-commands 4. 映射 rust-mappings

简 介 rust-intro

本插件提供 Rust 文件类型的语法和支持功能。完整功能需要 Vim 8 或更高版本。有些 命令在之前的版本不可用。

设 置 rust-settings

本插件有若干可在 vimrc 中定义的变量,用来改变插件的行为。 部分变量可设为局部于缓冲区 ( :b 前缀),缓冲区局部变量优先于全局 g: 的对标变 量。 g:rustc_path g:rustc_path 此选项设置 rustc 的路径,用于 :RustRun:RustExpand 命令。如果不 设,"rustc" 会从 $PATH 中寻找: let g:rustc_path = $HOME."/bin/rustc" g:rustc_makeprg_no_percent g:rustc_makeprg_no_percent 此选项如设为 1,'makeprg' 缺省为 "rustc" 而不是 "rustc %": let g:rustc_makeprg_no_percent = 1 g:rust_conceal g:rust_conceal 置位此选项打开基本 conceal 支持: let g:rust_conceal = 1 g:rust_conceal_mod_path g:rust_conceal_mod_path 置位此选项隐藏 conceal 路径连接符 "::": let g:rust_conceal_mod_path = 1 g:rust_conceal_pub g:rust_conceal_pub 置位此选项隐藏 conceal "pub" 符: let g:rust_conceal_pub = 1 g:rust_recommended_style g:rust_recommended_style 置位此选项设置 vim 缩进和 textwidth 以遵循 rust 标准库的风格惯例 (即, 缩进为 4 个空格,'textwidth' 设为 99)。缺省打开此选项。要关闭之: let g:rust_recommended_style = 0 g:rust_fold 置位此选项打开 folding : let g:rust_fold = 1 值 效果 0 无折叠 1 折叠花括号块。缺省打开全部折叠。 2 折叠花括号块。'foldlevel' 仍用其全局值 (缺省关闭全部折 叠)。 g:rust_bang_comment_leader g:rust_bang_comment_leader 设置此选项为 1 以保留使用 /*! 语法的多行文档注释的中间注释字符串: let g:rust_bang_comment_leader = 1 g:rust_use_custom_ctags_defs g:rust_use_custom_ctags_defs 设置此选项为 1 以应用自定的 ctags Rust 定义而不用 rust.vim 自带的: let g:rust_use_custom_ctags_defs = 1 注意: rust.vim 内建定义只在有安装 Tagbar Vim 插件, 当没检测到 Universal Ctags 的情况下才会应用。这是因为 Universal Ctags 已有内建和 Tagbar 配合使用的 Rust 支持。 另外 注意 除非使用的是 Universal Ctags, 其它的 ctags 不会在生成 Vim 用 于在不同源文件间转向到各种定义的 tags 文件时自动应用。如果要生成 tags 文件,可把 rust.vim/ctags/rust.ctags 复制到自己的 ~/.ctags 里。 g:ftplugin_rust_source_path g:ftplugin_rust_source_path 此选项设置加到 'path' 前部的路径,用于 Rust 源文件: let g:ftplugin_rust_source_path = $HOME.'/dev/rust' g:rustfmt_command g:rustfmt_command 此选项设置 $PATH 里 'rustfmt' 可执行文件的名字。如果不设,缺省为 'rustfmt' : let g:rustfmt_command = 'rustfmt' g:rustfmt_autosave g:rustfmt_autosave 设置此选项为 1 使缓冲区保存时自动执行 :RustFmt 。如果不设,缺省为 0: let g:rustfmt_autosave = 0 也有缓冲区局部 b:rustfmt_autosave 用于相同目的,且优先于全局设置。 g:rustfmt_autosave_if_config_present g:rustfmt_autosave_if_config_present 设置此选项为 1 时,如果待编辑文件的任何一级父目录里有 rustfmt.toml 文件,自动置位 b:rustfmt_autosave 。如果不设,缺省为 0: let g:rustfmt_autosave_if_config_present = 0 用于仅在有 rustfmt.toml 配置的项目里才会在保存时执行 rustfmt 。 也有缓冲区局部 b:rustfmt_autosave_if_config_present 用于相同目的,且优 先于全局设置。 g:rustfmt_fail_silently g:rustfmt_fail_silently 设置此选项为 1 则 'rustfmt' 不在 location-list 中保存错误。如果不 设,缺省为 0: let g:rustfmt_fail_silently = 0 g:rustfmt_options g:rustfmt_options 此选项设置传递给 'rustfmt' 的选项字符串。write-mode 已经设为 'overwrite'。如果不设,缺省为 '' : let g:rustfmt_options = '' g:rustfmt_emit_files g:rustfmt_emit_files 如无设定,rust.vim 试图根据 rustfmt 报告的版本来检测传递给该命令的合适 的参数。否则,它会决定运行 rustfmt 时用 '--emit=files' (选项值为 1) 或 是 '--write-mode=overwrite'。 let g:rustfmt_emit_files = 0 g:rust_playpen_url g:rust_playpen_url 此选项覆盖 playpen 使用的 url: let g:rust_playpen_url = 'https://play.rust-lang.org/' g:rust_shortener_url g:rust_shortener_url 此选项覆盖短 url 生成器使用的 url: let g:rust_shortener_url = 'https://is.gd/' g:rust_clip_command g:rust_clip_command 此选项设置在你的 OS 里把 Rust Play url 复制到剪贴板的命令: let g:rust_clip_command = 'xclip -selection clipboard' g:cargo_makeprg_params g:cargo_makeprg_params 此选项设置传给 cargo 的参数字符串。如果不设,缺省为 '$*' : let g:cargo_makeprg_params = 'build' g:cargo_shell_command_runner g:cargo_shell_command_runner 此选项可用于改变为 cargo 命令 :Cargo:Cbuild:Crun 等运行外壳 命令的方法。 缺省,使用 :terminal 来在终端窗口里异步运行外壳命令。但如果你更想用 :! 来运行命令,可指定: let g:cargo_shell_command_runner = '!' Syntastic 集成 rust-syntastic

此插件自动和 Syntastic 检查器集成。提供了两种检查器: 'rustc''cargo'。后者 调用 'Cargo' 以构建代码,而前者把单个编辑过的 '.rs' 文件作为编译目标直接发送给 Rust 编译器, rustc 。 当前 Rust 代码构建基本完全都用 Cargo,因此缺省的检查器是 'cargo' let g:syntastic_rust_checkers = ['cargo'] 要改变之,把 g:syntastic_rust_checkers 设为其他值。 g:rust_cargo_avoid_whole_workspace b:rust_cargo_avoid_whole_workspace g:rust_cargo_avoid_whole_workspace 编辑 Cargo 工作空间组成部分的箱 (crate) 时,如果此选项设为 1 (缺省), 直接在箱目录而不是在工作空间目录里执行 'cargo'。设为 0 则反之 - 不过要 小心在巨大工作空间里,Cargo 命令会消耗更多时间,而且 Syntastic 错误列 表会包含工作空间里的所有箱里的错误。 let g:rust_cargo_avoid_whole_workspace = 0 g:rust_cargo_check_all_targets b:rust_cargo_check_all_targets g:rust_cargo_check_all_targets 设为 1 时,Syntastic 执行 cargo 时传入 --all-targets 选项,以 lint 包里的所有目标 (targets)。 缺省为 0。 g:rust_cargo_check_all_features b:rust_cargo_check_all_features g:rust_cargo_check_all_features 设为 1 时,Syntastic 执行 cargo 时传入 --all-features 选项,以 lint 包里的所有特性 (features)。 缺省为 0。 g:rust_cargo_check_examples b:rust_cargo_check_examples g:rust_cargo_check_examples 设为 1 时,Syntastic 执行 cargo 时传入 --examples 选项,lint 时不会 排除示例。示例通常在箱的 examples/ 目录里。 缺省为 0。 g:rust_cargo_check_tests b:rust_cargo_check_tests g:rust_cargo_check_tests 设为 1 时,Syntastic 执行 cargo 时传入 --tests 选项,lint 时不会排除 测试。测试通常在箱的 tests/ 目录里。 缺省为 0。 g:rust_cargo_check_benches b:rust_cargo_check_benches g:rust_cargo_check_benches 设为 1 时,Syntastic 执行 cargo 时传入 --benches 选项,基准测试通常 在箱的 benches/ 目录里。 缺省为 0。 auto-pairs 集成 rust-auto-pairs

此插件自动配置 auto-pairs 插件,使之不加倍单引号,因为单引号更常用于生命周期标 注而不是单字符常量。 g:rust_keep_autopairs_default g:rust_keep_autopairs_default 不为 Rust 文件类型修改 auto-pairs 缺省值。缺省为 0。

命 令 rust-commands

调用 Cargo

此插件定义了在 Vim 里调用 Cargo 的简单的快捷方式。 :Cargo <args> :Cargo 运行 'cargo',传入给定的参数。 :Cbuild <args> :Cbuild 'cargo build` 的快捷方式。 :Cclean <args> :Cclean 'cargo clean` 的快捷方式。 :Cdoc <args> :Cdoc 'cargo doc` 的快捷方式。 :Cinit <args> :Cinit 'cargo init` 的快捷方式。 :Crun <args> :Crun 'cargo run` 的快捷方式。 :Ctest <args> :Ctest 'cargo test` 的快捷方式。 :Cupdate <args> :Cupdate 'cargo update` 的快捷方式。 :Cbench <args> :Cbench 'cargo bench` 的快捷方式。 :Csearch <args> :Csearch 'cargo search` 的快捷方式。 :Cpublish <args> :Cpublish 'cargo publish` 的快捷方式。 :Cinstall <args> :Cinstall 'cargo install` 的快捷方式。 :Cruntarget <args> :Cruntarget 根据当前打开的缓冲区决定运行 'cargo run --bin` 或者 'cargo run --example' 之一的快捷方式。 排版

:RustFmt :RustFmt 在当前缓冲区上执行 g:rustfmt_command 。如果设置了 g:rustfmt_options ,传递给该程序。 如果 g:rustfmt_fail_silently 为 0 (缺省),则 g:rustfmt_command 的错误会添加到 location-list 里。 如果 g:rustfmt_fail_silently 设为 1,则不会写入错误到 location-list 。 :RustFmtRange :RustFmtRange 在指定范围上执行 g:rustfmt_command 。其它信息见 :RustFmt 。 Playpen 集成

:RustPlay :RustPlay 此命令只有在安装了 web-api.vim 后才能工作 (可从这里找到 https://github.com/mattn/webapi-vim)。发送当前选择区,如果没有 选择过的话,则发送整个缓冲区到 Rust playpen,并发送缩短过的 URL 到 playpen。 g:rust_playpen_url 是 playpen 的基准 URL,缺省是 "https://play.rust-lang.org/"。 g:rust_shortener_url 是短 url 生成器的基准 url,缺省是 "https://is.gd/" g:rust_clip_command 是你的系统里把 playpen url 复制到剪贴板 的命令。 单个 Rust 文件的运行

注意: 这些命令仅用于运行单个 Rust 文件。一般 Rust 开发流程通常不会如此。如果要在 Vim 里构建 Rust 箱 (crate),可用 Vim 的 make、Synastic 或利用其他插件的功能。 :RustRun [args] :RustRun :RustRun! [rustc-args] [--] [args] 编译并运行当前文件。如果有未保存的改动,先用 :update 保存。 如果当前文件为无名缓冲区,先写入一个临时文件。编译后的程序总是 放在一个临时目录里,但从当前目录处执行。 :RustRun 接受的参数传递给编译后的程序。 如果指定 !,取而代之的是参数会传递给 rustc。"--" 参数可用来分 隔 rustc 的参数和传递给执行程序的参数。 g:rustc_path 如有定义,则用作 rustc 的路径。否则假定在 $PATH 中找到 rustc。 :RustExpand [args] :RustExpand :RustExpand! [TYPE] [args] 使用 --pretty 扩展当前文件,并在新分割中显示结果。如果当前文件 有未保存的改动,先用 :update 保存。如果当前文件为无名缓冲 区,先写入一个临时文件。 :RustExpand 接受的参数传递给 rustc。主要用来指定各种 --cfg 配置。 如果指定 !,首个参数是传给 rustc --pretty 的扩展类型。否则缺省 为 "expanded"。 g:rustc_path 如有定义,则用作 rustc 的路径。否则假定在 $PATH 中找到 rustc。 :RustEmitIr [args] :RustEmitIr 编译当前文件到 LLVM IR 并在新分割中显示结果。如果当前文件有未 保存的改动,先用 :update 保存。如果当前文件为无名缓冲区,先 写入一个临时文件。 :RustEmitIr 接受的参数传递给 rustc。 g:rustc_path 如有定义,则用作 rustc 的路径。否则假定在 $PATH 中找到 rustc。 :RustEmitAsm [args] :RustEmitAsm 编译当前文件到汇编程序,并在新分割中显示结果。如果当前文件有未 保存的改动,先用 :update 保存。如果当前文件为无名缓冲区,先 写入一个临时文件。 :RustEmitAsm 接受的参数传递给 rustc。 g:rustc_path 如有定义,则用作 rustc 的路径。否则假定在 $PATH 中找到 rustc。 运行测试

:[N]RustTest[!] [options] :RustTest 如果当前缓冲区在 cargo 项目里,用 "cargo test" 命令运行光标下 的测试。如果命令在光标下没找到测试函数,停止并报错。 给出 N 时,调整新窗口大小为 N 行或列。 给出 ! 时,运行所有测试,无视当前光标位置。 给出 [options] 时,用作传入 "cargo" 命令的参数。 如果当前缓冲区在 cargo 项目之外,作为后备,此命令运行 "rustc --test" 命令而非 "cargo test"。不管有无 ! 都会运行所有 的测试,因为 rustc 无法指定特定的测试函数。此时 [options] 是传 给 "rustc" 命令的参数。 接受可选的修饰符 (见 <mods> ): :tab RustTest :belowright 16RustTest :leftabove vert 80RustTest rust.vim 调试

:RustInfo :RustInfo 释出 Vim Rust 插件的调试信息。 :RustInfoToClipboard :RustInfoClipboard 把 Vim Rust 插件的调试信息保存在缺省寄存器里。 :RustInfoToFile [filename] :RustInfoToFile 把 Vim Rust 插件的调试信息保存在给定文件里,必要时覆盖之。

映 射 rust-mappings

此插件定义了 [[]] 的映射以支持悬挂缩进。

vim:tw=78:sw=4:noet:ts=8:ft=help:norl: