Skip to content

Commit c6bbcbe

Browse files
authored
LS: Proc macro disable option in config (#6706)
1 parent 38261e3 commit c6bbcbe

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

crates/cairo-lang-language-server/src/config.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ pub struct Config {
3838
/// The property is set by the user under the `cairo1.traceMacroDiagnostics` key in client
3939
/// configuration.
4040
pub trace_macro_diagnostics: bool,
41+
/// Whether to resolve procedural macros or ignore them.
42+
///
43+
/// The property is set by the user under the `cairo1.enableProcMacros` key in client
44+
/// configuration.
45+
pub enable_proc_macros: bool,
4146
}
4247

4348
impl Config {
@@ -61,6 +66,10 @@ impl Config {
6166
scope_uri: None,
6267
section: Some("cairo1.traceMacroDiagnostics".to_owned()),
6368
},
69+
ConfigurationItem {
70+
scope_uri: None,
71+
section: Some("cairo1.enableProcMacros".to_owned()),
72+
},
6473
];
6574
let expected_len = items.len();
6675

@@ -86,8 +95,12 @@ impl Config {
8695
.map(Into::into);
8796
state.config.trace_macro_diagnostics =
8897
response.pop_front().as_ref().and_then(Value::as_bool).unwrap_or_default();
98+
state.config.enable_proc_macros =
99+
response.pop_front().as_ref().and_then(Value::as_bool).unwrap_or(true);
89100

90101
debug!("reloaded configuration: {:#?}", state.config);
102+
103+
state.proc_macro_controller.on_config_change(&mut state.db, &state.config);
91104
})
92105
};
93106

crates/cairo-lang-language-server/src/lang/proc_macros/controller.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use tracing::error;
1515
use super::client::connection::ProcMacroServerConnection;
1616
use super::client::status::ClientStatus;
1717
use super::client::{ProcMacroClient, RequestParams};
18+
use crate::config::Config;
1819
use crate::lang::db::AnalysisDatabase;
1920
use crate::lang::proc_macros::db::ProcMacroGroup;
2021
use crate::lang::proc_macros::plugins::proc_macro_plugin_suite;
@@ -84,18 +85,18 @@ impl ProcMacroClientController {
8485
}
8586
}
8687

