| @@ -21,9 +21,16 @@ |
| 21 | 21 | //! client: the session goes on (issue #223). Parameters that don't parse |
| 22 | 22 | //! are `InvalidParams`. |
| 23 | 23 | //! A manifest that is invalid, or dependencies missing from the git cache, |
| 24 | | −//! are reported as a diagnostic in the manifest (`E0801`). The package is |
| 25 | | −//! not reloaded when its manifest or the files on disk change: restart the |
| 26 | | −//! server. |
| 24 | +//! are reported as a diagnostic in the manifest (`E0801`). When files change |
| 25 | +//! on disk (`workspace/didChangeWatchedFiles`: a manifest, a lockfile, a |
| 26 | +//! module the editor doesn't have open), every package is loaded again, |
| 27 | +//! with the open buffers over it. The VS Code extension asks for those |
| 28 | +//! events; another client must send them for `**/louise.toml`, |
| 29 | +//! `**/louise.lock` and `**/*.lse`. |
| 30 | +//! |
| 31 | +//! Changes that arrived while an analysis ran are applied together, and |
| 32 | +//! analysed once. Diagnostics include `@fip` errors (E0901) once a package |
| 33 | +//! has no other error. |
| 27 | 34 | |
| 28 | 35 | use std::collections::{HashMap, HashSet}; |
| 29 | 36 | use std::error::Error; |
| @@ -37,8 +44,8 @@ use louise_package::Target; |
| 37 | 44 | use louise_span::{FileId, LineCol, SourceDb, Span}; |
| 38 | 45 | use lsp_server::{Connection, Message, Notification}; |
| 39 | 46 | use lsp_types::notification::{ |
| 40 | | − DidChangeTextDocument, DidCloseTextDocument, DidOpenTextDocument, Notification as _, |
| 41 | | − PublishDiagnostics, |
| 47 | + DidChangeTextDocument, DidChangeWatchedFiles, DidCloseTextDocument, DidOpenTextDocument, |
| 48 | + Notification as _, PublishDiagnostics, |
| 42 | 49 | }; |
| 43 | 50 | use lsp_types::request::{ |
| 44 | 51 | CodeActionRequest, Completion, DocumentSymbolRequest, Formatting, GotoDefinition, HoverRequest, |
| @@ -109,7 +116,16 @@ pub fn capabilities() -> ServerCapabilities { |
| 109 | 116 | pub fn serve(connection: &Connection) -> Result<(), BoxError> { |
| 110 | 117 | connection.initialize(serde_json::to_value(capabilities())?)?; |
| 111 | 118 | let mut server = Server::default(); |
| 112 | | − for msg in &connection.receiver { |
| 119 | + // A message read while gathering a burst of notifications. |
| 120 | + let mut pending: Option<Message> = None; |
| 121 | + loop { |
| 122 | + let msg = match pending.take() { |
| 123 | + Some(msg) => msg, |
| 124 | + None => match connection.receiver.recv() { |
| 125 | + Ok(msg) => msg, |
| 126 | + Err(_) => break, |
| 127 | + }, |
| 128 | + }; |
| 113 | 129 | match msg { |
| 114 | 130 | Message::Request(req) => { |
| 115 | 131 | if connection.handle_shutdown(&req)? { |
| @@ -151,12 +167,24 @@ pub fn serve(connection: &Connection) -> Result<(), BoxError> { |
| 151 | 167 | connection.sender.send(resp.into())?; |
| 152 | 168 | } |
| 153 | 169 | Message::Notification(note) => { |
| 170 | + // Those queued behind it too (edits typed while the last |
| 171 | + // analysis ran): applied together, analysed once. |
| 172 | + let mut notes = vec![note]; |
| 173 | + while let Ok(msg) = connection.receiver.try_recv() { |
| 174 | + match msg { |
| 175 | + Message::Notification(note) => notes.push(note), |
| 176 | + msg => { |
| 177 | + pending = Some(msg); |
| 178 | + break; |
| 179 | + } |
| 180 | + } |
| 181 | + } |
| 154 | 182 | let published = catch_unwind(AssertUnwindSafe(|| { |
| 155 | | − if server.handle(note) { |
| 156 | | − Some(server.publish()) |
| 157 | | − } else { |
| 158 | | − None |
| 183 | + let mut changed = false; |
| 184 | + for note in notes { |
| 185 | + changed |= server.handle(note); |
| 159 | 186 | } |
| 187 | + changed.then(|| server.publish()) |
| 160 | 188 | })); |
| 161 | 189 | match published { |
| 162 | 190 | Ok(Some(params)) => { |
| @@ -226,6 +254,9 @@ fn panic_message(panic: &(dyn std::any::Any + Send)) -> String { |
| 226 | 254 | struct Server { |
| 227 | 255 | db: Db, |
| 228 | 256 | loaded_roots: Vec<PathBuf>, |
| 257 | + /// The open buffers' texts, put back over the disk's when the packages |
| 258 | + /// are loaded again. |
| 259 | + buffers: HashMap<PathBuf, String>, |
| 229 | 260 | /// Files that currently have published diagnostics, by path. |
| 230 | 261 | published: HashSet<PathBuf>, |
| 231 | 262 | /// The analysis of the current texts (refreshed on every change). |
| @@ -248,6 +279,8 @@ impl Server { |
| 248 | 279 | let Some(path) = uri_to_path(&p.text_document.uri) else { |
| 249 | 280 | return false; |
| 250 | 281 | }; |
| 282 | + self.buffers |
| 283 | + .insert(path.clone(), p.text_document.text.clone()); |
| 251 | 284 | self.open(&path, p.text_document.text); |
| 252 | 285 | true |
| 253 | 286 | } |
| @@ -264,6 +297,7 @@ impl Server { |
| 264 | 297 | let Some(change) = p.content_changes.into_iter().last() else { |
| 265 | 298 | return false; |
| 266 | 299 | }; |
| 300 | + self.buffers.insert(path.clone(), change.text.clone()); |
| 267 | 301 | match self.file(&path) { |
| 268 | 302 | Some(file) => self.db.set_text(file, change.text), |
| 269 | 303 | None => self.open(&path, change.text), |
| @@ -281,6 +315,7 @@ impl Server { |
| 281 | 315 | let Some(path) = uri_to_path(&p.text_document.uri) else { |
| 282 | 316 | return false; |
| 283 | 317 | }; |
| 318 | + self.buffers.remove(&path); |
| 284 | 319 | let Some(file) = self.file(&path) else { |
| 285 | 320 | return false; |
| 286 | 321 | }; |
| @@ -288,10 +323,43 @@ impl Server { |
| 288 | 323 | .set_text(file, std::fs::read_to_string(&path).unwrap_or_default()); |
| 289 | 324 | true |
| 290 | 325 | } |
| 326 | + // Files changed on disk. An open buffer's own saves change |
| 327 | + // nothing (the buffer is what is analysed); anything else — a |
| 328 | + // manifest, a lockfile, a module created, deleted or edited |
| 329 | + // elsewhere — reloads the packages. |
| 330 | + DidChangeWatchedFiles::METHOD => { |
| 331 | + let Ok(p) = |
| 332 | + serde_json::from_value::<lsp_types::DidChangeWatchedFilesParams>(note.params) |
| 333 | + else { |
| 334 | + return false; |
| 335 | + }; |
| 336 | + let outside = p.changes.iter().any(|c| { |
| 337 | + uri_to_path(&c.uri).is_none_or(|path| !self.buffers.contains_key(&path)) |
| 338 | + }); |
| 339 | + if outside { |
| 340 | + self.reload(); |
| 341 | + } |
| 342 | + outside |
| 343 | + } |
| 291 | 344 | _ => false, |
| 292 | 345 | } |
| 293 | 346 | } |
| 294 | 347 | |
| 348 | + /// Load every package again from disk, then put the open buffers back |
| 349 | + /// over it. The source database only grows, so a fresh one is |
| 350 | + /// simpler than unloading a package from it. |
| 351 | + fn reload(&mut self) { |
| 352 | + self.db = Db::default(); |
| 353 | + self.analysis = None; |
| 354 | + for root in std::mem::take(&mut self.loaded_roots) { |
| 355 | + self.db.load_package(&root); |
| 356 | + self.loaded_roots.push(root); |
| 357 | + } |
| 358 | + for (path, text) in self.buffers.clone() { |
| 359 | + self.open(&path, text); |
| 360 | + } |
| 361 | + } |
| 362 | + |
| 295 | 363 | /// The database's file at `path`, as an editor names it. |
| 296 | 364 | fn file(&self, path: &Path) -> Option<FileId> { |
| 297 | 365 | self.db.file_by_display_path(&path.display().to_string()) |
| @@ -354,7 +422,16 @@ impl Server { |
| 354 | 422 | |
| 355 | 423 | /// Diagnostics for every file that has some, or had some before. |
| 356 | 424 | fn publish(&mut self) -> Vec<PublishDiagnosticsParams> { |
| 357 | | − let analysis = self.db.analyze(); |
| 425 | + let mut analysis = self.db.analyze(); |
| 426 | + // `@fip` (ADR 0040) once the rest is clean, as `louise check` does. |
| 427 | + if !analysis |
| 428 | + .diagnostics |
| 429 | + .iter() |
| 430 | + .any(|d| d.severity == Severity::Error) |
| 431 | + { |
| 432 | + let fip = louise_db::fip_diagnostics(&analysis); |
| 433 | + analysis.diagnostics.extend(fip); |
| 434 | + } |
| 358 | 435 | let sources = self.db.sources(); |
| 359 | 436 | let mut by_file: HashMap<FileId, Vec<lsp_types::Diagnostic>> = HashMap::new(); |
| 360 | 437 | for d in &analysis.diagnostics { |