Update the uri_classes_index, use_map, and namespace_map for a given file URI by parsing its content. Returns `true` when at least one class signature in this file changed (or a class was added/removed), meaning other open files that reference those classes may have stale diagnostics.
(&self, uri: &str, content: &str)
| 33 | /// changed (or a class was added/removed), meaning other open files |
| 34 | /// that reference those classes may have stale diagnostics. |
| 35 | pub fn update_ast(&self, uri: &str, content: &str) -> bool { |
| 36 | // Invalidate thread-local mixin cache so stale ClassInfo is not |
| 37 | // served after a file changes. |
| 38 | crate::virtual_members::phpdoc::bump_mixin_generation(); |
| 39 | |
| 40 | let content_to_parse = if self.is_blade_file(uri) { |
| 41 | let (virtual_php, source_map) = crate::blade::preprocessor::preprocess(content); |
| 42 | self.blade_source_maps |
| 43 | .write() |
| 44 | .insert(uri.to_string(), source_map); |
| 45 | self.blade_virtual_content |
| 46 | .write() |
| 47 | .insert(uri.to_string(), virtual_php.clone()); |
| 48 | virtual_php |
| 49 | } else { |
| 50 | content.to_string() |
| 51 | }; |
| 52 | |
| 53 | // The mago-syntax parser contains `unreachable!()` and `.expect()` |
| 54 | // calls that can panic on malformed PHP (e.g. partially-written |
| 55 | // heredocs/nowdocs, which are common while editing). Wrap the |
| 56 | // entire parse + extraction in `catch_unwind` so a parser panic |
| 57 | // doesn't crash the LSP server and produce a zombie process. |
| 58 | // |
| 59 | // On panic the file is simply skipped — no maps are updated, and |
| 60 | // the user gets stale (but not missing) completions until the |
| 61 | // file is saved in a parseable state. |
| 62 | let content_owned = content_to_parse; |
| 63 | let uri_owned = uri.to_string(); |
| 64 | |
| 65 | let result = crate::util::catch_panic_unwind_safe("parse", uri, None, || { |
| 66 | self.update_ast_inner(&uri_owned, &content_owned) |
| 67 | }); |
| 68 | |
| 69 | match result { |
| 70 | Some(changed) => changed, |
| 71 | None => { |
| 72 | // Parser panicked — store a single "Parse failed" error |
| 73 | // so the syntax-error diagnostic collector can report it. |
| 74 | self.parse_errors.write().insert( |
| 75 | uri.to_string(), |
| 76 | vec![("Parse failed (internal error)".to_string(), 0, 0)], |
| 77 | ); |
| 78 | false |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /// Inner implementation of [`update_ast`] that performs the actual |
| 84 | /// parsing and map updates. Separated so that [`update_ast`] can |