87-
/// Start proc-macro-server.
88+
/// Start proc-macro-server after config reload.
8889
/// Note that this will only try to go from `ClientStatus::Pending` to
89-
/// `ClientStatus::Starting`.
90-
pub fn initialize_once(&mut self, db: &mut AnalysisDatabase) {
90+
/// `ClientStatus::Starting` if config allows this.
91+
pub fn on_config_change(&mut self, db: &mut AnalysisDatabase, config: &Config) {
9192
if db.proc_macro_client_status().is_pending() {
92-
self.try_initialize(db);
93+
self.try_initialize(db, config);
9394
}
9495
}
9596

9697
/// Check if an error was reported. If so, try to restart.
97-
pub fn handle_error(&mut self, db: &mut AnalysisDatabase) {
98-
if !self.try_initialize(db) {
98+
pub fn handle_error(&mut self, db: &mut AnalysisDatabase, config: &Config) {
99+
if !self.try_initialize(db, config) {
99100
self.fatal_failed(db, ProcMacroServerInitializationFailedParams::NoMoreRetries {
100101
retries: RATE_LIMITER_RETRIES,
101102
in_minutes: RATE_LIMITER_PERIOD_SEC / 60,
@@ -104,11 +105,11 @@ impl ProcMacroClientController {
104105
}
105106

106107
/// If the client is ready, apply all available responses.
107-
pub fn on_response(&mut self, db: &mut AnalysisDatabase) {
108+
pub fn on_response(&mut self, db: &mut AnalysisDatabase, config: &Config) {
108109
match db.proc_macro_client_status() {
109110
ClientStatus::Starting(client) => {
110111
let Ok(defined_macros) = client.finish_initialize() else {
111-
self.handle_error(db);
112+
self.handle_error(db, config);
112113

113114
return;
114115
};
@@ -124,17 +125,19 @@ impl ProcMacroClientController {
124125
db.set_proc_macro_client_status(ClientStatus::Ready(client));
125126
}
126127
ClientStatus::Ready(client) => {
127-
self.apply_responses(db, &client);
128+
self.apply_responses(db, config, &client);
128129
}
129130
_ => {}
130131
}
131132
}
132133

133-
/// Tries starting proc-macro-server initialization process.
134+
/// Tries starting proc-macro-server initialization process, if allowed by config.
134135
///
135136
/// Returns value indicating if initialization was attempted.
136-
fn try_initialize(&mut self, db: &mut AnalysisDatabase) -> bool {
137-
let initialize = self.initialization_retries.check().is_ok();
137+
fn try_initialize(&mut self, db: &mut AnalysisDatabase, config: &Config) -> bool {
138+
// Keep the rate limiter check as second condition when config doesn't allow it to make
139+
// sure it is not impacted.
140+
let initialize = config.enable_proc_macros && self.initialization_retries.check().is_ok();
138141

139142
if initialize {
140143
self.spawn_server(db);
@@ -176,7 +179,12 @@ impl ProcMacroClientController {
176179
self.notifier.notify::<ProcMacroServerInitializationFailed>(params);
177180
}
178181

179-
fn apply_responses(&mut self, db: &mut AnalysisDatabase, client: &ProcMacroClient) {
182+
fn apply_responses(
183+
&mut self,
184+
db: &mut AnalysisDatabase,
185+
config: &Config,
186+
client: &ProcMacroClient,
187+
) {
180188
let mut attribute_resolutions = Arc::unwrap_or_clone(db.attribute_macro_resolution());
181189
let mut attribute_resolutions_changed = false;
182190

@@ -218,7 +226,7 @@ impl ProcMacroClientController {
218226
// This must be called AFTER `client.available_responses()` is dropped, otherwise we can
219227
// deadlock.
220228
if error_occurred {
221-
self.handle_error(db);
229+
self.handle_error(db, config);
222230
}
223231

224232
// Set input only if resolution changed, this way we don't recompute queries if there were

crates/cairo-lang-language-server/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,6 @@ impl Backend {
269269
fn run(self) -> Result<JoinHandle<Result<()>>> {
270270
event_loop_thread(move || {
271271
let Self { mut state, connection } = self;
272-
273-
state.proc_macro_controller.initialize_once(&mut state.db);
274272
let proc_macro_channels = state.proc_macro_controller.init_channels();
275273

276274
let mut scheduler = Scheduler::new(&mut state, connection.make_sender());
@@ -380,13 +378,13 @@ impl Backend {
380378
/// Calls [`lang::proc_macros::controller::ProcMacroClientController::handle_error`] to do its
381379
/// work.
382380
fn on_proc_macro_error(state: &mut State, _: Notifier, _: &mut Requester<'_>, _: Responder) {
383-
state.proc_macro_controller.handle_error(&mut state.db);
381+
state.proc_macro_controller.handle_error(&mut state.db, &state.config);
384382
}
385383

386384
/// Calls [`lang::proc_macros::controller::ProcMacroClientController::on_response`] to do its
387385
/// work.
388386
fn on_proc_macro_response(state: &mut State, _: Notifier, _: &mut Requester<'_>, _: Responder) {
389-
state.proc_macro_controller.on_response(&mut state.db);
387+
state.proc_macro_controller.on_response(&mut state.db, &state.config);
390388
}
391389

392390
/// Calls [`lang::db::AnalysisDatabaseSwapper::maybe_swap`] to do its work.

vscode-cairo/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,12 @@
141141
"default": true,
142142
"description": "Show CairoLS information in the status bar.",
143143
"scope": "window"
144+
},
145+
"cairo1.enableProcMacros": {
146+
"type": "boolean",
147+
"default": true,
148+
"description": "Enable support for procedural macros.",
149+
"scope": "window"
144150
}
145151
}
146152
}

0 commit comments

Comments
 (0)