Skip to content

Commit

Permalink
Migrate to ShowMessage where possible (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotmag769 authored Dec 4, 2024
1 parent 3e3f0ce commit fece532
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 62 deletions.
48 changes: 38 additions & 10 deletions src/lang/proc_macros/controller.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::{Display, Formatter};
use std::num::NonZeroU32;
use std::sync::Arc;
use std::time::Duration;
Expand All @@ -8,6 +9,8 @@ use crossbeam::channel::{Receiver, Sender};
use governor::clock::QuantaClock;
use governor::state::{InMemoryState, NotKeyed};
use governor::{Quota, RateLimiter};
use lsp_types::notification::ShowMessage;
use lsp_types::{MessageType, ShowMessageParams};
use scarb_proc_macro_server_types::jsonrpc::RpcResponse;
use scarb_proc_macro_server_types::methods::ProcMacroResult;
use tracing::error;
Expand All @@ -19,9 +22,6 @@ use crate::config::Config;
use crate::lang::db::AnalysisDatabase;
use crate::lang::proc_macros::db::ProcMacroGroup;
use crate::lang::proc_macros::plugins::proc_macro_plugin_suite;
use crate::lsp::ext::{
ProcMacroServerInitializationFailed, ProcMacroServerInitializationFailedParams,
};
use crate::server::client::Notifier;
use crate::toolchain::scarb::ScarbToolchain;

Expand Down Expand Up @@ -110,10 +110,7 @@ impl ProcMacroClientController {
/// Check if an error was reported. If so, try to restart.
pub fn handle_error(&mut self, db: &mut AnalysisDatabase, config: &Config) {
if !self.try_initialize(db, config) {
self.fatal_failed(db, ProcMacroServerInitializationFailedParams::NoMoreRetries {
retries: RATE_LIMITER_RETRIES,
in_minutes: RATE_LIMITER_PERIOD_SEC / 60,
});
self.fatal_failed(db, InitializationFailedInfo::NoMoreRetries);
}
}

Expand Down Expand Up @@ -182,19 +179,22 @@ impl ProcMacroClientController {
Err(err) => {
error!("spawning proc-macro-server failed: {err:?}");

self.fatal_failed(db, ProcMacroServerInitializationFailedParams::SpawnFail);
self.fatal_failed(db, InitializationFailedInfo::SpawnFail);
}
}
}

fn fatal_failed(
&self,
db: &mut AnalysisDatabase,
params: ProcMacroServerInitializationFailedParams,
initialization_failed_info: InitializationFailedInfo,
) {
db.set_proc_macro_client_status(ClientStatus::Crashed);

self.notifier.notify::<ProcMacroServerInitializationFailed>(params);
self.notifier.notify::<ShowMessage>(ShowMessageParams {
typ: MessageType::ERROR,
message: initialization_failed_info.to_string(),
});
}

fn apply_responses(
Expand Down Expand Up @@ -298,3 +298,31 @@ impl ProcMacroChannels {
self.response_receiver.try_iter().for_each(|_| {});
}
}

enum InitializationFailedInfo {
NoMoreRetries,
SpawnFail,
}

impl Display for InitializationFailedInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
InitializationFailedInfo::NoMoreRetries => {
write!(
f,
"Starting proc-macro-server failed {RATE_LIMITER_RETRIES} times in {} minutes.",
RATE_LIMITER_PERIOD_SEC / 60
)
}
InitializationFailedInfo::SpawnFail => {
write!(f, "Starting proc-macro-server failed fatally.")
}
}?;

write!(
f,
" The proc-macro-server will not be restarted, procedural macros will not be \
analyzed. See the output for more information"
)
}
}
40 changes: 0 additions & 40 deletions src/lsp/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,46 +51,6 @@ impl Notification for CorelibVersionMismatch {
const METHOD: &'static str = "cairo/corelib-version-mismatch";
}

/// Notifies about scarb metadata failure.
#[derive(Debug)]
pub struct ScarbMetadataFailed;

impl Notification for ScarbMetadataFailed {
type Params = ();
const METHOD: &'static str = "cairo/scarb-metadata-failed";
}

