Skip to content

Commit c7ff411

Browse files
committed
feat: support more languages
1 parent 725604e commit c7ff411

File tree

5 files changed

+167
-67
lines changed

5 files changed

+167
-67
lines changed

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"

src/classes.rs

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

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
}

src/stack_graphs_wrapper/mod.rs

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use crate::Language;
12
use pyo3::exceptions::PyException;
23
use pyo3::PyErr;
34
use stack_graphs::storage::{SQLiteReader, SQLiteWriter};
45
use std::path::PathBuf;
56
use tree_sitter_stack_graphs::cli::query::{Querier, QueryResult};
67
use tree_sitter_stack_graphs::cli::util::SourcePosition;
78
use tree_sitter_stack_graphs::cli::{index::Indexer, util::reporter::ConsoleReporter};
9+
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
810
use tree_sitter_stack_graphs::{loader::Loader, NoCancellation};
911

1012
// TODO(@nohehf): Better error handling
@@ -19,13 +21,29 @@ impl std::convert::From<StackGraphsError> for PyErr {
1921
}
2022
}
2123

22-
pub fn index(paths: Vec<PathBuf>, db_path: &str) -> Result<(), StackGraphsError> {
23-
let py_config = tree_sitter_stack_graphs_python::language_configuration(&NoCancellation);
24-
let js_config = tree_sitter_stack_graphs_javascript::language_configuration(&NoCancellation);
24+
fn get_langauge_configuration(lang: Language) -> LanguageConfiguration {
25+
match lang {
26+
Language::Python => {
27+
tree_sitter_stack_graphs_python::language_configuration(&NoCancellation)
28+
}
29+
Language::JavaScript => {
30+
tree_sitter_stack_graphs_javascript::language_configuration(&NoCancellation)
31+
}
32+
Language::TypeScript => {
33+
tree_sitter_stack_graphs_typescript::language_configuration(&NoCancellation)
34+
}
35+
Language::Java => tree_sitter_stack_graphs_java::language_configuration(&NoCancellation),
36+
}
37+
}
2538

26-
let configs = vec![js_config, py_config];
39+
pub fn index(
40+
paths: Vec<PathBuf>,
41+
db_path: &str,
42+
language: Language,
43+
) -> Result<(), StackGraphsError> {
44+
let configurations = vec![get_langauge_configuration(language)];
2745

28-
let mut loader = match Loader::from_language_configurations(configs, None) {
46+
let mut loader = match Loader::from_language_configurations(configurations, None) {
2947
Ok(ldr) => ldr,
3048
Err(e) => {
3149
return Err(StackGraphsError {
@@ -81,16 +99,6 @@ pub fn query_definition(
8199
message: format!("Failed to query definitions: {}", e),
82100
}),
83101
}
84-
85-
// if results.is_empty() {
86-
// println!("No definitions found");
87-
// return Ok(());
88-
// }
89-
90-
// for res in results {
91-
// println!("Source: {:?}", res.source);
92-
// println!("Targets: {:?}", res.targets);
93-
// }
94102
}
95103

96104
// https://github.com/github/stack-graphs/blob/7db914c01b35ce024f6767e02dd1ad97022a6bc1/tree-sitter-stack-graphs/src/cli/index.rs#L118

0 commit comments

Comments
 (0)