Skip to content

Commit d7aa075

Browse files
authored
Merge pull request #1 from nohehf/feat/support-more-languages
Feat/support more languages
2 parents f05005f + 8ace84b commit d7aa075

File tree

11 files changed

+219
-85
lines changed

11 files changed

+219
-85
lines changed

.github/workflows/CI.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ on:
1212
pull_request:
1313
workflow_dispatch:
1414

15+
concurrency:
16+
group: ${{ github.workflow }}-${{ github.ref }}
17+
cancel-in-progress: true
18+
1519
permissions:
1620
contents: read
1721

@@ -31,7 +35,7 @@ jobs:
3135
- uses: actions/checkout@v4
3236
- uses: actions/setup-python@v5
3337
with:
34-
python-version: '3.10'
38+
python-version: '3.11'
3539
- name: Build wheels
3640
uses: PyO3/maturin-action@v1
3741
with:
@@ -58,7 +62,7 @@ jobs:
5862
- uses: actions/checkout@v4
5963
- uses: actions/setup-python@v5
6064
with:
61-
python-version: '3.10'
65+
python-version: '3.11'
6266
architecture: ${{ matrix.platform.target }}
6367
- name: Build wheels
6468
uses: PyO3/maturin-action@v1
@@ -85,7 +89,7 @@ jobs:
8589
- uses: actions/checkout@v4
8690
- uses: actions/setup-python@v5
8791
with:
88-
python-version: '3.10'
92+
python-version: '3.11'
8993
- name: Build wheels
9094
uses: PyO3/maturin-action@v1
9195
with:

Cargo.lock

Lines changed: 72 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,7 @@ crate-type = ["cdylib"]
1212
pyo3 = "0.20.0"
1313
stack-graphs = { version = "0.13.0", features = ["storage"] }
1414
tree-sitter-stack-graphs = { version = "0.8.1", features = ["cli"] }
15+
tree-sitter-stack-graphs-java = "0.3.0"
1516
tree-sitter-stack-graphs-javascript = "0.1.0"
1617
tree-sitter-stack-graphs-python = "0.1.0"
18+
tree-sitter-stack-graphs-typescript = "0.2.0"

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.PHONY: develop
2+
develop:
3+
maturin develop
4+
5+
6+
test: develop
7+
## TODO: Add actual tests with pytest
8+
python tests/test.py

README.md

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,35 @@ This is a proof of concept draft, to test scripting utilities using stack-graphs
88

99
It uses pyo3 and maturin to generate the bindings.
1010

11-
## Ressources
11+
## Installation & Usage
1212

13-
https://pyo3.rs/v0.21.2/getting-started
14-
15-
## Setup
13+
```bash
14+
pip install stack-graphs-python-bindings # or poetry, ...
15+
```
1616

