Skip to content

Commit

Permalink
Notify about invalid cairo_project.toml (#3)
Browse files Browse the repository at this point in the history
* Asynchronous project update

# Conflicts:
#	src/project/mod.rs

* Notify about invalid project config

� Conflicts:
�	src/lsp/ext.rs
  • Loading branch information
piotmag769 authored Dec 2, 2024
1 parent bb136bb commit ee41ca7
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 9 deletions.
15 changes: 15 additions & 0 deletions src/lsp/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ impl Notification for ProcMacroServerInitializationFailed {
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
27 changes: 21 additions & 6 deletions src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use tracing::{debug, error, trace, warn};

pub use self::crate_data::Crate;
pub use self::project_manifest_path::*;
use crate::lsp::ext::CorelibVersionMismatch;
use crate::lsp::ext::{
CorelibVersionMismatch, ProjectConfigParsingFailed, ProjectConfigParsingFailedParams,
};
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 @@ -41,12 +43,16 @@ impl ProjectController {
///
/// The background thread is responsible for fetching changes to the project model: check
/// [`ProjectControllerThread::send_project_update_for_file`] for more information.
pub fn initialize(scarb_toolchain: ScarbToolchain) -> Self {
pub fn initialize(scarb_toolchain: ScarbToolchain, notifier: Notifier) -> Self {
let (requests_sender, requests_receiver) = crossbeam::channel::unbounded();
let (response_sender, response_receiver) = crossbeam::channel::unbounded();

let thread =
ProjectControllerThread::spawn(requests_receiver, response_sender, scarb_toolchain);
let thread = ProjectControllerThread::spawn(
requests_receiver,
response_sender,
scarb_toolchain,
notifier,
);

ProjectController { requests_sender, response_receiver, _thread: thread }
}
Expand Down Expand Up @@ -132,6 +138,7 @@ struct ProjectControllerThread {
requests_receiver: Receiver<ProjectControllerRequest>,
response_sender: Sender<ProjectUpdate>,
scarb_toolchain: ScarbToolchain,
notifier: Notifier,
}

impl ProjectControllerThread {
Expand All @@ -140,12 +147,14 @@ impl ProjectControllerThread {
requests_receiver: Receiver<ProjectControllerRequest>,
response_sender: Sender<ProjectUpdate>,
scarb_toolchain: ScarbToolchain,
notifier: Notifier,
) -> JoinHandle {
let this = Self {
loaded_scarb_manifests: Default::default(),
requests_receiver,
response_sender,
scarb_toolchain,
notifier,
};

thread::Builder::new(ThreadPriority::Worker)
Expand Down Expand Up @@ -211,8 +220,14 @@ impl ProjectControllerThread {
assert!(config_path.is_absolute());

let maybe_project_config = ProjectConfig::from_file(&config_path)
// TODO: send failure notification
.inspect_err(|err| error!("{err:?}"))
.inspect_err(|err| {
error!("{err:?}");
self.notifier.notify::<ProjectConfigParsingFailed>(
ProjectConfigParsingFailedParams {
project_config_path: config_path.to_string_lossy().into(),
},
);
})
.ok();
ProjectUpdate::CairoProjectToml(maybe_project_config)
}
Expand Down
4 changes: 2 additions & 2 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ impl State {
scarb_toolchain: scarb_toolchain.clone(),
db_swapper: AnalysisDatabaseSwapper::new(),
tricks: Owned::new(tricks.into()),
diagnostics_controller: DiagnosticsController::new(notifier),
diagnostics_controller: DiagnosticsController::new(notifier.clone()),
proc_macro_controller,
project_controller: ProjectController::initialize(scarb_toolchain),
project_controller: ProjectController::initialize(scarb_toolchain, notifier),
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/e2e/support/mock_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use std::time::Duration;
use std::{fmt, mem, process};

use cairo_language_server::build_service_for_e2e_tests;
use cairo_language_server::lsp::ext::testing::ProjectUpdatingFinished;
use lsp_server::{Message, Notification, Request, Response};
use lsp_types::request::{RegisterCapability, Request as LspRequest};
use lsp_types::{lsp_notification, lsp_request};
use serde_json::Value;
use cairo_language_server::lsp::ext::testing::ProjectUpdatingFinished;

use crate::support::fixture::Fixture;
use crate::support::jsonrpc::RequestIdGenerator;

Expand Down

0 comments on commit ee41ca7

Please sign in to comment.