主題
Claude Code LSP 整合
LSP(Language Server Protocol)讓 Claude Code 擁有 IDE 等級的程式碼智慧,包括跳轉定義、尋找引用、即時錯誤診斷等功能。
為什麼需要 LSP?
| 沒有 LSP | 使用 LSP |
|---|---|
| 用 grep 搜尋函數定義(45 秒) | 直接跳轉到定義(50ms) |
| 猜測函數在哪裡被使用 | 精確列出所有引用位置 |
| 等 Claude 讀完整個檔案才知道型別 | 即時顯示型別資訊 |
| 執行後才發現語法錯誤 | 即時診斷錯誤和警告 |
效能提升:900 倍
傳統 grep 搜尋:45 秒
LSP 語義搜尋:50 毫秒支援的語言
Claude Code LSP 支援 11 種程式語言:
| 語言 | Language Server | Plugin 名稱 |
|---|---|---|
| Python | Pyright | pyright |
| TypeScript/JavaScript | typescript-language-server | typescript |
| Go | gopls | gopls |
| Rust | rust-analyzer | rust-analyzer |
| Java | jdtls | jdtls |
| C/C++ | clangd | clangd |
| C# | OmniSharp | omnisharp |
| PHP | phpactor | phpactor |
| Kotlin | kotlin-language-server | kotlin |
| Ruby | solargraph | solargraph |
| HTML/CSS | vscode-langservers | html-css |
快速安裝
步驟 1:啟用 LSP 功能
bash
# 方法 1:環境變數(臨時)
ENABLE_LSP_TOOLS=1 claude
# 方法 2:設定檔(永久)
echo 'export ENABLE_LSP_TOOLS=1' >> ~/.zshrc
source ~/.zshrc步驟 2:新增 Plugin Marketplace
/plugin marketplace add boostvolt/claude-code-lsps步驟 3:安裝語言 Plugin
/plugin install pyright@claude-code-lsps各語言詳細設定
Python(Pyright)
bash
# 安裝 Language Server
pip install pyright
# 或
npm install -g pyright
# 安裝 Claude Code Plugin
/plugin install pyright@claude-code-lsps設定檔 pyrightconfig.json(選用):
json
{
"include": ["src"],
"exclude": ["**/node_modules", "**/__pycache__"],
"typeCheckingMode": "basic"
}TypeScript/JavaScript
bash
# 安裝 Language Server
npm install -g typescript typescript-language-server
# 安裝 Plugin
/plugin install typescript@claude-code-lspsGo
bash
# 安裝 Language Server
go install golang.org/x/tools/gopls@latest
# 確認在 PATH 中
which gopls
# 安裝 Plugin
/plugin install gopls@claude-code-lspsRust
bash
# 安裝 Language Server(透過 rustup)
rustup component add rust-analyzer
# 安裝 Plugin
/plugin install rust-analyzer@claude-code-lspsJava
bash
# 需要 Java 21+
java --version
# 安裝 jdtls(macOS)
brew install jdtls
# 安裝 Plugin
/plugin install jdtls@claude-code-lspsC/C++
bash
# 安裝 clangd(macOS)
brew install llvm
# 或 Ubuntu
sudo apt install clangd
# 安裝 Plugin
/plugin install clangd@claude-code-lspsLSP 工具說明
啟用 LSP 後,Claude Code 會獲得以下工具:
goToDefinition
跳轉到符號的定義位置:
Go to the definition of the `createUser` function回傳:
Found definition at src/services/user.ts:42findReferences
尋找符號的所有引用:
Find all references to the `UserService` class回傳:
Found 15 references:
- src/controllers/auth.ts:12
- src/controllers/profile.ts:8
- src/routes/api.ts:25
...hover
取得符號的型別資訊和文件:
What is the type of the `config` variable in app.ts line 10?回傳:
const config: AppConfig
- port: number
- database: DatabaseConfig
- redis: RedisConfigdocumentSymbol
列出檔案中的所有符號(函數、類別、變數等):
Show me all symbols in src/services/user.ts回傳:
UserService (class) - line 10
- constructor - line 12
- createUser - line 25
- updateUser - line 45
- deleteUser - line 67getDiagnostics
取得檔案的錯誤和警告:
Check for errors in src/app.ts回傳:
2 errors, 1 warning:
- Error (line 15): Property 'nam' does not exist on type 'User'. Did you mean 'name'?
- Error (line 23): Argument of type 'string' is not assignable to parameter of type 'number'
- Warning (line 8): 'config' is declared but never used使用方式
自然語言查詢
LSP 整合後,你可以用自然語言請求 Claude:
Jump to where handleSubmit is definedFind all places where the User type is usedWhat's the signature of the authenticate function?Show me the structure of this fileAre there any type errors in this file?搭配重構
Find all references to `oldFunctionName` and rename them to `newFunctionName`Claude 會:
- 使用
findReferences找到所有引用 - 顯示將被修改的檔案
- 執行批次重新命名
效能考量
記憶體使用
| 專案規模 | 大約記憶體 |
|---|---|
| < 10K 行 | 100-200 MB |
| 10K-100K 行 | 200-500 MB |
| > 100K 行 | 500MB+ |
啟動時間
首次啟動 Language Server 需要索引專案,可能需要幾秒到幾分鐘(依專案大小)。後續查詢會非常快速。
建議
- 使用
.gitignore排除node_modules、build等目錄 - 大型專案考慮使用
exclude設定排除不需要索引的目錄 - 確保 Language Server 有足夠記憶體
驗證安裝
測試 LSP 是否正常運作:
Go to the definition of [any function in your project]正常回應:
Found definition at src/utils/helper.ts:25未啟用 LSP:
[Claude 會用 grep 搜尋,速度較慢]故障排除
LSP 工具不可用
確認環境變數已設定:
bashecho $ENABLE_LSP_TOOLS # 應該顯示 1確認 Plugin 已安裝:
/plugin list確認 Language Server 在 PATH 中:
bashwhich pyright # 或其他 language server
Language Server 啟動失敗
- 檢查版本相容性
- 確認專案有正確的設定檔(如
tsconfig.json、pyrightconfig.json) - 查看 Language Server 的 log
找不到定義
- 確認專案已被正確索引
- 檢查 import 路徑是否正確
- 確認 Language Server 設定包含該目錄
與 IDE 的比較
| 功能 | Claude Code + LSP | VS Code | Cursor |
|---|---|---|---|
| Go to Definition | ✅ | ✅ | ✅ |
| Find References | ✅ | ✅ | ✅ |
| Type Information | ✅ | ✅ | ✅ |
| Real-time Diagnostics | ✅ | ✅ | ✅ |
| AI 整合 | ✅ 原生 | 需擴充 | ✅ |
| Terminal 原生 | ✅ | ❌ | ❌ |
| 語言支援廣度 | 11 種 | 非常廣 | 非常廣 |
Claude Code LSP 的優勢在於 AI 原生整合:LSP 資訊直接輸入到 Claude 的推理過程,而不只是顯示給開發者看。
參考資源
下一步
- Hooks 自動化 - 事件處理機制
- Plugins 系統 - 擴充功能
- Sub-agents - 並行代理