Skip to content

Commit

Permalink
Add similar path setting to language configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
hendrikvanantwerpen committed Nov 13, 2023
1 parent a92b9b0 commit ad9c0da
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 25 deletions.
2 changes: 0 additions & 2 deletions languages/tree-sitter-stack-graphs-java/rust/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -39,7 +38,6 @@ pub fn try_language_configuration(
STACK_GRAPHS_BUILTINS_SOURCE,
)),
Some(STACK_GRAPHS_BUILTINS_CONFIG),
FileAnalyzers::new(),
cancellation_flag,
)
}
9 changes: 5 additions & 4 deletions languages/tree-sitter-stack-graphs-javascript/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -38,7 +37,7 @@ pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> Langu
pub fn try_language_configuration(
cancellation_flag: &dyn CancellationFlag,
) -> Result<LanguageConfiguration, LoadError> {
LanguageConfiguration::from_sources(
let mut lc = LanguageConfiguration::from_sources(
tree_sitter_javascript::language(),
Some(String::from("source.js")),
None,
Expand All @@ -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)
}
13 changes: 7 additions & 6 deletions languages/tree-sitter-stack-graphs-typescript/rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,7 +40,7 @@ pub fn language_configuration(cancellation_flag: &dyn CancellationFlag) -> Langu
pub fn try_language_configuration(
cancellation_flag: &dyn CancellationFlag,
) -> Result<LanguageConfiguration, LoadError> {
LanguageConfiguration::from_sources(
let mut lc = LanguageConfiguration::from_sources(
tree_sitter_typescript::language_typescript(),
Some(String::from("source.ts")),
None,
Expand All @@ -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)
}
13 changes: 12 additions & 1 deletion stack-graphs/src/stitching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,18 @@ impl<H: Clone> ForwardPartialPathStitcher<H> {
#[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 {
Expand Down
4 changes: 3 additions & 1 deletion tree-sitter-stack-graphs/src/cli/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
Expand Down
2 changes: 0 additions & 2 deletions tree-sitter-stack-graphs/src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -758,8 +758,6 @@ impl ProjectSettings<'_> {
STACK_GRAPHS_BUILTINS_SOURCE,
)),
Some(STACK_GRAPHS_BUILTINS_CONFIG),
FileAnalyzers::new(),
StitcherConfig::default(),
cancellation_flag,
)
}}
Expand Down
5 changes: 4 additions & 1 deletion tree-sitter-stack-graphs/src/cli/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
8 changes: 5 additions & 3 deletions tree-sitter-stack-graphs/src/cli/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,16 @@ 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() {
ForwardPartialPathStitcher::find_minimal_partial_path_set_in_file(
&test.graph,
&mut partials,
file,
StitcherConfig::default(),
stitcher_config,
&cancellation_flag.as_ref(),
|g, ps, p| {
db.add_partial_path(g, ps, p.clone());
Expand All @@ -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;
Expand All @@ -356,7 +358,7 @@ impl TestArgs {
&mut db,
&|_: &StackGraph, h: &Handle<File>| files.contains(h),
success,
StitcherConfig::default(),
stitcher_config,
cancellation_flag.as_ref(),
)?
} else {
Expand Down
5 changes: 4 additions & 1 deletion tree-sitter-stack-graphs/src/cli/visualize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,13 @@ impl VisualizeArgs {
.filter(|n| graph[*n].is_reference())
.collect::<Vec<_>>();
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());
Expand Down
34 changes: 30 additions & 4 deletions tree-sitter-stack-graphs/src/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
Expand All @@ -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<Self, LoadError<'a>> {
let sgl = StackGraphLanguage::from_source(language, tsg_path.clone(), tsg_source).map_err(
Expand Down Expand Up @@ -97,7 +97,8 @@ impl LanguageConfiguration {
file_types,
sgl,
builtins,
special_files,
special_files: FileAnalyzers::new(),
has_similar_paths: true,
})
}

Expand Down Expand Up @@ -143,7 +144,7 @@ impl FileAnalyzers {
}
}

pub fn add(
pub fn with(
mut self,
file_name: String,
analyzer: impl FileAnalyzer + Send + Sync + 'static,
Expand All @@ -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<Arc<dyn FileAnalyzer + Send + Sync>> {
self.file_analyzers.get(file_name).cloned()
}
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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));

Expand Down
1 change: 1 addition & 0 deletions tree-sitter-stack-graphs/tests/it/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down

0 comments on commit ad9c0da

Please sign in to comment.