From ad9c0dad5e2f91835233a36e652d05ac4ac1a631 Mon Sep 17 00:00:00 2001 From: Hendrik van Antwerpen Date: Mon, 13 Nov 2023 14:48:29 +0100 Subject: [PATCH] Add similar path setting to language configuration --- .../tree-sitter-stack-graphs-java/rust/lib.rs | 2 -- .../rust/lib.rs | 9 ++--- .../rust/lib.rs | 13 +++---- stack-graphs/src/stitching.rs | 13 ++++++- tree-sitter-stack-graphs/src/cli/index.rs | 4 ++- tree-sitter-stack-graphs/src/cli/init.rs | 2 -- tree-sitter-stack-graphs/src/cli/query.rs | 5 ++- tree-sitter-stack-graphs/src/cli/test.rs | 8 +++-- tree-sitter-stack-graphs/src/cli/visualize.rs | 5 ++- tree-sitter-stack-graphs/src/loader.rs | 34 ++++++++++++++++--- tree-sitter-stack-graphs/tests/it/loader.rs | 1 + 11 files changed, 71 insertions(+), 25 deletions(-) diff --git a/languages/tree-sitter-stack-graphs-java/rust/lib.rs b/languages/tree-sitter-stack-graphs-java/rust/lib.rs index 09cb043a5..942785044 100644 --- a/languages/tree-sitter-stack-graphs-java/rust/lib.rs +++ b/languages/tree-sitter-stack-graphs-java/rust/lib.rs @@ -1,4 +1,3 @@ -use tree_sitter_stack_graphs::loader::FileAnalyzers; use tree_sitter_stack_graphs::loader::LanguageConfiguration; use tree_sitter_stack_graphs::loader::LoadError; use tree_sitter_stack_graphs::CancellationFlag; @@ -39,7 +38,6 @@ pub fn try_language_configuration( STACK_GRAPHS_BUILTINS_SOURCE, )), Some(STACK_GRAPHS_BUILTINS_CONFIG), - FileAnalyzers::new(), cancellation_flag, ) } diff --git a/languages/tree-sitter-stack-graphs-javascript/rust/lib.rs b/languages/tree-sitter-stack-graphs-javascript/rust/lib.rs index 508ba58b9..61f113149 100644 --- a/languages/tree-sitter-stack-graphs-javascript/rust/lib.rs +++ b/languages/tree-sitter-stack-graphs-javascript/rust/lib.rs @@ -5,7 +5,6 @@ // Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details. // ------------------------------------------------------------------------------------------------ -use tree_sitter_stack_graphs::loader::FileAnalyzers; use tree_sitter_stack_graphs::loader::LanguageConfiguration; use tree_sitter_stack_graphs::loader::LoadError; use tree_sitter_stack_graphs::CancellationFlag; @@ -38,7 +37,7 @@ pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> Langu pub fn try_language_configuration( cancellation_flag: &dyn CancellationFlag, ) -> Result { - LanguageConfiguration::from_sources( + let mut lc = LanguageConfiguration::from_sources( tree_sitter_javascript::language(), Some(String::from("source.js")), None, @@ -50,7 +49,9 @@ pub fn try_language_configuration( STACK_GRAPHS_BUILTINS_SOURCE, )), Some(STACK_GRAPHS_BUILTINS_CONFIG), - FileAnalyzers::new().add("package.json".to_string(), NpmPackageAnalyzer {}), cancellation_flag, - ) + )?; + lc.special_files + .add("package.json".to_string(), NpmPackageAnalyzer {}); + Ok(lc) } diff --git a/languages/tree-sitter-stack-graphs-typescript/rust/lib.rs b/languages/tree-sitter-stack-graphs-typescript/rust/lib.rs index 45ce6d255..0f84d64c1 100644 --- a/languages/tree-sitter-stack-graphs-typescript/rust/lib.rs +++ b/languages/tree-sitter-stack-graphs-typescript/rust/lib.rs @@ -5,7 +5,6 @@ // Please see the LICENSE-APACHE or LICENSE-MIT files in this distribution for license details. // ------------------------------------------------------------------------------------------------ -use tree_sitter_stack_graphs::loader::FileAnalyzers; use tree_sitter_stack_graphs::loader::LanguageConfiguration; use tree_sitter_stack_graphs::loader::LoadError; use tree_sitter_stack_graphs::CancellationFlag; @@ -41,7 +40,7 @@ pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> Langu pub fn try_language_configuration( cancellation_flag: &dyn CancellationFlag, ) -> Result { - LanguageConfiguration::from_sources( + let mut lc = LanguageConfiguration::from_sources( tree_sitter_typescript::language_typescript(), Some(String::from("source.ts")), None, @@ -53,9 +52,11 @@ pub fn try_language_configuration( STACK_GRAPHS_BUILTINS_SOURCE, )), Some(STACK_GRAPHS_BUILTINS_CONFIG), - FileAnalyzers::new() - .add("tsconfig.json".to_string(), TsConfigAnalyzer {}) - .add("package.json".to_string(), NpmPackageAnalyzer {}), cancellation_flag, - ) + )?; + lc.special_files + .add("tsconfig.json".to_string(), TsConfigAnalyzer {}) + .add("package.json".to_string(), NpmPackageAnalyzer {}); + lc.has_similar_paths = false; + Ok(lc) } diff --git a/stack-graphs/src/stitching.rs b/stack-graphs/src/stitching.rs index 9e9dd8832..f8e8b0522 100644 --- a/stack-graphs/src/stitching.rs +++ b/stack-graphs/src/stitching.rs @@ -1183,7 +1183,18 @@ impl ForwardPartialPathStitcher { #[derive(Clone, Copy, Debug)] pub struct StitcherConfig { /// Enables similar path detection during path stitching. - pub detect_similar_paths: bool, + detect_similar_paths: bool, +} + +impl StitcherConfig { + pub fn detect_similar_paths(&self) -> bool { + self.detect_similar_paths + } + + pub fn with_detect_similar_paths(mut self, detect_similar_paths: bool) -> Self { + self.detect_similar_paths = detect_similar_paths; + self + } } impl StitcherConfig { diff --git a/tree-sitter-stack-graphs/src/cli/index.rs b/tree-sitter-stack-graphs/src/cli/index.rs index 436caca1a..a37806d45 100644 --- a/tree-sitter-stack-graphs/src/cli/index.rs +++ b/tree-sitter-stack-graphs/src/cli/index.rs @@ -280,6 +280,8 @@ impl<'a> Indexer<'a> { } Err(e) => return Err(IndexError::LoadError(e)), }; + let stitcher_config = + StitcherConfig::default().with_detect_similar_paths(lcs.has_similar_paths()); let source = file_reader.get(source_path)?; let tag = sha1(source); @@ -356,7 +358,7 @@ impl<'a> Indexer<'a> { &graph, &mut partials, file, - StitcherConfig::default(), + stitcher_config, &(&cancellation_flag as &dyn CancellationFlag), |_g, _ps, p| { paths.push(p.clone()); diff --git a/tree-sitter-stack-graphs/src/cli/init.rs b/tree-sitter-stack-graphs/src/cli/init.rs index c287909e6..d9fc0cd59 100644 --- a/tree-sitter-stack-graphs/src/cli/init.rs +++ b/tree-sitter-stack-graphs/src/cli/init.rs @@ -758,8 +758,6 @@ impl ProjectSettings<'_> { STACK_GRAPHS_BUILTINS_SOURCE, )), Some(STACK_GRAPHS_BUILTINS_CONFIG), - FileAnalyzers::new(), - StitcherConfig::default(), cancellation_flag, ) }} diff --git a/tree-sitter-stack-graphs/src/cli/query.rs b/tree-sitter-stack-graphs/src/cli/query.rs index 1063e49e9..6bbf46702 100644 --- a/tree-sitter-stack-graphs/src/cli/query.rs +++ b/tree-sitter-stack-graphs/src/cli/query.rs @@ -186,10 +186,13 @@ impl<'a> Querier<'a> { }; let mut reference_paths = Vec::new(); + let stitcher_config = StitcherConfig::default() + // always detect similar paths, we don't know the language configurations for the data in the database + .with_detect_similar_paths(true); if let Err(err) = ForwardPartialPathStitcher::find_all_complete_partial_paths( self.db, std::iter::once(node), - StitcherConfig::default(), + stitcher_config, &cancellation_flag, |_g, _ps, p| { reference_paths.push(p.clone()); diff --git a/tree-sitter-stack-graphs/src/cli/test.rs b/tree-sitter-stack-graphs/src/cli/test.rs index 459573277..3331a713b 100644 --- a/tree-sitter-stack-graphs/src/cli/test.rs +++ b/tree-sitter-stack-graphs/src/cli/test.rs @@ -325,6 +325,8 @@ impl TestArgs { Ok(_) => {} } } + let stitcher_config = + StitcherConfig::default().with_detect_similar_paths(lc.has_similar_paths); let mut partials = PartialPaths::new(); let mut db = Database::new(); for file in test.graph.iter_files() { @@ -332,7 +334,7 @@ impl TestArgs { &test.graph, &mut partials, file, - StitcherConfig::default(), + stitcher_config, &cancellation_flag.as_ref(), |g, ps, p| { db.add_partial_path(g, ps, p.clone()); @@ -342,7 +344,7 @@ impl TestArgs { let result = test.run( &mut partials, &mut db, - StitcherConfig::default(), + stitcher_config, cancellation_flag.as_ref(), )?; let success = result.failure_count() == 0; @@ -356,7 +358,7 @@ impl TestArgs { &mut db, &|_: &StackGraph, h: &Handle| files.contains(h), success, - StitcherConfig::default(), + stitcher_config, cancellation_flag.as_ref(), )? } else { diff --git a/tree-sitter-stack-graphs/src/cli/visualize.rs b/tree-sitter-stack-graphs/src/cli/visualize.rs index 9f2d53c50..f17f23b88 100644 --- a/tree-sitter-stack-graphs/src/cli/visualize.rs +++ b/tree-sitter-stack-graphs/src/cli/visualize.rs @@ -55,10 +55,13 @@ impl VisualizeArgs { .filter(|n| graph[*n].is_reference()) .collect::>(); let mut complete_paths_db = Database::new(); + let stitcher_config = StitcherConfig::default() + // always detect similar paths, we don't know the language configurations for the data in the database + .with_detect_similar_paths(true); ForwardPartialPathStitcher::find_all_complete_partial_paths( &mut db, starting_nodes, - StitcherConfig::default(), + stitcher_config, cancellation_flag, |g, ps, p| { complete_paths_db.add_partial_path(g, ps, p.clone()); diff --git a/tree-sitter-stack-graphs/src/loader.rs b/tree-sitter-stack-graphs/src/loader.rs index 0feb6a28c..b44996ffe 100644 --- a/tree-sitter-stack-graphs/src/loader.rs +++ b/tree-sitter-stack-graphs/src/loader.rs @@ -44,11 +44,12 @@ pub struct LanguageConfiguration { pub sgl: StackGraphLanguage, pub builtins: StackGraph, pub special_files: FileAnalyzers, + pub has_similar_paths: bool, } impl LanguageConfiguration { /// Build a language configuration from tsg and builtins sources. The tsg path - /// is kept for informational only, see [`StackGraphLanguage::from_source`][]. + /// is kept for informational use only, see [`StackGraphLanguage::from_source`][]. pub fn from_sources<'a>( language: Language, scope: Option, @@ -58,7 +59,6 @@ impl LanguageConfiguration { tsg_source: &'a str, builtins_source: Option<(PathBuf, &'a str)>, builtins_config: Option<&str>, - special_files: FileAnalyzers, cancellation_flag: &dyn CancellationFlag, ) -> Result> { let sgl = StackGraphLanguage::from_source(language, tsg_path.clone(), tsg_source).map_err( @@ -97,7 +97,8 @@ impl LanguageConfiguration { file_types, sgl, builtins, - special_files, + special_files: FileAnalyzers::new(), + has_similar_paths: true, }) } @@ -143,7 +144,7 @@ impl FileAnalyzers { } } - pub fn add( + pub fn with( mut self, file_name: String, analyzer: impl FileAnalyzer + Send + Sync + 'static, @@ -152,6 +153,15 @@ impl FileAnalyzers { self } + pub fn add( + &mut self, + file_name: String, + analyzer: impl FileAnalyzer + Send + Sync + 'static, + ) -> &mut Self { + self.file_analyzers.insert(file_name, Arc::new(analyzer)); + self + } + pub fn get(&self, file_name: &str) -> Option> { self.file_analyzers.get(file_name).cloned() } @@ -358,6 +368,20 @@ impl FileLanguageConfigurations<'_> { pub fn has_some(&self) -> bool { self.primary.is_some() || !self.secondary.is_empty() } + + pub fn has_similar_paths(&self) -> bool { + if let Some(lc) = &self.primary { + if lc.has_similar_paths { + return true; + } + } + for (lc, _) in &self.secondary { + if lc.has_similar_paths { + return true; + } + } + return false; + } } #[derive(Debug, Error)] @@ -568,6 +592,8 @@ impl PathLoader { sgl, builtins, special_files: FileAnalyzers::new(), + // always detect similar paths, we don't know the language configuration when loading from the file system + has_similar_paths: true, }; self.cache.push((language.language, lc)); diff --git a/tree-sitter-stack-graphs/tests/it/loader.rs b/tree-sitter-stack-graphs/tests/it/loader.rs index 8fcf0ccac..176600435 100644 --- a/tree-sitter-stack-graphs/tests/it/loader.rs +++ b/tree-sitter-stack-graphs/tests/it/loader.rs @@ -35,6 +35,7 @@ fn can_load_from_provided_language_configuration() { sgl, builtins: StackGraph::new(), special_files: FileAnalyzers::new(), + has_similar_paths: true, }; let mut loader = Loader::from_language_configurations(vec![lc], None).expect("Expected loader to succeed");