Skip to content

Commit ad9c0da

Browse files
Add similar path setting to language configuration
1 parent a92b9b0 commit ad9c0da

File tree

11 files changed

+71
-25
lines changed

11 files changed

+71
-25
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use tree_sitter_stack_graphs::loader::FileAnalyzers;
21
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
32
use tree_sitter_stack_graphs::loader::LoadError;
43
use tree_sitter_stack_graphs::CancellationFlag;
@@ -39,7 +38,6 @@ pub fn try_language_configuration(
3938
STACK_GRAPHS_BUILTINS_SOURCE,
4039
)),
4140
Some(STACK_GRAPHS_BUILTINS_CONFIG),
42-
FileAnalyzers::new(),
4341
cancellation_flag,
4442
)
4543
}

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

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

8-
use tree_sitter_stack_graphs::loader::FileAnalyzers;
98
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
109
use tree_sitter_stack_graphs::loader::LoadError;
1110
use tree_sitter_stack_graphs::CancellationFlag;
@@ -38,7 +37,7 @@ pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> Langu
3837
pub fn try_language_configuration(
3938
cancellation_flag: &dyn CancellationFlag,
4039
) -> Result<LanguageConfiguration, LoadError> {
41-
LanguageConfiguration::from_sources(
40+
let mut lc = LanguageConfiguration::from_sources(
4241
tree_sitter_javascript::language(),
4342
Some(String::from("source.js")),
4443
None,
@@ -50,7 +49,9 @@ pub fn try_language_configuration(
5049
STACK_GRAPHS_BUILTINS_SOURCE,
5150
)),
5251
Some(STACK_GRAPHS_BUILTINS_CONFIG),
53-
FileAnalyzers::new().add("package.json".to_string(), NpmPackageAnalyzer {}),
5452
cancellation_flag,
55-
)
53+
)?;
54+
lc.special_files
55+
.add("package.json".to_string(), NpmPackageAnalyzer {});
56+
Ok(lc)
5657
}

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

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

8-
use tree_sitter_stack_graphs::loader::FileAnalyzers;
98
use tree_sitter_stack_graphs::loader::LanguageConfiguration;
109
use tree_sitter_stack_graphs::loader::LoadError;
1110
use tree_sitter_stack_graphs::CancellationFlag;
@@ -41,7 +40,7 @@ pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> Langu
4140
pub fn try_language_configuration(
4241
cancellation_flag: &dyn CancellationFlag,
4342
) -> Result<LanguageConfiguration, LoadError> {
44-
LanguageConfiguration::from_sources(
43+
let mut lc = LanguageConfiguration::from_sources(
4544
tree_sitter_typescript::language_typescript(),
4645
Some(String::from("source.ts")),
4746
None,
@@ -53,9 +52,11 @@ pub fn try_language_configuration(
5352
STACK_GRAPHS_BUILTINS_SOURCE,
5453
)),
5554
Some(STACK_GRAPHS_BUILTINS_CONFIG),
56-
FileAnalyzers::new()
57-
.add("tsconfig.json".to_string(), TsConfigAnalyzer {})
58-
.add("package.json".to_string(), NpmPackageAnalyzer {}),
5955
cancellation_flag,
60-
)
56+
)?;
57+
lc.special_files
58+
.add("tsconfig.json".to_string(), TsConfigAnalyzer {})
59+
.add("package.json".to_string(), NpmPackageAnalyzer {});
60+
lc.has_similar_paths = false;
61+
Ok(lc)
6162
}

stack-graphs/src/stitching.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,18 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
11831183
#[derive(Clone, Copy, Debug)]
11841184
pub struct StitcherConfig {
11851185
/// Enables similar path detection during path stitching.
1186-
pub detect_similar_paths: bool,
1186+
detect_similar_paths: bool,
1187+
}
1188+
1189+
impl StitcherConfig {
1190+
pub fn detect_similar_paths(&self) -> bool {
1191+
self.detect_similar_paths
1192+
}
1193+
1194+
pub fn with_detect_similar_paths(mut self, detect_similar_paths: bool) -> Self {
1195+
self.detect_similar_paths = detect_similar_paths;
1196+
self
1197+
}
11871198
}
11881199

11891200
impl StitcherConfig {

tree-sitter-stack-graphs/src/cli/index.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ impl<'a> Indexer<'a> {
280280
}
281281
Err(e) => return Err(IndexError::LoadError(e)),
282282
};
283+
let stitcher_config =
284+
StitcherConfig::default().with_detect_similar_paths(lcs.has_similar_paths());
283285

284286
let source = file_reader.get(source_path)?;
285287
let tag = sha1(source);
@@ -356,7 +358,7 @@ impl<'a> Indexer<'a> {
356358
&graph,
357359
&mut partials,
358360
file,
359-
StitcherConfig::default(),
361+
stitcher_config,
360362
&(&cancellation_flag as &dyn CancellationFlag),
361363
|_g, _ps, p| {
362364
paths.push(p.clone());

tree-sitter-stack-graphs/src/cli/init.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -758,8 +758,6 @@ impl ProjectSettings<'_> {
758758
STACK_GRAPHS_BUILTINS_SOURCE,
759759
)),
760760
Some(STACK_GRAPHS_BUILTINS_CONFIG),
761-
FileAnalyzers::new(),
762-
StitcherConfig::default(),
763761
cancellation_flag,
764762
)
765763
}}

tree-sitter-stack-graphs/src/cli/query.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,10 +186,13 @@ impl<'a> Querier<'a> {
186186
};
187187

