Report progress when archiving flake

This commit is contained in:
oxalica 2023-04-17 11:19:15 +08:00
parent 66dc5e7d94
commit 51da89f1ec
2 changed files with 66 additions and 2 deletions

View file

@ -27,6 +27,7 @@ pub(crate) fn negotiate_capabilities(
// This is required for knowing which action is performed.
.additional_properties_support
),
server_initiated_progress: test!(client_caps.window.work_done_progress),
};
let server_caps = ServerCapabilities {
@ -79,4 +80,5 @@ pub(crate) fn negotiate_capabilities(
#[derive(Clone, Debug, Default)]
pub(crate) struct NegotiatedCapabilities {
pub client_show_message_request: bool,
pub server_initiated_progress: bool,
}

View file

@ -10,8 +10,9 @@ use lsp_types::{
notification as notif, ConfigurationItem, ConfigurationParams, DidChangeConfigurationParams,
DidChangeTextDocumentParams, DidCloseTextDocumentParams, DidOpenTextDocumentParams,
InitializeParams, InitializeResult, InitializedParams, MessageActionItem,
MessageActionItemProperty, MessageType, PublishDiagnosticsParams, ServerInfo,
ShowMessageParams, ShowMessageRequestParams, Url,
MessageActionItemProperty, MessageType, NumberOrString, ProgressParams, ProgressParamsValue,
PublishDiagnosticsParams, ServerInfo, ShowMessageParams, ShowMessageRequestParams, Url,
WorkDoneProgress, WorkDoneProgressBegin, WorkDoneProgressCreateParams, WorkDoneProgressEnd,
};
use nix_interop::nixos_options::{self, NixosOptions};
use nix_interop::{flake_lock, FLAKE_FILE, FLAKE_LOCK_FILE};
@ -29,6 +30,7 @@ use tokio::task::{AbortHandle, JoinHandle};
use tokio::{fs, task};
const LSP_SERVER_NAME: &str = "nil";
const FLAKE_ARCHIVE_PROGRESS_TOKEN: &str = "nil/flakeArchiveProgress";
const NIXOS_OPTIONS_FLAKE_INPUT: &str = "nixpkgs";
@ -331,6 +333,15 @@ impl Server {
};
if do_fetch {
let _progress = Progress::new(
&client,
&caps,
FLAKE_ARCHIVE_PROGRESS_TOKEN,
"Fetching flake with inputs",
"nix flake archive".to_owned(),
)
.await;
let ret = task::spawn_blocking({
let nix_binary = config.nix_binary.clone();
tracing::info!("Archiving flake");
@ -630,6 +641,57 @@ trait ClientExt: BorrowMut<ClientSocket> {
impl ClientExt for ClientSocket {}
struct Progress {
client: ClientSocket,
token: Option<String>,
}
impl Progress {
async fn new(
client: &ClientSocket,
caps: &NegotiatedCapabilities,
token: impl fmt::Display,
title: impl fmt::Display,
message: impl Into<Option<String>>,
) -> Self {
let token = token.to_string();
let created = caps.server_initiated_progress
&& client
.request::<req::WorkDoneProgressCreate>(WorkDoneProgressCreateParams {
token: NumberOrString::String(token.clone()),
})
.await
.is_ok();
let this = Self {
client: client.clone(),
token: created.then_some(token),
};
this.notify(WorkDoneProgress::Begin(WorkDoneProgressBegin {
title: title.to_string(),
cancellable: None,
message: message.into(),
percentage: None,
}));
this
}
fn notify(&self, progress: WorkDoneProgress) {
let Some(token) = &self.token else { return };
let _: Result<_, _> = self.client.notify::<notif::Progress>(ProgressParams {
token: NumberOrString::String(token.clone()),
value: ProgressParamsValue::WorkDone(progress),
});
}
}
impl Drop for Progress {
fn drop(&mut self) {
if self.token.is_some() {
self.notify(WorkDoneProgress::End(WorkDoneProgressEnd { message: None }));
}
}
}
fn with_catch_unwind<T>(ctx: &str, f: impl FnOnce() -> Result<T> + UnwindSafe) -> Result<T> {
static INSTALL_PANIC_HOOK: Once = Once::new();
thread_local! {