/// Notifies about `proc-macro-server` fatal fail.
#[derive(Debug)]
pub struct ProcMacroServerInitializationFailed;

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", rename_all_fields = "camelCase", tag = "reason")]
pub enum ProcMacroServerInitializationFailedParams {
NoMoreRetries { retries: u32, in_minutes: u64 },
SpawnFail,
}

impl Notification for ProcMacroServerInitializationFailed {
type Params = ProcMacroServerInitializationFailedParams;
const METHOD: &'static str = "cairo/procMacroServerInitializationFailed";
}

/// Notifies about `cairo_project.toml` parsing failure.
#[derive(Debug)]
pub struct ProjectConfigParsingFailed;

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ProjectConfigParsingFailedParams {
pub project_config_path: String,
}

impl Notification for ProjectConfigParsingFailed {
type Params = ProjectConfigParsingFailedParams;
const METHOD: &'static str = "cairo/projectConfigParsingFailed";
}

#[cfg(feature = "testing")]
pub mod testing {
use lsp_types::notification::Notification;
Expand Down
21 changes: 12 additions & 9 deletions src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use cairo_lang_compiler::db::validate_corelib;
use cairo_lang_compiler::project::{setup_project, update_crate_roots_from_project_config};
use cairo_lang_project::ProjectConfig;
use crossbeam::channel::{Receiver, Sender};
use lsp_types::notification::ShowMessage;
use lsp_types::{MessageType, ShowMessageParams};
use tracing::{debug, error, trace, warn};

pub use self::crate_data::Crate;
pub use self::project_manifest_path::*;
use crate::lsp::ext::{
CorelibVersionMismatch, ProjectConfigParsingFailed, ProjectConfigParsingFailedParams,
};
use crate::lsp::ext::CorelibVersionMismatch;
use crate::project::scarb::{extract_crates, get_workspace_members_manifests};
use crate::project::unmanaged_core_crate::try_to_init_unmanaged_core;
use crate::server::client::Notifier;
Expand Down Expand Up @@ -221,12 +221,15 @@ impl ProjectControllerThread {

let maybe_project_config = ProjectConfig::from_file(&config_path)
.inspect_err(|err| {
error!("{err:?}");
self.notifier.notify::<ProjectConfigParsingFailed>(
ProjectConfigParsingFailedParams {
project_config_path: config_path.to_string_lossy().into(),
},
);
let config_path_lossy = config_path.to_string_lossy();
error!("parsing {config_path_lossy} failed: {err:?}");
self.notifier.notify::<ShowMessage>(ShowMessageParams {
typ: MessageType::ERROR,
message: format!(
"Failed to parse: {config_path_lossy}. Project analysis will not \
be available.",
),
});
})
.ok();
ProjectUpdate::CairoProjectToml(maybe_project_config)
Expand Down
11 changes: 8 additions & 3 deletions src/toolchain/scarb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::process::{Child, Command, Stdio};
use std::sync::{Arc, OnceLock};

use anyhow::{Context, Result, bail};
use lsp_types::notification::Notification;
use lsp_types::notification::{Notification, ShowMessage};
use lsp_types::{MessageType, ShowMessageParams};
use scarb_metadata::{Metadata, MetadataCommand};
use tracing::{error, warn};
use which::which;

use crate::env_config;
use crate::lsp::ext::ScarbMetadataFailed;
use crate::server::client::Notifier;

pub const SCARB_TOML: &str = "Scarb.toml";
Expand Down Expand Up @@ -139,7 +139,12 @@ impl ScarbToolchain {
self.notifier.notify::<ScarbResolvingFinish>(());

if result.is_err() {
self.notifier.notify::<ScarbMetadataFailed>(());
self.notifier.notify::<ShowMessage>(ShowMessageParams {
typ: MessageType::ERROR,
message: "`scarb metadata` failed. Check if your project builds correctly via \
`scarb build`."
.to_string(),
});
}
}

Expand Down

0 comments on commit fece532

Please sign in to comment.