Skip to content

Commit

Permalink
LS: Restart proc macro server on Scarb.toml change
Browse files Browse the repository at this point in the history
commit-id:2337ffeb
  • Loading branch information
Draggu committed Nov 29, 2024
1 parent 0374a20 commit 3ec8c30
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions crates/cairo-lang-language-server/src/lang/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ impl AnalysisDatabase {
})
}

/// Removes plugin suit from database.
pub fn remove_plugin_suite(&mut self, plugins: PluginSuite) {
self.with_plugins(move |macro_plugins, analyzer_plugins, inline_macro_plugins| {
remove_plugin_suite(plugins, macro_plugins, analyzer_plugins, inline_macro_plugins)
})
}

/// Adds plugin suit to database.
pub fn add_plugin_suite(&mut self, plugins: PluginSuite) {
self.with_plugins(move |macro_plugins, analyzer_plugins, inline_macro_plugins| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ impl ProcMacroClientController {
}
}

pub fn force_restart(&mut self, db: &mut AnalysisDatabase, config: &Config) {
// We have to make sure that snapshots will not report errors from previous client after we
// create new one.
db.cancell_all();

Check warning on line 94 in crates/cairo-lang-language-server/src/lang/proc_macros/controller.rs

View workflow job for this annotation

GitHub Actions / typos

"cancell" should be "cancel".

// Otherwise we can get messages from old client after initialization of new one.
self.channels.clear_all();

if let Some(plugin_suite) = self.plugin_suite.take() {
db.remove_plugin_suite(plugin_suite);
}

self.try_initialize(db, config);
}

/// 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) {
Expand Down Expand Up @@ -272,4 +287,10 @@ impl ProcMacroChannels {

Self { response_sender, response_receiver, error_sender, error_receiver }
}

/// Make all channels empty in a non-blocking manner.
fn clear_all(&self) {
let _ = self.error_receiver.try_iter().for_each(|_| {});
let _ = self.response_receiver.try_iter().for_each(|_| {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ impl SyncNotificationHandler for DidChangeWatchedFiles {
if ["Scarb.toml", "cairo_project.toml"].map(Some).contains(&changed_file_name.to_str())
{
Backend::reload(state, requester)?;

state.proc_macro_controller.force_restart(&mut state.db, &state.config);
}
}

Expand Down

0 comments on commit 3ec8c30

Please sign in to comment.