A Language Server Protocol (LSP) implementation for Solidity development using Foundry's compilation and linting infrastructure.
This work is a continuation from foundry-rs/foundry PR #11187.
Install binary from crates.io
cargo install forge-lspStart the LSP server using:
forge-lsp --stdioGeneral
-
initialize- Server initialization -
initialized- Server initialized notification -
shutdown- Server shutdown
Text Synchronization
-
textDocument/didOpen- Handle file opening -
textDocument/didChange- Handle file content changes -
textDocument/didSave- Handle file saving with diagnostics refresh -
textDocument/didClose- Handle file closing -
textDocument/willSave- File will save notification -
textDocument/willSaveWaitUntil- File will save wait until
Diagnostics
-
textDocument/publishDiagnostics- Publish compilation errors and warnings viaforge build -
textDocument/publishDiagnostics- Publish linting errors and warnings viaforge lint
Language Features
-
textDocument/definition- Go to definition -
textDocument/declaration- Go to declaration -
textDocument/references- Find all references -
textDocument/documentSymbol- Document symbol outline (contracts, functions, variables, events, structs, enums, etc.) -
textDocument/rename- Rename symbols across files -
textDocument/completion- Code completion -
textDocument/hover- Hover information -
textDocument/signatureHelp- Function signature help -
textDocument/typeDefinition- Go to type definition -
textDocument/implementation- Go to implementation -
textDocument/documentHighlight- Document highlighting -
textDocument/codeAction- Code actions (quick fixes, refactoring) -
textDocument/codeLens- Code lens -
textDocument/documentLink- Document links -
textDocument/documentColor- Color information -
textDocument/colorPresentation- Color presentation -
textDocument/formatting- Document formatting -
textDocument/rangeFormatting- Range formatting -
textDocument/onTypeFormatting- On-type formatting -
textDocument/prepareRename- Prepare rename validation -
textDocument/foldingRange- Folding ranges -
textDocument/selectionRange- Selection ranges -
textDocument/semanticTokens- Semantic tokens -
textDocument/semanticTokens/full- Full semantic tokens -
textDocument/semanticTokens/range- Range semantic tokens -
textDocument/semanticTokens/delta- Delta semantic tokens
Workspace Features
-
workspace/symbol- Workspace-wide symbol search -
workspace/didChangeConfiguration- Acknowledges configuration changes (logs only) -
workspace/didChangeWatchedFiles- Acknowledges watched file changes (logs only) -
workspace/didChangeWorkspaceFolders- Acknowledges workspace folder changes (logs only) -
workspace/executeCommand- Execute workspace commands (stub implementation) -
workspace/applyEdit- Apply workspace edits -
workspace/willCreateFiles- File creation preview -
workspace/willRenameFiles- File rename preview -
workspace/willDeleteFiles- File deletion preview
Window Features
-
window/showMessage- Show message to user -
window/showMessageRequest- Show message request to user -
window/workDoneProgress- Work done progress
cargo build --releasecargo testYou can add the following to VSCode (or cursor) using a lsp-proxy extension see comment here:
[
{
"languageId": "solidity",
"command": "forge-lsp",
"fileExtensions": [
".sol"
],
}
]If you have neovim 0.11+ installed add these to your config
-- lsp/forge_lsp.lua
return {
cmd = { "forge-lsp" },
filetypes = { "solidity" },
root_markers = { "foundry.toml", ".git" },
root_dir = vim.fs.root(0, { "foundry.toml", ".git" }),
}
-- init.lua
vim.lsp.enable("forge_lsp")Lsp logs are stored in ~/.local/state/nvim/lsp.log
To clear lsp logs run:
> -f ~/.local/state/nvim/lsp.logTo monitor logs in real time run:
tail -f ~/.local/state/nvim/lsp.logEnable traces in neovim to view full traces in logs:
:lua vim.lsp.set_log_level("trace")Check out the foundry contribution guide.