17-
TODO
17+
```python
18+
from stack_graphs_python import index, query_definition, Position
1819

19-
```bash
20-
pipx install maturin
20+
# ...
2121
```
2222

23+
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.
24+
2325
## Development
2426

25-
TODO
27+
### Ressources
28+
29+
https://pyo3.rs/v0.21.2/getting-started
30+
31+
### Setup
2632

2733
```bash
28-
maturin develop
34+
pipx install maturin # or pip, ...
2935
```
3036

31-
## Install
37+
### Testing
3238

33-
TODO
39+
```bash
40+
maturin develop
41+
python tests/test.py
42+
```

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "maturin"
44

55
[project]
66
name = "stack-graphs-python-bindings"
7-
requires-python = ">=3.8"
7+
requires-python = ">=3.11"
88
classifiers = [
99
"Programming Language :: Rust",
1010
"Programming Language :: Python :: Implementation :: CPython",

src/classes.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
use std::fmt::Display;
2+
3+
use pyo3::prelude::*;
4+
5+
use tree_sitter_stack_graphs::cli::util::{SourcePosition, SourceSpan};
6+
7+
#[pyclass]
8+
#[derive(Clone)]
9+
pub enum Language {
10+
Python,
11+
JavaScript,
12+
TypeScript,
13+
Java,
14+
}
15+
16+
#[pyclass]
17+
#[derive(Clone)]
18+
pub struct Position {
19+
#[pyo3(get, set)]
20+
path: String,
21+
#[pyo3(get, set)]
22+
line: usize,
23+
#[pyo3(get, set)]
24+
column: usize,
25+
}
26+
27+
#[pymethods]
28+
impl Position {
29+
#[new]
30+
fn new(path: String, line: usize, column: usize) -> Self {
31+
Position { path, line, column }
32+
}
33+
}
34+
35+
impl Display for Position {
36+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37+
write!(f, "{}:{}:{}", self.path, self.line, self.column)
38+
}
39+
}
40+
41+
impl From<Position> for SourcePosition {
42+
fn from(pos: Position) -> Self {
43+
SourcePosition {
44+
path: pos.path.into(),
45+
line: pos.line,
46+
column: pos.column,
47+
}
48+
}
49+
}
50+
51+
impl Into<Position> for SourceSpan {
52+
fn into(self) -> Position {
53+
Position {
54+
// TODO(@nohehf): Unwrap is unsafe
55+
path: self.path.to_str().unwrap().to_string(),
56+
line: self.span.start.line,
57+
// TODO(@nohehf): Handle both utf8 and utf16
58+
column: self.span.start.column.utf8_offset,
59+
}
60+
}
61+
}

src/lib.rs

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use core::fmt::Display;
2-
31
use pyo3::prelude::*;
42

3+
mod classes;
54
mod stack_graphs_wrapper;
65

7-
use tree_sitter_stack_graphs::cli::util::{SourcePosition, SourceSpan};
6+
use classes::{Language, Position};
87

98
/// Formats the sum of two numbers as string.
109
#[pyfunction]
@@ -13,61 +12,19 @@ fn sum_as_string(a: usize, b: usize) -> PyResult<String> {
1312
}
1413
/// Indexes the given paths into stack graphs, and stores the results in the given database.
1514
#[pyfunction]
16-
fn index(paths: Vec<String>, db_path: String) -> PyResult<()> {
15+
fn index(paths: Vec<String>, db_path: String, language: Language) -> PyResult<()> {
16+
// TODO(@nohehf): Add a verbose mode to toggle the logs
1717
println!("Indexing paths: {:?}", paths);
1818
println!("Database path: {:?}", db_path);
1919

2020
let paths: Vec<std::path::PathBuf> =
2121
paths.iter().map(|p| std::path::PathBuf::from(p)).collect();
2222

23-
Ok(stack_graphs_wrapper::index(paths, &db_path)?)
24-
}
25-
26-
#[pyclass]
27-
#[derive(Clone)]
28-
struct Position {
29-
#[pyo3(get, set)]
30-
path: String,
31-
#[pyo3(get, set)]
32-
line: usize,
33-
#[pyo3(get, set)]
34-
column: usize,
35-
}
36-
37-
#[pymethods]
38-
impl Position {
39-
#[new]
40-
fn new(path: String, line: usize, column: usize) -> Self {
41-
Position { path, line, column }
42-
}
43-
}
44-
45-
impl Display for Position {
46-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47-
write!(f, "{}:{}:{}", self.path, self.line, self.column)
48-
}
49-
}
50-
51-
impl From<Position> for SourcePosition {
52-
fn from(pos: Position) -> Self {
53-
SourcePosition {
54-
path: pos.path.into(),
55-
line: pos.line,
56-
column: pos.column,
57-
}
58-
}
59-
}
60-
61-
impl Into<Position> for SourceSpan {
62-
fn into(self) -> Position {
63-
Position {
64-
// TODO(@nohehf): Unwrap is unsafe
65-
path: self.path.to_str().unwrap().to_string(),
66-
line: self.span.start.line,
67-
// TODO(@nohehf): Handle both utf8 and utf16
68-
column: self.span.start.column.utf8_offset,
69-
}
70-
}
23+
Ok(stack_graphs_wrapper::index(
24+
paths,
25+
&db_path,
26+
language.into(),
27+
)?)
7128
}
7229

7330
/// Indexes the given paths into stack graphs, and stores the results in the given database.
@@ -96,5 +53,6 @@ fn stack_graphs_python(_py: Python, m: &PyModule) -> PyResult<()> {
9653
m.add_function(wrap_pyfunction!(index, m)?)?;
9754
m.add_function(wrap_pyfunction!(query_definition, m)?)?;
9855
m.add_class::<Position>()?;
56+
m.add_class::<Language>()?;
9957
Ok(())
10058
}

0 commit comments

Comments
 (0)