lsp.mdx 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. ---
  2. title: LSPサーバー
  3. description: OpenCode は LSP サーバーと統合します。
  4. ---
  5. OpenCode は言語サーバー プロトコル (LSP) と統合して、LLM がコードベースと対話できるようにします。診断を使用して LLM にフィードバックを提供します。
  6. ---
  7. ## 内蔵
  8. OpenCode には、一般的な言語用のいくつかの組み込み LSP サーバーが付属しています。
  9. | LSP Server | Extensions | Requirements |
  10. | ------------------ | ------------------------------------------------------------------- | ------------------------------------------------------------ |
  11. | astro | .astro | Auto-installs for Astro projects |
  12. | bash | .sh, .bash, .zsh, .ksh | Auto-installs bash-language-server |
  13. | clangd | .c, .cpp, .cc, .cxx, .c++, .h, .hpp, .hh, .hxx, .h++ | Auto-installs for C/C++ projects |
  14. | csharp | .cs | `.NET SDK` installed |
  15. | clojure-lsp | .clj, .cljs, .cljc, .edn | `clojure-lsp` command available |
  16. | dart | .dart | `dart` command available |
  17. | deno | .ts, .tsx, .js, .jsx, .mjs | `deno` command available (auto-detects deno.json/deno.jsonc) |
  18. | elixir-ls | .ex, .exs | `elixir` command available |
  19. | eslint | .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts, .vue | `eslint` dependency in project |
  20. | fsharp | .fs, .fsi, .fsx, .fsscript | `.NET SDK` installed |
  21. | gleam | .gleam | `gleam` command available |
  22. | gopls | .go | `go` command available |
  23. | hls | .hs, .lhs | `haskell-language-server-wrapper` command available |
  24. | jdtls | .java | `Java SDK (version 21+)` installed |
  25. | kotlin-ls | .kt, .kts | Auto-installs for Kotlin projects |
  26. | lua-ls | .lua | Auto-installs for Lua projects |
  27. | nixd | .nix | `nixd` command available |
  28. | ocaml-lsp | .ml, .mli | `ocamllsp` command available |
  29. | oxlint | .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts, .vue, .astro, .svelte | `oxlint` dependency in project |
  30. | php intelephense | .php | Auto-installs for PHP projects |
  31. | prisma | .prisma | `prisma` command available |
  32. | pyright | .py, .pyi | `pyright` dependency installed |
  33. | ruby-lsp (rubocop) | .rb, .rake, .gemspec, .ru | `ruby` and `gem` commands available |
  34. | rust | .rs | `rust-analyzer` command available |
  35. | sourcekit-lsp | .swift, .objc, .objcpp | `swift` installed (`xcode` on macOS) |
  36. | svelte | .svelte | Auto-installs for Svelte projects |
  37. | terraform | .tf, .tfvars | Auto-installs from GitHub releases |
  38. | tinymist | .typ, .typc | Auto-installs from GitHub releases |
  39. | typescript | .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts | `typescript` dependency in project |
  40. | vue | .vue | Auto-installs for Vue projects |
  41. | yaml-ls | .yaml, .yml | Auto-installs Red Hat yaml-language-server |
  42. | zls | .zig, .zon | `zig` command available |
  43. 上記のファイル拡張子のいずれかが検出され、要件が満たされると、LSP サーバーは自動的に有効になります。
  44. :::note
  45. `OPENCODE_DISABLE_LSP_DOWNLOAD` 環境変数を `true` に設定すると、LSP サーバーの自動ダウンロードを無効にできます。
  46. :::
  47. ---
  48. ## 仕組み
  49. opencode がファイルを開くと、次のようになります。
  50. 1. 有効なすべての LSP サーバーに対してファイル拡張子をチェックします。
  51. 2. 適切な LSP サーバーがまだ実行されていない場合は開始します。
  52. ---
  53. ## 設定する
  54. Opencode 構成の `lsp` セクションを通じて LSP サーバーをカスタマイズできます。
  55. ```json title="opencode.json"
  56. {
  57. "$schema": "https://opencode.ai/config.json",
  58. "lsp": {}
  59. }
  60. ```
  61. 各 LSP サーバーは以下をサポートします。
  62. | プロパティ | タイプ | 説明 |
  63. | ---------------- | ------------ | ---------------------------------------------------------- |
  64. | `disabled` | ブール値 | LSP サーバーを無効にするには、これを `true` に設定します。 |
  65. | `command` | 文字列[] | LSP サーバーを起動するコマンド |
  66. | `extensions` | 文字列[] | この LSP サーバーが処理するファイル拡張子 |
  67. | `env` | オブジェクト | サーバーの起動時に設定する環境変数 |
  68. | `initialization` | オブジェクト | LSP サーバーに送信する初期化オプション |
  69. いくつかの例を見てみましょう。
  70. ---
  71. ### 環境変数
  72. LSP サーバーの起動時に `env` プロパティを使用して環境変数を設定します。
  73. ```json title="opencode.json" {5-7}
  74. {
  75. "$schema": "https://opencode.ai/config.json",
  76. "lsp": {
  77. "rust": {
  78. "env": {
  79. "RUST_LOG": "debug"
  80. }
  81. }
  82. }
  83. }
  84. ```
  85. ---
  86. ### 初期化オプション
  87. `initialization` プロパティを使用して、初期化オプションを LSP サーバーに渡します。これらは、LSP `initialize` リクエスト中に送信されるサーバー固有の設定です。
  88. ```json title="opencode.json" {5-9}
  89. {
  90. "$schema": "https://opencode.ai/config.json",
  91. "lsp": {
  92. "typescript": {
  93. "initialization": {
  94. "preferences": {
  95. "importModuleSpecifierPreference": "relative"
  96. }
  97. }
  98. }
  99. }
  100. }
  101. ```
  102. :::note
  103. 初期化オプションは LSP サーバーによって異なります。利用可能なオプションについては、LSP サーバーのドキュメントを確認してください。
  104. :::
  105. ---
  106. ### LSPサーバーの無効化
  107. **すべて** LSP サーバーをグローバルに無効にするには、`lsp` を `false` に設定します。
  108. ```json title="opencode.json" {3}
  109. {
  110. "$schema": "https://opencode.ai/config.json",
  111. "lsp": false
  112. }
  113. ```
  114. **特定** LSP サーバーを無効にするには、`disabled` を `true` に設定します。
  115. ```json title="opencode.json" {5}
  116. {
  117. "$schema": "https://opencode.ai/config.json",
  118. "lsp": {
  119. "typescript": {
  120. "disabled": true
  121. }
  122. }
  123. }
  124. ```
  125. ---
  126. ### カスタムLSPサーバー
  127. コマンドとファイル拡張子を指定して、カスタム LSP サーバーを追加できます。
  128. ```json title="opencode.json" {4-7}
  129. {
  130. "$schema": "https://opencode.ai/config.json",
  131. "lsp": {
  132. "custom-lsp": {
  133. "command": ["custom-lsp-server", "--stdio"],
  134. "extensions": [".custom"]
  135. }
  136. }
  137. }
  138. ```
  139. ---
  140. ## 追加情報
  141. ### PHP インテリフェンス
  142. PHP Intelephense は、ライセンス キーを通じてプレミアム機能を提供します。ライセンス キーを指定するには、次の場所にあるテキスト ファイルにキー (のみ) を配置します。
  143. - macOS/Linux の場合: `$HOME/intelephense/licence.txt`
  144. - Windows の場合: `%USERPROFILE%/intelephense/licence.txt`
  145. ファイルにはライセンス キーのみが含まれており、追加のコンテンツは含まれていません。