Add lsp_hover tool - #53010
Conversation
|
We require contributors to sign our Contributor License Agreement, and we don't have @AJenbo on file. You can sign our CLA at https://zed.dev/cla. Once you've signed, post a comment here that says '@cla-bot check'. |
lsp_hover, lsp_definition, and lsp_references agent tools
|
@cla-bot check |
|
The cla-bot has been summoned, and re-checked this pull request! |
|
For some added context I'm working on a new LSP for PHP. The LSP can print out a full project diagnostics in CLI mode, I usually feed it to the agent to diagnose issues, but the last 10% had the the agent pretty stumped, it kept failing to find the actual cause and going on unfruitful scavenger hunts. After implementing just these 3 basic tools it was able to identify the cause of nearly all the remaining issues in a single session. So while this may only be a minor improvement in general I think it can make a big difference for some edge cases where agents would previously struggle. |
|
Some overlap with #55744 - ok to close this one? |
|
#55744 was just merged, and there's a overlap (the find_references tool). But this PR has LSP hover, which enables agents to get type definition for any piece of code, which is really powerful, specially for code with heavy usage of generics. (Agents can today retrieve type signatures of public APIs from documentation, but they can't easily get the type of a variable, specially after generics instantiation. For languages like Rust, Haskell or C++, that have libraries that do arbitrary type-level computation, this could make a lot of difference) Maybe this could rebased on top of #55744's changes or something. |
|
I'm happy to rebase this. In the future are there things I can do to get a review and merge in 5h like #55744 rather then being suggested for closing after 1 month :D Update: |
lsp_hover, lsp_definition, and lsp_references agent toolslsp_hover tool
|
@dlight it's now rebased and only adds the LSP hover tool. |
Add an lsp_hover tool that queries language servers for type information, documentation, and signatures. This complements the LSP tools added in f482f9e (goto_definition, find_references, rename_symbol, code_actions) with hover functionality that provides quick type lookups without needing to navigate to definitions. The tool uses a dedicated symbol resolution module (lsp_tool_utils) that supports: - Fuzzy column matching to disambiguate multiple occurrences on a line - Nearby line search when the exact line doesn't match - Case-insensitive fallback for symbol lookup - Word boundary awareness for accurate symbol identification
|
Fixed the CI stuff ... I think |
|
Thanks for taking a look into this! |
|
@benbrandt did the team ever do research into LSP diagnostic injections and language level rules for them to avoid hammering the model with 500 python lint errors for example while still making sure all TS type errors get through and on the timing of the injections, or has this not been explored yet? |
|
@versecafe generally speaking, it's the LLM that is supposed to decide which tools they want to call, and when to make a call (tool calling is pull based, not push based). Zed can provide a tool for diagnostics or other LSP functions, but the model is in charge of selecting when they look at the LSP server. If the model is not behaving correctly, current best practice is to steer it through AGENTS.md, skills, and other prompting techniques. There's a tradeoff here in that if you add too much tools, you will waste tokens of the context window with the description of each tool. The description of a tool is the thing the model uses to decide whether and when to call it, so you don't want to be so succinct as to make the tool seem irrelevant, but you don't want to describe too much as to waste your context window. I'd expect that if anything, models might underuse lsp servers a lot, simply because it's less likely they are adequately trained for it. |
That's been my over all experience, they will often fall back to tried and true tools even when the LSP tools would have been more effective. But they do use them and it can really help with complicated cases where reading files isn't able to provide good info. But you can just tell them to rely more on it either via chat or AGENTS.md, which is probably good since each language's LSP option varies in usefulness. |
This PR adds an
lsp_hovertool to the agent, complementing the LSP tools added in #55744 (go_to_definition,find_references,rename_symbol,get_code_actions) with hover functionality that provides quick type lookups without needing to navigate to definitions.Motivation
This has been requested independently by multiple users:
The problem hover solves
The agent currently has no way to know what type a variable holds without reading class definitions, tracing inheritance chains, and guessing. For codebases where types come from magic methods, generics, or container resolution, this is especially fragile. A single
lsp_hovercall returns the type immediately.$order->status?"capturePayment()take?"OrderStatus?"What it does
lsp_hover— Get type information, documentation, and deprecation notices for a symbol. Returns the same information you see when hovering in the editor: type signatures, docblocks, enum variants, parameter lists. UsesProject::hover()which routes to the appropriate LSP. The tool description positions it as a cheap "type oracle" for usage sites — try hover before grep. Includes guidance to avoid hovering on definition sites (where the LSP typically returns nothing useful) and encourages parallel batch hovering to build a complete type map of unfamiliar code.The tool accepts
(path, line, symbol)and optionallycolumnfor disambiguation when the same symbol appears multiple times on a line.Implementation details
find_project_path, sameopen_bufferpattern, same cancellation support.lsp_tool_utils.rs) with richer position resolution than the existingsymbol_locator:LspToolFeatureFlagas the other LSP tools.greptool description includes a nudge toward LSP tools for type/definition/reference lookups.Testing
Tested against a real PHP monorepo with PHPantom LSP:
Paymentdoesn't match insidecaptureReservedPayment.PaymentTokentopaymentToken.tokenon lines with multiple occurrences.Addresses #29724, #35841, #47942
Release Notes:
lsp_hovertool to the agent, giving it direct access to language server hover information for type lookups, documentation, and signatures across any language with LSP support.