diff --git a/Makefile b/Makefile index b9e7300..548819b 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ .PHONY: setup setup: - pyenv virtualenvs | grep stack-graphs-python-bindings || pyenv virtualenv 3.11.9 stack-graphs-python-bindings - pyenv activate stack-graphs-python-bindings + python -m venv .venv + . .venv/bin/activate pip install maturin .PHONY: develop diff --git a/README.md b/README.md index 2bd0d4f..d76351d 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,43 @@ pip install stack-graphs-python-bindings # or poetry, ... ``` ```python -from stack_graphs_python import index, query_definition, Position +import os +from stack_graphs_python import index, Querier, Position, Language -# ... +db_path = os.path.abspath("./db.sqlite") +dir = os.path.abspath("./tests/js_sample") + +# Index the directory (creates stack-graphs database) +index([dir], db_path, language=Language.JavaScript) + +# Instantiate a querier +querier = Querier(db_path) + +# Query a reference at a given position (0-indexed line and column): +# foo in: const baz = foo +source_reference = Position(path=dir + "/index.js", line=2, column=12) +results = querier.definitions(source_reference) + +for r in results: + print(f"{r.path}, l:{r.line}, c: {r.column}") ``` -You can refer to the example in [test/test.py](https://github.com/nohehf/stack-graphs-python-bindings/blob/main/tests/test.py) for a concrete usage example. +Will result in: + +```bash +[...]/stack-graphs-python-bindings/tests/js_sample/index.js, l:0, c: 9 +[...]/stack-graphs-python-bindings/tests/js_sample/module.js, l:0, c: 13 +``` + +That translates to: + +```javascript +// index.js +import { foo } from "./module" + +// module.js +export const foo = "bar" +``` ## Development @@ -28,15 +59,20 @@ You can refer to the example in [test/test.py](https://github.com/nohehf/stack-g https://pyo3.rs/v0.21.2/getting-started +### Requirements + +- Rust +- Python 3.11+ + ### Setup ```bash -pipx install maturin # or pip, ... +# Setup venv and install maturin through pip +make setup ``` ### Testing ```bash -maturin develop -python tests/test.py +make test ```