AI-powered LSP diagnostic explanations for Neovim — Get intelligent explanations of compiler errors and warnings directly in your editor, powered by Google's Gemini API.
lsp-explain is a Neovim plugin that uses AI to explain LSP diagnostics (errors, warnings, info) in your code. When you hover over an error, it uses an LLM to:
- 📝 Provide a clear summary of what went wrong
- 🔍 Identify root causes with confidence levels
- 🛠️ Suggest fixes with step-by-step guidance
- 📊 Generate code patches when applicable
- 💾 Cache responses for repeated diagnostics
This project consists of two main components:
A lightweight HTTP server that:
- Receives LSP diagnostic information from Neovim
- Sends requests to Google's Gemini API for analysis
- Caches responses using an in-memory LRU cache
- Returns structured JSON responses with explanations and fixes
Key files:
cmd/main.go— Server entry pointinternal/api/handlers.go— HTTP request handlersinternal/api/types.go— Request/response schemasinternal/llm/gemini.go— Gemini API clientinternal/cache/cache.go— LRU response cache
- Neovim 0.9.0+
- Go 1.25.6+
- Google Gemini API key
cd /path/to/nvim_plugin
source env.sh # if you have local config
# Set your API key
export GEMINI_API_KEY="your-gemini-api-key"
# Run the server (defaults to 127.0.0.1:7777)
go run cmd/main.goEnvironment variables:
GEMINI_API_KEY— (required) Your Google Gemini API keyGEMINI_MODEL— (default:models/gemini-2.5-flash) Gemini model to useLSP_EXPLAIN_ADDR— (default:127.0.0.1:7777) Server address and portLSP_EXPLAIN_TIMEOUT— (default:30s) Request timeout
Using your favorite plugin manager. For example, with lazy.nvim:
{
"path/to/lsp-explain.nvim",
config = function()
require("lsp_explain").setup({
endpoint = "http://127.0.0.1:7777",
timeout_ms = 30000,
context_lines = 60,
include_hover = true,
show_raw_on_error = true,
})
end,
}:LspExplain— Explain the diagnostic under the cursor:LspFix— Apply the last suggested fix (if a patch was provided)
- Open a file with diagnostics in Neovim
- Position cursor on a diagnostic error/warning
- Run
:LspExplain - View the explanation popup with root causes and fix suggestions
- Optionally run
:LspFixto apply the suggested patch
require("lsp_explain").setup({
endpoint = "http://127.0.0.1:7777", -- Backend endpoint
timeout_ms = 30000, -- Request timeout in ms
context_lines = 60, -- Lines of context before/after diagnostic
include_hover = true, -- Include LSP hover info
show_raw_on_error = true, -- Show raw JSON if parsing fails
})Request:
{
"language": "go",
"provider": "gopls",
"file_path": "/path/to/file.go",
"workspace_root": "/path/to/workspace",
"file_sha": "sha256hash",
"tooling": {
"go_version": "1.21",
"gopls_version": "v0.14.0"
},
"diagnostic": {
"message": "undefined: myVar",
"severity": "error",
"range": {
"start": { "line": 42, "character": 10 },
"end": { "line": 42, "character": 15 }
}
},
"code_context": {
"before": "...",
"snippet": "if myVar {",
"after": "..."
}
}Response:
{
"summary": "Clear explanation of the error",
"root_causes": [
{
"cause": "Description of the root cause",
"confidence": 0.95,
"evidence": ["Supporting evidence 1", "Evidence 2"]
}
],
"fixes": [
{
"title": "Fix title",
"steps": ["Step 1", "Step 2"],
"confidence": 0.9,
"risk": "safe",
"patch": "unified diff patch",
"why_it_works": "Explanation of why this fix works"
}
],
"notes": [],
"meta": {
"cache_hit": false,
"model": "models/gemini-2.5-flash"
}
}Simple health check endpoint.
Response:
{
"ok": true,
"timestamp": "2026-01-25T10:00:00Z",
"model": "models/gemini-2.5-flash"
}The backend maintains an LRU cache (default: 256 entries) to avoid redundant API calls for identical diagnostics. Cache keys are based on:
- Model name
- Language & provider
- File SHA256 hash
- Diagnostic message
- Line & character range
go build -o lsp-explain cmd/main.gogo test ./...The model output may not be valid JSON. Check:
- Your
GEMINI_API_KEYis correct - The backend logs for API errors
- Try with a different model via
GEMINI_MODELenv var
- Ensure your LSP client (e.g.,
gopls,rust-analyzer) is running - Run
:LspInfoin Neovim to check LSP status
- Verify Go 1.25.6+ is installed
- Check
GEMINI_API_KEYenvironment variable is set - Ensure port is not already in use
This project is licensed under the MIT License — see LICENSE for details.
Contributions are welcome! Please feel free to submit issues and pull requests.

