Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 698efcb

Browse files
author
Hendrik van Antwerpen
committed
Add convenience method for constructing LanguageConfiguration from sources
1 parent 8cf62fa commit 698efcb

File tree

2 files changed

+45
-25
lines changed
  • languages/tree-sitter-stack-graphs-typescript/rust
  • tree-sitter-stack-graphs/src

2 files changed

+45
-25
lines changed

languages/tree-sitter-stack-graphs-typescript/rust/lib.rs

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
// Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details.
66
// ------------------------------------------------------------------------------------------------
77

8-
use stack_graphs::graph::StackGraph;
98
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
10-
use tree_sitter_stack_graphs::loader::Loader;
119
use tree_sitter_stack_graphs::CancellationFlag;
12-
use tree_sitter_stack_graphs::StackGraphLanguage;
13-
use tree_sitter_stack_graphs::Variables;
1410

1511
/// The stack graphs tsg source for this language
1612
const STACK_GRAPHS_TSG_SOURCE: &str = include_str!("../src/stack-graphs.tsg");
@@ -21,27 +17,15 @@ const STACK_GRAPHS_BUILTINS_CONFIG: &str = include_str!("../src/builtins.cfg");
2117
const STACK_GRAPHS_BUILTINS_SOURCE: &str = include_str!("../src/builtins.ts");
2218

2319
pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> LanguageConfiguration {
24-
let language = tree_sitter_typescript::language_typescript();
25-
let sgl = StackGraphLanguage::from_str(language, STACK_GRAPHS_TSG_SOURCE).unwrap();
26-
let mut builtins = StackGraph::new();
27-
let file = builtins.add_file("<builtins>").unwrap();
28-
let mut builtins_globals = Variables::new();
29-
Loader::load_globals_from_config_str(STACK_GRAPHS_BUILTINS_CONFIG, &mut builtins_globals)
30-
.unwrap();
31-
sgl.build_stack_graph_into(
32-
&mut builtins,
33-
file,
34-
STACK_GRAPHS_BUILTINS_SOURCE,
35-
&builtins_globals,
20+
LanguageConfiguration::from_tsg_str(
21+
tree_sitter_typescript::language_typescript(),
22+
Some(String::from("source.ts")),
23+
None,
24+
vec![String::from("ts")],
25+
STACK_GRAPHS_TSG_SOURCE,
26+
Some(STACK_GRAPHS_BUILTINS_SOURCE),
27+
Some(STACK_GRAPHS_BUILTINS_CONFIG),
3628
cancellation_flag,
3729
)
38-
.unwrap();
39-
LanguageConfiguration {
40-
language,
41-
scope: Some(String::from("source.ts")),
42-
content_regex: None,
43-
file_types: vec![String::from("ts")],
44-
sgl,
45-
builtins,
46-
}
30+
.unwrap()
4731
}

tree-sitter-stack-graphs/src/loader.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,42 @@ pub struct LanguageConfiguration {
4545
}
4646

4747
impl LanguageConfiguration {
48+
pub fn from_tsg_str(
49+
language: Language,
50+
scope: Option<String>,
51+
content_regex: Option<Regex>,
52+
file_types: Vec<String>,
53+
tsg_source: &str,
54+
builtins_source: Option<&str>,
55+
builtins_config: Option<&str>,
56+
cancellation_flag: &dyn CancellationFlag,
57+
) -> Result<Self, LoadError> {
58+
let sgl = StackGraphLanguage::from_str(language, tsg_source)?;
59+
let mut builtins = StackGraph::new();
60+
if let Some(builtins_source) = builtins_source {
61+
let mut builtins_globals = Variables::new();
62+
if let Some(builtins_config) = builtins_config {
63+
Loader::load_globals_from_config_str(builtins_config, &mut builtins_globals)?;
64+
}
65+
let file = builtins.add_file("<builtins>").unwrap();
66+
sgl.build_stack_graph_into(
67+
&mut builtins,
68+
file,
69+
builtins_source,
70+
&builtins_globals,
71+
cancellation_flag,
72+
)?;
73+
}
74+
Ok(LanguageConfiguration {
75+
language,
76+
scope,
77+
content_regex,
78+
file_types,
79+
sgl,
80+
builtins,
81+
})
82+
}
83+
4884
pub fn matches_file(&self, path: &Path, content: Option<&str>) -> bool {
4985
matches_file(&self.file_types, &self.content_regex, path, content).is_some()
5086
}

0 commit comments

Comments
 (0)