ollie/tools/lsp_definition

46 lines
1.5 KiB
Python
Executable File

#!/usr/bin/env python3
# ollie:parallel read
# ollie:prompt
# ## lsp_definition
#
# Go to definition of symbol at position.
#
# **Args**: `[path, line, col]`
#
# ```
# call_tool: calls=[{tool: "lsp_definition", args: ["/abs/path.go", "42", "10"]}]
# ```
#
# **Returns**: `file:line:col` location(s).
# ollie:end
import os
import sys
sys.path.insert(0, os.environ.get('OLLIE_TOOLS_PATH', os.path.dirname(os.path.abspath(__file__))))
from _lib.lsp.client import request
if len(sys.argv) < 4:
print('{"schema": {"type": "array", "items": [{"name": "file", "type": "string", "description": "File path", "required": true}, {"name": "line", "type": "string", "description": "Line number (1-indexed)", "required": true}, {"name": "col", "type": "string", "description": "Column number", "required": true}], "examples": [["/path/to/file.go", "42", "5"]]}}')
sys.exit(1)
filepath = os.path.abspath(sys.argv[1])
line = int(sys.argv[2]) - 1 # convert 1-indexed to 0-indexed
col = int(sys.argv[3]) - 1
result = request("definition", file=filepath, line=line, col=col)
if not result:
print("(no definition found)")
sys.exit(0)
# Normalize: result can be a Location or list of Locations
locations = result if isinstance(result, list) else [result]
for loc in locations:
uri = loc.get("uri", loc.get("targetUri", ""))
path = uri[7:] if uri.startswith("file://") else uri
pos = loc.get("range", loc.get("targetRange", {})).get("start", {})
ln = pos.get("line", 0) + 1
ch = pos.get("character", 0) + 1
print(f"{path}:{ln}:{ch}")