Rust

Rustのサポートは、Zedでネイティブに利用できます。

インレイヒント

以下の設定を使用して、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_optionsrust-analyzerのターゲットディレクトリを設定できます

{
  "lsp": {
    "rust-analyzer": {
      "initialization_options": {
        "rust": {
          "analyzerTargetDir": true
        }
      }
    }
  }
}

trueの設定では、ターゲットディレクトリがtarget/rust-analyzerに設定されます。trueの代わりに、"target/analyzer"などの文字列を使用して、カスタムディレクトリを設定できます。

詳細なサーバー設定

Rust-analyzer マニュアルでは、rust-analyzer言語サーバーのさまざまな機能と設定オプションについて説明しています。ZedのRust-analyzerは既定のパラメーターで実行されます。

大規模プロジェクトとパフォーマンス

大規模プロジェクトでリソースを大量に使用する場合の主な注意点の1つは、以下の機能の組み合わせです

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-clickを使用してエラーに移動し、保存時のチェック機能を制限または無効にすることができます。

保存時のチェック機能は、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"
                        }
                    }
                }
            }
        }
    }
}