diff --git a/crates/ide-build/src/kotlin_bridge.rs b/crates/ide-build/src/kotlin_bridge.rs index 396773b1a6..81923f0d44 100644 --- a/crates/ide-build/src/kotlin_bridge.rs +++ b/crates/ide-build/src/kotlin_bridge.rs @@ -91,12 +91,14 @@ fn plan(root: &Path, model: &BuildModel, cancel: &CancellationToken) -> IdeResul /// Run the project's configured wrapper/settings/JDK and lifecycle, with an output /// redirector loaded before Maven calculates its lifecycle execution plans. +/// Builds the Kotlin libraries; `on_progress` gets Maven's progress (message, fraction 0..1). pub fn build( root: &Path, model: &BuildModel, bundle: &Path, generation: &Path, cancel: &CancellationToken, + on_progress: &(dyn Fn(&str, Option) + Sync), ) -> IdeResult { std::fs::create_dir_all(generation)?; let mut plan = plan(root, model, cancel)?; @@ -124,10 +126,10 @@ pub fn build( for step in &plan.steps { let mut reactor = plan.clone(); reactor.steps = vec![step.clone()]; - let result = compile::run(root, &reactor, cancel, &|event| { - if let BuildEvent::Output { text, .. } = event { - tracing::debug!("kotlin bridge: {}", text.trim_end()); - } + let result = compile::run(root, &reactor, cancel, &|event| match event { + BuildEvent::Output { text, .. } => tracing::debug!("kotlin bridge: {}", text.trim_end()), + BuildEvent::Progress { message, fraction } => on_progress(&message, fraction), + _ => {} })?; messages.extend(result.messages); } @@ -456,6 +458,7 @@ mod tests { Path::new("unused.jar"), &directory.path().join("private"), &CancellationToken::new(), + &|_, _| {}, ) .err() .unwrap(); diff --git a/crates/ide-host/src/kotlin_bridge.rs b/crates/ide-host/src/kotlin_bridge.rs index 2aec581c5a..f7bb91a6bd 100644 --- a/crates/ide-host/src/kotlin_bridge.rs +++ b/crates/ide-host/src/kotlin_bridge.rs @@ -2,7 +2,7 @@ use ide_build::kotlin_bridge::{self, Outcome, ProjectLibraries}; use ide_core::event::NotifyLevel; -use ide_core::{CancellationToken, Diagnostic, IdeEvent, Pos, Range, Severity, SharedSink}; +use ide_core::{CancellationToken, Diagnostic, IdeEvent, Pos, Range, Severity, SharedSink, TaskId}; use ide_lsp::LspManager; use ide_workspace::SettingsStore; use ide_workspace::documents::DocumentObserver; @@ -223,9 +223,15 @@ impl KotlinBridge { let staging = directory.clone(); let root = self.root.clone(); let token = cancel.clone(); - let result = - tokio::task::spawn_blocking(move || kotlin_bridge::build(&root, &model, &bundle, &staging, &token)) - .await; + // The build runs Maven over the Kotlin modules, which can take a minute: it shows in + // the status bar and can be stopped there (the next saved change builds again). + let task = BuildTask::start(self.sink.clone(), &cancel); + let progress = task.reporter(); + let result = tokio::task::spawn_blocking(move || { + kotlin_bridge::build(&root, &model, &bundle, &staging, &token, &progress) + }) + .await; + drop(task); if !self.revision.lock().current(generation) || self.lifetime.is_cancelled() { remove_directory(directory).await; continue; @@ -306,6 +312,58 @@ impl KotlinBridge { } } +/// The status bar's background task for one bridge build; ends when dropped. +struct BuildTask { + id: TaskId, + sink: SharedSink, + _cancel: ide_core::task::CancelHandle, +} + +impl BuildTask { + const TITLE: &str = "Compiling Kotlin for Java"; + + fn start(sink: SharedSink, cancel: &CancellationToken) -> Self { + let id = TaskId::next(); + let cancel = cancel.clone(); + let handle = ide_core::task::on_cancel(id, move || cancel.cancel()); + let task = Self { id, sink, _cancel: handle }; + task.emit(None, None, false); + task + } + + fn emit(&self, message: Option, fraction: Option, done: bool) { + self.sink.emit(IdeEvent::Task { + id: self.id, + title: Self::TITLE.into(), + message, + fraction, + done, + cancellable: !done, + }); + } + + /// Forwards Maven's progress to the task. + fn reporter(&self) -> impl Fn(&str, Option) + Send + Sync + 'static { + let (id, sink) = (self.id, self.sink.clone()); + move |message: &str, fraction| { + sink.emit(IdeEvent::Task { + id, + title: Self::TITLE.into(), + message: Some(message.to_owned()), + fraction, + done: false, + cancellable: true, + }) + } + } +} + +impl Drop for BuildTask { + fn drop(&mut self) { + self.emit(None, None, true); + } +} + /// The error text of a failed bridge build, and whether to notify about it. Compiler errors /// in the user's own Kotlin already show in its editor and stay in Problems; only a build /// that failed without one (Maven itself, a missing plugin) is worth a notification. @@ -369,6 +427,41 @@ impl DocumentObserver for KotlinBridge { mod tests { use super::*; + #[test] + fn a_build_shows_as_a_cancellable_background_task() { + let events = Arc::new(Mutex::new(Vec::new())); + let sink: SharedSink = Arc::new({ + let events = events.clone(); + move |e: IdeEvent| events.lock().push(e) + }); + let cancel = CancellationToken::new(); + let task = BuildTask::start(sink, &cancel); + let progress = task.reporter(); + progress("Building app 1.0 [2/3]", Some(0.5)); + assert!(ide_core::task::cancel(task.id), "the status bar can stop it"); + assert!(cancel.is_cancelled()); + drop(task); + let seen: Vec<_> = events + .lock() + .iter() + .map(|e| match e { + IdeEvent::Task { title, message, fraction, done, cancellable, .. } => { + assert_eq!(title, "Compiling Kotlin for Java"); + (message.clone(), *fraction, *done, *cancellable) + } + other => panic!("{other:?}"), + }) + .collect(); + assert_eq!( + seen, + [ + (None, None, false, true), + (Some("Building app 1.0 [2/3]".into()), Some(0.5), false, true), + (None, None, true, false), + ] + ); + } + #[test] fn target_package_directory_does_not_trigger() { let dir = tempfile::tempdir().unwrap();