Rust 语言服务器 (RLS)

原文:Rust Language Server (RLS)
翻译:Peter

此项目处于发展的起始阶段。在某些情况下,可能会出现BUG;使用请谨慎。

RLS提供了一个在后台运行的服务器,提供了Rust编程的相关信息,包括IDE,编辑器和其它工具。它支持诸如“goto定义”,符号搜索,重新格式化和代码完成等功能,并支持重命名和重构。

RLS从编译器和Racer获取源数据。在可能的情况下,它使用来自编译器的精确而完整的数据。在某些情况下(例如代码实现,构建太慢),它使用Racer。

由于Rust编译器不支持端到端增量编译,因此我们无法提供完美的体验。然而,通过优化我们对编译器的使用并回归到Racer,我们可以为中小型的项目提供相当不错的体验。随着RLS和编译器的发展,我们将为越来越大的项目提供更好的体验。

RLS设计为前端独立。我们希望它将被不同的编辑器和IDE广泛采用。为了快速开发,我们提供了Visual Studio代码RLS前端的参考实现

设置

步骤一:安装rustup

可以在许多平台上安装rustup。这将有助于我们快速安装rls及其相关插件。

如果已经安装了rustup,请务必进行升级,以确保rustup为最新版本:

rustup self update

如果要使用 VSCode 扩展,可以略过步骤2和3。

步骤二:更新nightly

更新nightly编译器。您不必将其用作默认编译器, 但在你系统上需要安装的有:

rustup update nightly

步骤三:安装 RLS

安装 rustup 后, 请运行以下命令:

>rustup component add rls --toolchain nightly
rustup component add rust-analysis --toolchain nightly
rustup component add rust-src --toolchain nightly

如果您之前从未设置过Racer,则需要设置RUST_SRC_PATH变量。 为此,您可以按照Racer配置步骤进行操作。

扫描二维码关注公众号,回复: 2283199 查看本文章

运行

虽然 RLS 的建立是为了与许多 IDE 和编辑器一起工作, 但我们目前使用 VSCode 测试 RLS。

要使用 VSCode 运行,您需要安装最新的 VSCode 版本

接下来, 您需要运行 VSCode 扩展 (对于此步骤, 您需要安装最近的节点:

git clone https://github.com/rust-lang-nursery/rls-vscode  
cd rls-vscode  
npm install  
code .

VSCode将打开进入rls-vscode项目。 从这里,单击左侧的调试按钮。 接下来,点击顶部的绿色三角形。这将启动一个新的 VSCode 与 rls-VSCode 插件启用的实例。 VSCode设置“window.openFoldersInNewWindow”不能设置为“on”。 从那里,您可以使用RLS打开您的Rust项目。

当您在底部的状态栏中看到这一点时,您会知道它正在工作,并使用spinning指示器:

RLS analysis: working /

一旦你看到:

RLS analysis: done 

那么你可以拥有全套的功能。 您可以goto def,找到所有参考,重命名,goto类型等。完成也可以使用Racer 提供的启发式。 当您键入时,您的代码将被检查,并且错误发生时将报告错误提示。 您可以悬停查看错误的文本。

配置

RLS可以根据每个项目进行配置,使用官方Visual Studio代码扩展,这将通过工作区设置文件settings.json完成。

其他编辑器将有自己的方式发送workspace / DidChangeConfiguration方法。

此文件中的条目将影响RLS的运行方式以及如何构建项目。

目前我们接受以下操作:

  • build_lib (bool, defaults to false) checks the project as if you passed the –lib argument to cargo. Mutually exclusive with, and preferred over build_bin.
  • build_bin (String, defaults to “”) checks the project as if you passed – bin argument to cargo. Mutually exclusive with build_lib.
  • cfg_test (bool, defaults to true) checks the project as if you were running cargo test rather than cargo build. I.e., compiles (but does not run) test code.
  • unstable_features (bool, defaults to false) enables unstable features. Currently, this includes only range formatting.
  • sysroot (String, defaults to “”) if the given string is not empty, use the given path as the sysroot for all rustc invocations instead of trying to detect the sysroot automatically
  • target (String, defaults to “”) if the given string is not empty, use the given target triple for all rustc invocations
  • wait_to_build (u64, defaults to 500) time in milliseconds between receiving a change notification and starting build
  • workspace_mode (bool, defaults to false) Experimental mode. When turned on, RLS will try to scan current workspace and analyze every package in it.
  • analyze_package (String, defaults to “”) When workspace_mode is enabled, analysis will be only provided for the specified package (runs as if -p was passed).

疑难解答

有关调试和疑问的提示,请参见debugging.md

参与

您可以查看contributing.md,以了解更多有关该项目的参与方式。

猜你喜欢

转载自blog.csdn.net/u011886336/article/details/78091400