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