188188
let mut reference_paths = Vec::new();
189+
let stitcher_config = StitcherConfig::default()
190+
// always detect similar paths, we don't know the language configurations for the data in the database
191+
.with_detect_similar_paths(true);
189192
if let Err(err) = ForwardPartialPathStitcher::find_all_complete_partial_paths(
190193
self.db,
191194
std::iter::once(node),
192-
StitcherConfig::default(),
195+
stitcher_config,
193196
&cancellation_flag,
194197
|_g, _ps, p| {
195198
reference_paths.push(p.clone());

tree-sitter-stack-graphs/src/cli/test.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,14 +325,16 @@ impl TestArgs {
325325
Ok(_) => {}
326326
}
327327
}
328+
let stitcher_config =
329+
StitcherConfig::default().with_detect_similar_paths(lc.has_similar_paths);
328330
let mut partials = PartialPaths::new();
329331
let mut db = Database::new();
330332
for file in test.graph.iter_files() {
331333
ForwardPartialPathStitcher::find_minimal_partial_path_set_in_file(
332334
&test.graph,
333335
&mut partials,
334336
file,
335-
StitcherConfig::default(),
337+
stitcher_config,
336338
&cancellation_flag.as_ref(),
337339
|g, ps, p| {
338340
db.add_partial_path(g, ps, p.clone());
@@ -342,7 +344,7 @@ impl TestArgs {
342344
let result = test.run(
343345
&mut partials,
344346
&mut db,
345-
StitcherConfig::default(),
347+
stitcher_config,
346348
cancellation_flag.as_ref(),
347349
)?;
348350
let success = result.failure_count() == 0;
@@ -356,7 +358,7 @@ impl TestArgs {
356358
&mut db,
357359
&|_: &StackGraph, h: &Handle<File>| files.contains(h),
358360
success,
359-
StitcherConfig::default(),
361+
stitcher_config,
360362
cancellation_flag.as_ref(),
361363
)?
362364
} else {

tree-sitter-stack-graphs/src/cli/visualize.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ impl VisualizeArgs {
5555
.filter(|n| graph[*n].is_reference())
5656
.collect::<Vec<_>>();
5757
let mut complete_paths_db = Database::new();
58+
let stitcher_config = StitcherConfig::default()
59+
// always detect similar paths, we don't know the language configurations for the data in the database
60+
.with_detect_similar_paths(true);
5861
ForwardPartialPathStitcher::find_all_complete_partial_paths(
5962
&mut db,
6063
starting_nodes,
61-
StitcherConfig::default(),
64+
stitcher_config,
6265
cancellation_flag,
6366
|g, ps, p| {
6467
complete_paths_db.add_partial_path(g, ps, p.clone());

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

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ pub struct LanguageConfiguration {
4444
pub sgl: StackGraphLanguage,
4545
pub builtins: StackGraph,
4646
pub special_files: FileAnalyzers,
47+
pub has_similar_paths: bool,
4748
}
4849

4950
impl LanguageConfiguration {
5051
/// Build a language configuration from tsg and builtins sources. The tsg path
51-
/// is kept for informational only, see [`StackGraphLanguage::from_source`][].
52+
/// is kept for informational use only, see [`StackGraphLanguage::from_source`][].
5253
pub fn from_sources<'a>(
5354
language: Language,
5455
scope: Option<String>,
@@ -58,7 +59,6 @@ impl LanguageConfiguration {
5859
tsg_source: &'a str,
5960
builtins_source: Option<(PathBuf, &'a str)>,
6061
builtins_config: Option<&str>,
61-
special_files: FileAnalyzers,
6262
cancellation_flag: &dyn CancellationFlag,
6363
) -> Result<Self, LoadError<'a>> {
6464
let sgl = StackGraphLanguage::from_source(language, tsg_path.clone(), tsg_source).map_err(
@@ -97,7 +97,8 @@ impl LanguageConfiguration {
9797
file_types,
9898
sgl,
9999
builtins,
100-
special_files,
100+
special_files: FileAnalyzers::new(),
101+
has_similar_paths: true,
101102
})
102103
}
103104

@@ -143,7 +144,7 @@ impl FileAnalyzers {
143144
}
144145
}
145146

146-
pub fn add(
147+
pub fn with(
147148
mut self,
148149
file_name: String,
149150
analyzer: impl FileAnalyzer + Send + Sync + 'static,
@@ -152,6 +153,15 @@ impl FileAnalyzers {
152153
self
153154
}
154155

156+
pub fn add(
157+
&mut self,
158+
file_name: String,
159+
analyzer: impl FileAnalyzer + Send + Sync + 'static,
160+
) -> &mut Self {
161+
self.file_analyzers.insert(file_name, Arc::new(analyzer));
162+
self
163+
}
164+
155165
pub fn get(&self, file_name: &str) -> Option<Arc<dyn FileAnalyzer + Send + Sync>> {
156166
self.file_analyzers.get(file_name).cloned()
157167
}
@@ -358,6 +368,20 @@ impl FileLanguageConfigurations<'_> {
358368
pub fn has_some(&self) -> bool {
359369
self.primary.is_some() || !self.secondary.is_empty()
360370
}
371+
372+
pub fn has_similar_paths(&self) -> bool {
373+
if let Some(lc) = &self.primary {
374+
if lc.has_similar_paths {
375+
return true;
376+
}
377+
}
378+
for (lc, _) in &self.secondary {
379+
if lc.has_similar_paths {
380+
return true;
381+
}
382+
}
383+
return false;
384+
}
361385
}
362386

363387
#[derive(Debug, Error)]
@@ -568,6 +592,8 @@ impl PathLoader {
568592
sgl,
569593
builtins,
570594
special_files: FileAnalyzers::new(),
595+
// always detect similar paths, we don't know the language configuration when loading from the file system
596+
has_similar_paths: true,
571597
};
572598
self.cache.push((language.language, lc));
573599

0 commit comments

Comments
 (0)