Skip to content

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 ServerPlugin 名稱
PythonPyrightpyright
TypeScript/JavaScripttypescript-language-servertypescript
Gogoplsgopls
Rustrust-analyzerrust-analyzer
Javajdtlsjdtls
C/C++clangdclangd
C#OmniSharpomnisharp
PHPphpactorphpactor
Kotlinkotlin-language-serverkotlin
Rubysolargraphsolargraph
HTML/CSSvscode-langservershtml-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-lsps

Go

bash
# 安裝 Language Server
go install golang.org/x/tools/gopls@latest

# 確認在 PATH 中
which gopls

# 安裝 Plugin
/plugin install gopls@claude-code-lsps

Rust

bash
# 安裝 Language Server(透過 rustup)
rustup component add rust-analyzer

# 安裝 Plugin
/plugin install rust-analyzer@claude-code-lsps

Java

bash
# 需要 Java 21+
java --version

# 安裝 jdtls(macOS)
brew install jdtls

# 安裝 Plugin
/plugin install jdtls@claude-code-lsps

C/C++

bash
# 安裝 clangd(macOS)
brew install llvm

# 或 Ubuntu
sudo apt install clangd

# 安裝 Plugin
/plugin install clangd@claude-code-lsps

LSP 工具說明

啟用 LSP 後,Claude Code 會獲得以下工具:

goToDefinition

跳轉到符號的定義位置:

Go to the definition of the `createUser` function

回傳:

Found definition at src/services/user.ts:42

findReferences

尋找符號的所有引用:

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: RedisConfig

documentSymbol

列出檔案中的所有符號(函數、類別、變數等):

Show me all symbols in src/services/user.ts

回傳:

UserService (class) - line 10
  - constructor - line 12
  - createUser - line 25
  - updateUser - line 45
  - deleteUser - line 67

getDiagnostics

取得檔案的錯誤和警告:

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 defined
Find all places where the User type is used
What's the signature of the authenticate function?
Show me the structure of this file
Are there any type errors in this file?

搭配重構

Find all references to `oldFunctionName` and rename them to `newFunctionName`

Claude 會:

  1. 使用 findReferences 找到所有引用
  2. 顯示將被修改的檔案
  3. 執行批次重新命名

效能考量

記憶體使用

專案規模大約記憶體
< 10K 行100-200 MB
10K-100K 行200-500 MB
> 100K 行500MB+

啟動時間

首次啟動 Language Server 需要索引專案,可能需要幾秒到幾分鐘(依專案大小)。後續查詢會非常快速。

建議

  • 使用 .gitignore 排除 node_modulesbuild 等目錄
  • 大型專案考慮使用 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 工具不可用

  1. 確認環境變數已設定:

    bash
    echo $ENABLE_LSP_TOOLS
    # 應該顯示 1
  2. 確認 Plugin 已安裝:

    /plugin list
  3. 確認 Language Server 在 PATH 中:

    bash
    which pyright  # 或其他 language server

Language Server 啟動失敗

  1. 檢查版本相容性
  2. 確認專案有正確的設定檔(如 tsconfig.jsonpyrightconfig.json
  3. 查看 Language Server 的 log

找不到定義

  1. 確認專案已被正確索引
  2. 檢查 import 路徑是否正確
  3. 確認 Language Server 設定包含該目錄

與 IDE 的比較

功能Claude Code + LSPVS CodeCursor
Go to Definition
Find References
Type Information
Real-time Diagnostics
AI 整合✅ 原生需擴充
Terminal 原生
語言支援廣度11 種非常廣非常廣

Claude Code LSP 的優勢在於 AI 原生整合:LSP 資訊直接輸入到 Claude 的推理過程,而不只是顯示給開發者看。

參考資源

下一步

AI 時代的軟體工程工作坊教學手冊