|
| 1 | +use std::collections::HashSet; |
| 2 | +use std::sync::atomic::{AtomicBool, AtomicU64, Ordering}; |
| 3 | +use std::sync::{Arc, Mutex}; |
| 4 | + |
| 5 | +use lsp_types::notification::Notification; |
| 6 | + |
| 7 | +use crate::id_generator::IdGenerator; |
| 8 | +use crate::server::client::Notifier; |
| 9 | +use crate::state::Beacon; |
| 10 | + |
| 11 | +/// A facade for `AnalysisProgressController` that allows to track progress of diagnostics |
| 12 | +/// generation and procmacro requests. |
| 13 | +#[derive(Clone)] |
| 14 | +pub struct AnalysisProgressTracker { |
| 15 | + controller: AnalysisProgressController, |
| 16 | +} |
| 17 | + |
| 18 | +impl AnalysisProgressTracker { |
| 19 | + /// Signals that a request to proc macro server was made during the current generation of |
| 20 | + /// diagnostics. |
| 21 | + pub fn register_procmacro_request(&self) { |
| 22 | + self.controller.set_did_submit_procmacro_request(true); |
| 23 | + } |
| 24 | + |
| 25 | + /// Sets handlers for tracking beacons sent to threads. |
| 26 | + /// The beacons are wrapping snapshots, which are signalling when diagnostics finished |
| 27 | + /// calculating for a given snapshot (used for calculating files diagnostics or removing |
| 28 | + /// stale ones) |
| 29 | + pub fn track_analysis<'a>(&self, beacons: impl Iterator<Item = &'a mut Beacon>) { |
| 30 | + let gen_id = self.controller.next_generation_id(); |
| 31 | + |
| 32 | + self.controller.clear_active_snapshots(); |
| 33 | + |
| 34 | + beacons.enumerate().for_each(|(i, beacon)| { |
| 35 | + self.controller.insert_active_snapshot(i); |
| 36 | + |
| 37 | + let controller_ref: AnalysisProgressController = self.controller.clone(); |
| 38 | + beacon.on_signal(move || controller_ref.on_snapshot_deactivate(gen_id, i)); |
| 39 | + }); |
| 40 | + |
| 41 | + self.controller.start_analysis(); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// Controller used to send notifications to the client about analysis progress. |
| 46 | +/// Uses information provided from other controllers (diagnostics controller, procmacro controller) |
| 47 | +/// to assess if diagnostics are in fact calculated. |
| 48 | +#[derive(Debug, Clone)] |
| 49 | +pub struct AnalysisProgressController { |
| 50 | + notifier: Notifier, |
| 51 | + /// ID of the diagnostics "generation" - the scheduled diagnostics jobs set. |
| 52 | + /// Used to filter out stale threads finishing when new ones (from newer "generation") |
| 53 | + /// are already in progress and being tracked by the controller. |
| 54 | + generation_id: Arc<AtomicU64>, |
| 55 | + /// Sequential IDs of state snapshots from the current generation, used to track their status |
| 56 | + /// (present meaning it's still being used) |
| 57 | + active_snapshots: Arc<Mutex<HashSet<usize>>>, |
| 58 | + id_generator: Arc<IdGenerator>, |
| 59 | + /// If `true` - a request to procmacro server was submitted, meaning that analysis will extend |
| 60 | + /// beyond the current generation of diagnostics. |
| 61 | + did_submit_procmacro_request: Arc<AtomicBool>, |
| 62 | + /// Indicates that a notification was sent and analysis (i.e. macro expansion) is taking place. |
| 63 | + analysis_in_progress: Arc<AtomicBool>, |
| 64 | + /// Loaded asynchronously from config - unset if config was not loaded yet. |
| 65 | + /// Has to be set in order for analysis to finish. |
| 66 | + procmacros_enabled: Arc<Mutex<Option<bool>>>, |
| 67 | +} |
| 68 | + |
| 69 | +impl AnalysisProgressController { |
| 70 | + pub fn tracker(&self) -> AnalysisProgressTracker { |
| 71 | + AnalysisProgressTracker { controller: self.clone() } |
| 72 | + } |
| 73 | + |
| 74 | + pub fn new(notifier: Notifier) -> Self { |
| 75 | + let id_generator = Arc::new(IdGenerator::default()); |
| 76 | + Self { |
| 77 | + notifier, |
| 78 | + id_generator: id_generator.clone(), |
| 79 | + active_snapshots: Arc::new(Mutex::new(HashSet::default())), |
| 80 | + did_submit_procmacro_request: Arc::new(AtomicBool::new(false)), |
| 81 | + analysis_in_progress: Arc::new(AtomicBool::new(false)), |
| 82 | + procmacros_enabled: Arc::new(Mutex::new(None)), |
| 83 | + generation_id: Arc::new(AtomicU64::new(id_generator.unique_id())), |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + pub fn set_did_submit_procmacro_request(&self, value: bool) { |
| 88 | + self.did_submit_procmacro_request.store(value, Ordering::SeqCst); |
| 89 | + } |
| 90 | + |
| 91 | + /// Allows to set the procmacro configuration to whatever is in the config, upon loading it. |
| 92 | + pub fn set_procmacros_enabled(&self, value: bool) { |
| 93 | + let mut guard = self.procmacros_enabled.lock().unwrap(); |
| 94 | + *guard = Some(value); |
| 95 | + } |
| 96 | + |
| 97 | + pub fn insert_active_snapshot(&self, snapshot_id: usize) { |
| 98 | + let mut active_snapshots = self.active_snapshots.lock().unwrap(); |
| 99 | + active_snapshots.insert(snapshot_id); |
| 100 | + } |
| 101 | + |
| 102 | + pub fn on_snapshot_deactivate(&self, snapshot_gen_id: u64, snapshot_id: usize) { |
| 103 | + let current_gen = self.get_generation_id(); |
| 104 | + if current_gen == snapshot_gen_id { |
| 105 | + self.remove_active_snapshot(snapshot_id); |
| 106 | + self.try_stop_analysis(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + pub fn next_generation_id(&self) -> u64 { |
| 111 | + let new_gen_id = self.id_generator.unique_id(); |
| 112 | + self.generation_id.store(new_gen_id, Ordering::SeqCst); |
| 113 | + new_gen_id |
| 114 | + } |
| 115 | + |
| 116 | + pub fn get_generation_id(&self) -> u64 { |
| 117 | + self.generation_id.load(Ordering::SeqCst) |
| 118 | + } |
| 119 | + |
| 120 | + pub fn remove_active_snapshot(&self, snapshot_id: usize) { |
| 121 | + let mut active_snapshots = self.active_snapshots.lock().unwrap(); |
| 122 | + active_snapshots.remove(&snapshot_id); |
| 123 | + } |
| 124 | + |
| 125 | + pub fn clear_active_snapshots(&self) { |
| 126 | + let active_snapshots_ref = self.active_snapshots.clone(); |
| 127 | + active_snapshots_ref.lock().unwrap().clear(); |
| 128 | + } |
| 129 | + |
| 130 | + /// Starts a next generation of diagnostics, sends a notification |
| 131 | + fn start_analysis(&self) { |
| 132 | + let analysis_in_progress = self.analysis_in_progress.load(Ordering::SeqCst); |
| 133 | + let config_loaded = self.procmacros_enabled.lock().unwrap().is_some(); |
| 134 | + // We want to clear this flag always when starting a new generation to track the requests |
| 135 | + // properly |
| 136 | + self.did_submit_procmacro_request.store(false, Ordering::SeqCst); |
| 137 | + |
| 138 | + if !analysis_in_progress && config_loaded { |
| 139 | + self.analysis_in_progress.store(true, Ordering::SeqCst); |
| 140 | + self.notifier.notify::<DiagnosticsCalculationStart>(()); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /// Checks a bunch of conditions and if they are fulfilled, sends stop notification |
| 145 | + /// and resets the state back to start of generation defaults. |
| 146 | + fn try_stop_analysis(&self) { |
| 147 | + let did_submit_procmacro_request = self.did_submit_procmacro_request.load(Ordering::SeqCst); |
| 148 | + let snapshots_empty = self.active_snapshots.lock().unwrap().is_empty(); |
| 149 | + let analysis_in_progress = self.analysis_in_progress.load(Ordering::SeqCst); |
| 150 | + let procmacros_enabled = *self.procmacros_enabled.lock().unwrap(); |
| 151 | + |
| 152 | + if snapshots_empty |
| 153 | + && (!did_submit_procmacro_request || (procmacros_enabled == Some(false))) |
| 154 | + && analysis_in_progress |
| 155 | + { |
| 156 | + self.did_submit_procmacro_request.store(false, Ordering::SeqCst); |
| 157 | + self.analysis_in_progress.store(false, Ordering::SeqCst); |
| 158 | + |
| 159 | + self.notifier.notify::<DiagnosticsCalculationFinish>(()); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +/// Notifies about diagnostics generation which is beginning to calculate |
| 165 | +#[derive(Debug)] |
| 166 | +pub struct DiagnosticsCalculationStart; |
| 167 | + |
| 168 | +impl Notification for DiagnosticsCalculationStart { |
| 169 | + type Params = (); |
| 170 | + const METHOD: &'static str = "cairo/diagnosticsCalculationStart"; |
| 171 | +} |
| 172 | + |
| 173 | +/// Notifies about diagnostics generation which ended calculating |
| 174 | +#[derive(Debug)] |
| 175 | +pub struct DiagnosticsCalculationFinish; |
| 176 | + |
| 177 | +impl Notification for DiagnosticsCalculationFinish { |
| 178 | + type Params = (); |
| 179 | + const METHOD: &'static str = "cairo/diagnosticsCalculationFinish"; |
| 180 | +} |
0 commit comments