Skip to main content

php_lsp/backend/
mod.rs

1use std::path::PathBuf;
2use std::sync::Arc;
3
4#[allow(unused_imports)]
5use self::helpers::*;
6
7use arc_swap::ArcSwap;
8
9/// Sent to the client once Phase 3 (reference index build) finishes.
10/// Allows tests and tooling to wait for the codebase fast path to be active.
11enum IndexReadyNotification {}
12impl tower_lsp::lsp_types::notification::Notification for IndexReadyNotification {
13    type Params = ();
14    const METHOD: &'static str = "$/php-lsp/indexReady";
15}
16use tower_lsp::Client;
17use tower_lsp::lsp_types::*;
18
19/// Response for the `$/php-lsp/debugStats` custom request.
20#[derive(Debug, serde::Serialize, serde::Deserialize)]
21pub struct DebugStats {
22    /// Cumulative count of real `ParsedDoc` parses (cache misses).
23    pub parses: u64,
24    /// Times mir's legacy `RefIndex` was locked. The session opts out of
25    /// index maintenance, so this must stay flat across edits and reads.
26    pub ref_index_locks: u64,
27}
28
29use crate::document::ast::ParsedDoc;
30use crate::document::document_store::DocumentStore;
31use crate::document::open_files::OpenFiles;
32use crate::lang::autoload::Psr4Map;
33use crate::lang::config::LspConfig;
34use crate::lang::phpstorm_meta::PhpStormMeta;
35use crate::text::fqn_short_name;
36
37use crate::navigation::references::find_constructor_references;
38
39use crate::analysis::diagnostics::merge_file_diagnostics;
40use crate::document::open_files::compute_open_file_diagnostics;
41
42pub struct Backend {
43    client: Client,
44    docs: Arc<DocumentStore>,
45    /// Open-file state: text, version token, parse diagnostics.
46    /// Files that are only background-indexed (never opened in the editor)
47    /// do not appear here; they live only in `DocumentStore`'s salsa layer.
48    open_files: OpenFiles,
49    root_paths: Arc<ArcSwap<Vec<PathBuf>>>,
50    psr4: Arc<ArcSwap<Psr4Map>>,
51    meta: Arc<ArcSwap<PhpStormMeta>>,
52    config: Arc<ArcSwap<LspConfig>>,
53}
54
55impl Backend {
56    pub fn new(client: Client) -> Self {
57        // No imperative Codebase field anymore — analysis reads the
58        // salsa-memoized `codebase` query, which composes bundled stubs + every
59        // file's StubSlice and returns a fresh `Arc<Codebase>` (or the memoized
60        // one when inputs are unchanged).
61        let docs = Arc::new(DocumentStore::new());
62        let psr4 = docs.psr4_arc();
63        Backend {
64            client,
65            docs,
66            open_files: OpenFiles::new(),
67            root_paths: Arc::new(ArcSwap::from_pointee(Vec::new())),
68            psr4,
69            meta: Arc::new(ArcSwap::from_pointee(PhpStormMeta::default())),
70            config: Arc::new(ArcSwap::from_pointee(LspConfig::default())),
71        }
72    }
73
74    /// `$/php-lsp/debugStats` — internal observability counters, used by the
75    /// stress tests to assert the read path doesn't parse the whole workspace
76    /// and that no edit/read ever touches mir's legacy reference index.
77    pub async fn debug_stats(&self) -> tower_lsp::jsonrpc::Result<DebugStats> {
78        Ok(DebugStats {
79            parses: self.docs.parse_count(),
80            ref_index_locks: self.docs.ref_index_lock_count(),
81        })
82    }
83
84    fn set_open_text(&self, uri: Url, text: String) -> u64 {
85        self.open_files.set_open_text(&self.docs, uri, text)
86    }
87
88    fn close_open_file(&self, uri: &Url) {
89        self.open_files.close(&self.docs, uri);
90    }
91
92    /// Background-index a file from disk, but only if it isn't currently
93    /// open in the editor — the editor's buffer is authoritative while a
94    /// file is open, and we must not overwrite it with disk contents.
95    fn ingest_if_not_open(&self, uri: Url, text: &str) {
96        if !self.open_files.contains(&uri) {
97            self.docs.ingest(uri, text);
98        }
99    }
100
101    /// Variant of [`ingest_if_not_open`] that reuses an already-parsed doc.
102    fn ingest_from_doc_if_not_open(&self, uri: Url, doc: &ParsedDoc) {
103        if !self.open_files.contains(&uri) {
104            self.docs.ingest_from_doc(uri, doc);
105        }
106    }
107
108    fn get_open_text(&self, uri: &Url) -> Option<String> {
109        self.open_files.text(uri)
110    }
111
112    fn set_parse_diagnostics(&self, uri: &Url, diagnostics: Vec<Diagnostic>) {
113        self.open_files.set_parse_diagnostics(uri, diagnostics);
114    }
115
116    fn get_parse_diagnostics(&self, uri: &Url) -> Option<Vec<Diagnostic>> {
117        self.open_files.parse_diagnostics(uri)
118    }
119
120    fn all_open_files_with_diagnostics(&self) -> Vec<(Url, Vec<Diagnostic>, Option<i64>)> {
121        self.open_files.all_with_diagnostics()
122    }
123
124    fn open_urls(&self) -> Vec<Url> {
125        self.open_files.urls()
126    }
127
128    fn get_doc(&self, uri: &Url) -> Option<Arc<ParsedDoc>> {
129        self.open_files.get_doc(&self.docs, uri)
130    }
131
132    fn get_doc_stale(&self, uri: &Url) -> Option<Arc<ParsedDoc>> {
133        self.open_files.get_doc_stale(&self.docs, uri)
134    }
135
136    /// `use Foo as Bar;` map for a single file, read directly from the AST.
137    fn file_imports(&self, uri: &Url) -> std::collections::HashMap<String, String> {
138        self.docs
139            .get_doc_salsa(uri)
140            .map(|doc| crate::references::collect_file_imports(&doc))
141            .unwrap_or_default()
142    }
143
144    /// Reference call sites for a class's `__construct`.
145    ///
146    /// The constructor's call sites are `new OwningClass(...)`, not
147    /// `->__construct()`, so name-only matching would return every class's
148    /// constructor declaration. We search for `new` expressions only, scoped to
149    /// the owning class.
150    ///
151    /// `class_name` is the FQN when the constructor is inside a namespace
152    /// (e.g. `"Shop\\Order"`). The AST walker searches for the *short* name
153    /// (`"Order"`) since that's what appears at call sites, while the FQN is
154    /// used only to scope the search and prevent collisions between two classes
155    /// with the same short name in different namespaces.
156    fn construct_references(
157        &self,
158        uri: &Url,
159        source: &str,
160        position: Position,
161        class_name: &str,
162        include_declaration: bool,
163    ) -> Vec<Location> {
164        let short_name = fqn_short_name(class_name).to_owned();
165        let class_fqn = class_name.contains('\\').then_some(class_name);
166        // Prefilter to files mentioning the short class name before parsing.
167        let candidate_docs = self.docs.candidate_docs_for(&short_name);
168        // `find_constructor_references` walks `new` expressions directly —
169        // bypasses the codebase/salsa index whose `ClassReference` key is too
170        // broad (covers type hints, `instanceof`, `extends`, `implements`).
171        let mut locations = find_constructor_references(&short_name, &candidate_docs, class_fqn);
172        // The cursor is already on the `__construct` name, so derive the span
173        // from the identifier under the cursor rather than re-searching via
174        // str_offset (which finds the first occurrence in the file and would
175        // point at the wrong constructor in files with more than one class).
176        if include_declaration && let Some(range) = crate::text::word_range_at(source, position) {
177            locations.push(Location {
178                uri: uri.clone(),
179                range,
180            });
181        }
182        locations
183    }
184
185    /// Resolve the FQN of the symbol at the cursor so reference lookups can match
186    /// by exact FQN instead of short name (fixes cross-namespace overmatch for
187    /// Function/Class and unrelated-class overmatch for Method via the owning
188    /// FQCN). Returns `None` when the kind doesn't carry an FQN or it can't be
189    /// resolved. For class constants, returns the owning class short name.
190    fn resolve_reference_target_fqn(
191        &self,
192        uri: &Url,
193        doc_opt: Option<&Arc<ParsedDoc>>,
194        word: &str,
195        kind: Option<crate::navigation::references::SymbolKind>,
196        position: Position,
197        constant_owner: Option<String>,
198    ) -> Option<String> {
199        use crate::navigation::references::SymbolKind;
200        let doc = doc_opt?;
201        let imports = self.file_imports(uri);
202        match kind {
203            Some(SymbolKind::Function) | Some(SymbolKind::Class) => {
204                let resolved = crate::navigation::moniker::resolve_fqn(doc, word, &imports);
205                resolved.contains('\\').then_some(resolved)
206            }
207            Some(SymbolKind::Method) => {
208                // Owning FQCN: the class/interface/trait/enum that contains the cursor.
209                let short_owner =
210                    crate::types::type_map::enclosing_class_at(doc.source(), doc, position)?;
211                // `resolve_fqn` walks the doc and applies the namespace prefix if any.
212                Some(crate::navigation::moniker::resolve_fqn(
213                    doc,
214                    &short_owner,
215                    &imports,
216                ))
217            }
218            Some(SymbolKind::Property) => {
219                // Only resolve the owning class when the cursor is on a property
220                // declaration — for access sites (`$obj->prop`) enclosing_class_at
221                // returns the accessing class, not the declaring class, so the session
222                // would be queried with the wrong key. Access sites fall back to the
223                // AST walker which finds all `->prop` occurrences.
224                let stmts = &doc.program().stmts;
225                crate::backend::helpers::cursor_is_on_property_decl(doc.source(), stmts, position)?;
226                let short_owner =
227                    crate::types::type_map::enclosing_class_at(doc.source(), doc, position)?;
228                Some(crate::navigation::moniker::resolve_fqn(
229                    doc,
230                    &short_owner,
231                    &imports,
232                ))
233            }
234            Some(SymbolKind::Constant) => {
235                if constant_owner.is_some() {
236                    // Class constant: the owning class short name as-is.
237                    constant_owner
238                } else {
239                    // Global/namespace constant: compute FQN so cross-namespace
240                    // references like `\Config\DB_HOST` can be found.
241                    let fqn = crate::navigation::moniker::resolve_fqn(doc, word, &imports);
242                    fqn.contains('\\').then_some(fqn)
243                }
244            }
245            _ => None,
246        }
247    }
248
249    /// Resolve the PHP version to use. See `autoload::resolve_php_version_from_roots`
250    /// for the full priority order.
251    fn resolve_php_version(&self, explicit: Option<&str>) -> (String, &'static str) {
252        let roots = self.root_paths.load();
253        crate::lang::autoload::resolve_php_version_from_roots(&roots, explicit)
254    }
255}
256
257/// Refine the cursor's `(word, kind)` for a references request using
258/// declaration-aware heuristics, returning the (possibly rewritten) word, its
259/// symbol kind, and — for class constants — the owning class short name.
260///
261/// Checks, in order: promoted constructor property params (so `$name` in
262/// `__construct(public string $name)` resolves to the `->name` property, not
263/// `$name` variable occurrences), then method / property / constant
264/// declarations, falling back to the character-based `symbol_kind_at` heuristic.
265fn resolve_reference_symbol(
266    doc_opt: Option<&Arc<ParsedDoc>>,
267    source: &str,
268    position: Position,
269    word: String,
270) -> (
271    String,
272    Option<crate::navigation::references::SymbolKind>,
273    Option<String>,
274) {
275    use crate::navigation::references::SymbolKind;
276    let mut constant_owner: Option<String> = None;
277    let (word, kind) = if let Some(doc) = doc_opt
278        && let Some(prop_name) =
279            promoted_property_at_cursor(doc.source(), &doc.program().stmts, position)
280    {
281        (prop_name, Some(SymbolKind::Property))
282    } else if let Some(doc) = doc_opt {
283        let stmts = &doc.program().stmts;
284        if cursor_is_on_method_decl(doc.source(), stmts, position) {
285            (word, Some(SymbolKind::Method))
286        } else if let Some(prop_name) = cursor_is_on_property_decl(doc.source(), stmts, position) {
287            (prop_name, Some(SymbolKind::Property))
288        } else if let Some((const_name, owner)) =
289            cursor_is_on_constant_decl(doc.source(), stmts, position)
290        {
291            constant_owner = owner;
292            (const_name, Some(SymbolKind::Constant))
293        } else {
294            let k = symbol_kind_at(source, position, &word);
295            // For constant access sites (`ClassName::CONST`, `self::CONST`),
296            // extract the owning class so the constant walker is scoped to
297            // the right class rather than falling back to the global-constant
298            // path (which would look for a top-level constant named `CONST`).
299            if matches!(k, Some(SymbolKind::Constant))
300                && let Some(raw) = class_before_double_colon(source, position)
301            {
302                constant_owner = Some(match raw.as_str() {
303                    "self" | "static" => {
304                        crate::types::type_map::enclosing_class_at(doc.source(), doc, position)
305                            .unwrap_or(raw)
306                    }
307                    _ => raw,
308                });
309            }
310            (word, k)
311        }
312    } else {
313        let k = symbol_kind_at(source, position, &word);
314        (word, k)
315    };
316    (word, kind, constant_owner)
317}
318
319/// Extract the class name (or pseudo-keyword) immediately to the left of `::` at
320/// the cursor position. Returns `None` when the cursor is not on an identifier
321/// preceded by `::`.
322///
323/// Used to populate `constant_owner` for constant access sites so that
324/// `Status::ACTIVE` (cursor on `ACTIVE`) scopes the constant walker to `Status`
325/// rather than treating `ACTIVE` as a global constant.
326fn class_before_double_colon(source: &str, position: Position) -> Option<String> {
327    let line = source.lines().nth(position.line as usize)?;
328    let chars: Vec<char> = line.chars().collect();
329    let col = position.character as usize;
330
331    let mut utf16_col = 0usize;
332    let mut char_idx = 0usize;
333    for ch in &chars {
334        if utf16_col >= col {
335            break;
336        }
337        utf16_col += ch.len_utf16();
338        char_idx += 1;
339    }
340
341    let is_word = |c: char| c.is_alphanumeric() || c == '_';
342    while char_idx > 0 && is_word(chars[char_idx - 1]) {
343        char_idx -= 1;
344    }
345
346    if char_idx < 2 || chars[char_idx - 1] != ':' || chars[char_idx - 2] != ':' {
347        return None;
348    }
349
350    let class_end = char_idx - 2;
351    let mut class_start = class_end;
352    while class_start > 0 && (is_word(chars[class_start - 1]) || chars[class_start - 1] == '\\') {
353        class_start -= 1;
354    }
355
356    let name: String = chars[class_start..class_end].iter().collect();
357    if name.is_empty() { None } else { Some(name) }
358}
359
360/// Off-`self` variant of `Backend::compute_dependent_publishes`. Needed
361/// because did_change's blocking republish runs inside a detached
362/// `tokio::spawn` that captures `Arc<Backend>` indirectly via clones of
363/// `docs` / `open_files` rather than `&self`.
364async fn compute_dependent_publishes_owned(
365    docs: Arc<DocumentStore>,
366    open_files: OpenFiles,
367    changed_uri: Url,
368    diag_cfg: crate::lang::config::DiagnosticsConfig,
369) -> Vec<(Url, Vec<Diagnostic>)> {
370    tokio::task::spawn_blocking(move || {
371        // rust-analyzer model: we only ever publish for files the editor has
372        // open, so re-analyze exactly that set and let salsa memoization make
373        // the unaffected ones ~free. No dependent set is computed at all —
374        // the old path rebuilt mir's dependency_graph() on every keystroke,
375        // an O(all-ingested-files) walk whose cost grew with session age.
376        let open_set: Vec<std::sync::Arc<str>> = open_files
377            .urls()
378            .into_iter()
379            .filter(|u| u != &changed_uri)
380            .map(|u| std::sync::Arc::from(u.as_str()))
381            .collect();
382        if open_set.is_empty() {
383            return Vec::new();
384        }
385
386        let php_version = docs.workspace_php_version();
387        let session = docs.analysis_session(php_version);
388        // The user is typing: pause the background scan's write storm so this
389        // sweep's snapshots aren't repeatedly cancelled while indexing runs.
390        let _interactive = docs.interactive_read_guard();
391        // Cancel any older in-flight sweep and take a fresh token: when the
392        // next edit starts its own sweep, this one stops at its next file
393        // boundary instead of blocking typing behind the previous sweep.
394        let cancel = docs.begin_reanalyze();
395        let analyses = session.reanalyze_files_cancellable(&open_set, &cancel);
396        // A newer edit that flipped `cancel` mid-sweep is now authoritative and
397        // will republish; drop this sweep's partial results so they can't land
398        // out of order and leave stale diagnostics as the final state.
399        if cancel.is_cancelled() || analyses.is_empty() {
400            return Vec::new();
401        }
402
403        let dependents: Vec<(Url, mir_analyzer::FileAnalysis)> = analyses
404            .into_iter()
405            .filter_map(|(file, analysis)| {
406                let url = Url::parse(file.as_ref()).ok()?;
407                Some((url, analysis))
408            })
409            .collect();
410        if dependents.is_empty() {
411            return Vec::new();
412        }
413
414        // Workspace-level class issues (circular inheritance, override
415        // violations, abstract-method gaps) aren't in `FileAnalysis` —
416        // pull them in one batched call covering every affected file.
417        let dep_files: Vec<Arc<str>> = dependents
418            .iter()
419            .map(|(u, _)| Arc::from(u.as_str()))
420            .collect();
421        let class_issues = session.class_issues(&dep_files);
422        let mut class_issues_by_file: std::collections::HashMap<Arc<str>, Vec<mir_issues::Issue>> =
423            std::collections::HashMap::new();
424        for issue in class_issues {
425            if issue.suppressed {
426                continue;
427            }
428            let file = issue.location.file.clone();
429            class_issues_by_file.entry(file).or_default().push(issue);
430        }
431
432        let mut out: Vec<(Url, Vec<Diagnostic>)> = Vec::with_capacity(dependents.len());
433        for (url, analysis) in dependents {
434            let parse = open_files.parse_diagnostics(&url).unwrap_or_default();
435            let mut issues: Vec<mir_issues::Issue> = analysis
436                .issues
437                .into_iter()
438                .filter(|i| !i.suppressed)
439                .collect();
440            if let Some(extra) = class_issues_by_file.remove(&Arc::<str>::from(url.as_str())) {
441                issues.extend(extra);
442            }
443            let semantic =
444                crate::semantic_diagnostics::issues_to_diagnostics(&issues, &url, &diag_cfg);
445            out.push((url, merge_file_diagnostics(parse, semantic)));
446        }
447        out
448    })
449    .await
450    .unwrap_or_default()
451}
452
453/// Content hash of a diagnostics set, for skipping republishes the client
454/// already displays. In-process only — never persisted.
455pub(super) fn diagnostics_content_hash(diagnostics: &[Diagnostic]) -> u64 {
456    use std::collections::hash_map::DefaultHasher;
457    use std::hash::{Hash, Hasher};
458    let mut hasher = DefaultHasher::new();
459    serde_json::to_string(diagnostics)
460        .unwrap_or_default()
461        .hash(&mut hasher);
462    hasher.finish()
463}
464
465/// Compute and publish diagnostics for `uri`, then republish any open files
466/// that depend on it. Requires `open_files.set_parse_diagnostics` to be up to
467/// date for `uri` before this is called.
468///
469/// The edited file always publishes (editors rely on one publish per change).
470/// Dependents publish only when their content differs from the last publish —
471/// the sweep re-analyzes every open file on every edit, and unchanged results
472/// would otherwise flood the client with no-op notifications.
473pub(super) async fn publish_with_dependents(
474    client: Client,
475    docs: Arc<DocumentStore>,
476    open_files: OpenFiles,
477    uri: Url,
478    diag_cfg: crate::lang::config::DiagnosticsConfig,
479) {
480    let docs_ref = Arc::clone(&docs);
481    let open_files_ref = open_files.clone();
482    let uri_ref = uri.clone();
483    let diag_cfg_ref = diag_cfg.clone();
484    let all_diags = tokio::task::spawn_blocking(move || {
485        compute_open_file_diagnostics(&docs_ref, &open_files_ref, &uri_ref, &diag_cfg_ref)
486    })
487    .await
488    .unwrap_or_default();
489    open_files.note_published(&uri, diagnostics_content_hash(&all_diags));
490    client
491        .publish_diagnostics(uri.clone(), all_diags, None)
492        .await;
493    let dependents =
494        compute_dependent_publishes_owned(docs, open_files.clone(), uri, diag_cfg).await;
495    for (dep_uri, dep_diags) in dependents {
496        let hash = diagnostics_content_hash(&dep_diags);
497        if open_files.published_hash(&dep_uri) == Some(hash) {
498            continue;
499        }
500        open_files.note_published(&dep_uri, hash);
501        client.publish_diagnostics(dep_uri, dep_diags, None).await;
502    }
503}
504
505/// Generate a stable result_id for diagnostics. Uses the count and position of diagnostics
506/// to create a stable identifier. Same diagnostics = same result_id.
507fn compute_diagnostic_result_id(diagnostics: &[Diagnostic], uri: &str) -> String {
508    use std::collections::hash_map::DefaultHasher;
509    use std::hash::{Hash, Hasher};
510
511    let mut hasher = DefaultHasher::new();
512    uri.hash(&mut hasher);
513    diagnostics.len().hash(&mut hasher);
514
515    for diag in diagnostics {
516        diag.range.start.line.hash(&mut hasher);
517        diag.range.start.character.hash(&mut hasher);
518        diag.range.end.line.hash(&mut hasher);
519        diag.range.end.character.hash(&mut hasher);
520        diag.message.hash(&mut hasher);
521        let severity_val = match diag.severity {
522            Some(tower_lsp::lsp_types::DiagnosticSeverity::ERROR) => 1,
523            Some(tower_lsp::lsp_types::DiagnosticSeverity::WARNING) => 2,
524            Some(tower_lsp::lsp_types::DiagnosticSeverity::INFORMATION) => 3,
525            Some(tower_lsp::lsp_types::DiagnosticSeverity::HINT) => 4,
526            None => 0,
527            _ => 5, // Unknown variants
528        };
529        severity_val.hash(&mut hasher);
530        if let Some(code) = &diag.code {
531            format!("{:?}", code).hash(&mut hasher);
532        }
533        if let Some(source) = &diag.source {
534            source.hash(&mut hasher);
535        }
536        if let Some(tags) = &diag.tags {
537            for tag in tags {
538                let tag_val = match *tag {
539                    tower_lsp::lsp_types::DiagnosticTag::UNNECESSARY => 1,
540                    tower_lsp::lsp_types::DiagnosticTag::DEPRECATED => 2,
541                    _ => 3,
542                };
543                tag_val.hash(&mut hasher);
544            }
545        }
546    }
547
548    format!("v1:{:x}", hasher.finish())
549}
550
551mod handlers;
552mod helpers;
553pub mod panic_guard;
554mod server;
555#[cfg(test)]
556mod tests;