File size: 11,566 Bytes
0dbc9de | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | ---
title: LSPサーバー
description: OpenCode は LSP サーバーと統合します。
---
OpenCode は Language Server Protocol (LSP) サーバーと統合し、診断を agent へのフィードバックとして使用できます。
---
## 組み込み
OpenCode には、一般的な言語用のいくつかの組み込み LSP サーバーが付属しています。
| LSP Server | Extensions | Requirements |
| ------------------ | ------------------------------------------------------------------- | ------------------------------------------------------------ |
| astro | .astro | Auto-installs for Astro projects |
| bash | .sh, .bash, .zsh, .ksh | Auto-installs bash-language-server |
| clangd | .c, .cpp, .cc, .cxx, .c++, .h, .hpp, .hh, .hxx, .h++ | Auto-installs for C/C++ projects |
| csharp | .cs | `.NET SDK` installed |
| clojure-lsp | .clj, .cljs, .cljc, .edn | `clojure-lsp` command available |
| dart | .dart | `dart` command available |
| deno | .ts, .tsx, .js, .jsx, .mjs | `deno` command available (auto-detects deno.json/deno.jsonc) |
| elixir-ls | .ex, .exs | `elixir` command available |
| eslint | .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts, .vue | `eslint` dependency in project |
| fsharp | .fs, .fsi, .fsx, .fsscript | `.NET SDK` installed |
| gleam | .gleam | `gleam` command available |
| gopls | .go | `go` command available |
| hls | .hs, .lhs | `haskell-language-server-wrapper` command available |
| jdtls | .java | `Java SDK (version 21+)` installed |
| julials | .jl | `julia` and `LanguageServer.jl` installed |
| kotlin-ls | .kt, .kts | Auto-installs for Kotlin projects |
| lua-ls | .lua | Auto-installs for Lua projects |
| nixd | .nix | `nixd` command available |
| ocaml-lsp | .ml, .mli | `ocamllsp` command available |
| oxlint | .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts, .vue, .astro, .svelte | `oxlint` dependency in project |
| php intelephense | .php | Auto-installs for PHP projects |
| prisma | .prisma | `prisma` command available |
| pyright | .py, .pyi | `pyright` dependency installed |
| ruby-lsp (rubocop) | .rb, .rake, .gemspec, .ru | `ruby` and `gem` commands available |
| rust | .rs | `rust-analyzer` command available |
| sourcekit-lsp | .swift, .objc, .objcpp | `swift` installed (`xcode` on macOS) |
| svelte | .svelte | Auto-installs for Svelte projects |
| terraform | .tf, .tfvars | Auto-installs from GitHub releases |
| tinymist | .typ, .typc | Auto-installs from GitHub releases |
| typescript | .ts, .tsx, .js, .jsx, .mjs, .cjs, .mts, .cts | `typescript` dependency in project |
| vue | .vue | Auto-installs for Vue projects |
| yaml-ls | .yaml, .yml | Auto-installs Red Hat yaml-language-server |
| zls | .zig, .zon | `zig` command available |
LSP はデフォルトで無効です。有効にすると、上記のファイル拡張子のいずれかが検出され、要件が満たされた場合にサーバーが起動します。
:::note
`OPENCODE_DISABLE_LSP_DOWNLOAD` 環境変数を `true` に設定すると、LSP サーバーの自動ダウンロードを無効にできます。
:::
---
## 仕組み
LSP が有効で OpenCode がファイルを開くと、次のようになります。
1. 有効なすべての LSP サーバーに対してファイル拡張子をチェックします。
2. 適切な LSP サーバーがまだ実行されていない場合は開始します。
---
## ベストプラクティス
LSP は、言語サーバーの診断を提供することで、agent が問題を見つけて修正する助けになります。これは一部のプロジェクトで有用ですが、常に明確な利点になるとは限りません。
言語サーバーは同期が外れたり、多くのメモリを使用したり、バージョンやプロジェクトによって挙動が変わったり、agent のワークフローを遅くすることがあります。多くのプロジェクトでは、agent に lint、typecheck、その他の診断用 CLI ツールを直接実行させる方が適しています。これにより、これらのトレードオフを避けながら、エラーを agent のループに戻せます。これらのコマンドを `AGENTS.md` や skills などの instruction files に記載し、agent が何を実行すべきか分かるようにしてください。追加の言語サーバーフィードバックが有効な場合に LSP を有効にしてください。
---
## 設定
OpenCode 設定の `lsp` セクションを通じて LSP サーバーを有効化し、カスタマイズできます。
すべての組み込み LSP サーバーを有効にするには、`lsp` を `true` に設定します。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"lsp": true
}
```
オブジェクトを使用すると、組み込みサーバーを有効にしたまま、上書き設定やカスタムサーバーを設定できます。
```json title="opencode.json"
{
"$schema": "https://opencode.ai/config.json",
"lsp": {}
}
```
各 LSP サーバーは以下をサポートします。
| プロパティ | タイプ | 説明 |
| ---------------- | ------------ | ---------------------------------------------------------- |
| `disabled` | ブール値 | LSP サーバーを無効にするには、これを `true` に設定します。 |
| `command` | 文字列[] | LSP サーバーを起動するコマンド |
| `extensions` | 文字列[] | この LSP サーバーが処理するファイル拡張子 |
| `env` | オブジェクト | サーバーの起動時に設定する環境変数 |
| `initialization` | オブジェクト | LSP サーバーに送信する初期化オプション |
いくつかの例を見てみましょう。
---
### 環境変数
LSP サーバーの起動時に `env` プロパティを使用して環境変数を設定します。
```json title="opencode.json" {5-7}
{
"$schema": "https://opencode.ai/config.json",
"lsp": {
"rust": {
"env": {
"RUST_LOG": "debug"
}
}
}
}
```
---
### 初期化オプション
`initialization` プロパティを使用して、初期化オプションを LSP サーバーに渡します。これらは、LSP `initialize` リクエスト中に送信されるサーバー固有の設定です。
```json title="opencode.json" {5-9}
{
"$schema": "https://opencode.ai/config.json",
"lsp": {
"typescript": {
"initialization": {
"preferences": {
"importModuleSpecifierPreference": "relative"
}
}
}
}
}
```
:::note
初期化オプションは LSP サーバーによって異なります。利用可能なオプションについては、LSP サーバーのドキュメントを確認してください。
:::
---
### LSPサーバーの無効化
`lsp` を省略すると、すべての LSP サーバーは無効になります。別の設定で有効化されたすべての LSP サーバーを無効にするには、`lsp` を `false` に設定します。
```json title="opencode.json" {3}
{
"$schema": "https://opencode.ai/config.json",
"lsp": false
}
```
**特定** LSP サーバーを無効にするには、`disabled` を `true` に設定します。
```json title="opencode.json" {5}
{
"$schema": "https://opencode.ai/config.json",
"lsp": {
"typescript": {
"disabled": true
}
}
}
```
---
### カスタム LSP サーバー
コマンドとファイル拡張子を指定して、カスタム LSP サーバーを追加できます。
```json title="opencode.json" {4-7}
{
"$schema": "https://opencode.ai/config.json",
"lsp": {
"custom-lsp": {
"command": ["custom-lsp-server", "--stdio"],
"extensions": [".custom"]
}
}
}
```
---
## 追加情報
### PHP Intelephense
PHP Intelephense は、ライセンスキーを通じてプレミアム機能を提供します。ライセンスキーを指定するには、次の場所にあるテキストファイルにキー (のみ) を配置します。
- macOS/Linux の場合: `$HOME/intelephense/license.txt`
- Windows の場合: `%USERPROFILE%/intelephense/license.txt`
ファイルにはライセンスキーのみが含まれており、追加のコンテンツは含まれていません。
|