Rust
Zed 原生支援 Rust。
- Tree Sitter: tree-sitter-rust
- 語言伺服器: rust-analyzer
嵌入提示
以下設定可用於啟用 Rust 的嵌入提示
"inlayHints": {
"maxLength": null,
"lifetimeElisionHints": {
"useParameterNames": true,
"enable": "skip_trivial"
},
"closureReturnTypeHints": {
"enable": "always"
}
}
讓語言伺服器在 Zed 設定中啟用嵌入提示時傳回嵌入提示。
使用
"lsp": {
"$LANGUAGE_SERVER_NAME": {
"initialization_options": {
....
}
}
}
覆蓋這些設定。
更多資訊請參考 https://rust-analyzer.github.io/manual.html#inlay-hints。
目標目錄
可以在 initialization_options
中設定 rust-analyzer
目標目錄
{
"lsp": {
"rust-analyzer": {
"initialization_options": {
"rust": {
"analyzerTargetDir": true
}
}
}
}
}
設定為 true
會將目標目錄設為 target/rust-analyzer
。您可以使用字串(例如 "target/analyzer"
)而非 true
來設定自訂目錄。
更多伺服器設定
Rust-analyzer 手冊 描述了 rust-analyzer 語言伺服器的各種功能和設定選項。Zed 中的 Rust-analyzer 使用預設參數執行。
大型專案和效能
可能導致大型專案大量資源使用的一個主要問題是以下功能的組合
rust-analyzer.checkOnSave (default: true)
Run the check command for diagnostics on save.
rust-analyzer.check.workspace (default: true)
Whether --workspace should be passed to cargo check. If false, -p <package> will be passed instead.
rust-analyzer.cargo.allTargets (default: true)
Pass --all-targets to cargo invocation
這表示每次 Zed [自動] 儲存時,都會執行 cargo check --workspace --all-targets
命令,檢查整個專案(工作區)、lib、doc、test、bin、bench 和 其他目標。
雖然這在小型專案上運作良好,但它無法良好地擴展。
替代方案是使用 任務,因為 Zed 已經提供了 cargo check --workspace --all-targets
任務以及能夠透過 cmd/ctrl-點擊終端機輸出導航到錯誤,並限制或完全關閉儲存時檢查功能。
儲存時檢查功能負責根據 cargo check 輸出傳回部分診斷資訊,因此關閉它會限制 rust-analyzer 使用其自身的 診斷資訊。
請考慮手冊中更多 rust-analyzer.cargo.
、rust-analyzer.check.
和 rust-analyzer.diagnostics.
設定,以進行更精細的設定。以下是一段 Zed settings.json 的程式碼片段(編輯並儲存 lsp.rust-analyzer
區段後,語言伺服器將自動重新啟動)
"lsp": {
"rust-analyzer": {
"initialization_options": {
// get more cargo-less diagnostics from rust-analyzer,
// which might include false-positives (those can be turned off by their names)
"diagnostics": {
"experimental": {
"enable": true
}
},
// To disable the checking entirely
// (ignores all cargo and check settings below)
"checkOnSave": false,
// To check the `lib` target only.
"cargo": {
"allTargets": false
},
// Use `-p` instead of `--workspace` for cargo check
"check": {
"workspace": false
}
}
}
}
程式碼片段
有一種方法可以從 rust-analyzer 取得自訂完成項目,它會根據程式碼片段主體轉換程式碼
"lsp": {
"rust-analyzer": {
"initialization_options": {
"completion": {
"snippets": {
"custom": {
"Arc::new": {
"postfix": "arc",
"body": ["Arc::new(${receiver})"],
"requires": "std::sync::Arc",
"scope": "expr"
},
"Some": {
"postfix": "some",
"body": ["Some(${receiver})"],
"scope": "expr"
},
"Ok": {
"postfix": "ok",
"body": ["Ok(${receiver})"],
"scope": "expr"
},
"Rc::new": {
"postfix": "rc",
"body": ["Rc::new(${receiver})"],
"requires": "std::rc::Rc",
"scope": "expr"
},
"Box::pin": {
"postfix": "boxpin",
"body": ["Box::pin(${receiver})"],
"requires": "std::boxed::Box",
"scope": "expr"
},
"vec!": {
"postfix": "vec",
"body": ["vec![${receiver}]"],
"description": "vec![]",
"scope": "expr"
}
}
}
}
}
}
}