This repository has been archived on 2026-08-16. You can view files and clone it, but cannot push or open issues or pull requests.
ollie-9p/tools/lsp_definition

35 lines
1.1 KiB
Python
Executable File

#!/usr/bin/env python3
# ollie:parallel read
# description: Go to definition of symbol at position. Returns file:line:col.
# Args: <file> <line> <col>
import os
import sys
sys.path.insert(0, os.path.join(os.environ['OLLIE'], 's', os.environ['OLLIE_SESSION_ID'], 't'))
from _lib.lsp.client import request
if len(sys.argv) < 4:
print("usage: lsp_definition <file> <line> <col>")
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}")