lsp-hover.ts 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import { z } from "zod"
  2. import { Tool } from "./tool"
  3. import path from "path"
  4. import { LSP } from "../lsp"
  5. import { App } from "../app/app"
  6. export const LspHoverTool = Tool.define({
  7. id: "opencode.lsp_hover",
  8. description: `
  9. Looks up hover information for a given position in a source file using the Language Server Protocol (LSP).
  10. This includes type information, documentation, or symbol details at the specified line and character.
  11. Useful for providing code insights, explanations, or context-aware assistance based on the user's current cursor location.
  12. `,
  13. parameters: z.object({
  14. file: z.string().describe("The path to the file to get diagnostics."),
  15. line: z.number().describe("The line number to get diagnostics."),
  16. character: z.number().describe("The character number to get diagnostics."),
  17. }),
  18. execute: async (args) => {
  19. const app = App.info()
  20. const file = path.isAbsolute(args.file)
  21. ? args.file
  22. : path.join(app.path.cwd, args.file)
  23. await LSP.file(file)
  24. const result = await LSP.hover({
  25. ...args,
  26. file,
  27. })
  28. console.log(result)
  29. return {
  30. metadata: {
  31. result,
  32. },
  33. output: JSON.stringify(result, null, 2),
  34. }
  35. },
  36. })