Skip to content
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
10 changes: 5 additions & 5 deletions crates/ruff_benchmark/benches/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ruff_python_ast::PythonVersion;
use ty_project::metadata::options::{EnvironmentOptions, Options};
use ty_project::metadata::value::RangedValue;
use ty_project::watch::{ChangeEvent, ChangedKind};
use ty_project::{Db, DummyReporter, ProjectDatabase, ProjectMetadata};
use ty_project::{Db, ProjectDatabase, ProjectMetadata};

struct Case {
db: ProjectDatabase,
Expand Down Expand Up @@ -164,7 +164,7 @@ fn benchmark_incremental(criterion: &mut Criterion) {
fn setup() -> Case {
let case = setup_tomllib_case();

let result: Vec<_> = case.db.check(&DummyReporter).unwrap();
let result: Vec<_> = case.db.check().unwrap();

assert_diagnostics(&case.db, &result, EXPECTED_TOMLLIB_DIAGNOSTICS);

Expand Down Expand Up @@ -192,7 +192,7 @@ fn benchmark_incremental(criterion: &mut Criterion) {
None,
);

let result = db.check(&DummyReporter).unwrap();
let result = db.check().unwrap();

assert_eq!(result.len(), EXPECTED_TOMLLIB_DIAGNOSTICS.len());
}
Expand All @@ -212,7 +212,7 @@ fn benchmark_cold(criterion: &mut Criterion) {
setup_tomllib_case,
|case| {
let Case { db, .. } = case;
let result: Vec<_> = db.check(&DummyReporter).unwrap();
let result: Vec<_> = db.check().unwrap();

assert_diagnostics(db, &result, EXPECTED_TOMLLIB_DIAGNOSTICS);
},
Expand Down Expand Up @@ -326,7 +326,7 @@ fn benchmark_many_string_assignments(criterion: &mut Criterion) {
},
|case| {
let Case { db, .. } = case;
let result = db.check(&DummyReporter).unwrap();
let result = db.check().unwrap();
assert_eq!(result.len(), 0);
},
BatchSize::SmallInput,
Expand Down
34 changes: 19 additions & 15 deletions crates/ty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,10 @@ impl MainLoop {
self.run_with_progress::<IndicatifReporter>(db)
}

fn run_with_progress<R: Reporter>(mut self, db: &mut ProjectDatabase) -> Result<ExitStatus> {
fn run_with_progress<R>(mut self, db: &mut ProjectDatabase) -> Result<ExitStatus>
where
R: Reporter + Default + 'static,
{
self.sender.send(MainLoopMessage::CheckWorkspace).unwrap();

let result = self.main_loop::<R>(db);
Expand All @@ -226,7 +229,10 @@ impl MainLoop {
result
}

fn main_loop<R: Reporter>(&mut self, db: &mut ProjectDatabase) -> Result<ExitStatus> {
fn main_loop<R>(&mut self, db: &mut ProjectDatabase) -> Result<ExitStatus>
where
R: Reporter + Default + 'static,
{
// Schedule the first check.
tracing::debug!("Starting main loop");

Expand All @@ -237,12 +243,12 @@ impl MainLoop {
MainLoopMessage::CheckWorkspace => {
let db = db.clone();
let sender = self.sender.clone();
let reporter = R::default();
let mut reporter = R::default();

// Spawn a new task that checks the project. This needs to be done in a separate thread
// to prevent blocking the main loop here.
rayon::spawn(move || {
match db.check(&reporter) {
match db.check_with_reporter(&mut reporter) {
Ok(result) => {
// Send the result back to the main loop for printing.
sender
Expand Down Expand Up @@ -353,11 +359,12 @@ impl MainLoop {
}

/// A progress reporter for `ty check`.
struct IndicatifReporter(indicatif::ProgressBar);
#[derive(Default)]
struct IndicatifReporter(Option<indicatif::ProgressBar>);

impl Default for IndicatifReporter {
fn default() -> IndicatifReporter {
let progress = indicatif::ProgressBar::new(0);
impl ty_project::Reporter for IndicatifReporter {
fn set_files(&mut self, files: usize) {
let progress = indicatif::ProgressBar::new(files as u64);
progress.set_style(
indicatif::ProgressStyle::with_template(
"{msg:8.dim} {bar:60.green/dim} {pos}/{len} files",
Expand All @@ -366,17 +373,14 @@ impl Default for IndicatifReporter {
.progress_chars("--"),
);
progress.set_message("Checking");
IndicatifReporter(progress)
}
}

impl ty_project::Reporter for IndicatifReporter {
fn set_files(&self, files: usize) {
self.0.set_length(files as u64);
self.0 = Some(progress);
}

fn report_file(&self, _file: &ruff_db::files::File) {
self.0.inc(1);
if let Some(ref progress_bar) = self.0 {
progress_bar.inc(1);
}
}
}

Expand Down
12 changes: 3 additions & 9 deletions crates/ty/tests/file_watching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ty_project::metadata::options::{EnvironmentOptions, Options};
use ty_project::metadata::pyproject::{PyProject, Tool};
use ty_project::metadata::value::{RangedValue, RelativePathBuf};
use ty_project::watch::{directory_watcher, ChangeEvent, ProjectWatcher};
use ty_project::{Db, DummyReporter, ProjectDatabase, ProjectMetadata};
use ty_project::{Db, ProjectDatabase, ProjectMetadata};
use ty_python_semantic::{resolve_module, ModuleName, PythonPlatform};

struct TestCase {
Expand Down Expand Up @@ -1117,10 +1117,7 @@ print(sys.last_exc, os.getegid())
Ok(())
})?;

let diagnostics = case
.db
.check(&DummyReporter)
.context("Failed to check project.")?;
let diagnostics = case.db.check().context("Failed to check project.")?;

assert_eq!(diagnostics.len(), 2);
assert_eq!(
Expand All @@ -1145,10 +1142,7 @@ print(sys.last_exc, os.getegid())
})
.expect("Search path settings to be valid");

let diagnostics = case
.db
.check(&DummyReporter)
.context("Failed to check project.")?;
let diagnostics = case.db.check().context("Failed to check project.")?;
assert!(diagnostics.is_empty());

Ok(())
Expand Down
17 changes: 14 additions & 3 deletions crates/ty_project/src/db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::panic::RefUnwindSafe;
use std::panic::{AssertUnwindSafe, RefUnwindSafe};
use std::sync::Arc;

use crate::DEFAULT_LINT_REGISTRY;
use crate::{DummyReporter, DEFAULT_LINT_REGISTRY};
use crate::{Project, ProjectMetadata, Reporter};
use ruff_db::diagnostic::Diagnostic;
use ruff_db::files::{File, Files};
Expand Down Expand Up @@ -68,7 +68,18 @@ impl ProjectDatabase {
}

/// Checks all open files in the project and its dependencies.
pub fn check(&self, reporter: &impl Reporter) -> Result<Vec<Diagnostic>, Cancelled> {
pub fn check(&self) -> Result<Vec<Diagnostic>, Cancelled> {
let mut reporter = DummyReporter;
let reporter = AssertUnwindSafe(&mut reporter as &mut dyn Reporter);
self.with_db(|db| db.project().check(db, reporter))
}

/// Checks all open files in the project and its dependencies, using the given reporter.
pub fn check_with_reporter(
&self,
reporter: &mut dyn Reporter,
) -> Result<Vec<Diagnostic>, Cancelled> {
let reporter = AssertUnwindSafe(reporter);
self.with_db(|db| db.project().check(db, reporter))
}

Expand Down
15 changes: 10 additions & 5 deletions crates/ty_project/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use rustc_hash::FxHashSet;
use salsa::Durability;
use salsa::Setter;
use std::backtrace::BacktraceStatus;
use std::panic::{AssertUnwindSafe, RefUnwindSafe, UnwindSafe};
use std::panic::{AssertUnwindSafe, UnwindSafe};
use std::sync::Arc;
use thiserror::Error;
use tracing::error;
Expand Down Expand Up @@ -107,9 +107,9 @@ pub struct Project {
}

/// A progress reporter.
pub trait Reporter: Default + Send + Sync + RefUnwindSafe + 'static {
pub trait Reporter: Send + Sync {
/// Initialize the reporter with the number of files.
fn set_files(&self, files: usize);
fn set_files(&mut self, files: usize);

/// Report the completion of a given file.
fn report_file(&self, file: &File);
Expand All @@ -120,7 +120,7 @@ pub trait Reporter: Default + Send + Sync + RefUnwindSafe + 'static {
pub struct DummyReporter;

impl Reporter for DummyReporter {
fn set_files(&self, _files: usize) {}
fn set_files(&mut self, _files: usize) {}
fn report_file(&self, _file: &File) {}
}

Expand Down Expand Up @@ -186,7 +186,11 @@ impl Project {
}

/// Checks all open files in the project and its dependencies.
pub(crate) fn check(self, db: &ProjectDatabase, reporter: &impl Reporter) -> Vec<Diagnostic> {
pub(crate) fn check(
self,
db: &ProjectDatabase,
mut reporter: AssertUnwindSafe<&mut dyn Reporter>,
) -> Vec<Diagnostic> {
let project_span = tracing::debug_span!("Project::check");
let _span = project_span.enter();

Expand Down Expand Up @@ -215,6 +219,7 @@ impl Project {
let db = db.clone();
let file_diagnostics = &file_diagnostics;
let project_span = &project_span;
let reporter = &reporter;

rayon::scope(move |scope| {
for file in &files {
Expand Down
4 changes: 2 additions & 2 deletions crates/ty_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use ty_ide::{goto_type_definition, hover, inlay_hints, MarkupKind};
use ty_project::metadata::options::Options;
use ty_project::metadata::value::ValueSource;
use ty_project::watch::{ChangeEvent, ChangedKind, CreatedKind, DeletedKind};
use ty_project::ProjectMetadata;
use ty_project::{Db, ProjectDatabase};
use ty_project::{DummyReporter, ProjectMetadata};
use ty_python_semantic::Program;
use wasm_bindgen::prelude::*;

Expand Down Expand Up @@ -186,7 +186,7 @@ impl Workspace {

/// Checks all open files
pub fn check(&self) -> Result<Vec<Diagnostic>, Error> {
let result = self.db.check(&DummyReporter).map_err(into_error)?;
let result = self.db.check().map_err(into_error)?;

Ok(result.into_iter().map(Diagnostic::wrap).collect())
}
Expand Down
Loading