Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make Analyzer accept the diagnostics configuration. #274

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion gauntlet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use report::Status;
use report::UnmatchedStatus;
pub use repository::Repository;
use wdl::analysis::Analyzer;
use wdl::analysis::DiagnosticsConfig;
use wdl::analysis::rules;
use wdl::ast::Diagnostic;
use wdl::lint::LintVisitor;
Expand Down Expand Up @@ -174,7 +175,7 @@ pub async fn gauntlet(args: Args) -> Result<()> {

let analyzer = Analyzer::new_with_validator(
// Don't bother duplicating analysis warnings for arena mode
if args.arena { Vec::new() } else { rules() },
DiagnosticsConfig::new(if args.arena { Vec::new() } else { rules() }),
move |_: (), _, _, _| async move {},
move || {
let mut validator = if !args.arena {
Expand Down
2 changes: 2 additions & 0 deletions wdl-analysis/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

* Changed the `new` and `new_with_validator` methods of `Analyzer` to take the
diagnostics configuration rather than a rule iterator ([#274](https://github.com/stjude-rust-labs/wdl/pull/274)).
* Refactored the `AnalysisResult` and `Document` types to move properties of
the former into the latter; this will assist in evaluation of documents in
that the `Document` alone can be passed into evaluation ([#265](https://github.com/stjude-rust-labs/wdl/pull/265)).
Expand Down
30 changes: 13 additions & 17 deletions wdl-analysis/src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ pub struct DiagnosticsConfig {

impl DiagnosticsConfig {
/// Creates a new diagnostics configuration from a rule set.
pub fn new<T: AsRef<dyn Rule>>(rules: impl Iterator<Item = T>) -> Self {
pub fn new<T: AsRef<dyn Rule>>(rules: impl IntoIterator<Item = T>) -> Self {
let mut unused_import = None;
let mut unused_input = None;
let mut unused_declaration = None;
Expand Down Expand Up @@ -402,35 +402,32 @@ impl<Context> Analyzer<Context>
where
Context: Send + Clone + 'static,
{
/// Constructs a new analyzer with the given analysis rules.
/// Constructs a new analyzer with the given diagnostics config.
///
/// The provided progress callback will be invoked during analysis.
///
/// The analyzer will use a default validator for validation.
///
/// The analyzer must be constructed from the context of a Tokio runtime.
pub fn new<T: AsRef<dyn Rule>, Progress, Return>(
rules: impl IntoIterator<Item = T>,
progress: Progress,
) -> Self
pub fn new<Progress, Return>(config: DiagnosticsConfig, progress: Progress) -> Self
where
Progress: Fn(Context, ProgressKind, usize, usize) -> Return + Send + 'static,
Return: Future<Output = ()>,
{
Self::new_with_validator(rules, progress, Validator::default)
Self::new_with_validator(config, progress, Validator::default)
}

/// Constructs a new analyzer with the given analysis rules and validator
/// function.
/// Constructs a new analyzer with the given diagnostics config and
/// validator function.
///
/// The provided progress callback will be invoked during analysis.
///
/// This validator function will be called once per worker thread to
/// initialize a thread-local validator.
///
/// The analyzer must be constructed from the context of a Tokio runtime.
pub fn new_with_validator<T: AsRef<dyn Rule>, Progress, Return, Validator>(
rules: impl IntoIterator<Item = T>,
pub fn new_with_validator<Progress, Return, Validator>(
config: DiagnosticsConfig,
progress: Progress,
validator: Validator,
) -> Self
Expand All @@ -441,7 +438,6 @@ where
{
let (tx, rx) = mpsc::unbounded_channel();
let tokio = Handle::current();
let config = DiagnosticsConfig::new(rules.into_iter());
let handle = std::thread::spawn(move || {
let queue = AnalysisQueue::new(config, tokio, progress, validator);
queue.run(rx);
Expand Down Expand Up @@ -706,7 +702,7 @@ mod test {

#[tokio::test]
async fn it_returns_empty_results() {
let analyzer = Analyzer::new(rules(), |_: (), _, _, _| async {});
let analyzer = Analyzer::new(DiagnosticsConfig::new(rules()), |_: (), _, _, _| async {});
let results = analyzer.analyze(()).await.unwrap();
assert!(results.is_empty());
}
Expand All @@ -730,7 +726,7 @@ workflow test {
.expect("failed to create test file");

// Analyze the file and check the resulting diagnostic
let analyzer = Analyzer::new(rules(), |_: (), _, _, _| async {});
let analyzer = Analyzer::new(DiagnosticsConfig::new(rules()), |_: (), _, _, _| async {});
analyzer
.add_document(path_to_uri(&path).expect("should convert to URI"))
.await
Expand Down Expand Up @@ -785,7 +781,7 @@ workflow test {
.expect("failed to create test file");

// Analyze the file and check the resulting diagnostic
let analyzer = Analyzer::new(rules(), |_: (), _, _, _| async {});
let analyzer = Analyzer::new(DiagnosticsConfig::new(rules()), |_: (), _, _, _| async {});
analyzer
.add_document(path_to_uri(&path).expect("should convert to URI"))
.await
Expand Down Expand Up @@ -857,7 +853,7 @@ workflow test {
.expect("failed to create test file");

// Analyze the file and check the resulting diagnostic
let analyzer = Analyzer::new(rules(), |_: (), _, _, _| async {});
let analyzer = Analyzer::new(DiagnosticsConfig::new(rules()), |_: (), _, _, _| async {});
analyzer
.add_document(path_to_uri(&path).expect("should convert to URI"))
.await
Expand Down Expand Up @@ -933,7 +929,7 @@ workflow test {
.expect("failed to create test file");

// Add all three documents to the analyzer
let analyzer = Analyzer::new(rules(), |_: (), _, _, _| async {});
let analyzer = Analyzer::new(DiagnosticsConfig::new(rules()), |_: (), _, _, _| async {});
analyzer
.add_directory(dir.path().to_path_buf())
.await
Expand Down
5 changes: 3 additions & 2 deletions wdl-analysis/tests/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use path_clean::clean;
use pretty_assertions::StrComparison;
use wdl_analysis::AnalysisResult;
use wdl_analysis::Analyzer;
use wdl_analysis::DiagnosticsConfig;
use wdl_analysis::path_to_uri;
use wdl_analysis::rules;
use wdl_ast::Diagnostic;
Expand Down Expand Up @@ -164,7 +165,7 @@ async fn main() {
println!("\nrunning {} tests\n", tests.len());

// Start with a single analysis pass over all the test files
let analyzer = Analyzer::new(rules(), |_, _, _, _| async {});
let analyzer = Analyzer::new(DiagnosticsConfig::new(rules()), |_, _, _, _| async {});
for test in &tests {
analyzer
.add_directory(test.clone())
Expand Down Expand Up @@ -215,7 +216,7 @@ async fn main() {
// Some tests are sensitive to the order in which files are parsed (e.g.
// detecting cycles) For those, use a new analyzer and analyze the
// `source.wdl` directly
let analyzer = Analyzer::new(rules(), |_, _, _, _| async {});
let analyzer = Analyzer::new(DiagnosticsConfig::new(rules()), |_, _, _, _| async {});
for test_name in single_file {
let test = Path::new("tests/analysis").join(test_name);
let document = test.join("source.wdl");
Expand Down
3 changes: 2 additions & 1 deletion wdl-doc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use pulldown_cmark::Options;
use pulldown_cmark::Parser;
use tokio::io::AsyncWriteExt;
use wdl_analysis::Analyzer;
use wdl_analysis::DiagnosticsConfig;
use wdl_analysis::rules;
use wdl_ast::AstToken;
use wdl_ast::Document as AstDocument;
Expand Down Expand Up @@ -141,7 +142,7 @@ pub async fn document_workspace(path: PathBuf) -> Result<()> {
std::fs::create_dir(&docs_dir)?;
}

let analyzer = Analyzer::new(rules(), |_: (), _, _, _| async {});
let analyzer = Analyzer::new(DiagnosticsConfig::new(rules()), |_: (), _, _, _| async {});
analyzer.add_directory(abs_path.clone()).await?;
let results = analyzer.analyze(()).await?;

Expand Down
3 changes: 2 additions & 1 deletion wdl-engine/tests/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use pretty_assertions::StrComparison;
use rayon::prelude::*;
use wdl_analysis::AnalysisResult;
use wdl_analysis::Analyzer;
use wdl_analysis::DiagnosticsConfig;
use wdl_analysis::rules;
use wdl_analysis::types::Types;
use wdl_ast::Diagnostic;
Expand Down Expand Up @@ -197,7 +198,7 @@ async fn main() {
println!("\nrunning {} tests\n", tests.len());

// Start with a single analysis pass over all the test files
let analyzer = Analyzer::new(rules(), |_, _, _, _| async {});
let analyzer = Analyzer::new(DiagnosticsConfig::new(rules()), |_, _, _, _| async {});
for test in &tests {
analyzer
.add_directory(test.clone())
Expand Down
3 changes: 2 additions & 1 deletion wdl-engine/tests/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ use tempfile::TempDir;
use walkdir::WalkDir;
use wdl_analysis::AnalysisResult;
use wdl_analysis::Analyzer;
use wdl_analysis::DiagnosticsConfig;
use wdl_analysis::document::Document;
use wdl_analysis::rules;
use wdl_ast::Diagnostic;
Expand Down Expand Up @@ -411,7 +412,7 @@ async fn main() {
println!("\nrunning {} tests\n", tests.len());

// Start with a single analysis pass over all the test files
let analyzer = Analyzer::new(rules(), |_, _, _, _| async {});
let analyzer = Analyzer::new(DiagnosticsConfig::new(rules()), |_, _, _, _| async {});
for test in &tests {
analyzer
.add_directory(test.clone())
Expand Down
3 changes: 2 additions & 1 deletion wdl-lsp/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use tracing::error;
use tracing::info;
use uuid::Uuid;
use wdl_analysis::Analyzer;
use wdl_analysis::DiagnosticsConfig;
use wdl_analysis::IncrementalChange;
use wdl_analysis::SourceEdit;
use wdl_analysis::SourcePosition;
Expand Down Expand Up @@ -252,7 +253,7 @@ impl Server {
client,
options,
analyzer: Analyzer::<ProgressToken>::new_with_validator(
rules(),
DiagnosticsConfig::new(rules()),
move |token, kind, current, total| {
let client = analyzer_client.clone();
async move {
Expand Down
3 changes: 2 additions & 1 deletion wdl/src/bin/wdl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use wdl::ast::Validator;
use wdl::lint::LintVisitor;
use wdl_analysis::AnalysisResult;
use wdl_analysis::Analyzer;
use wdl_analysis::DiagnosticsConfig;
use wdl_analysis::Rule;
use wdl_analysis::path_to_uri;
use wdl_analysis::rules;
Expand Down Expand Up @@ -95,7 +96,7 @@ async fn analyze<T: AsRef<dyn Rule>>(
);

let analyzer = Analyzer::new_with_validator(
rules,
DiagnosticsConfig::new(rules),
move |bar: ProgressBar, kind, completed, total| async move {
bar.set_position(completed.try_into().unwrap());
if completed == 0 {
Expand Down
Loading