From 92e0dc18e6f353787e50d3ab71221eed3609a8f2 Mon Sep 17 00:00:00 2001 From: sidux Date: Thu, 27 Aug 2026 00:11:28 +0200 Subject: [PATCH 01/10] feat(php): Add scalable reference CodeLens Use the coarse reference index for conclusive zero counts and cache bounded exact member locations for non-zero lenses. Refresh-capable clients avoid eager resolve storms while older clients retain lazy resolution. --- docs/ARCHITECTURE.md | 4 +- docs/CHANGELOG.md | 1 + src/code_lens.rs | 325 +++++++++++++++++++++- src/lib.rs | 11 +- src/mem_audit.rs | 23 +- src/reference_counts.rs | 475 +++++++++++++++++++++++++++++---- src/reference_index.rs | 27 ++ src/references/members.rs | 7 +- src/server.rs | 44 ++- tests/integration/code_lens.rs | 339 +++++++++++++++++++++++ 10 files changed, 1195 insertions(+), 61 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 5cc37501a..3907b37de 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -63,7 +63,7 @@ src/ ├── composer.rs # composer.json / PSR-4 autoload parsing ├── names.rs # Name resolution (FQN, use-map, namespace) ├── reference_index.rs # Workspace-wide reference index for find-references / rename -├── reference_counts.rs # Background-computed member reference counts for the declaration inlay hints +├── reference_counts.rs # Bounded exact member-reference cache for declaration hints and lenses │ │ # Class & type resolution ├── resolution.rs # Multi-phase class/function lookup across files (find_or_load_class) @@ -883,6 +883,8 @@ Both phases parse files in parallel using `std::thread::scope`. The work is spli Parsed files stay cached in `uri_classes_index`, `symbol_maps`, `file_imports`, and `file_namespaces` after the scan completes. There is no post-scan eviction; keeping the entries means subsequent operations (a second find-references call, go-to-definition on a cross-file symbol) benefit from the work already done. +The workspace reference index remains deliberately coarse: it stores candidate URIs and occurrence counts, not a second copy of every source position. CodeLens can therefore answer a conclusive zero without a semantic scan. Nonzero member references are resolved with the same hierarchy-aware search as Find References and cached as exact locations behind a 50,000-location bound. URI strings are shared inside that cache. Refresh-capable clients receive the lens after the background result is ready; other clients retain lazy `codeLens/resolve` as a compatibility path. + ### Cross-file scanning The `user_file_symbol_maps()` helper snapshots all symbol maps whose URI does not fall under the vendor directory or the internal stub scheme. With `Arc`, the snapshot is a vector of cheap reference-count increments rather than deep clones. Four scanners use this snapshot: diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index e330c52c1..90d336919 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- **Reference CodeLens.** PHP declarations show clickable exact reference counts. Declarations with no indexed uses are answered immediately, while semantic member locations are cached in a bounded background index so opening a large file does not fan out into an expensive resolve request per lens. Clients that support CodeLens refresh receive only ready, fully resolved member lenses. Contributed by @sidux. - **`analyze` takes more than one path.** `phpantom_lsp analyze app/ lib/Helper.php tests/` scans the union of everything named, mixing directories and single files freely, so a pre-commit hook or a CI step can hand it exactly the paths that changed instead of running the whole project or invoking the binary once per path. Overlapping arguments are reported once, and a path that does not exist still stops the run with exit code 2. Naming no path scans the entire project, as before. ### Changed diff --git a/src/code_lens.rs b/src/code_lens.rs index 9061c1d85..440382f96 100644 --- a/src/code_lens.rs +++ b/src/code_lens.rs @@ -1,14 +1,16 @@ //! Code Lens (`textDocument/codeLens`) support. //! -//! Shows override/implement annotations linking to the prototype declaration. +//! Shows reference counts plus override/implement annotations. use tower_lsp::lsp_types::*; use crate::Backend; use crate::atom::Atom; use crate::definition::member::MemberKind; +use crate::reference_index::ReferenceIndexKey; +use crate::symbol_map::SymbolKind; use crate::text_position::offset_to_position; -use crate::types::{ClassInfo, ClassLikeKind, MAX_INHERITANCE_DEPTH}; +use crate::types::{ClassInfo, ClassLikeKind, MAX_INHERITANCE_DEPTH, Visibility}; fn line_indent(content: &str, byte_offset: usize) -> u32 { let line_start = content[..byte_offset] @@ -36,12 +38,12 @@ struct Prototype { impl Backend { /// Handle a `textDocument/codeLens` request. /// - /// Returns a code lens for each method in the file that overrides - /// a parent class method or implements an interface method. + /// Returns reference lenses for PHP declarations and navigation lenses + /// for methods that override or implement an ancestor declaration. pub fn handle_code_lens(&self, uri: &str, content: &str) -> Option> { let classes = { let map = self.symbols.uri_classes_index.read(); - map.get(uri)?.clone() + map.get(uri).cloned().unwrap_or_default() }; let mut lenses = Vec::new(); @@ -49,6 +51,15 @@ impl Backend { for class in &classes { let class_fqn = class.fqn(); + if let Some(lens) = self.build_declaration_reference_lens( + uri, + content, + self.class_declaration_name_offset(uri, class), + &ReferenceIndexKey::class(&class_fqn), + ) { + lenses.push(lens); + } + if let Some(lens) = self.build_covers_lens(class, uri, content) { lenses.push(lens); } @@ -75,6 +86,19 @@ impl Backend { }; let proto = self.find_prototype(class, &class_fqn, &method.name, uri, content); + if !method.name.starts_with("__") + && proto.is_none() + && let Some(lens) = self.build_member_reference_lens( + uri, + content, + method.name_offset, + class_fqn, + method.name, + method.is_static, + ) + { + lenses.push(lens); + } if let Some(proto) = proto { let icon = if proto.is_interface { "◆" } else { "↑" }; let title = format!("{} {}::{}", icon, proto.ancestor_name, method.name); @@ -93,6 +117,64 @@ impl Backend { }); } } + + for property in &class.properties { + if property.name_offset == 0 + || property.is_virtual + || property.visibility == Visibility::Private + { + continue; + } + let member_name = property.name.strip_prefix('$').unwrap_or(&property.name); + if let Some(lens) = self.build_member_reference_lens( + uri, + content, + property.name_offset, + class_fqn, + crate::atom::atom(member_name), + property.is_static, + ) { + lenses.push(lens); + } + } + + for constant in &class.constants { + if constant.name_offset == 0 || constant.visibility == Visibility::Private { + continue; + } + if let Some(lens) = self.build_member_reference_lens( + uri, + content, + constant.name_offset, + class_fqn, + constant.name, + true, + ) { + lenses.push(lens); + } + } + } + + if let Some(symbol_map) = self.symbol_maps.read().get(uri).cloned() { + for span in &symbol_map.spans { + let key = match &span.kind { + SymbolKind::FunctionCall { + name, + is_definition: true, + .. + } => self.function_reference_key(uri, span.start, name), + SymbolKind::ConstantReference { + name, + is_definition: true, + } => ReferenceIndexKey::Constant(self.constant_fqn_at(uri, span.start, name)), + _ => continue, + }; + if let Some(lens) = + self.build_declaration_reference_lens(uri, content, span.start, &key) + { + lenses.push(lens); + } + } } if lenses.is_empty() { @@ -102,6 +184,239 @@ impl Backend { } } + /// Build a declaration reference lens from the candidate index. + /// + /// A zero count is returned fully resolved because semantic filtering can + /// only remove candidates. Non-zero declarations take the LSP's lazy + /// resolve path, which computes exact locations only when the client asks. + fn build_declaration_reference_lens( + &self, + origin_uri: &str, + content: &str, + declaration_offset: u32, + key: &ReferenceIndexKey, + ) -> Option { + if declaration_offset == 0 { + return None; + } + + let candidate_count = self.indexed_reference_count(key)?; + let origin_url = Url::parse(origin_uri).ok()?; + let position = offset_to_position(content, declaration_offset as usize); + let range = Range::new( + Position::new(position.line, 0), + Position::new(position.line, 0), + ); + if candidate_count == 0 { + return Some(CodeLens { + range, + command: Some(Self::reference_lens_command( + origin_url, + position, + Vec::new(), + )), + data: None, + }); + } + + Some(CodeLens { + range, + command: None, + data: Some(serde_json::json!({ + "kind": "phpReferences", + "uri": origin_uri, + "position": position, + })), + }) + } + + fn build_member_reference_lens( + &self, + origin_uri: &str, + content: &str, + declaration_offset: u32, + class_fqn: Atom, + member: Atom, + is_static: bool, + ) -> Option { + if declaration_offset == 0 { + return None; + } + let key = ReferenceIndexKey::Member { + name: member.to_string(), + is_static, + }; + let candidate_count = self.indexed_reference_count(&key)?; + let origin_url = Url::parse(origin_uri).ok()?; + let position = offset_to_position(content, declaration_offset as usize); + let range = Range::new( + Position::new(position.line, 0), + Position::new(position.line, 0), + ); + if candidate_count == 0 { + return Some(CodeLens { + range, + command: Some(Self::reference_lens_command( + origin_url, + position, + Vec::new(), + )), + data: None, + }); + } + + if let Some(locations) = self.member_ref_locations_cached( + origin_uri, + declaration_offset, + class_fqn, + member, + is_static, + ) { + return Some(CodeLens { + range, + command: Some(Self::reference_lens_command( + origin_url, position, locations, + )), + data: None, + }); + } + + // Clients with refresh support can re-pull once the shared background + // worker fills the exact cache. Omitting the cold lens avoids an + // eager resolve burst merely to obtain titles for the viewport. + if self + .supports_code_lens_refresh + .load(std::sync::atomic::Ordering::Acquire) + { + return None; + } + + Some(CodeLens { + range, + command: None, + data: Some(serde_json::json!({ + "kind": "phpMemberReferences", + "uri": origin_uri, + "position": position, + "offset": declaration_offset, + "classFqn": class_fqn.as_str(), + "member": member.as_str(), + "isStatic": is_static, + })), + }) + } + + fn class_declaration_name_offset(&self, uri: &str, class: &ClassInfo) -> u32 { + let maps = self.symbol_maps.read(); + let Some(map) = maps.get(uri) else { + return class.keyword_offset; + }; + map.spans + .iter() + .find(|span| { + matches!( + &span.kind, + SymbolKind::ClassDeclaration { name } if *name == class.name + ) && span.start >= class.decl_start_offset + && span.start <= class.start_offset + }) + .map(|span| span.start) + .unwrap_or(class.keyword_offset) + } + + fn reference_lens_command( + origin_uri: Url, + origin_position: Position, + locations: Vec, + ) -> Command { + let count = locations.len(); + Command { + title: format!( + "{count} {}", + if count == 1 { + "reference" + } else { + "references" + } + ), + command: "editor.action.showReferences".to_string(), + arguments: Some(vec![ + serde_json::json!(origin_uri), + serde_json::json!(origin_position), + serde_json::json!(locations), + ]), + } + } + + pub(crate) fn resolve_code_lens_item(&self, mut lens: CodeLens) -> CodeLens { + if lens.command.is_some() { + return lens; + } + let Some(data) = lens.data.as_ref() else { + return lens; + }; + let Some(kind) = data.get("kind").and_then(serde_json::Value::as_str) else { + return lens; + }; + let Some(uri) = data.get("uri").and_then(serde_json::Value::as_str) else { + return lens; + }; + let Some(position) = data + .get("position") + .cloned() + .and_then(|value| serde_json::from_value::(value).ok()) + else { + return lens; + }; + let locations = match kind { + "phpReferences" => { + let Some(content) = self.get_file_content(uri) else { + return lens; + }; + let Some(locations) = self.find_references(uri, &content, position, false) else { + return lens; + }; + locations + } + "phpMemberReferences" => { + let Some(offset) = data + .get("offset") + .and_then(serde_json::Value::as_u64) + .and_then(|offset| u32::try_from(offset).ok()) + else { + return lens; + }; + let Some(class_fqn) = data.get("classFqn").and_then(serde_json::Value::as_str) + else { + return lens; + }; + let Some(member) = data.get("member").and_then(serde_json::Value::as_str) else { + return lens; + }; + let Some(is_static) = data.get("isStatic").and_then(serde_json::Value::as_bool) + else { + return lens; + }; + self.resolve_member_ref_locations( + uri, + offset, + crate::atom::atom(class_fqn), + crate::atom::atom(member), + is_static, + ) + } + _ => return lens, + }; + let Ok(origin_uri) = Url::parse(uri) else { + return lens; + }; + + lens.command = Some(Self::reference_lens_command( + origin_uri, position, locations, + )); + lens + } + /// Build the "which tests cover this class" lens for a class /// declaration, from the test classes whose PHPUnit coverage metadata /// (`@covers` / `@uses` / `#[CoversClass]` and friends) names it. diff --git a/src/lib.rs b/src/lib.rs index 3c6bbf1a6..aa182a1bf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -879,6 +879,12 @@ pub struct Backend { /// this, editors keep showing tokens computed from the pre-edit /// symbol map until the next unrelated request. pub(crate) supports_semantic_tokens_refresh: Arc, + /// Whether the client supports `workspace/codeLens/refresh`. + /// + /// Exact member-reference locations are computed outside the CodeLens + /// request. Supporting clients re-pull once that bounded cache is warm, + /// avoiding a burst of lazy resolve requests for every declaration. + pub(crate) supports_code_lens_refresh: Arc, /// Whether the client supports `workspace/inlayHint/refresh`. /// /// Set during `initialize` from the client's @@ -887,7 +893,7 @@ pub struct Backend { /// without a refresh the editor keeps the hints it pulled before they /// were ready. pub(crate) supports_inlay_hint_refresh: Arc, - /// Reference counts for member declarations, feeding the inlay hints. + /// Exact member references shared by declaration inlay hints and lenses. pub(crate) member_ref_counts: Arc, /// Set to `true` once `initialized` finishes indexing (PSR-4, /// classmap, stubs, vendor). Background workers and the pull @@ -1147,6 +1153,7 @@ impl Backend { ), supports_show_document: Arc::new(std::sync::atomic::AtomicBool::new(false)), supports_semantic_tokens_refresh: Arc::new(std::sync::atomic::AtomicBool::new(false)), + supports_code_lens_refresh: Arc::new(std::sync::atomic::AtomicBool::new(false)), supports_inlay_hint_refresh: Arc::new(std::sync::atomic::AtomicBool::new(false)), member_ref_counts: reference_counts::new_member_ref_counts(), init_complete: Arc::new(std::sync::atomic::AtomicBool::new(false)), @@ -1254,6 +1261,7 @@ impl Backend { ), supports_show_document: Arc::new(std::sync::atomic::AtomicBool::new(false)), supports_semantic_tokens_refresh: Arc::new(std::sync::atomic::AtomicBool::new(false)), + supports_code_lens_refresh: Arc::new(std::sync::atomic::AtomicBool::new(false)), supports_inlay_hint_refresh: Arc::new(std::sync::atomic::AtomicBool::new(false)), member_ref_counts: reference_counts::new_member_ref_counts(), init_complete: Arc::new(std::sync::atomic::AtomicBool::new(false)), @@ -1893,6 +1901,7 @@ impl Backend { ), supports_show_document: Arc::clone(&self.supports_show_document), supports_semantic_tokens_refresh: Arc::clone(&self.supports_semantic_tokens_refresh), + supports_code_lens_refresh: Arc::clone(&self.supports_code_lens_refresh), supports_inlay_hint_refresh: Arc::clone(&self.supports_inlay_hint_refresh), member_ref_counts: Arc::clone(&self.member_ref_counts), init_complete: Arc::clone(&self.init_complete), diff --git a/src/mem_audit.rs b/src/mem_audit.rs index 92cbec5c0..8b0f133f7 100644 --- a/src/mem_audit.rs +++ b/src/mem_audit.rs @@ -217,6 +217,8 @@ fn variant_name(t: &PhpType) -> &'static str { TypeKind::IndexAccess(..) => "IndexAccess", TypeKind::Literal(_) => "Literal", TypeKind::Raw(_) => "Raw", + TypeKind::Benevolent(_) => "Benevolent", + TypeKind::ListShape(_) => "ListShape", } } @@ -248,7 +250,12 @@ fn ty(t: &PhpType) -> Sz { match t.kind() { TypeKind::Named(_) | TypeKind::StaticType(_) | TypeKind::ThisType(_) => {} - TypeKind::Nullable(b) | TypeKind::Array(b) | TypeKind::KeyOf(b) | TypeKind::ValueOf(b) => { + TypeKind::Nullable(b) + | TypeKind::Array(b) + | TypeKind::KeyOf(b) + | TypeKind::ValueOf(b) + | TypeKind::Benevolent(b) + | TypeKind::ListShape(b) => { z.slot(1); z += ty(b); } @@ -1378,6 +1385,17 @@ pub(crate) fn report(backend: &Backend, runner_content_bytes: usize) { refs.allocs, ); + let (member_names, member_entries, member_locations, member_bytes, member_allocations) = + backend.member_ref_counts.audit_heap(); + eprintln!( + "── member_reference_cache: {} names, {} declarations, {} locations, {:.1} MB ({} allocs)", + member_names, + member_entries, + member_locations, + mb(member_bytes), + member_allocations, + ); + // ── 7. Remaining session stores ───────────────────────────────── let mut open = Sz::default(); { @@ -1627,6 +1645,9 @@ pub(crate) fn report(backend: &Backend, runner_content_bytes: usize) { probe("member_completion_cache", &mut || { backend.member_completion_cache.lock().clear() }); + probe("member_reference_cache", &mut || { + backend.member_ref_counts.clear_cached() + }); probe("auth_user_type_cache", &mut || { backend.auth_user_type_cache.write().clear() }); diff --git a/src/reference_counts.rs b/src/reference_counts.rs index 2cc2d3ed5..4753adf1f 100644 --- a/src/reference_counts.rs +++ b/src/reference_counts.rs @@ -1,18 +1,20 @@ -//! Cached member reference counts for the declaration inlay hints. +//! Cached exact member references for declaration annotations. //! //! A count shown next to a declaration has to mean "references to *this* //! symbol", which is the search Find References runs: resolve the receiver //! of every candidate access and keep the ones whose type is in the //! declaring class' hierarchy. That search is far too slow for the -//! inlay-hint request path (hundreds of milliseconds for one member on a -//! large project), so counts are computed on a background thread and -//! served from this cache. +//! inlay-hint and CodeLens request paths (hundreds of milliseconds for one +//! member on a large project), so exact locations are computed on a background +//! thread and served from this bounded cache. Inlay hints read their count; +//! CodeLens reuses the locations when the user opens the reference list. //! //! A hint is emitted only for a member whose count is already cached, and //! a cached value keeps being served once it goes stale so the annotation -//! does not blink out between edits. The reference index marks entries -//! stale rather than dropping them, and the next inlay-hint request queues -//! them for recomputation. +//! does not blink out between edits. Clickable lenses require fresh locations +//! and are omitted for refresh-capable clients until recomputation finishes. +//! The reference index marks entries stale rather than dropping them, and the +//! next annotation request queues them for recomputation. use std::collections::hash_map::DefaultHasher; use std::collections::{HashMap, HashSet}; @@ -21,6 +23,7 @@ use std::sync::Arc; use std::sync::atomic::{AtomicBool, Ordering}; use parking_lot::{Mutex, RwLock}; +use tower_lsp::lsp_types::{Location, Range, Url}; use crate::Backend; use crate::atom::{Atom, AtomMap}; @@ -31,6 +34,12 @@ use crate::class_lookup::find_class_at_offset; /// whole: keeping it in LRU order would cost more than recomputing. const MAX_CACHED_MEMBERS: usize = 20_000; +/// Keep exact locations only while their aggregate stays small enough for an +/// interactive cache. Counts remain cacheable for unusually popular symbols. +const MAX_CACHED_LOCATIONS: usize = 50_000; +const MAX_LOCATIONS_PER_MEMBER: usize = 5_000; +const MAX_CACHED_URIS: usize = 50_000; + /// A member declaration whose count still has to be computed. #[derive(Clone, PartialEq, Eq, Hash)] struct PendingCount { @@ -42,12 +51,33 @@ struct PendingCount { is_static: bool, } -#[derive(Clone, Copy)] -struct CachedCount { +#[derive(Clone)] +struct CachedReferences { count: u32, + locations: Option>, /// Set when the reference index changed in a way that can affect this /// count. The value is still served; it is only a recompute request. - stale: bool, + count_stale: bool, + /// Set after any source edit, since receiver type resolution can change + /// without changing the indexed member name or candidate count. + locations_stale: bool, +} + +/// A cached LSP location without a separately allocated `Url` string for +/// every occurrence. URI strings are interned per cache below. +#[derive(Clone, PartialEq, Eq)] +struct CompactLocation { + uri: Arc, + range: Range, +} + +impl CompactLocation { + fn to_lsp(&self) -> Option { + Some(Location { + uri: Url::parse(&self.uri).ok()?, + range: self.range, + }) + } } /// Per-member-name counts, keyed by the class that declares the member. @@ -56,11 +86,18 @@ struct CachedCount { /// reference index can invalidate at: a file that gains or loses an access /// to `save` can only change counts of members named `save`. The two /// slots are the instance and static member of that name. -type MemberCounts = AtomMap<[Option; 2]>; +type MemberCounts = AtomMap<[Option; 2]>; + +#[derive(Default)] +struct ReferenceCache { + by_member: AtomMap, + location_count: usize, + uris: HashSet>, +} #[derive(Default)] pub(crate) struct MemberRefCounts { - counts: RwLock>, + counts: RwLock, pending: Mutex>, /// Per-file digest of the inheritance each class declares, so a file /// that starts extending something can be told from one that only @@ -69,6 +106,10 @@ pub(crate) struct MemberRefCounts { /// Set while a background computation runs, so a burst of inlay-hint /// requests schedules one job rather than one each. computing: AtomicBool, + /// Serialises exact searches started by background refreshes and lazy + /// CodeLens resolves. A resolve that races the worker reuses its result + /// instead of launching the same expensive scan twice. + compute_lock: Mutex<()>, } fn slot(is_static: bool) -> usize { @@ -76,39 +117,114 @@ fn slot(is_static: bool) -> usize { } impl MemberRefCounts { - fn get(&self, class_fqn: Atom, member: Atom, is_static: bool) -> Option { - self.counts.read().get(&member)?.get(&class_fqn)?[slot(is_static)] + fn get(&self, class_fqn: Atom, member: Atom, is_static: bool) -> Option { + self.counts.read().by_member.get(&member)?.get(&class_fqn)?[slot(is_static)].clone() } - /// Store a freshly computed count, returning whether it differs from - /// the one the editor was last given. - fn store(&self, class_fqn: Atom, member: Atom, is_static: bool, count: u32) -> bool { - let mut counts = self.counts.write(); - if counts.len() >= MAX_CACHED_MEMBERS { - counts.clear(); + /// Store freshly computed references, returning whether they differ from + /// the result the editor was last given. + fn store( + &self, + class_fqn: Atom, + member: Atom, + is_static: bool, + locations: Vec, + ) -> bool { + let mut cache = self.counts.write(); + let previous = cache + .by_member + .get(&member) + .and_then(|members| members.get(&class_fqn)) + .and_then(|slots| slots[slot(is_static)].clone()); + let previous_location_count = previous + .as_ref() + .and_then(|cached| cached.locations.as_ref()) + .map_or(0, |locations| locations.len()); + let count = locations.len() as u32; + let cache_locations = locations.len() <= MAX_LOCATIONS_PER_MEMBER; + let new_location_count = if cache_locations { locations.len() } else { 0 }; + + if cache.by_member.len() >= MAX_CACHED_MEMBERS + || cache.location_count - previous_location_count + new_location_count + > MAX_CACHED_LOCATIONS + || cache.uris.len() >= MAX_CACHED_URIS + { + cache.by_member.clear(); + cache.location_count = 0; + cache.uris.clear(); + } else { + cache.location_count -= previous_location_count; } - let entry = &mut counts + + let cached_locations = cache_locations.then(|| { + let locations: Vec = locations + .into_iter() + .map(|location| { + let uri = match cache.uris.get(location.uri.as_str()) { + Some(uri) => Arc::clone(uri), + None => { + let uri: Arc = Arc::from(location.uri.as_str()); + cache.uris.insert(Arc::clone(&uri)); + uri + } + }; + CompactLocation { + uri, + range: location.range, + } + }) + .collect(); + Arc::<[CompactLocation]>::from(locations) + }); + + let changed = previous.as_ref().is_none_or(|cached| { + cached.count_stale + || cached.locations_stale + || cached.count != count + || cached.locations.as_deref() != cached_locations.as_deref() + }); + let entry = &mut cache + .by_member .entry(member) .or_default() .entry(class_fqn) .or_default()[slot(is_static)]; - let changed = entry.is_none_or(|cached| cached.count != count); - *entry = Some(CachedCount { + *entry = Some(CachedReferences { count, - stale: false, + locations: cached_locations, + count_stale: false, + locations_stale: false, }); + cache.location_count += new_location_count; changed } /// Mark every count for members of this name as needing recomputation. pub(crate) fn invalidate_member(&self, member: Atom) { - let mut counts = self.counts.write(); - let Some(entries) = counts.get_mut(&member) else { + let mut cache = self.counts.write(); + let Some(entries) = cache.by_member.get_mut(&member) else { return; }; for slots in entries.values_mut() { for cached in slots.iter_mut().flatten() { - cached.stale = true; + cached.count_stale = true; + cached.locations_stale = true; + } + } + } + + /// Mark exact locations stale while preserving cached counts. + /// + /// A source edit can change the resolved receiver class without changing + /// the member name or number of indexed candidates. Counts are invalidated + /// more selectively, but clickable locations must never survive that edit. + pub(crate) fn invalidate_locations_all(&self) { + let mut cache = self.counts.write(); + for entries in cache.by_member.values_mut() { + for slots in entries.values_mut() { + for cached in slots.iter_mut().flatten() { + cached.locations_stale = true; + } } } } @@ -118,11 +234,12 @@ impl MemberRefCounts { /// Used when a class' place in the inheritance graph changes, since /// that moves which accesses belong to which declaration. pub(crate) fn invalidate_all(&self) { - let mut counts = self.counts.write(); - for entries in counts.values_mut() { + let mut cache = self.counts.write(); + for entries in cache.by_member.values_mut() { for slots in entries.values_mut() { for cached in slots.iter_mut().flatten() { - cached.stale = true; + cached.count_stale = true; + cached.locations_stale = true; } } } @@ -136,7 +253,55 @@ impl MemberRefCounts { /// hint has been asked for, and the reference index skips its /// invalidation bookkeeping until then. pub(crate) fn is_empty(&self) -> bool { - self.counts.read().is_empty() + self.counts.read().by_member.is_empty() + } + + #[cfg(feature = "mem-audit")] + pub(crate) fn audit_heap(&self) -> (usize, usize, usize, usize, usize) { + use std::mem::size_of; + + let cache = self.counts.read(); + let mut bytes = + cache.by_member.capacity() * (size_of::() + size_of::() + 1); + let mut allocations = usize::from(cache.by_member.capacity() > 0); + let mut entries = 0usize; + for members in cache.by_member.values() { + bytes += members.capacity() + * (size_of::() + size_of::<[Option; 2]>() + 1); + allocations += usize::from(members.capacity() > 0); + for cached in members.values().flat_map(|slots| slots.iter().flatten()) { + entries += 1; + if let Some(locations) = &cached.locations { + bytes += + size_of::() * 2 + locations.len() * size_of::(); + allocations += 1; + } + } + } + bytes += cache.uris.capacity() * (size_of::>() + 1); + allocations += usize::from(cache.uris.capacity() > 0); + for uri in &cache.uris { + bytes += size_of::() * 2 + uri.len(); + allocations += 1; + } + ( + cache.by_member.len(), + entries, + cache.location_count, + bytes, + allocations, + ) + } + + #[cfg(feature = "mem-audit")] + pub(crate) fn clear_cached(&self) { + let mut cache = self.counts.write(); + cache.by_member.clear(); + cache.location_count = 0; + cache.uris.clear(); + drop(cache); + self.pending.lock().clear(); + self.class_shapes.write().clear(); } } @@ -196,18 +361,101 @@ impl Backend { is_static: bool, ) -> Option { let cached = self.member_ref_counts.get(class_fqn, member, is_static); - if cached.is_none_or(|cached| cached.stale) { - self.member_ref_counts.pending.lock().insert(PendingCount { - uri: Arc::from(uri), - offset, - class_fqn, - member, - is_static, - }); + if cached.as_ref().is_none_or(|cached| cached.count_stale) { + self.queue_member_references(uri, offset, class_fqn, member, is_static); } cached.map(|cached| cached.count) } + /// Fresh exact locations for a member declaration, if already cached. + /// Missing or stale entries are queued for the shared background worker. + pub(crate) fn member_ref_locations_cached( + &self, + uri: &str, + offset: u32, + class_fqn: Atom, + member: Atom, + is_static: bool, + ) -> Option> { + let cached = self.member_ref_counts.get(class_fqn, member, is_static); + if cached + .as_ref() + .is_none_or(|cached| cached.count_stale || cached.locations_stale) + { + self.queue_member_references(uri, offset, class_fqn, member, is_static); + } + cached.and_then(|cached| { + if cached.count_stale || cached.locations_stale { + return None; + } + cached.locations.map(|locations| { + locations + .iter() + .filter_map(CompactLocation::to_lsp) + .collect() + }) + }) + } + + fn queue_member_references( + &self, + uri: &str, + offset: u32, + class_fqn: Atom, + member: Atom, + is_static: bool, + ) { + self.member_ref_counts.pending.lock().insert(PendingCount { + uri: Arc::from(uri), + offset, + class_fqn, + member, + is_static, + }); + } + + /// Exact locations for a lazy CodeLens resolve, reusing a fresh cache hit + /// or computing and storing the declaration once under the shared search + /// lock. + pub(crate) fn resolve_member_ref_locations( + &self, + uri: &str, + offset: u32, + class_fqn: Atom, + member: Atom, + is_static: bool, + ) -> Vec { + if let Some(locations) = + self.member_ref_locations_cached(uri, offset, class_fqn, member, is_static) + { + return locations; + } + + let _compute_guard = self.member_ref_counts.compute_lock.lock(); + if let Some(cached) = self.member_ref_counts.get(class_fqn, member, is_static) + && !cached.count_stale + && !cached.locations_stale + && let Some(locations) = cached.locations + { + return locations + .iter() + .filter_map(CompactLocation::to_lsp) + .collect(); + } + + let locations = self.member_declaration_references(uri, offset, &member, is_static); + self.member_ref_counts + .store(class_fqn, member, is_static, locations.clone()); + self.member_ref_counts.pending.lock().remove(&PendingCount { + uri: Arc::from(uri), + offset, + class_fqn, + member, + is_static, + }); + locations + } + /// Compute every queued member reference count. /// /// Returns `true` when at least one count changed, which is the signal @@ -215,6 +463,7 @@ impl Backend { /// References runs, so the number matches what the user gets when they /// follow it. pub(crate) fn compute_pending_member_ref_counts(&self) -> bool { + let _compute_guard = self.member_ref_counts.compute_lock.lock(); // Taken rather than drained: a request that arrives while this // runs sees the counts it wants still stale and queues them // again, and clearing them at the end keeps that from buying a @@ -242,7 +491,7 @@ impl Backend { if !self.declaration_still_at(item) { continue; } - let count = self.member_declaration_reference_count( + let locations = self.member_declaration_references( &item.uri, item.offset, &item.member, @@ -252,7 +501,7 @@ impl Backend { item.class_fqn, item.member, item.is_static, - count as u32, + locations, ); } @@ -306,10 +555,13 @@ impl Backend { match changed { Some(true) => { - if backend.supports_inlay_hint_refresh.load(Ordering::Acquire) - && let Some(ref client) = backend.client - { - let _ = client.inlay_hint_refresh().await; + if let Some(ref client) = backend.client { + if backend.supports_inlay_hint_refresh.load(Ordering::Acquire) { + let _ = client.inlay_hint_refresh().await; + } + if backend.supports_code_lens_refresh.load(Ordering::Acquire) { + let _ = client.code_lens_refresh().await; + } } } Some(false) => {} @@ -384,6 +636,30 @@ mod tests { }) } + #[test] + fn exact_location_cache_is_bounded_and_interns_uris() { + let cache = MemberRefCounts::default(); + let location = Location { + uri: Url::parse("file:///uses.php").unwrap(), + range: Range::new(Position::new(1, 2), Position::new(1, 6)), + }; + + for index in 0..=MAX_CACHED_LOCATIONS / MAX_LOCATIONS_PER_MEMBER { + cache.store( + crate::atom::atom("Order"), + crate::atom::atom(&format!("member{index}")), + false, + vec![location.clone(); MAX_LOCATIONS_PER_MEMBER], + ); + } + + let state = cache.counts.read(); + assert!(state.location_count <= MAX_CACHED_LOCATIONS); + assert_eq!(state.location_count, MAX_LOCATIONS_PER_MEMBER); + assert_eq!(state.by_member.len(), 1); + assert_eq!(state.uris.len(), 1); + } + const ONE_CALL: &str = r#"save();", "$order->save();\n $order->save();"); parse(&backend, &edited); + assert!( + backend + .member_ref_locations_cached( + URI, + declaration_offset, + crate::atom::atom("Order"), + crate::atom::atom("save"), + false, + ) + .is_none(), + "stale locations must not be served to a clickable lens" + ); + // The count the editor already has keeps being served until the // new one is ready, so the annotation does not blink out. assert_eq!( @@ -418,6 +721,88 @@ function persist(Order $order): void { count_on_line(&hints(&backend, &edited), 2).as_deref(), Some(" 2 references") ); + assert_eq!( + backend + .member_ref_locations_cached( + URI, + declaration_offset, + crate::atom::atom("Order"), + crate::atom::atom("save"), + false, + ) + .expect("edited exact locations should replace the stale cache") + .len(), + 2 + ); + } + + #[test] + fn changing_only_a_receiver_type_invalidates_cached_locations() { + const ORDER_URI: &str = "file:///Order.php"; + const BUYER_URI: &str = "file:///Buyer.php"; + const CONSUMER_URI: &str = "file:///Consumer.php"; + let backend = Backend::new_test(); + let order = "save(); }\n"; + parse_extra(&backend, ORDER_URI, order); + parse_extra(&backend, BUYER_URI, buyer); + parse_extra(&backend, CONSUMER_URI, consumer); + + let declaration_offset = order.find("save").unwrap() as u32; + assert!( + backend + .member_ref_locations_cached( + ORDER_URI, + declaration_offset, + crate::atom::atom("Order"), + crate::atom::atom("save"), + false, + ) + .is_none() + ); + backend.compute_pending_member_ref_counts(); + assert_eq!( + backend + .member_ref_locations_cached( + ORDER_URI, + declaration_offset, + crate::atom::atom("Order"), + crate::atom::atom("save"), + false, + ) + .unwrap() + .len(), + 1 + ); + + let edited = consumer.replace("Order $value", "Buyer $value"); + parse_extra(&backend, CONSUMER_URI, &edited); + assert!( + backend + .member_ref_locations_cached( + ORDER_URI, + declaration_offset, + crate::atom::atom("Order"), + crate::atom::atom("save"), + false, + ) + .is_none(), + "a type-only edit must not leave a clickable lens pointing at stale locations" + ); + backend.compute_pending_member_ref_counts(); + assert!( + backend + .member_ref_locations_cached( + ORDER_URI, + declaration_offset, + crate::atom::atom("Order"), + crate::atom::atom("save"), + false, + ) + .unwrap() + .is_empty() + ); } #[test] diff --git a/src/reference_index.rs b/src/reference_index.rs index 4b7b6d6f9..680d67796 100644 --- a/src/reference_index.rs +++ b/src/reference_index.rs @@ -154,6 +154,9 @@ impl Backend { }; evict_reference_index_uri_locked(&mut index, uri); drop(index); + if track_members { + self.member_ref_counts.invalidate_locations_all(); + } for name in dropped.into_keys() { self.member_ref_counts.invalidate_member(name); } @@ -178,6 +181,26 @@ impl Backend { Some(uris) } + /// Number of indexed reference occurrences for `key`. + /// + /// `None` means the workspace index cannot answer yet. A returned zero + /// is conclusive even for the deliberately coarse member keys: semantic + /// filtering can remove name matches, but it cannot create a reference + /// that the symbol map did not index. + pub(crate) fn indexed_reference_count(&self, key: &ReferenceIndexKey) -> Option { + if self.skip_reference_index || !self.workspace_indexed.load(Ordering::Acquire) { + return None; + } + + Some( + self.reference_index + .read() + .get(key) + .map(|entries| entries.values().map(|&count| count as usize).sum()) + .unwrap_or(0), + ) + } + /// Narrow `uris` to the files that reference one of `keys`. /// /// A file the index does not track at all is kept: the index skips @@ -251,6 +274,10 @@ impl Backend { .filter_map(|(idx, item)| keep[idx].then_some(item)) .collect(); + if track_members && !rebuilt.is_empty() { + self.member_ref_counts.invalidate_locations_all(); + } + // Which member names each file contributed a reference to, so the // counts cached for those members can be marked stale. Only the // members whose contribution actually changed are invalidated: diff --git a/src/references/members.rs b/src/references/members.rs index 4667f0a24..07ec82a2a 100644 --- a/src/references/members.rs +++ b/src/references/members.rs @@ -168,18 +168,18 @@ impl Backend { push_unique_location(locations, &parsed_uri, start, end); } - /// Count the references to a member declaration, scoped to the class + /// Find the references to a member declaration, scoped to the class /// hierarchy that declares it. /// /// This is the same search Find References runs on the declaration, so /// the number matches what the user sees when they follow the hint. - pub(crate) fn member_declaration_reference_count( + pub(crate) fn member_declaration_references( &self, uri: &str, offset: u32, member_name: &str, is_static: bool, - ) -> usize { + ) -> Vec { let mode = ReferenceSearchMode::References; let hierarchy = self.resolve_member_declaration_hierarchy(uri, offset, member_name, is_static, mode); @@ -192,7 +192,6 @@ impl Backend { hierarchy.as_ref(), declaration_scope.as_ref(), ) - .len() } /// Find all references to a member (method, property, or constant) diff --git a/src/server.rs b/src/server.rs index 828d2fdb1..11d145f12 100644 --- a/src/server.rs +++ b/src/server.rs @@ -177,6 +177,16 @@ impl LanguageServer for Backend { self.supports_semantic_tokens_refresh .store(client_supports_semantic_tokens_refresh, Ordering::Release); + let client_supports_code_lens_refresh = params + .capabilities + .workspace + .as_ref() + .and_then(|ws| ws.code_lens.as_ref()) + .and_then(|code_lens| code_lens.refresh_support) + .unwrap_or(false); + self.supports_code_lens_refresh + .store(client_supports_code_lens_refresh, Ordering::Release); + // Reference counts on declarations are computed off the request // path, so the hints an editor holds are the ones from before the // counts landed unless it can be asked to re-pull them. @@ -274,7 +284,7 @@ impl LanguageServer for Backend { workspace_symbol_provider: Some(OneOf::Left(true)), folding_range_provider: Some(FoldingRangeProviderCapability::Simple(true)), code_lens_provider: Some(CodeLensOptions { - resolve_provider: Some(false), + resolve_provider: Some(true), }), selection_range_provider: Some(SelectionRangeProviderCapability::Simple(true)), document_formatting_provider: Some(OneOf::Left(true)), @@ -878,6 +888,12 @@ impl LanguageServer for Backend { { let _ = client.inlay_hint_refresh().await; } + if refresh_backend + .supports_code_lens_refresh + .load(Ordering::Acquire) + { + let _ = client.code_lens_refresh().await; + } } }); } @@ -1471,12 +1487,25 @@ impl LanguageServer for Backend { let uri = params.text_document.uri.to_string(); let backend = self.clone_for_blocking(); let u = uri.clone(); - self.coalesced_whole_file("code_lens", &uri, move || { - backend.handle_with_uri("code_lens", &u, |content| { - backend.handle_code_lens(&u, content) + let lenses = self + .coalesced_whole_file("code_lens", &uri, move || { + backend.handle_with_uri("code_lens", &u, |content| { + backend.handle_code_lens(&u, content) + }) }) + .await; + self.schedule_member_ref_counts(); + lenses + } + + async fn code_lens_resolve(&self, params: CodeLens) -> Result { + let fallback = params.clone(); + let backend = self.clone_for_blocking(); + Ok(run_blocking_cancel_safe("code_lens_resolve", move || { + backend.resolve_code_lens_item(params) }) .await + .unwrap_or(fallback)) } async fn execute_command( @@ -1909,6 +1938,13 @@ impl Backend { { let _ = client.inlay_hint_refresh().await; } + if progress_backend + .supports_code_lens_refresh + .load(Ordering::Acquire) + && let Some(ref client) = progress_backend.client + { + let _ = client.code_lens_refresh().await; + } // With the whole workspace parsed, eagerly resolve every // class so interactive requests hit a warm cache. This diff --git a/tests/integration/code_lens.rs b/tests/integration/code_lens.rs index 651fae2a4..3569c6b31 100644 --- a/tests/integration/code_lens.rs +++ b/tests/integration/code_lens.rs @@ -1,4 +1,5 @@ use crate::common::{create_psr4_workspace, create_test_backend}; +use tower_lsp::LanguageServer; use tower_lsp::lsp_types::*; /// Helper: open a file in the backend and return its code lenses. @@ -15,6 +16,344 @@ fn lens_titles(lenses: &[CodeLens]) -> Vec<&str> { .collect() } +async fn open_doc(backend: &phpantom_lsp::Backend, uri: Url, text: &str) { + backend + .did_open(DidOpenTextDocumentParams { + text_document: TextDocumentItem { + uri, + language_id: "php".to_string(), + version: 1, + text: text.to_string(), + }, + }) + .await; +} + +#[tokio::test] +async fn zero_candidate_reference_lenses_need_no_resolve_requests() { + let content = r#" = lenses + .iter() + .filter(|lens| { + lens.command + .as_ref() + .is_some_and(|command| command.title.ends_with("references")) + }) + .collect(); + + assert_eq!(reference_lenses.len(), 33); + assert!(reference_lenses.iter().all(|lens| { + lens.command + .as_ref() + .is_some_and(|command| command.title == "0 references") + && lens.data.is_none() + })); +} + +#[tokio::test] +async fn member_reference_lens_resolves_only_the_declaring_hierarchy() { + let order = r#"save(); + $order->save(); +} +"#; + let unrelated = r#"save(); + $value->save(); + $value->save(); +} +"#; + let (backend, dir) = create_psr4_workspace( + r#"{ "autoload": { "psr-4": { "App\\": "src/" } } }"#, + &[("src/Order.php", order), ("src/Unrelated.php", unrelated)], + ); + let uri = Url::from_file_path(dir.path().join("src/Order.php")).unwrap(); + open_doc(&backend, uri.clone(), order).await; + + backend + .references(ReferenceParams { + text_document_position: TextDocumentPositionParams { + text_document: TextDocumentIdentifier { uri: uri.clone() }, + position: Position::new(3, 20), + }, + context: ReferenceContext { + include_declaration: false, + }, + work_done_progress_params: WorkDoneProgressParams::default(), + partial_result_params: PartialResultParams::default(), + }) + .await + .unwrap(); + + let lenses = backend + .code_lens(CodeLensParams { + text_document: TextDocumentIdentifier { uri: uri.clone() }, + work_done_progress_params: WorkDoneProgressParams::default(), + partial_result_params: PartialResultParams::default(), + }) + .await + .unwrap() + .expect("expected declaration reference lenses"); + let lens = lenses + .into_iter() + .find(|lens| lens.range.start.line == 3 && lens.command.is_none()) + .expect("expected an unresolved reference lens above Order::save"); + + let resolved = backend + .code_lens_resolve(lens) + .await + .expect("reference lens should resolve"); + assert_eq!( + resolved + .command + .as_ref() + .map(|command| command.title.as_str()), + Some("2 references") + ); + let locations: Vec = serde_json::from_value( + resolved + .command + .as_ref() + .and_then(|command| command.arguments.as_ref()) + .and_then(|arguments| arguments.get(2)) + .cloned() + .expect("expected reference locations"), + ) + .expect("reference targets should be locations"); + assert_eq!(locations.len(), 2); + assert!(locations.iter().all(|location| location.uri == uri)); +} + +#[tokio::test] +async fn refresh_capable_clients_receive_only_warm_member_reference_lenses() { + let content = r#"save(); +} +"#; + let (backend, dir) = create_psr4_workspace( + r#"{ "autoload": { "psr-4": { "App\\": "src/" } } }"#, + &[("src/Order.php", content)], + ); + let initialize = backend + .initialize( + serde_json::from_value(serde_json::json!({ + "capabilities": { + "workspace": { + "codeLens": { "refreshSupport": true } + } + } + })) + .unwrap(), + ) + .await + .unwrap(); + assert!(matches!( + initialize.capabilities.code_lens_provider, + Some(CodeLensOptions { + resolve_provider: Some(true) + }) + )); + + let uri = Url::from_file_path(dir.path().join("src/Order.php")).unwrap(); + open_doc(&backend, uri.clone(), content).await; + backend + .references(ReferenceParams { + text_document_position: TextDocumentPositionParams { + text_document: TextDocumentIdentifier { uri: uri.clone() }, + position: Position::new(3, 20), + }, + context: ReferenceContext { + include_declaration: false, + }, + work_done_progress_params: WorkDoneProgressParams::default(), + partial_result_params: PartialResultParams::default(), + }) + .await + .unwrap(); + + let params = CodeLensParams { + text_document: TextDocumentIdentifier { uri }, + work_done_progress_params: WorkDoneProgressParams::default(), + partial_result_params: PartialResultParams::default(), + }; + let cold = backend + .code_lens(params.clone()) + .await + .unwrap() + .unwrap_or_default(); + assert!( + cold.iter().all(|lens| lens.range.start.line != 3), + "a cold member lens would make the client resolve it eagerly: {cold:?}" + ); + + let warm = tokio::time::timeout(std::time::Duration::from_secs(2), async { + loop { + let lenses = backend + .code_lens(params.clone()) + .await + .unwrap() + .unwrap_or_default(); + if let Some(lens) = lenses.into_iter().find(|lens| { + lens.range.start.line == 3 + && lens + .command + .as_ref() + .is_some_and(|command| command.title == "1 reference") + }) { + break lens; + } + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + } + }) + .await + .expect("background member-reference cache did not warm"); + assert!(warm.data.is_none()); +} + +#[tokio::test] +async fn class_and_function_reference_lenses_resolve_exact_locations() { + let content = r#" Date: Thu, 27 Aug 2026 14:31:39 +0200 Subject: [PATCH 02/10] fix(indexing): Reuse workspace index for annotations Stop reference counts and CodeLens resolves from repeating the full workspace walk for every declaration. Internal annotation requests share the initial index, including callers queued behind it, while an explicit Find References command keeps its single refresh for files created without watcher notifications. --- docs/CHANGELOG.md | 1 + src/code_lens.rs | 4 ++- src/indexing/preload.rs | 63 +++++++++++++++++++++++++++++++++++--- src/lib.rs | 11 ++++--- src/references/dispatch.rs | 24 +++++++++++++++ src/references/mod.rs | 4 +-- src/references/tests.rs | 48 +++++++++++++++++++++++++++-- 7 files changed, 141 insertions(+), 14 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 90d336919..3911e5838 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -54,6 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **`@phpstan-assert` narrows a property of the receiver, not just an argument.** A tag naming a path through `$this` (`@phpstan-assert bool $this->resolved`) was ignored, because the subject was looked for among the call's arguments and such a call often has none. The lazy-initialiser idiom (`if ($this->resolved === null) { $this->resolve(); } return $this->resolved;`) therefore reported the nullable property as the return value. The tag's `$this` now stands for whatever the call was made on, so it narrows through a variable receiver as well. - **A class named `Scalar` or `Numeric` is a class, not a PHPDoc pseudo-type.** `scalar` and `numeric` have no native spelling in PHP, so a project may name a class either of them, and nikic/php-parser does exactly that with `PhpParser\Node\Scalar`. Any capitalised spelling was folded into the pseudo-type instead of being resolved through the file's imports, which left the name unqualified and every check against it failing: passing a `Scalar` to a parameter typed as its own parent was reported as a mismatch, in a native type hint, a `@param`, a `@return`, and a `@implements` type argument alike. The all-lowercase spellings keep their PHPDoc meaning, which is the same rule already applied to `Number`, `Integer`, `Boolean`, `Double`, and `Resource`. - **An `&&` operand that pins a value to one class outranks a later operand that only lists alternatives.** `$bound instanceof GenericType && ($class === GenericType::class || $bound instanceof TemplateType)` read the two operands as peers and answered `GenericType|TemplateType`, so passing the value on to anything expecting a `GenericType` was reported as a mismatch — even though the first operand alone settles the question. The disjunction can only narrow the value further, never widen it past what was already proven. A disjunction still narrows on its own when nothing in the chain pinned the subject down. +- **Reference and CodeLens annotations reuse the completed workspace index.** Resolving many lenses in a large project no longer starts another full filesystem walk for every declaration. Concurrent annotations share the first indexing pass, while an explicit Find References command still refreshes once so files created without an editor notification remain discoverable. Contributed by @sidux. - **Argument checks accept the widenings PHP performs and the types the engine admits it does not know.** Four shapes of correct code were reported as type mismatches: a bounded `int<0, max>` passed to a `float` parameter, even though PHP widens an integer to a float on the way in; a `class-string` passed to `non-empty-string`, even though a string that names a class always has content; an `array-key` passed to `int` or to `string`, which is the key type of an array nobody described rather than a value measured to be two things; and a closure body doing `$a & $b` on untyped parameters, which produces a string from two strings just as readily as an int from two numbers. - **`get_class($x) === Foo::class` narrows the same subjects `instanceof` does.** The identity check only pinned a plain variable, so `get_class($this->held) === Sub::class` and `get_class($items[0]) === Sub::class` left the subject at its declared type and every member read past the check was reported as missing. A property fetch, an array element, and a call result are all narrowed now, in the `$x::class === Foo::class` spelling as well. - **A global function written with a leading backslash is the same function.** `\get_class($x) === Foo::class` and `\is_a($x, Foo::class)` narrowed nothing, and `if (!\class_exists('Vendor\Optional\Config')) { return; }` read as an un-negated guard, so it protected the `return;` instead of everything after it and the guarded class was reported as not found. A class named in such a guard with escaped backslashes (`'Vendor\\Optional\\Config'`) is now matched against the reference it guards, too. diff --git a/src/code_lens.rs b/src/code_lens.rs index 440382f96..235e8a554 100644 --- a/src/code_lens.rs +++ b/src/code_lens.rs @@ -373,7 +373,9 @@ impl Backend { let Some(content) = self.get_file_content(uri) else { return lens; }; - let Some(locations) = self.find_references(uri, &content, position, false) else { + let Some(locations) = + self.find_references_from_workspace_index(uri, &content, position, false) + else { return lens; }; locations diff --git a/src/indexing/preload.rs b/src/indexing/preload.rs index 5b8d422ff..f51939c7e 100644 --- a/src/indexing/preload.rs +++ b/src/indexing/preload.rs @@ -122,6 +122,26 @@ impl Backend { } } + /// Wait for the initial workspace index when necessary, but reuse a + /// completed index without refreshing the filesystem. + /// + /// Internal consumers such as declaration CodeLens and cached reference + /// counts call this once per symbol. Explicit Find References requests use + /// [`ensure_workspace_indexed_for_request`](Self::ensure_workspace_indexed_for_request) + /// once at their entry point so they retain the existing on-demand refresh + /// that discovers files created without a watcher notification. + pub(crate) fn ensure_workspace_index_ready_for_request(&self) { + match self.request_progress.as_deref() { + Some(state) => { + let forward = |percentage: u32, message: String| { + state.set_percentage(percentage.min(100) * 4 / 5, message); + }; + self.ensure_workspace_index_ready_with_progress(Some(&forward)); + } + None => self.ensure_workspace_index_ready_with_progress(None), + } + } + /// Acquire `workspace_index_lock`, mirroring the in-flight index's own /// progress into `progress` while another thread holds it. /// @@ -190,7 +210,41 @@ impl Backend { &self, progress: Option<&(dyn Fn(u32, String) + Sync)>, ) { + self.ensure_workspace_indexed_with_progress_mode(progress, true); + } + + pub(crate) fn ensure_workspace_index_ready_with_progress( + &self, + progress: Option<&(dyn Fn(u32, String) + Sync)>, + ) { + self.ensure_workspace_indexed_with_progress_mode(progress, false); + } + + fn ensure_workspace_indexed_with_progress_mode( + &self, + progress: Option<&(dyn Fn(u32, String) + Sync)>, + refresh_completed: bool, + ) { + // Reference counts and CodeLens resolution can ask for the complete + // index once per declaration. Once the initial pass has published + // every batch, those requests must reuse it instead of walking the + // workspace again. Watched-file notifications keep the completed + // index current after this point. + if !refresh_completed && self.workspace_indexed.load(Ordering::Acquire) { + return; + } + let _workspace_index_guard = self.acquire_workspace_index_lock(progress); + + // Another request may have completed the index while this one was + // waiting for the single-flight lock. + if !refresh_completed && self.workspace_indexed.load(Ordering::Acquire) { + if let Some(progress) = progress { + progress(100, "Workspace index ready".to_string()); + } + return; + } + let start = std::time::Instant::now(); self.report_workspace_index_progress(progress, 1, "Preparing workspace index"); let existing_uris: HashSet = self.symbol_maps.read().keys().cloned().collect(); @@ -221,10 +275,11 @@ impl Backend { // ── Phase 2: workspace directory scan ─────────────────────────── // - // Even after the initial scan, repeat the walk so newly-created PHP - // files that are not open in the editor can still be discovered. - // The existing-URI filter below keeps this cheap by parsing only files - // that are not already in `symbol_maps`. + // The initial pass discovers every PHP and resource file. Watched-file + // notifications apply later changes incrementally. Explicit reference + // requests may still refresh this walk to discover a file created + // without a watcher event; per-symbol internal consumers only wait for + // the initial pass and reuse it. let workspace_root = self.workspace.workspace_root.read().clone(); let phase1_uri_set: HashSet<&str> = phase1_uris.iter().map(|uri| uri.as_str()).collect(); let phase2_work = if let Some(root) = workspace_root.clone() { diff --git a/src/lib.rs b/src/lib.rs index aa182a1bf..f213c29e6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -928,12 +928,13 @@ pub struct Backend { /// symbol map recorded a candidate site ever get an entry. pub(crate) typed_receiver_view_spans_cache: Arc>>, - /// Whether the workspace directory has been fully scanned for PHP files. + /// Whether the workspace directory has been fully scanned for PHP and + /// resource files. /// - /// Set to `true` after the first Phase 2 walk in `ensure_workspace_indexed`. - /// Subsequent calls still re-walk the directory to discover newly created - /// files, but the flag lets us log the difference between initial and - /// refresh scans. + /// Set to `true` after the initial `ensure_workspace_indexed` pass. + /// Per-symbol consumers reuse that index, watched-file notifications + /// update it incrementally, and an explicit reference search may refresh + /// it once to discover filesystem changes the editor did not report. pub(crate) workspace_indexed: Arc, /// Serializes whole-workspace indexing so a foreground request does not /// duplicate the background full-index parse. diff --git a/src/references/dispatch.rs b/src/references/dispatch.rs index 277fedcae..b3252b1a1 100644 --- a/src/references/dispatch.rs +++ b/src/references/dispatch.rs @@ -27,6 +27,29 @@ impl Backend { position: Position, include_declaration: bool, ) -> Option> { + // Refresh once for the user command so files created without a + // watcher event remain discoverable. The per-symbol scanners below + // only wait for/reuse that completed index. + self.ensure_workspace_indexed_for_request(); + self.find_references_inner( + uri, + content, + position, + include_declaration, + ReferenceSearchMode::References, + ) + } + + /// Resolve declaration annotations against the completed index without + /// turning every CodeLens item into another workspace refresh. + pub(crate) fn find_references_from_workspace_index( + &self, + uri: &str, + content: &str, + position: Position, + include_declaration: bool, + ) -> Option> { + self.ensure_workspace_index_ready_for_request(); self.find_references_inner( uri, content, @@ -45,6 +68,7 @@ impl Backend { position: Position, include_declaration: bool, ) -> Option> { + self.ensure_workspace_indexed_for_request(); self.find_references_inner( uri, content, diff --git a/src/references/mod.rs b/src/references/mod.rs index 6c173db94..41ee68dfc 100644 --- a/src/references/mod.rs +++ b/src/references/mod.rs @@ -67,7 +67,7 @@ impl Backend { /// vendor directory or the internal stub scheme. All four cross-file /// reference scanners use this to restrict results to user code. pub(crate) fn user_file_symbol_maps(&self) -> Vec<(String, Arc)> { - self.ensure_workspace_indexed_for_request(); + self.ensure_workspace_index_ready_for_request(); self.user_file_symbol_maps_matching(None) } @@ -85,7 +85,7 @@ impl Backend { &self, keys: &[ReferenceIndexKey], ) -> Vec<(String, Arc)> { - self.ensure_workspace_indexed_for_request(); + self.ensure_workspace_index_ready_for_request(); let candidate_uris = self.reference_candidate_uris_for_keys(keys); self.user_file_symbol_maps_matching(candidate_uris.as_ref()) } diff --git a/src/references/tests.rs b/src/references/tests.rs index 5400087ac..a2e1ed923 100644 --- a/src/references/tests.rs +++ b/src/references/tests.rs @@ -2429,7 +2429,7 @@ fn user_file_symbol_maps_exclude_vendor_and_stubs() { } #[test] -fn workspace_index_progress_covers_known_files_and_refresh_walks() { +fn workspace_index_progress_covers_known_and_discovered_files() { let dir = tempfile::tempdir().expect("temp dir"); let src = dir.path().join("src"); std::fs::create_dir_all(&src).expect("src dir"); @@ -2510,6 +2510,33 @@ fn workspace_index_progress_covers_known_files_and_refresh_walks() { ); } +/// Once the first workspace pass is complete, each reference-count or +/// CodeLens query must reuse it. In particular, a concurrent caller must not +/// queue behind the workspace lock and begin another disk walk. +#[test] +fn completed_workspace_index_is_reused_without_waiting() { + let backend = Backend::new_test(); + backend + .workspace_indexed + .store(true, std::sync::atomic::Ordering::Release); + let indexing = backend.workspace_index_lock.lock(); + + let (done_tx, done_rx) = std::sync::mpsc::channel(); + let waiter = { + let backend = backend.clone_for_blocking(); + std::thread::spawn(move || { + backend.ensure_workspace_index_ready_with_progress(None); + done_tx.send(()).expect("report completion"); + }) + }; + + done_rx + .recv_timeout(std::time::Duration::from_secs(1)) + .expect("a completed index should bypass the in-flight lock"); + drop(indexing); + waiter.join().expect("waiter thread"); +} + #[test] fn request_progress_maps_indexing_into_lower_window() { let dir = tempfile::tempdir().expect("temp dir"); @@ -2559,7 +2586,7 @@ fn blocked_request_reports_in_flight_index_status() { let backend = backend.clone_for_blocking(); let reports = std::sync::Arc::clone(&reports); std::thread::spawn(move || { - backend.ensure_workspace_indexed_with_progress(Some(&|percentage, message| { + backend.ensure_workspace_index_ready_with_progress(Some(&|percentage, message| { reports .lock() .expect("reports lock") @@ -2583,6 +2610,12 @@ fn blocked_request_reports_in_flight_index_status() { "Waiting for workspace index: Parsing workspace files (3/9)" ); + // Stand in for the lock owner publishing the completed index before it + // releases the single-flight guard. + backend + .workspace_indexed + .store(true, std::sync::atomic::Ordering::Release); + *backend.workspace_index_status.lock() = None; drop(indexing); waiter.join().expect("waiter thread"); @@ -2590,6 +2623,17 @@ fn blocked_request_reports_in_flight_index_status() { backend.workspace_index_status.lock().is_none(), "a finished indexing pass clears the shared status" ); + let reports = reports.lock().expect("reports lock"); + assert!( + !reports + .iter() + .any(|(_, message)| message == "Preparing workspace index"), + "the waiting request must reuse the index published by the lock owner" + ); + assert_eq!( + reports.last(), + Some(&(100, "Workspace index ready".to_string())) + ); } #[test] From be8c516a010fff0e6bd0f2ddec744a8568d7f3ed Mon Sep 17 00:00:00 2001 From: sidux Date: Thu, 27 Aug 2026 15:34:49 +0200 Subject: [PATCH 03/10] fix(indexing): Batch member reference counts --- docs/CHANGELOG.md | 1 + src/code_lens.rs | 33 ++++--- src/reference_counts.rs | 88 ++++++++++++++---- src/references/members.rs | 183 +++++++++++++++++++++++++++++++++++--- src/references/mod.rs | 1 + 5 files changed, 267 insertions(+), 39 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 3911e5838..300a9c624 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -54,6 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **`@phpstan-assert` narrows a property of the receiver, not just an argument.** A tag naming a path through `$this` (`@phpstan-assert bool $this->resolved`) was ignored, because the subject was looked for among the call's arguments and such a call often has none. The lazy-initialiser idiom (`if ($this->resolved === null) { $this->resolve(); } return $this->resolved;`) therefore reported the nullable property as the return value. The tag's `$this` now stands for whatever the call was made on, so it narrows through a variable receiver as well. - **A class named `Scalar` or `Numeric` is a class, not a PHPDoc pseudo-type.** `scalar` and `numeric` have no native spelling in PHP, so a project may name a class either of them, and nikic/php-parser does exactly that with `PhpParser\Node\Scalar`. Any capitalised spelling was folded into the pseudo-type instead of being resolved through the file's imports, which left the name unqualified and every check against it failing: passing a `Scalar` to a parameter typed as its own parent was reported as a mismatch, in a native type hint, a `@param`, a `@return`, and a `@implements` type argument alike. The all-lowercase spellings keep their PHPDoc meaning, which is the same rule already applied to `Number`, `Integer`, `Boolean`, `Double`, and `Resource`. - **An `&&` operand that pins a value to one class outranks a later operand that only lists alternatives.** `$bound instanceof GenericType && ($class === GenericType::class || $bound instanceof TemplateType)` read the two operands as peers and answered `GenericType|TemplateType`, so passing the value on to anything expecting a `GenericType` was reported as a mismatch — even though the first operand alone settles the question. The disjunction can only narrow the value further, never widen it past what was already proven. A disjunction still narrows on its own when nothing in the chain pinned the subject down. +- **Reference CodeLens stays responsive in large projects.** Queued member lenses share one semantic pass over their candidate files and one parsed syntax tree per file; clients without lens refresh compute only the lens they resolve. Contributed by @sidux. - **Reference and CodeLens annotations reuse the completed workspace index.** Resolving many lenses in a large project no longer starts another full filesystem walk for every declaration. Concurrent annotations share the first indexing pass, while an explicit Find References command still refreshes once so files created without an editor notification remain discoverable. Contributed by @sidux. - **Argument checks accept the widenings PHP performs and the types the engine admits it does not know.** Four shapes of correct code were reported as type mismatches: a bounded `int<0, max>` passed to a `float` parameter, even though PHP widens an integer to a float on the way in; a `class-string` passed to `non-empty-string`, even though a string that names a class always has content; an `array-key` passed to `int` or to `string`, which is the key type of an array nobody described rather than a value measured to be two things; and a closure body doing `$a & $b` on untyped parameters, which produces a string from two strings just as readily as an int from two numbers. - **`get_class($x) === Foo::class` narrows the same subjects `instanceof` does.** The identity check only pinned a plain variable, so `get_class($this->held) === Sub::class` and `get_class($items[0]) === Sub::class` left the subject at its declared type and every member read past the check was reported as missing. A property fetch, an array element, and a call result are all narrowed now, in the `$x::class === Foo::class` spelling as well. diff --git a/src/code_lens.rs b/src/code_lens.rs index 235e8a554..be9176924 100644 --- a/src/code_lens.rs +++ b/src/code_lens.rs @@ -265,13 +265,27 @@ impl Backend { }); } - if let Some(locations) = self.member_ref_locations_cached( - origin_uri, - declaration_offset, - class_fqn, - member, - is_static, - ) { + let supports_refresh = self + .supports_code_lens_refresh + .load(std::sync::atomic::Ordering::Acquire); + let cached_locations = if supports_refresh { + self.member_ref_locations_cached( + origin_uri, + declaration_offset, + class_fqn, + member, + is_static, + ) + } else { + self.member_ref_locations_ready( + origin_uri, + declaration_offset, + class_fqn, + member, + is_static, + ) + }; + if let Some(locations) = cached_locations { return Some(CodeLens { range, command: Some(Self::reference_lens_command( @@ -284,10 +298,7 @@ impl Backend { // Clients with refresh support can re-pull once the shared background // worker fills the exact cache. Omitting the cold lens avoids an // eager resolve burst merely to obtain titles for the viewport. - if self - .supports_code_lens_refresh - .load(std::sync::atomic::Ordering::Acquire) - { + if supports_refresh { return None; } diff --git a/src/reference_counts.rs b/src/reference_counts.rs index 4753adf1f..722c4e8f0 100644 --- a/src/reference_counts.rs +++ b/src/reference_counts.rs @@ -376,11 +376,40 @@ impl Backend { class_fqn: Atom, member: Atom, is_static: bool, + ) -> Option> { + self.member_ref_locations(uri, offset, class_fqn, member, is_static, true) + } + + /// Fresh exact locations without queuing a background computation. + /// + /// Clients without CodeLens refresh receive a lazy lens and resolve only + /// the entries they display. Avoiding a background queue here prevents + /// that resolve from waiting behind every declaration in the file. + pub(crate) fn member_ref_locations_ready( + &self, + uri: &str, + offset: u32, + class_fqn: Atom, + member: Atom, + is_static: bool, + ) -> Option> { + self.member_ref_locations(uri, offset, class_fqn, member, is_static, false) + } + + fn member_ref_locations( + &self, + uri: &str, + offset: u32, + class_fqn: Atom, + member: Atom, + is_static: bool, + queue_if_missing: bool, ) -> Option> { let cached = self.member_ref_counts.get(class_fqn, member, is_static); - if cached - .as_ref() - .is_none_or(|cached| cached.count_stale || cached.locations_stale) + if queue_if_missing + && cached + .as_ref() + .is_none_or(|cached| cached.count_stale || cached.locations_stale) { self.queue_member_references(uri, offset, class_fqn, member, is_static); } @@ -482,21 +511,26 @@ impl Backend { let _chain_guard = crate::type_engine::resolver::with_chain_resolution_cache(); let _resolver_guard = crate::type_engine::call_resolution::activate_type_engine_caches(); + // A declaration may have moved or gone since the hint was requested. + // Exclude stale offsets before preparing the shared semantic scan so + // they cannot fall back to counting every member of that name. + let valid_pending: Vec<_> = pending + .iter() + .filter(|item| self.declaration_still_at(item)) + .collect(); + let queries: Vec<_> = valid_pending + .iter() + .map(|item| crate::references::MemberDeclarationReferenceQuery { + uri: Arc::clone(&item.uri), + offset: item.offset, + member: item.member, + is_static: item.is_static, + }) + .collect(); + let results = self.member_declaration_references_batch(&queries); + let mut changed = false; - for item in &pending { - // The declaration may have moved or gone since the hint was - // requested, and recomputing against a stale offset would scope - // the search to the wrong class (or to none at all, which falls - // back to counting every member of that name). - if !self.declaration_still_at(item) { - continue; - } - let locations = self.member_declaration_references( - &item.uri, - item.offset, - &item.member, - item.is_static, - ); + for (item, locations) in valid_pending.into_iter().zip(results) { changed |= self.member_ref_counts.store( item.class_fqn, item.member, @@ -669,6 +703,26 @@ function persist(Order $order): void { } "#; + #[test] + fn ready_only_location_lookup_does_not_queue_background_work() { + let backend = Backend::new_test(); + parse(&backend, ONE_CALL); + let declaration_offset = ONE_CALL.find("save").unwrap() as u32; + + assert!( + backend + .member_ref_locations_ready( + URI, + declaration_offset, + crate::atom::atom("Order"), + crate::atom::atom("save"), + false, + ) + .is_none() + ); + assert!(!backend.member_ref_counts.has_pending()); + } + #[test] fn an_edit_that_adds_an_access_recomputes_the_count() { let backend = Backend::new_test(); diff --git a/src/references/members.rs b/src/references/members.rs index 07ec82a2a..51ac35f76 100644 --- a/src/references/members.rs +++ b/src/references/members.rs @@ -13,12 +13,21 @@ use std::collections::HashMap; use tower_lsp::lsp_types::{Location, Range}; +use crate::atom::{Atom, AtomMap}; use crate::class_lookup::find_class_at_offset; use crate::references::push_unique_location; use crate::symbol_map::SymbolKind; use crate::text_position::offset_to_position; use crate::types::ClassInfo; +#[derive(Clone)] +pub(crate) struct MemberDeclarationReferenceQuery { + pub(crate) uri: Arc, + pub(crate) offset: u32, + pub(crate) member: Atom, + pub(crate) is_static: bool, +} + impl Backend { pub(super) fn find_laravel_macro_references( &self, @@ -180,18 +189,170 @@ impl Backend { member_name: &str, is_static: bool, ) -> Vec { - let mode = ReferenceSearchMode::References; - let hierarchy = - self.resolve_member_declaration_hierarchy(uri, offset, member_name, is_static, mode); - let declaration_scope = - self.resolve_member_declaration_scope(uri, offset, member_name, is_static, mode); - self.find_member_references( - member_name, + self.member_declaration_references_batch(&[MemberDeclarationReferenceQuery { + uri: Arc::from(uri), + offset, + member: crate::atom::atom(member_name), is_static, - false, - hierarchy.as_ref(), - declaration_scope.as_ref(), - ) + }]) + .pop() + .unwrap_or_default() + } + + /// Find exact references for several member declarations in one semantic + /// pass over the union of their candidate files. + /// + /// A viewport commonly queues many declarations at once. Resolving each + /// declaration separately reopens the same files and repeats receiver + /// inference for every same-named method in unrelated class hierarchies. + /// This batch resolves each matching access once, then attributes it only + /// to queries whose hierarchy contains the receiver class. + pub(crate) fn member_declaration_references_batch( + &self, + queries: &[MemberDeclarationReferenceQuery], + ) -> Vec> { + struct PreparedQuery { + member: Atom, + is_static: bool, + hierarchy: Option>, + } + + if queries.is_empty() { + return Vec::new(); + } + + let mode = ReferenceSearchMode::References; + let prepared: Vec<_> = queries + .iter() + .map(|query| PreparedQuery { + member: query.member, + is_static: query.is_static, + hierarchy: self.resolve_member_declaration_hierarchy( + &query.uri, + query.offset, + &query.member, + query.is_static, + mode, + ), + }) + .collect(); + + let mut by_member: AtomMap> = AtomMap::default(); + let mut candidate_keys = HashSet::new(); + for (query_index, query) in prepared.iter().enumerate() { + by_member.entry(query.member).or_default().push(query_index); + candidate_keys.extend(member_candidate_keys( + &query.member, + query.is_static, + query.hierarchy.as_ref(), + )); + } + + let candidate_keys: Vec<_> = candidate_keys.into_iter().collect(); + let snapshot = self.user_file_symbol_maps_for_reference_keys(&candidate_keys); + self.begin_request_scan_window(snapshot.len(), "Scanning for member references"); + + let mut locations = vec![Vec::new(); queries.len()]; + for (file_uri, symbol_map) in &snapshot { + self.request_scan_file_done(); + + let mut span_indices = Vec::new(); + for member in by_member.keys() { + span_indices.extend_from_slice(symbol_map.member_access_indices(member)); + } + if span_indices.is_empty() { + continue; + } + span_indices.sort_unstable(); + span_indices.dedup(); + + let Ok(parsed_uri) = Url::parse(file_uri) else { + continue; + }; + let Some(content) = self.reference_file_content_arc(file_uri) else { + continue; + }; + let _parse_cache_guard = crate::parser::with_parse_cache(&content); + let file_ctx = self.file_context(file_uri); + + for span_index in span_indices { + let span = &symbol_map.spans[span_index]; + let SymbolKind::MemberAccess { + member_name, + subject_text, + is_static, + .. + } = &span.kind + else { + continue; + }; + let Some(query_indices) = by_member.get(member_name) else { + continue; + }; + + let needs_receiver = query_indices + .iter() + .any(|&index| prepared[index].hierarchy.is_some()); + let subject_fqns = needs_receiver.then(|| { + self.resolve_subject_to_fqns( + subject_text.as_str(&content), + *is_static, + &file_ctx, + span.start, + &content, + ) + }); + let range = Range::new( + offset_to_position(&content, span.start as usize), + offset_to_position(&content, span.end as usize), + ); + + for &query_index in query_indices { + let query = &prepared[query_index]; + if let Some(hierarchy) = &query.hierarchy { + if !subject_fqns + .as_ref() + .is_some_and(|fqns| fqns.iter().any(|fqn| hierarchy.contains(fqn))) + { + continue; + } + } else if query.is_static != *is_static { + continue; + } + + locations[query_index].push(Location { + uri: parsed_uri.clone(), + range, + }); + } + } + } + + for (query, query_locations) in prepared.iter().zip(&mut locations) { + for location in + self.framework_member_reference_locations(&query.member, query.hierarchy.as_ref()) + { + push_unique_location( + query_locations, + &location.uri, + location.range.start, + location.range.end, + ); + } + for location in + self.framework_property_reference_locations(&query.member, query.hierarchy.as_ref()) + { + push_unique_location( + query_locations, + &location.uri, + location.range.start, + location.range.end, + ); + } + sort_locations_for_references(query_locations); + } + + locations } /// Find all references to a member (method, property, or constant) diff --git a/src/references/mod.rs b/src/references/mod.rs index 41ee68dfc..00c49feec 100644 --- a/src/references/mod.rs +++ b/src/references/mod.rs @@ -36,6 +36,7 @@ mod functions; mod members; mod variables; +pub(crate) use members::MemberDeclarationReferenceQuery; use std::collections::HashSet; use std::path::{Path, PathBuf}; use std::sync::Arc; From 4176e9e2f382e1cdf96be5b50b168c53ace796da Mon Sep 17 00:00:00 2001 From: sidux Date: Thu, 27 Aug 2026 16:04:54 +0200 Subject: [PATCH 04/10] fix(indexing): Reuse forward scopes for member counts --- docs/CHANGELOG.md | 2 +- src/reference_counts.rs | 41 ++++++++++++++++++++++++++ src/references/members.rs | 28 ++++++++++++++++++ src/type_engine/variable/resolution.rs | 15 ++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 300a9c624..f1d0d74fa 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -54,7 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **`@phpstan-assert` narrows a property of the receiver, not just an argument.** A tag naming a path through `$this` (`@phpstan-assert bool $this->resolved`) was ignored, because the subject was looked for among the call's arguments and such a call often has none. The lazy-initialiser idiom (`if ($this->resolved === null) { $this->resolve(); } return $this->resolved;`) therefore reported the nullable property as the return value. The tag's `$this` now stands for whatever the call was made on, so it narrows through a variable receiver as well. - **A class named `Scalar` or `Numeric` is a class, not a PHPDoc pseudo-type.** `scalar` and `numeric` have no native spelling in PHP, so a project may name a class either of them, and nikic/php-parser does exactly that with `PhpParser\Node\Scalar`. Any capitalised spelling was folded into the pseudo-type instead of being resolved through the file's imports, which left the name unqualified and every check against it failing: passing a `Scalar` to a parameter typed as its own parent was reported as a mismatch, in a native type hint, a `@param`, a `@return`, and a `@implements` type argument alike. The all-lowercase spellings keep their PHPDoc meaning, which is the same rule already applied to `Number`, `Integer`, `Boolean`, `Double`, and `Resource`. - **An `&&` operand that pins a value to one class outranks a later operand that only lists alternatives.** `$bound instanceof GenericType && ($class === GenericType::class || $bound instanceof TemplateType)` read the two operands as peers and answered `GenericType|TemplateType`, so passing the value on to anything expecting a `GenericType` was reported as a mismatch — even though the first operand alone settles the question. The disjunction can only narrow the value further, never widen it past what was already proven. A disjunction still narrows on its own when nothing in the chain pinned the subject down. -- **Reference CodeLens stays responsive in large projects.** Queued member lenses share one semantic pass over their candidate files and one parsed syntax tree per file; clients without lens refresh compute only the lens they resolve. Contributed by @sidux. +- **Reference CodeLens stays responsive in large projects.** Queued member lenses share one semantic pass over their candidate files, one parsed syntax tree per file, and one forward-walked variable scope per method body; clients without lens refresh compute only the lens they resolve. Contributed by @sidux. - **Reference and CodeLens annotations reuse the completed workspace index.** Resolving many lenses in a large project no longer starts another full filesystem walk for every declaration. Concurrent annotations share the first indexing pass, while an explicit Find References command still refreshes once so files created without an editor notification remain discoverable. Contributed by @sidux. - **Argument checks accept the widenings PHP performs and the types the engine admits it does not know.** Four shapes of correct code were reported as type mismatches: a bounded `int<0, max>` passed to a `float` parameter, even though PHP widens an integer to a float on the way in; a `class-string` passed to `non-empty-string`, even though a string that names a class always has content; an `array-key` passed to `int` or to `string`, which is the key type of an array nobody described rather than a value measured to be two things; and a closure body doing `$a & $b` on untyped parameters, which produces a string from two strings just as readily as an int from two numbers. - **`get_class($x) === Foo::class` narrows the same subjects `instanceof` does.** The identity check only pinned a plain variable, so `get_class($this->held) === Sub::class` and `get_class($items[0]) === Sub::class` left the subject at its declared type and every member read past the check was reported as missing. A property fetch, an array element, and a call result are all narrowed now, in the `$x::class === Foo::class` spelling as well. diff --git a/src/reference_counts.rs b/src/reference_counts.rs index 722c4e8f0..5c07bf910 100644 --- a/src/reference_counts.rs +++ b/src/reference_counts.rs @@ -703,6 +703,47 @@ function persist(Order $order): void { } "#; + #[test] + fn batch_member_counts_reuse_forward_walked_scope_snapshots() { + const ORDER_URI: &str = "file:///Order.php"; + const CONSUMER_URI: &str = "file:///Consumer.php"; + const ORDER: &str = "save(); + $order->save(); + $order->save(); +} +"#; + + let backend = Backend::new_test(); + parse_extra(&backend, ORDER_URI, ORDER); + parse_extra(&backend, CONSUMER_URI, CONSUMER); + hints_for(&backend, ORDER_URI, ORDER); + + crate::type_engine::variable::resolution::reset_test_scope_cache_hits(); + backend.compute_pending_member_ref_counts(); + + let declaration_offset = ORDER.find("save").unwrap() as u32; + assert_eq!( + backend + .member_ref_locations_cached( + ORDER_URI, + declaration_offset, + crate::atom::atom("Order"), + crate::atom::atom("save"), + false, + ) + .unwrap() + .len(), + 3 + ); + assert!( + crate::type_engine::variable::resolution::test_scope_cache_hits() >= 3, + "each repeated receiver lookup should reuse the one forward-walked file scope" + ); + } + #[test] fn ready_only_location_lookup_does_not_queue_background_work() { let backend = Backend::new_test(); diff --git a/src/references/members.rs b/src/references/members.rs index 51ac35f76..685cedefc 100644 --- a/src/references/members.rs +++ b/src/references/members.rs @@ -275,6 +275,34 @@ impl Backend { let _parse_cache_guard = crate::parser::with_parse_cache(&content); let file_ctx = self.file_context(file_uri); + // Receiver resolution for a bare variable normally walks its + // enclosing body from the start. A batch can contain hundreds of + // accesses from the same file, so build the forward-walked scope + // snapshots once and make every variable lookup below O(log N). + // The guard is per file: offsets from different files must never + // share one thread-local snapshot map. + let _scope_guard = + crate::type_engine::variable::forward_walk::with_diagnostic_scope_cache(); + let class_loader = self.class_loader(&file_ctx); + let function_loader = self.function_loader(&file_ctx); + let constant_loader = self.constant_loader(&file_ctx); + let config_resolver = |key: &str| self.resolve_config_type(key); + let trans_resolver = |key: &str| self.resolve_trans_type(key); + let loaders = crate::type_engine::resolver::Loaders { + function_loader: Some(&function_loader), + constant_loader: Some(&constant_loader), + config_resolver: Some(&config_resolver), + trans_resolver: Some(&trans_resolver), + }; + crate::type_engine::variable::forward_walk::build_diagnostic_scopes( + &content, + &file_ctx.classes, + &class_loader, + Some(self), + loaders, + Some(&self.resolved_class_cache), + ); + for span_index in span_indices { let span = &symbol_map.spans[span_index]; let SymbolKind::MemberAccess { diff --git a/src/type_engine/variable/resolution.rs b/src/type_engine/variable/resolution.rs index 5d4fe27ee..5621c09e3 100644 --- a/src/type_engine/variable/resolution.rs +++ b/src/type_engine/variable/resolution.rs @@ -75,6 +75,19 @@ thread_local! { /// of `(variable, offset)` questions get asked hundreds of times. static VAR_TYPE_MEMO: RefCell>>> = const { RefCell::new(None) }; + + #[cfg(test)] + static TEST_SCOPE_CACHE_HITS: std::cell::Cell = const { std::cell::Cell::new(0) }; +} + +#[cfg(test)] +pub(crate) fn reset_test_scope_cache_hits() { + TEST_SCOPE_CACHE_HITS.with(|count| count.set(0)); +} + +#[cfg(test)] +pub(crate) fn test_scope_cache_hits() -> usize { + TEST_SCOPE_CACHE_HITS.with(std::cell::Cell::get) } /// What identifies one "what is the type of `$var` here?" question: the @@ -309,6 +322,8 @@ pub(crate) fn resolve_variable_types( }; if let Some(types) = super::forward_walk::lookup_diagnostic_scope(&prefixed, cursor_offset) { + #[cfg(test)] + TEST_SCOPE_CACHE_HITS.with(|count| count.set(count.get() + 1)); return types; } // Variable not in the forward-walked scope — fall through to From 57571912bf493a4141ab617d825cf6485eca4251 Mon Sep 17 00:00:00 2001 From: sidux Date: Thu, 27 Aug 2026 16:16:27 +0200 Subject: [PATCH 05/10] fix(indexing): Coalesce member count refreshes --- docs/CHANGELOG.md | 2 +- src/reference_counts.rs | 30 +++++++++++++++++++++--------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index f1d0d74fa..d0621a53d 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -54,7 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **`@phpstan-assert` narrows a property of the receiver, not just an argument.** A tag naming a path through `$this` (`@phpstan-assert bool $this->resolved`) was ignored, because the subject was looked for among the call's arguments and such a call often has none. The lazy-initialiser idiom (`if ($this->resolved === null) { $this->resolve(); } return $this->resolved;`) therefore reported the nullable property as the return value. The tag's `$this` now stands for whatever the call was made on, so it narrows through a variable receiver as well. - **A class named `Scalar` or `Numeric` is a class, not a PHPDoc pseudo-type.** `scalar` and `numeric` have no native spelling in PHP, so a project may name a class either of them, and nikic/php-parser does exactly that with `PhpParser\Node\Scalar`. Any capitalised spelling was folded into the pseudo-type instead of being resolved through the file's imports, which left the name unqualified and every check against it failing: passing a `Scalar` to a parameter typed as its own parent was reported as a mismatch, in a native type hint, a `@param`, a `@return`, and a `@implements` type argument alike. The all-lowercase spellings keep their PHPDoc meaning, which is the same rule already applied to `Number`, `Integer`, `Boolean`, `Double`, and `Resource`. - **An `&&` operand that pins a value to one class outranks a later operand that only lists alternatives.** `$bound instanceof GenericType && ($class === GenericType::class || $bound instanceof TemplateType)` read the two operands as peers and answered `GenericType|TemplateType`, so passing the value on to anything expecting a `GenericType` was reported as a mismatch — even though the first operand alone settles the question. The disjunction can only narrow the value further, never widen it past what was already proven. A disjunction still narrows on its own when nothing in the chain pinned the subject down. -- **Reference CodeLens stays responsive in large projects.** Queued member lenses share one semantic pass over their candidate files, one parsed syntax tree per file, and one forward-walked variable scope per method body; clients without lens refresh compute only the lens they resolve. Contributed by @sidux. +- **Reference CodeLens stays responsive in large projects.** Queued member lenses share one semantic pass over their candidate files, one parsed syntax tree per file, and one forward-walked variable scope per method body. The worker drains a request burst before sending one editor refresh, and clients without lens refresh compute only the lens they resolve. Contributed by @sidux. - **Reference and CodeLens annotations reuse the completed workspace index.** Resolving many lenses in a large project no longer starts another full filesystem walk for every declaration. Concurrent annotations share the first indexing pass, while an explicit Find References command still refreshes once so files created without an editor notification remain discoverable. Contributed by @sidux. - **Argument checks accept the widenings PHP performs and the types the engine admits it does not know.** Four shapes of correct code were reported as type mismatches: a bounded `int<0, max>` passed to a `float` parameter, even though PHP widens an integer to a float on the way in; a `class-string` passed to `non-empty-string`, even though a string that names a class always has content; an `array-key` passed to `int` or to `string`, which is the key type of an array nobody described rather than a value measured to be two things; and a closure body doing `$a & $b` on untyped parameters, which produces a string from two strings just as readily as an int from two numbers. - **`get_class($x) === Foo::class` narrows the same subjects `instanceof` does.** The identity check only pinned a plain variable, so `get_class($this->held) === Sub::class` and `get_class($items[0]) === Sub::class` left the subject at its declared type and every member read past the check was reported as missing. A property fetch, an array element, and a call result are all narrowed now, in the `$x::class === Foo::class` spelling as well. diff --git a/src/reference_counts.rs b/src/reference_counts.rs index 5c07bf910..932b6d18c 100644 --- a/src/reference_counts.rs +++ b/src/reference_counts.rs @@ -561,9 +561,10 @@ impl Backend { /// Run the queued member reference counts on a background thread and /// ask the editor to re-pull inlay hints once they land. /// - /// At most one computation runs at a time: the counts a viewport needs - /// are queued again by the next request, so a dropped schedule costs - /// nothing but the wait. + /// At most one computation runs at a time. Requests that arrive while it + /// runs join the same burst, which is drained before one editor refresh. + /// Refreshing after every partial batch creates a feedback loop in clients + /// that immediately re-request lenses for all open buffers. pub(crate) fn schedule_member_ref_counts(&self) { if !self.member_ref_counts.has_pending() || self @@ -578,12 +579,23 @@ impl Backend { tokio::spawn(async move { let worker = backend.clone_for_blocking(); let changed = crate::server::run_blocking_cancel_safe("member ref counts", move || { - let changed = worker.compute_pending_member_ref_counts(); - worker - .member_ref_counts - .computing - .store(false, Ordering::Release); - changed + let mut changed = false; + loop { + changed |= worker.compute_pending_member_ref_counts(); + + // Pair the empty check with clearing `computing` under + // the queue lock. A request either lands before this and + // is drained by the loop, or lands afterwards, observes + // `computing == false`, and starts the next worker. + let pending = worker.member_ref_counts.pending.lock(); + if pending.is_empty() { + worker + .member_ref_counts + .computing + .store(false, Ordering::Release); + return changed; + } + } }) .await; From e1daa321ecf61450b70a79fb47767a8b5f373557 Mon Sep 17 00:00:00 2001 From: sidux Date: Thu, 27 Aug 2026 17:04:47 +0200 Subject: [PATCH 06/10] fix(indexing): Cache semantic member targets --- docs/ARCHITECTURE.md | 2 +- docs/CHANGELOG.md | 2 +- src/mem_audit.rs | 20 +++- src/parser/ast_update.rs | 4 + src/reference_counts.rs | 73 +++++++++++++ src/reference_index.rs | 136 +++++++++++++++++++++++- src/references/members.rs | 217 ++++++++++++++++++++++++++++---------- 7 files changed, 392 insertions(+), 62 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 3907b37de..003b137d7 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -883,7 +883,7 @@ Both phases parse files in parallel using `std::thread::scope`. The work is spli Parsed files stay cached in `uri_classes_index`, `symbol_maps`, `file_imports`, and `file_namespaces` after the scan completes. There is no post-scan eviction; keeping the entries means subsequent operations (a second find-references call, go-to-definition on a cross-file symbol) benefit from the work already done. -The workspace reference index remains deliberately coarse: it stores candidate URIs and occurrence counts, not a second copy of every source position. CodeLens can therefore answer a conclusive zero without a semantic scan. Nonzero member references are resolved with the same hierarchy-aware search as Find References and cached as exact locations behind a 50,000-location bound. URI strings are shared inside that cache. Refresh-capable clients receive the lens after the background result is ready; other clients retain lazy `codeLens/resolve` as a compatibility path. +The workspace reference index keeps its primary map deliberately coarse: it stores candidate URIs and occurrence counts, not a second copy of every source position. CodeLens can therefore answer a conclusive zero without a semantic scan. The first nonzero member query resolves every member receiver in each candidate file while one forward-walked variable scope is active, packs the target class atoms by symbol-span index, and retains that compact per-file semantic layer for later member names. Candidate files are filled in parallel; edits evict their own layer, and signature changes clear layers whose receiver types may have changed. Exact locations remain bounded behind the 50,000-location annotation cache. Refresh-capable clients receive the lens after the background result is ready; other clients retain lazy `codeLens/resolve` as a compatibility path. ### Cross-file scanning diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index d0621a53d..8959623ed 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -54,7 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **`@phpstan-assert` narrows a property of the receiver, not just an argument.** A tag naming a path through `$this` (`@phpstan-assert bool $this->resolved`) was ignored, because the subject was looked for among the call's arguments and such a call often has none. The lazy-initialiser idiom (`if ($this->resolved === null) { $this->resolve(); } return $this->resolved;`) therefore reported the nullable property as the return value. The tag's `$this` now stands for whatever the call was made on, so it narrows through a variable receiver as well. - **A class named `Scalar` or `Numeric` is a class, not a PHPDoc pseudo-type.** `scalar` and `numeric` have no native spelling in PHP, so a project may name a class either of them, and nikic/php-parser does exactly that with `PhpParser\Node\Scalar`. Any capitalised spelling was folded into the pseudo-type instead of being resolved through the file's imports, which left the name unqualified and every check against it failing: passing a `Scalar` to a parameter typed as its own parent was reported as a mismatch, in a native type hint, a `@param`, a `@return`, and a `@implements` type argument alike. The all-lowercase spellings keep their PHPDoc meaning, which is the same rule already applied to `Number`, `Integer`, `Boolean`, `Double`, and `Resource`. - **An `&&` operand that pins a value to one class outranks a later operand that only lists alternatives.** `$bound instanceof GenericType && ($class === GenericType::class || $bound instanceof TemplateType)` read the two operands as peers and answered `GenericType|TemplateType`, so passing the value on to anything expecting a `GenericType` was reported as a mismatch — even though the first operand alone settles the question. The disjunction can only narrow the value further, never widen it past what was already proven. A disjunction still narrows on its own when nothing in the chain pinned the subject down. -- **Reference CodeLens stays responsive in large projects.** Queued member lenses share one semantic pass over their candidate files, one parsed syntax tree per file, and one forward-walked variable scope per method body. The worker drains a request burst before sending one editor refresh, and clients without lens refresh compute only the lens they resolve. Contributed by @sidux. +- **Reference CodeLens stays responsive in large projects.** PHP member receivers are resolved once per file in parallel and kept in a compact semantic index, so later lens batches filter exact references without reopening or walking source files. The worker drains a request burst before sending one editor refresh, and clients without lens refresh compute only the lens they resolve. Contributed by @sidux. - **Reference and CodeLens annotations reuse the completed workspace index.** Resolving many lenses in a large project no longer starts another full filesystem walk for every declaration. Concurrent annotations share the first indexing pass, while an explicit Find References command still refreshes once so files created without an editor notification remain discoverable. Contributed by @sidux. - **Argument checks accept the widenings PHP performs and the types the engine admits it does not know.** Four shapes of correct code were reported as type mismatches: a bounded `int<0, max>` passed to a `float` parameter, even though PHP widens an integer to a float on the way in; a `class-string` passed to `non-empty-string`, even though a string that names a class always has content; an `array-key` passed to `int` or to `string`, which is the key type of an array nobody described rather than a value measured to be two things; and a closure body doing `$a & $b` on untyped parameters, which produces a string from two strings just as readily as an int from two numbers. - **`get_class($x) === Foo::class` narrows the same subjects `instanceof` does.** The identity check only pinned a plain variable, so `get_class($this->held) === Sub::class` and `get_class($items[0]) === Sub::class` left the subject at its declared type and every member read past the check was reported as missing. A property fetch, an array element, and a call result are all narrowed now, in the `$x::class === Foo::class` spelling as well. diff --git a/src/mem_audit.rs b/src/mem_audit.rs index 8b0f133f7..bd305ce85 100644 --- a/src/mem_audit.rs +++ b/src/mem_audit.rs @@ -1348,11 +1348,13 @@ pub(crate) fn report(backend: &Backend, runner_content_bytes: usize) { // count, so there is no per-span duplication left to account for. let mut refs = Sz::default(); let mut n_key_uri_pairs = 0usize; + let n_resolved_member_files; + let mut n_resolved_member_accesses = 0usize; let n_keys; let mut distinct_uris: HashSet<*const u8> = HashSet::new(); { let idx = backend.reference_index.read(); - let (by_key, uri_keys) = idx.audit_maps(); + let (by_key, uri_keys, resolved_members) = idx.audit_maps(); n_keys = by_key.len(); refs += map_buckets::, u32>>( by_key.capacity(), @@ -1375,11 +1377,25 @@ pub(crate) fn report(backend: &Backend, runner_content_bytes: usize) { refs.add(k.audit_heap()); } } + refs += map_buckets::, Arc>( + resolved_members.capacity(), + ); + n_resolved_member_files = resolved_members.len(); + for (uri, file) in resolved_members { + distinct_uris.insert(Arc::as_ptr(uri).cast::()); + let (accesses, bytes, allocations) = file.audit_heap(); + n_resolved_member_accesses += accesses; + refs.add(ARC + size_of::()); + refs.add(bytes); + refs.allocs += allocations; + } } eprintln!( - "── reference_index: {} keys, {} (key, uri) pairs, {} distinct uris, {:.1} MB ({} allocs)", + "── reference_index: {} keys, {} (key, uri) pairs, {} exact member accesses in {} files, {} distinct uris, {:.1} MB ({} allocs)", n_keys, n_key_uri_pairs, + n_resolved_member_accesses, + n_resolved_member_files, distinct_uris.len(), mb(refs.bytes), refs.allocs, diff --git a/src/parser/ast_update.rs b/src/parser/ast_update.rs index 3d21d3a79..3492e43ca 100644 --- a/src/parser/ast_update.rs +++ b/src/parser/ast_update.rs @@ -1322,6 +1322,10 @@ impl Backend { if changed { self.member_completion_cache.lock().clear(); + // Exact member targets in other files may depend on the return or + // property type that changed here. Rebuild those files lazily; + // the edited file itself is evicted by reference reindexing below. + self.clear_resolved_member_files(); // A receiver's type is settled against the classes of the whole // workspace, so a signature change anywhere can turn a call that // was not a render into one, or the other way round. diff --git a/src/reference_counts.rs b/src/reference_counts.rs index 932b6d18c..fcb196ff5 100644 --- a/src/reference_counts.rs +++ b/src/reference_counts.rs @@ -756,6 +756,79 @@ function persist(Order $order): void { ); } + #[test] + fn later_member_batches_reuse_the_semantic_file_index() { + const SERVICE_URI: &str = "file:///Service.php"; + const CONSUMER_URI: &str = "file:///Consumer.php"; + const SERVICE: &str = r#"save(); + $service->cancel(); +} +"#; + + let backend = Backend::new_test(); + parse_extra(&backend, SERVICE_URI, SERVICE); + parse_extra(&backend, CONSUMER_URI, CONSUMER); + backend.workspace_indexed.store(true, Ordering::Release); + + let class_fqn = crate::atom::atom("Service"); + let save_offset = SERVICE.find("save").unwrap() as u32; + assert!( + backend + .member_ref_count_cached( + SERVICE_URI, + save_offset, + class_fqn, + crate::atom::atom("save"), + false, + ) + .is_none() + ); + crate::type_engine::variable::resolution::reset_test_scope_cache_hits(); + backend.compute_pending_member_ref_counts(); + assert!(crate::type_engine::variable::resolution::test_scope_cache_hits() > 0); + + let consumer_map = backend + .symbol_maps + .read() + .get(CONSUMER_URI) + .cloned() + .unwrap(); + assert!( + backend + .resolved_member_file(CONSUMER_URI, &consumer_map) + .is_some(), + "the first member query should index every receiver in its candidate file" + ); + + let cancel_offset = SERVICE.find("cancel").unwrap() as u32; + assert!( + backend + .member_ref_count_cached( + SERVICE_URI, + cancel_offset, + class_fqn, + crate::atom::atom("cancel"), + false, + ) + .is_none() + ); + crate::type_engine::variable::resolution::reset_test_scope_cache_hits(); + backend.compute_pending_member_ref_counts(); + assert_eq!( + crate::type_engine::variable::resolution::test_scope_cache_hits(), + 0, + "a later member name must not rebuild or query the file's variable scopes" + ); + } + #[test] fn ready_only_location_lookup_does_not_queue_background_work() { let backend = Backend::new_test(); diff --git a/src/reference_index.rs b/src/reference_index.rs index 680d67796..c71f5d58f 100644 --- a/src/reference_index.rs +++ b/src/reference_index.rs @@ -14,7 +14,7 @@ use std::sync::atomic::Ordering; use parking_lot::RwLock; use crate::Backend; -use crate::atom::{AtomMap, AtomSet, atom}; +use crate::atom::{Atom, AtomMap, AtomSet, atom}; use crate::backend::file_access::namespace_in_spans; use crate::class_lookup::find_class_at_offset; use crate::symbol_map::{LaravelStringKind, SelfStaticParentKind, SymbolKind, SymbolMap}; @@ -111,6 +111,80 @@ impl ReferenceIndexKey { pub(crate) struct ReferenceIndexInner { by_key: HashMap, u32>>, uri_keys: HashMap, Vec>, + resolved_members: HashMap, Arc>, +} + +#[derive(Clone, Copy)] +struct ResolvedMemberAccess { + span_index: u32, + target_start: u32, + target_len: u32, +} + +/// Receiver classes resolved for every member access in one immutable symbol +/// map. Targets are packed into one allocation; the access table points into +/// it and stays sorted by symbol-span index for binary lookup. +pub(crate) struct ResolvedMemberFile { + symbol_map: Arc, + accesses: Vec, + targets: Vec, +} + +impl ResolvedMemberFile { + pub(crate) fn new(symbol_map: Arc, mut resolved: Vec<(usize, Vec)>) -> Self { + resolved.sort_unstable_by_key(|(span_index, _)| *span_index); + let target_count = resolved.iter().map(|(_, targets)| targets.len()).sum(); + let mut accesses = Vec::with_capacity(resolved.len()); + let mut packed_targets = Vec::with_capacity(target_count); + for (span_index, mut targets) in resolved { + if targets.is_empty() { + continue; + } + targets.sort_unstable(); + targets.dedup(); + let target_start = packed_targets.len(); + packed_targets.extend(targets); + accesses.push(ResolvedMemberAccess { + span_index: span_index as u32, + target_start: target_start as u32, + target_len: (packed_targets.len() - target_start) as u32, + }); + } + Self { + symbol_map, + accesses, + targets: packed_targets, + } + } + + pub(crate) fn targets_for_span(&self, span_index: usize) -> &[Atom] { + let Ok(span_index) = u32::try_from(span_index) else { + return &[]; + }; + let Ok(index) = self + .accesses + .binary_search_by_key(&span_index, |access| access.span_index) + else { + return &[]; + }; + let access = self.accesses[index]; + let start = access.target_start as usize; + &self.targets[start..start + access.target_len as usize] + } + + fn matches_symbol_map(&self, symbol_map: &Arc) -> bool { + Arc::ptr_eq(&self.symbol_map, symbol_map) + } + + #[cfg(feature = "mem-audit")] + pub(crate) fn audit_heap(&self) -> (usize, usize, usize) { + ( + self.accesses.len(), + self.accesses.capacity() * std::mem::size_of::() + + self.targets.capacity() * std::mem::size_of::(), + usize::from(self.accesses.capacity() > 0) + usize::from(self.targets.capacity() > 0), + ) + } } impl ReferenceIndexInner { @@ -127,8 +201,9 @@ impl ReferenceIndexInner { ) -> ( &HashMap, u32>>, &HashMap, Vec>, + &HashMap, Arc>, ) { - (&self.by_key, &self.uri_keys) + (&self.by_key, &self.uri_keys, &self.resolved_members) } #[cfg(test)] @@ -144,6 +219,62 @@ pub(crate) fn new_reference_index() -> ReferenceIndex { } impl Backend { + pub(crate) fn resolved_member_file( + &self, + uri: &str, + symbol_map: &Arc, + ) -> Option> { + self.reference_index + .read() + .resolved_members + .get(uri) + .filter(|file| file.matches_symbol_map(symbol_map)) + .cloned() + } + + pub(crate) fn cache_resolved_member_file( + &self, + uri: &str, + symbol_map: Arc, + resolved: Vec<(usize, Vec)>, + ) -> Arc { + let built = Arc::new(ResolvedMemberFile::new(Arc::clone(&symbol_map), resolved)); + + // A didChange parse may have replaced the symbol map while the + // semantic walk was running. The result remains usable by its caller, + // which owns the same snapshot, but must not become the current cache. + if !self + .symbol_maps + .read() + .get(uri) + .is_some_and(|current| Arc::ptr_eq(current, &symbol_map)) + { + return built; + } + + let mut index = self.reference_index.write(); + if let Some(existing) = index + .resolved_members + .get(uri) + .filter(|file| file.matches_symbol_map(&symbol_map)) + { + return Arc::clone(existing); + } + let interned_uri = index + .uri_keys + .get_key_value(uri) + .map(|(uri, _)| Arc::clone(uri)) + .unwrap_or_else(|| Arc::from(uri)); + index + .resolved_members + .insert(interned_uri, Arc::clone(&built)); + built + } + + pub(crate) fn clear_resolved_member_files(&self) { + self.reference_index.write().resolved_members.clear(); + } + pub(crate) fn evict_reference_index_uri(&self, uri: &str) { let track_members = !self.member_ref_counts.is_empty(); let mut index = self.reference_index.write(); @@ -645,6 +776,7 @@ fn member_contributions_of_entries(entries: &[(ReferenceIndexKey, bool)]) -> Ato } fn evict_reference_index_uri_locked(index: &mut ReferenceIndexInner, uri: &str) { + index.resolved_members.remove(uri); let Some(keys) = index.uri_keys.remove(uri) else { return; }; diff --git a/src/references/members.rs b/src/references/members.rs index 685cedefc..36e953c6a 100644 --- a/src/references/members.rs +++ b/src/references/members.rs @@ -252,62 +252,118 @@ impl Backend { let snapshot = self.user_file_symbol_maps_for_reference_keys(&candidate_keys); self.begin_request_scan_window(snapshot.len(), "Scanning for member references"); - let mut locations = vec![Vec::new(); queries.len()]; - for (file_uri, symbol_map) in &snapshot { - self.request_scan_file_done(); - + let scan_file = |file_uri: &str, + symbol_map: &Arc| + -> Vec<(usize, Location)> { let mut span_indices = Vec::new(); for member in by_member.keys() { span_indices.extend_from_slice(symbol_map.member_access_indices(member)); } if span_indices.is_empty() { - continue; + return Vec::new(); } span_indices.sort_unstable(); span_indices.dedup(); let Ok(parsed_uri) = Url::parse(file_uri) else { - continue; + return Vec::new(); }; let Some(content) = self.reference_file_content_arc(file_uri) else { - continue; - }; - let _parse_cache_guard = crate::parser::with_parse_cache(&content); - let file_ctx = self.file_context(file_uri); - - // Receiver resolution for a bare variable normally walks its - // enclosing body from the start. A batch can contain hundreds of - // accesses from the same file, so build the forward-walked scope - // snapshots once and make every variable lookup below O(log N). - // The guard is per file: offsets from different files must never - // share one thread-local snapshot map. - let _scope_guard = - crate::type_engine::variable::forward_walk::with_diagnostic_scope_cache(); - let class_loader = self.class_loader(&file_ctx); - let function_loader = self.function_loader(&file_ctx); - let constant_loader = self.constant_loader(&file_ctx); - let config_resolver = |key: &str| self.resolve_config_type(key); - let trans_resolver = |key: &str| self.resolve_trans_type(key); - let loaders = crate::type_engine::resolver::Loaders { - function_loader: Some(&function_loader), - constant_loader: Some(&constant_loader), - config_resolver: Some(&config_resolver), - trans_resolver: Some(&trans_resolver), + return Vec::new(); }; - crate::type_engine::variable::forward_walk::build_diagnostic_scopes( - &content, - &file_ctx.classes, - &class_loader, - Some(self), - loaders, - Some(&self.resolved_class_cache), - ); + let needs_receiver = prepared.iter().any(|query| query.hierarchy.is_some()); + let resolved_file = needs_receiver.then(|| { + self.resolved_member_file(file_uri, symbol_map) + .unwrap_or_else(|| { + let _parse_cache_guard = crate::parser::with_parse_cache(&content); + let file_ctx = self.file_context(file_uri); + let all_access_indices: Vec<_> = symbol_map + .spans + .iter() + .enumerate() + .filter_map(|(index, span)| { + matches!(span.kind, SymbolKind::MemberAccess { .. }) + .then_some(index) + }) + .collect(); + + // Build variable scopes once, then resolve every + // member access while those snapshots are hot. + // Later declaration names reuse this packed + // per-file result without reopening the PHP file. + let _scope_guard = + crate::type_engine::variable::forward_walk::with_diagnostic_scope_cache( + ); + let needs_variable_scopes = all_access_indices.iter().any(|&span_index| { + let SymbolKind::MemberAccess { subject_text, .. } = + &symbol_map.spans[span_index].kind + else { + return false; + }; + let subject = subject_text.as_str(&content).trim_start(); + subject.starts_with('$') && !subject.starts_with("$this") + }); + if needs_variable_scopes { + let class_loader = self.class_loader(&file_ctx); + let function_loader = self.function_loader(&file_ctx); + let constant_loader = self.constant_loader(&file_ctx); + let config_resolver = |key: &str| self.resolve_config_type(key); + let trans_resolver = |key: &str| self.resolve_trans_type(key); + let loaders = crate::type_engine::resolver::Loaders { + function_loader: Some(&function_loader), + constant_loader: Some(&constant_loader), + config_resolver: Some(&config_resolver), + trans_resolver: Some(&trans_resolver), + }; + crate::type_engine::variable::forward_walk::build_diagnostic_scopes( + &content, + &file_ctx.classes, + &class_loader, + Some(self), + loaders, + Some(&self.resolved_class_cache), + ); + } + let _chain_guard = + crate::type_engine::resolver::with_chain_resolution_cache(); + let _resolver_guard = + crate::type_engine::call_resolution::activate_type_engine_caches(); + let resolved = all_access_indices + .into_iter() + .filter_map(|span_index| { + let span = &symbol_map.spans[span_index]; + let SymbolKind::MemberAccess { + subject_text, + is_static, + .. + } = &span.kind + else { + return None; + }; + let targets = self + .resolve_subject_to_fqns( + subject_text.as_str(&content), + *is_static, + &file_ctx, + span.start, + &content, + ) + .into_iter() + .map(|target| crate::atom::atom(&target)) + .collect(); + Some((span_index, targets)) + }) + .collect(); + self.cache_resolved_member_file(file_uri, Arc::clone(symbol_map), resolved) + }) + }); + + let mut matches = Vec::new(); for span_index in span_indices { let span = &symbol_map.spans[span_index]; let SymbolKind::MemberAccess { member_name, - subject_text, is_static, .. } = &span.kind @@ -318,18 +374,9 @@ impl Backend { continue; }; - let needs_receiver = query_indices - .iter() - .any(|&index| prepared[index].hierarchy.is_some()); - let subject_fqns = needs_receiver.then(|| { - self.resolve_subject_to_fqns( - subject_text.as_str(&content), - *is_static, - &file_ctx, - span.start, - &content, - ) - }); + let subject_fqns = resolved_file + .as_ref() + .map_or(&[][..], |file| file.targets_for_span(span_index)); let range = Range::new( offset_to_position(&content, span.start as usize), offset_to_position(&content, span.end as usize), @@ -339,8 +386,8 @@ impl Backend { let query = &prepared[query_index]; if let Some(hierarchy) = &query.hierarchy { if !subject_fqns - .as_ref() - .is_some_and(|fqns| fqns.iter().any(|fqn| hierarchy.contains(fqn))) + .iter() + .any(|fqn| hierarchy.contains(fqn.as_str())) { continue; } @@ -348,11 +395,69 @@ impl Backend { continue; } - locations[query_index].push(Location { - uri: parsed_uri.clone(), - range, - }); + matches.push(( + query_index, + Location { + uri: parsed_uri.clone(), + range, + }, + )); + } + } + matches + }; + + let mut locations = vec![Vec::new(); queries.len()]; + if snapshot.len() <= 2 { + for (file_uri, symbol_map) in &snapshot { + self.request_scan_file_done(); + for (query_index, location) in scan_file(file_uri, symbol_map) { + locations[query_index].push(location); + } + } + } else { + let next = std::sync::atomic::AtomicUsize::new(0); + let thread_count = std::thread::available_parallelism() + .map(std::num::NonZeroUsize::get) + .unwrap_or(4) + .min(snapshot.len()); + let worker_results = std::thread::scope(|scope| { + let mut handles = Vec::with_capacity(thread_count); + for _ in 0..thread_count { + let next = &next; + let snapshot = &snapshot; + let scan_file = &scan_file; + handles.push( + std::thread::Builder::new() + .stack_size(crate::PARSE_WORKER_STACK_SIZE) + .spawn_scoped(scope, move || { + let mut matches = Vec::new(); + loop { + let index = + next.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let Some((file_uri, symbol_map)) = snapshot.get(index) else { + break; + }; + self.request_scan_file_done(); + matches.extend(scan_file(file_uri, symbol_map)); + } + matches + }) + .expect("spawn member reference worker"), + ); } + handles + .into_iter() + .flat_map(|handle| { + handle.join().unwrap_or_else(|_| { + tracing::error!("member reference worker panicked"); + Vec::new() + }) + }) + .collect::>() + }); + for (query_index, location) in worker_results { + locations[query_index].push(location); } } From f4bea428a6fa752211f933df38c5770aa7d686d3 Mon Sep 17 00:00:00 2001 From: sidux Date: Thu, 27 Aug 2026 19:56:37 +0200 Subject: [PATCH 07/10] fix(references): keep member batching framework-neutral --- src/references/members.rs | 22 +--------------------- src/references/mod.rs | 11 +++++++++++ 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/src/references/members.rs b/src/references/members.rs index 36e953c6a..94e4bf6bc 100644 --- a/src/references/members.rs +++ b/src/references/members.rs @@ -461,27 +461,7 @@ impl Backend { } } - for (query, query_locations) in prepared.iter().zip(&mut locations) { - for location in - self.framework_member_reference_locations(&query.member, query.hierarchy.as_ref()) - { - push_unique_location( - query_locations, - &location.uri, - location.range.start, - location.range.end, - ); - } - for location in - self.framework_property_reference_locations(&query.member, query.hierarchy.as_ref()) - { - push_unique_location( - query_locations, - &location.uri, - location.range.start, - location.range.end, - ); - } + for query_locations in &mut locations { sort_locations_for_references(query_locations); } diff --git a/src/references/mod.rs b/src/references/mod.rs index 00c49feec..56b1f2fbe 100644 --- a/src/references/mod.rs +++ b/src/references/mod.rs @@ -216,6 +216,17 @@ pub(super) fn is_constructor_name(name: &str) -> bool { name.eq_ignore_ascii_case("__construct") } +fn sort_locations_for_references(locations: &mut Vec) { + locations.sort_by(|a, b| { + a.uri + .as_str() + .cmp(b.uri.as_str()) + .then(a.range.start.line.cmp(&b.range.start.line)) + .then(a.range.start.character.cmp(&b.range.start.character)) + }); + locations.dedup(); +} + /// Check whether a resolved class name matches the target FQN. /// /// Two names match if their fully-qualified forms are equal, or if both From d06fdfe5568fdc11fab1cd40ae1a1b232b54aa36 Mon Sep 17 00:00:00 2001 From: sidux Date: Tue, 25 Aug 2026 23:22:40 +0200 Subject: [PATCH 08/10] feat(navigation): Navigate PHP symbols in YAML and XML Resolve fully-qualified classes and Class::member references from arbitrary YAML and XML positions without schema-specific rules. --- docs/ARCHITECTURE.md | 1 + docs/CHANGELOG.md | 1 + src/definition/member/mod.rs | 28 +++ src/lib.rs | 1 + src/resource_navigation.rs | 229 ++++++++++++++++++ src/server.rs | 41 +++- .../integration/definition_resource_files.rs | 166 +++++++++++++ tests/integration/main.rs | 1 + 8 files changed, 466 insertions(+), 2 deletions(-) create mode 100644 src/resource_navigation.rs create mode 100644 tests/integration/definition_resource_files.rs diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 003b137d7..b91547b51 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -98,6 +98,7 @@ src/ │ # LSP features (one module each) ├── hover/ # Hover: symbol-map dispatch, type/signature/docblock formatting ├── definition/ # Go-to-definition (resolve, member, variable/, implementation, type_definition) +├── resource_navigation.rs # Schema-free PHP class/member navigation in YAML/XML ├── references/, rename/, highlight/ ├── signature_help.rs, semantic_tokens.rs, inlay_hints.rs, folding.rs, code_lens.rs ├── document_symbols.rs, document_links.rs, workspace_symbols.rs, formatting.rs diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 8959623ed..981448e35 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added +- **Fully-qualified PHP classes navigate from YAML and XML.** Ctrl+Click a class name in any YAML key or value, or any XML attribute or text node, and PHPantom opens its PHP declaration without needing to know that file's schema. `Class::member` references navigate too. Unknown and unqualified strings are left alone. Contributed by @sidux. - **Reference CodeLens.** PHP declarations show clickable exact reference counts. Declarations with no indexed uses are answered immediately, while semantic member locations are cached in a bounded background index so opening a large file does not fan out into an expensive resolve request per lens. Clients that support CodeLens refresh receive only ready, fully resolved member lenses. Contributed by @sidux. - **`analyze` takes more than one path.** `phpantom_lsp analyze app/ lib/Helper.php tests/` scans the union of everything named, mixing directories and single files freely, so a pre-commit hook or a CI step can hand it exactly the paths that changed instead of running the whole project or invoking the binary once per path. Overlapping arguments are reported once, and a path that does not exist still stops the run with exit code 2. Naming no path scans the entire project, as before. diff --git a/src/definition/member/mod.rs b/src/definition/member/mod.rs index a9dae1a67..51b997817 100644 --- a/src/definition/member/mod.rs +++ b/src/definition/member/mod.rs @@ -96,6 +96,34 @@ pub(super) enum MemberAccessHint { impl Backend { // ─── Member Definition Resolution ─────────────────────────────────────── + /// Resolve a member named by external project metadata, where the class + /// is already a fully-qualified name and there is no PHP source context + /// to extract a subject expression from. + pub(crate) fn class_member_declaration_location( + &self, + class_fqn: &str, + member_name: &str, + ) -> Option { + let target_class = self.find_or_load_class(class_fqn)?; + let class_loader = |name: &str| self.find_or_load_class(name); + let (declaring_class, declaring_fqn) = + Self::find_declaring_class(&target_class, member_name, &class_loader)?; + let member_kind = + Self::classify_member(&declaring_class, member_name, MemberAccessHint::Unknown)?; + let (class_uri, class_content) = self.find_class_file_content(&declaring_fqn, "", "")?; + let member_position = Self::find_member_position( + &class_content, + member_name, + member_kind, + declaring_class.member_name_offset(member_name, member_kind.as_str()), + )?; + + Some(point_location( + Url::parse(&class_uri).ok()?, + member_position, + )) + } + /// Resolve a member access to its definition using pre-extracted context. /// /// The caller provides a [`MemberDefinitionCtx`] bundling the subject diff --git a/src/lib.rs b/src/lib.rs index f213c29e6..e5c88316e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -266,6 +266,7 @@ mod reference_index; mod references; mod rename; mod resolution; +mod resource_navigation; pub(crate) mod return_collection; pub(crate) mod scope_collector; mod selection_range; diff --git a/src/resource_navigation.rs b/src/resource_navigation.rs new file mode 100644 index 000000000..3091cc411 --- /dev/null +++ b/src/resource_navigation.rs @@ -0,0 +1,229 @@ +//! PHP symbol navigation from non-PHP resource files. +//! +//! YAML and XML often carry fully-qualified PHP class names in arbitrary +//! keys, values, attributes, and text. This module recognises those names +//! without knowing the schema of the file that contains them, then delegates +//! declaration lookup to the normal PHP class loader. + +use tower_lsp::lsp_types::{Location, Position}; + +use crate::Backend; + +#[derive(Debug, PartialEq, Eq)] +enum ResourceSymbol { + Class(String), + Member { + class_fqn: String, + member_name: String, + }, +} + +/// Whether `uri` names a YAML or XML document that can carry PHP symbols. +pub(crate) fn is_resource_document(uri: &str) -> bool { + let path = uri + .split(['?', '#']) + .next() + .unwrap_or(uri) + .to_ascii_lowercase(); + + [ + ".yaml", + ".yml", + ".xml", + ".yaml.dist", + ".yml.dist", + ".xml.dist", + ] + .iter() + .any(|suffix| path.ends_with(suffix)) +} + +impl Backend { + /// Resolve the fully-qualified PHP class or `Class::member` under the + /// cursor in a YAML/XML document. + pub(crate) fn resolve_resource_definition( + &self, + content: &str, + position: Position, + ) -> Option { + match symbol_at(content, position)? { + ResourceSymbol::Class(fqn) => self.class_declaration_location(&fqn), + ResourceSymbol::Member { + class_fqn, + member_name, + } => self.class_member_declaration_location(&class_fqn, &member_name), + } + } +} + +fn symbol_at(content: &str, position: Position) -> Option { + let offset = crate::text_position::position_to_offset(content, position) as usize; + let previous_offset = offset.checked_sub(1); + let bytes = content.as_bytes(); + let mut cursor = 0usize; + + while cursor < bytes.len() { + if !is_name_start(bytes[cursor]) || (cursor > 0 && is_name_char(bytes[cursor - 1])) { + cursor += 1; + continue; + } + + let class_start = cursor; + let mut class_end = cursor + 1; + while class_end < bytes.len() && is_name_char(bytes[class_end]) { + class_end += 1; + } + + let raw_name = &content[class_start..class_end]; + if raw_name.contains('\\') && !raw_name.ends_with('\\') { + let fqn = normalize_fqn(raw_name); + if is_class_fqn(&fqn) { + if contains_cursor(class_start, class_end, offset, previous_offset) { + return Some(ResourceSymbol::Class(fqn)); + } + + if bytes.get(class_end) == Some(&b':') && bytes.get(class_end + 1) == Some(&b':') { + let member_start = class_end + 2; + let member_end = scan_identifier(bytes, member_start); + if member_end > member_start + && contains_cursor(member_start, member_end, offset, previous_offset) + { + return Some(ResourceSymbol::Member { + class_fqn: fqn, + member_name: content[member_start..member_end].to_string(), + }); + } + } + } + } + + cursor = class_end; + } + + None +} + +fn contains_cursor(start: usize, end: usize, offset: usize, previous: Option) -> bool { + (start..end).contains(&offset) || previous.is_some_and(|offset| (start..end).contains(&offset)) +} + +fn is_name_start(byte: u8) -> bool { + byte == b'\\' || byte == b'_' || byte.is_ascii_alphabetic() || !byte.is_ascii() +} + +fn is_name_char(byte: u8) -> bool { + byte == b'\\' || byte == b'_' || byte.is_ascii_alphanumeric() || !byte.is_ascii() +} + +fn scan_identifier(bytes: &[u8], start: usize) -> usize { + if !bytes + .get(start) + .is_some_and(|byte| *byte == b'_' || byte.is_ascii_alphabetic() || !byte.is_ascii()) + { + return start; + } + + let mut end = start + 1; + while end < bytes.len() + && (bytes[end] == b'_' || bytes[end].is_ascii_alphanumeric() || !bytes[end].is_ascii()) + { + end += 1; + } + end +} + +fn normalize_fqn(raw: &str) -> String { + let mut normalized = String::with_capacity(raw.len()); + let mut previous_was_separator = false; + + for character in raw.trim_matches('\\').chars() { + if character == '\\' { + if !previous_was_separator { + normalized.push(character); + } + previous_was_separator = true; + } else { + normalized.push(character); + previous_was_separator = false; + } + } + + normalized +} + +fn is_class_fqn(name: &str) -> bool { + let mut segments = name.split('\\'); + let Some(first) = segments.next() else { + return false; + }; + if !is_identifier(first) { + return false; + } + + let mut has_namespace = false; + for segment in segments { + has_namespace = true; + if !is_identifier(segment) { + return false; + } + } + has_namespace +} + +fn is_identifier(value: &str) -> bool { + let mut characters = value.chars(); + let Some(first) = characters.next() else { + return false; + }; + (first == '_' || first.is_alphabetic()) + && characters.all(|character| character == '_' || character.is_alphanumeric()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn scans_classes_without_knowing_yaml_keys() { + let content = "anything: App\\UseCase\\Run\n"; + assert_eq!( + symbol_at(content, Position::new(0, 18)), + Some(ResourceSymbol::Class("App\\UseCase\\Run".to_string())) + ); + } + + #[test] + fn scans_xml_text_and_attributes() { + let content = r#"App\Handler\Fallback"#; + assert_eq!( + symbol_at(content, Position::new(0, 21)), + Some(ResourceSymbol::Class("App\\Handler\\Run".to_string())) + ); + assert_eq!( + symbol_at(content, Position::new(0, 39)), + Some(ResourceSymbol::Class("App\\Handler\\Fallback".to_string())) + ); + } + + #[test] + fn scans_class_members_and_yaml_escaped_names() { + let content = r#"callback: "App\\Handler\\Run::handle""#; + assert_eq!( + symbol_at(content, Position::new(0, 32)), + Some(ResourceSymbol::Member { + class_fqn: "App\\Handler\\Run".to_string(), + member_name: "handle".to_string(), + }) + ); + } + + #[test] + fn ignores_short_and_malformed_names() { + assert_eq!(symbol_at("handler: Run", Position::new(0, 10)), None); + assert_eq!(symbol_at("path: folder\\-file", Position::new(0, 9)), None); + assert_eq!( + symbol_at("prefix: App\\Handler\\", Position::new(0, 14)), + None + ); + } +} diff --git a/src/server.rs b/src/server.rs index 11d145f12..4a9aabe84 100644 --- a/src/server.rs +++ b/src/server.rs @@ -740,6 +740,14 @@ impl LanguageServer for Backend { .write() .insert(uri.clone(), Arc::clone(&text)); + // Resource documents are not PHP source. Their schema-free symbol + // navigation reads the open buffer directly when requested. + if crate::resource_navigation::is_resource_document(&uri) { + self.log(MessageType::INFO, format!("Opened resource file: {}", uri)) + .await; + return; + } + // Parse and update AST map, use map, and namespace map self.update_ast(&uri, &text); @@ -819,6 +827,10 @@ impl LanguageServer for Backend { .write() .insert(uri.clone(), Arc::clone(&text)); + if crate::resource_navigation::is_resource_document(&uri) { + return; + } + // Re-parse in a blocking background task so typing does not // monopolize the LSP service loop and delay completion requests. // @@ -935,13 +947,20 @@ impl LanguageServer for Backend { async fn did_save(&self, params: DidSaveTextDocumentParams) { let uri = params.text_document.uri.to_string(); + let is_resource = crate::resource_navigation::is_resource_document(&uri); if let Some(text) = params.text { let text = Arc::new(text); self.open_files .write() .insert(uri.clone(), Arc::clone(&text)); - self.update_ast(&uri, &text); + if !is_resource { + self.update_ast(&uri, &text); + } + } + + if is_resource { + return; } // A save is a reliable sync point: re-diagnose the saved file @@ -1012,6 +1031,22 @@ impl LanguageServer for Backend { let backend = self.clone_for_blocking(); let uri_clone = uri.clone(); run_blocking_cancel_safe("goto_definition", move || { + // YAML and XML may name PHP classes under any schema. Resolve + // fully-qualified class and Class::member tokens before entering + // the PHP-only symbol-map path below. + if crate::resource_navigation::is_resource_document(&uri_clone) { + let location = backend.get_file_content(&uri_clone).and_then(|content| { + crate::util::catch_panic_unwind_safe( + "goto_definition", + &uri_clone, + Some(position), + || backend.resolve_resource_definition(&content, position), + ) + .flatten() + }); + return Ok(location.map(GotoDefinitionResponse::Scalar)); + } + // A component tag is HTML, so it has no position in the virtual // PHP `handle_with_position` would swap in below; it is resolved // from the template's own source instead. @@ -2011,7 +2046,9 @@ impl Backend { // map, so a file passes through here once; a file the parser panics // on publishes nothing and is retried, which is the same work its // next keystroke would do anyway. - if !self.symbol_maps.read().contains_key(uri) { + if !crate::resource_navigation::is_resource_document(uri) + && !self.symbol_maps.read().contains_key(uri) + { self.update_ast(uri, &content); } diff --git a/tests/integration/definition_resource_files.rs b/tests/integration/definition_resource_files.rs new file mode 100644 index 000000000..99558c8c7 --- /dev/null +++ b/tests/integration/definition_resource_files.rs @@ -0,0 +1,166 @@ +use crate::common::create_psr4_workspace; +use phpantom_lsp::Backend; +use tower_lsp::LanguageServer; +use tower_lsp::lsp_types::*; + +const COMPOSER: &str = r#"{ + "autoload": { "psr-4": { "App\\": "src/" } } +}"#; + +async fn open_resource(backend: &Backend, uri: Url, language_id: &str, content: &str) { + backend + .did_open(DidOpenTextDocumentParams { + text_document: TextDocumentItem { + uri, + language_id: language_id.to_string(), + version: 1, + text: content.to_string(), + }, + }) + .await; +} + +fn position_in(content: &str, needle: &str, inside: usize) -> Position { + let offset = content.find(needle).expect("needle should exist") + inside; + let prefix = &content[..offset]; + Position::new( + prefix.bytes().filter(|byte| *byte == b'\n').count() as u32, + prefix + .rsplit_once('\n') + .map_or(prefix.len(), |(_, line)| line.len()) as u32, + ) +} + +async fn definition_at( + backend: &Backend, + uri: Url, + content: &str, + needle: &str, + inside: usize, +) -> Option { + backend + .goto_definition(GotoDefinitionParams { + text_document_position_params: TextDocumentPositionParams { + text_document: TextDocumentIdentifier { uri }, + position: position_in(content, needle, inside), + }, + work_done_progress_params: WorkDoneProgressParams::default(), + partial_result_params: PartialResultParams::default(), + }) + .await + .expect("definition request should succeed") +} + +#[tokio::test] +async fn navigates_php_classes_from_arbitrary_yaml_keys_and_values() { + let php = "App\Handler\Run"#; + let (backend, dir) = create_psr4_workspace( + COMPOSER, + &[("src/Handler/Run.php", php), ("config/arbitrary.xml", xml)], + ); + let xml_uri = Url::from_file_path(dir.path().join("config/arbitrary.xml")).unwrap(); + open_resource(&backend, xml_uri.clone(), "xml", xml).await; + + for needle in ["handler=\"App\\Handler\\Run", ">App\\Handler\\Run"] { + let result = definition_at(&backend, xml_uri.clone(), xml, needle, needle.len() - 2) + .await + .expect("class should resolve from XML"); + let GotoDefinitionResponse::Scalar(location) = result else { + panic!("expected one class definition"); + }; + assert!(location.uri.path().ends_with("/src/Handler/Run.php")); + } +} + +#[tokio::test] +async fn navigates_class_members_and_yaml_escaped_class_names() { + let php = concat!( + " Date: Thu, 27 Aug 2026 01:10:46 +0200 Subject: [PATCH 09/10] feat(navigation): Index YAML and XML PHP references Feed schema-free class and member occurrences into Find References and CodeLens, including transparent-proxy metadata aliases. --- docs/ARCHITECTURE.md | 6 +- docs/CHANGELOG.md | 2 +- src/code_lens.rs | 16 +- src/indexing/preload.rs | 51 +++++- src/indexing/watch.rs | 44 ++++- src/reference_index.rs | 25 ++- src/references/classes.rs | 7 +- src/references/mod.rs | 39 ++++- src/resource_navigation.rs | 160 ++++++++++++++++-- src/server.rs | 39 ++++- .../integration/definition_resource_files.rs | 150 ++++++++++++++++ 11 files changed, 486 insertions(+), 53 deletions(-) diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index b91547b51..91f295fd6 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -98,7 +98,7 @@ src/ │ # LSP features (one module each) ├── hover/ # Hover: symbol-map dispatch, type/signature/docblock formatting ├── definition/ # Go-to-definition (resolve, member, variable/, implementation, type_definition) -├── resource_navigation.rs # Schema-free PHP class/member navigation in YAML/XML +├── resource_navigation.rs # Schema-free PHP class/member indexing and navigation in YAML/XML ├── references/, rename/, highlight/ ├── signature_help.rs, semantic_tokens.rs, inlay_hints.rs, folding.rs, code_lens.rs ├── document_symbols.rs, document_links.rs, workspace_symbols.rs, formatting.rs @@ -878,9 +878,9 @@ When the user invokes "Find All References", PHPantom scans all user files for o Before scanning, `ensure_workspace_indexed` ensures all user files have symbol maps: 1. **Phase 1: fqn_uri_index files (user only)** — files already known from `update_ast` calls. Vendor and stub URIs are skipped. -2. **Phase 2: `.gitignore`-aware workspace walk** — uses the `ignore` crate's `WalkBuilder` to recursively discover PHP files under the workspace root, respecting `.gitignore` rules (including nested and global gitignore files). This automatically skips generated/cached directories like `storage/framework/views/` (Laravel blade cache), `var/cache/` (Symfony), and `node_modules/`. The vendor directory is always skipped regardless of `.gitignore` content. Hidden directories are skipped by default. +2. **Phase 2: `.gitignore`-aware workspace walk** — uses the `ignore` crate's `WalkBuilder` to recursively discover PHP plus YAML/XML resource files under the workspace root, respecting `.gitignore` rules (including nested and global gitignore files). This automatically skips generated/cached directories like `storage/framework/views/` (Laravel blade cache), `var/cache/` (Symfony), and `node_modules/`. The vendor directory is always skipped regardless of `.gitignore` content. Hidden directories are skipped by default. -Both phases parse files in parallel using `std::thread::scope`. The work is split into chunks (one per CPU core) and each thread reads a file from disk and calls `update_ast`, which acquires write locks briefly to store results while the expensive parsing step runs without any locks held. Batches of 2 or fewer files skip threading overhead. +PHP files are parsed in parallel using `std::thread::scope`. The work is split into chunks (one per CPU core) and each thread reads a file from disk and calls `update_ast`, which acquires write locks briefly to store results while the expensive parsing step runs without any locks held. Batches of 2 or fewer files skip threading overhead. YAML/XML files take the lightweight schema-free scanner and publish synthetic class/member symbol maps into the same reference index. Parsed files stay cached in `uri_classes_index`, `symbol_maps`, `file_imports`, and `file_namespaces` after the scan completes. There is no post-scan eviction; keeping the entries means subsequent operations (a second find-references call, go-to-definition on a cross-file symbol) benefit from the work already done. diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 981448e35..5422610fd 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added -- **Fully-qualified PHP classes navigate from YAML and XML.** Ctrl+Click a class name in any YAML key or value, or any XML attribute or text node, and PHPantom opens its PHP declaration without needing to know that file's schema. `Class::member` references navigate too. Unknown and unqualified strings are left alone. Contributed by @sidux. +- **Fully-qualified PHP classes navigate from YAML and XML.** Ctrl+Click a class name in any YAML key or value, or any XML attribute or text node, and PHPantom opens its PHP declaration without needing to know that file's schema. `Class::member` references navigate too. The same occurrences feed Find References and declaration CodeLens through the workspace reference index. Unknown and unqualified strings are left alone. Contributed by @sidux. - **Reference CodeLens.** PHP declarations show clickable exact reference counts. Declarations with no indexed uses are answered immediately, while semantic member locations are cached in a bounded background index so opening a large file does not fan out into an expensive resolve request per lens. Clients that support CodeLens refresh receive only ready, fully resolved member lenses. Contributed by @sidux. - **`analyze` takes more than one path.** `phpantom_lsp analyze app/ lib/Helper.php tests/` scans the union of everything named, mixing directories and single files freely, so a pre-commit hook or a CI step can hand it exactly the paths that changed instead of running the whole project or invoking the binary once per path. Overlapping arguments are reported once, and a path that does not exist still stops the run with exit code 2. Naming no path scans the entire project, as before. diff --git a/src/code_lens.rs b/src/code_lens.rs index be9176924..c4996ee85 100644 --- a/src/code_lens.rs +++ b/src/code_lens.rs @@ -242,11 +242,17 @@ impl Backend { if declaration_offset == 0 { return None; } - let key = ReferenceIndexKey::Member { - name: member.to_string(), - is_static, - }; - let candidate_count = self.indexed_reference_count(&key)?; + let member_name = member.to_string(); + let candidate_count = self.indexed_reference_count_for_keys(&[ + ReferenceIndexKey::Member { + name: member_name.clone(), + is_static, + }, + ReferenceIndexKey::Member { + name: member_name, + is_static: !is_static, + }, + ])?; let origin_url = Url::parse(origin_uri).ok()?; let position = offset_to_position(content, declaration_offset as usize); let range = Range::new( diff --git a/src/indexing/preload.rs b/src/indexing/preload.rs index f51939c7e..a58f2a60a 100644 --- a/src/indexing/preload.rs +++ b/src/indexing/preload.rs @@ -282,20 +282,24 @@ impl Backend { // the initial pass and reuse it. let workspace_root = self.workspace.workspace_root.read().clone(); let phase1_uri_set: HashSet<&str> = phase1_uris.iter().map(|uri| uri.as_str()).collect(); - let phase2_work = if let Some(root) = workspace_root.clone() { + let (phase2_work, resource_work) = if let Some(root) = workspace_root.clone() { let vendor_dir_paths = self.workspace.vendor_dir_paths.lock().clone(); self.report_workspace_index_progress(progress, 3, "Scanning workspace files"); let walk_start = std::time::Instant::now(); - let php_files = - crate::references::collect_php_files_gitignore(&root, &vendor_dir_paths); + let (php_files, resource_files) = + crate::references::collect_workspace_index_files_gitignore( + &root, + &vendor_dir_paths, + ); tracing::info!( - "ensure_workspace_indexed: Phase 2 disk walk found {} PHP files in {:?}", + "ensure_workspace_indexed: Phase 2 disk walk found {} PHP and {} resource files in {:?}", php_files.len(), + resource_files.len(), walk_start.elapsed() ); - php_files + let php_work = php_files .into_iter() .filter_map(|path| { let uri = crate::util::path_to_uri(&path); @@ -305,12 +309,20 @@ impl Backend { Some((uri, path)) } }) - .collect() + .collect(); + let resource_work = resource_files + .into_iter() + .filter_map(|path| { + let uri = crate::util::path_to_uri(&path); + (!existing_uris.contains(&uri)).then_some((uri, path)) + }) + .collect(); + (php_work, resource_work) } else { - Vec::new() + (Vec::new(), Vec::new()) }; - let total_to_parse = phase1_uris.len() + phase2_work.len(); + let total_to_parse = phase1_uris.len() + phase2_work.len() + resource_work.len(); let phase1_units: u64 = phase1_uris .iter() .map(|uri| self.index_progress_weight_for_uri(uri, None)) @@ -319,7 +331,14 @@ impl Backend { .iter() .map(|(_, path)| index_progress_weight_for_path(path)) .sum(); - let total_parse_units = phase1_units.saturating_add(phase2_units).max(1); + let resource_units: u64 = resource_work + .iter() + .map(|(_, path)| index_progress_weight_for_path(path)) + .sum(); + let total_parse_units = phase1_units + .saturating_add(phase2_units) + .saturating_add(resource_units) + .max(1); self.report_workspace_index_progress( progress, 5, @@ -376,6 +395,20 @@ impl Backend { }), ); } + if !resource_work.is_empty() { + self.report_workspace_index_progress( + progress, + workspace_parse_percentage( + phase1_units.saturating_add(phase2_units), + total_parse_units, + ), + format!( + "Indexing resource references ({}/{total_to_parse})", + phase1_uris.len() + phase2_work.len() + ), + ); + self.index_resource_paths_batch(&resource_work); + } self.report_workspace_index_progress(progress, 99, "Finalizing workspace index"); // Release pairs with the Acquire loads in // `reference_candidate_uris_for_keys` and `find_implementors`. diff --git a/src/indexing/watch.rs b/src/indexing/watch.rs index 7cb43c053..4073e6aeb 100644 --- a/src/indexing/watch.rs +++ b/src/indexing/watch.rs @@ -23,10 +23,10 @@ const GLOBAL_CONFIG_POLL_INTERVAL: Duration = Duration::from_secs(2); impl Backend { /// Apply a `workspace/didChangeWatchedFiles` batch to the indexes. /// - /// Returns `true` if any PHP file, composer file, or the project's own - /// `.phpantom.toml` was acted on (so the caller can ask the editor to - /// re-pull diagnostics). Runs entirely on a blocking thread; it parses - /// no files on the async runtime. + /// Returns `true` if any PHP/resource file, composer file, or the + /// project's own `.phpantom.toml` was acted on (so the caller can ask the + /// editor to refresh affected features). Runs entirely on a blocking + /// thread; it parses no files on the async runtime. /// /// Editors cannot watch the filesystem while the window is unfocused, so /// on refocus they resynchronise by reporting the *entire* workspace as @@ -52,6 +52,7 @@ impl Backend { let mut schema_full_rebuild = false; let mut migration_changes: Vec<(PathBuf, FileChangeType)> = Vec::new(); let mut php_changes: Vec<(String, PathBuf, FileChangeType)> = Vec::new(); + let mut resource_changes: Vec<(String, PathBuf, FileChangeType)> = Vec::new(); let mut migration_discovery = crate::virtual_members::laravel::database_schema::MigrationDiscovery::default(); let is_laravel = self.resolved_class_cache.read().is_laravel(); @@ -59,6 +60,7 @@ impl Backend { { let open = self.open_files.read(); let parsed = self.parsed_uris.read(); + let indexed = self.symbol_maps.read(); let laravel_config = self.config().laravel; for change in ¶ms.changes { let path_str = change.uri.path(); @@ -103,12 +105,30 @@ impl Backend { } continue; } + let uri_str = change.uri.to_string(); + if crate::resource_navigation::is_resource_document(path_str) { + if open.contains_key(&uri_str) { + continue; + } + let Ok(file_path) = change.uri.to_file_path() else { + continue; + }; + if change.typ == FileChangeType::CHANGED { + let canonical_uri = crate::util::path_to_uri(&file_path); + if !indexed.contains_key(&uri_str) + && !indexed.contains_key(canonical_uri.as_str()) + { + continue; + } + } + resource_changes.push((uri_str, file_path, change.typ)); + continue; + } if !path_str.ends_with(".php") { continue; } // Open files are already tracked via did_open/did_change. - let uri_str = change.uri.to_string(); if open.contains_key(&uri_str) { continue; } @@ -133,6 +153,7 @@ impl Backend { } if php_changes.is_empty() + && resource_changes.is_empty() && !composer_changed && !config_changed && !schema_full_rebuild @@ -175,6 +196,19 @@ impl Backend { self.rescan_composer_indexes(root); } + if !resource_changes.is_empty() { + tracing::info!( + "PHPantom: {} watched YAML/XML file(s) changed on disk, refreshing references", + resource_changes.len() + ); + for (uri, path, change_type) in &resource_changes { + if *change_type == FileChangeType::DELETED { + self.clear_file_maps(uri); + } else if let Ok(content) = std::fs::read_to_string(path) { + self.update_resource_symbol_index(uri, &content); + } + } + } if schema_full_rebuild { tracing::info!("PHPantom: Laravel schema files changed, reloading schema index"); self.reload_laravel_schema_index(root); diff --git a/src/reference_index.rs b/src/reference_index.rs index c71f5d58f..0759a4c74 100644 --- a/src/reference_index.rs +++ b/src/reference_index.rs @@ -319,16 +319,25 @@ impl Backend { /// filtering can remove name matches, but it cannot create a reference /// that the symbol map did not index. pub(crate) fn indexed_reference_count(&self, key: &ReferenceIndexKey) -> Option { + self.indexed_reference_count_for_keys(std::slice::from_ref(key)) + } + + /// Number of indexed occurrences across several alternative keys. + pub(crate) fn indexed_reference_count_for_keys( + &self, + keys: &[ReferenceIndexKey], + ) -> Option { if self.skip_reference_index || !self.workspace_indexed.load(Ordering::Acquire) { return None; } + let index = self.reference_index.read(); Some( - self.reference_index - .read() - .get(key) - .map(|entries| entries.values().map(|&count| count as usize).sum()) - .unwrap_or(0), + keys.iter() + .filter_map(|key| index.get(key)) + .flat_map(HashMap::values) + .map(|&count| count as usize) + .sum(), ) } @@ -560,6 +569,12 @@ impl Backend { ) -> Vec<(ReferenceIndexKey, bool)> { match &span.kind { SymbolKind::ClassReference { name, is_fqn, .. } => { + if *is_fqn && crate::resource_navigation::is_resource_document(uri) { + return vec![( + ReferenceIndexKey::class_owned(normalize_symbol_name(name)), + true, + )]; + } let resolved = if *is_fqn { normalize_symbol_name(name) } else if let Some(fqn) = self.resolved_name_at(uri, span.start) { diff --git a/src/references/classes.rs b/src/references/classes.rs index 21ef8c6fb..8a21341cd 100644 --- a/src/references/classes.rs +++ b/src/references/classes.rs @@ -45,6 +45,9 @@ impl Backend { let resolved_names = self.resolved_names.read().get(file_uri).cloned(); let file_namespace = self.first_file_namespace(file_uri); let file_use_map = std::cell::OnceCell::new(); + let class_matches = |resolved: &str| { + class_names_match(strip_fqn_prefix(resolved), target, target_short) + }; // First pass: resolved-name check to avoid unnecessary content work. // Aliased imports (`use Foo as Bar; new Bar`) must still reach the @@ -68,7 +71,7 @@ impl Backend { }); Self::resolve_to_fqn(name, use_map, &file_namespace) }; - class_names_match(strip_fqn_prefix(&resolved), target, target_short) + class_matches(&resolved) } } SymbolKind::ClassDeclaration { name } => { @@ -109,7 +112,7 @@ impl Backend { }); Self::resolve_to_fqn(name, use_map, &file_namespace) }; - class_names_match(strip_fqn_prefix(&resolved), target, target_short) + class_matches(&resolved) } SymbolKind::ClassDeclaration { name } if include_declaration => { if !name.eq_ignore_ascii_case(target_short) { diff --git a/src/references/mod.rs b/src/references/mod.rs index 56b1f2fbe..ed922ca48 100644 --- a/src/references/mod.rs +++ b/src/references/mod.rs @@ -327,9 +327,40 @@ pub(crate) fn collect_php_files_gitignore( root: &Path, vendor_dir_paths: &[PathBuf], ) -> Vec { + let mut result = Vec::new(); + visit_workspace_files_gitignore(root, vendor_dir_paths, |path| { + if path.extension().is_some_and(|extension| extension == "php") { + result.push(path.to_path_buf()); + } + }); + result +} + +/// Collect the PHP and schema-free YAML/XML inputs used by the full workspace +/// index in one `.gitignore`-aware walk. +pub(crate) fn collect_workspace_index_files_gitignore( + root: &Path, + vendor_dir_paths: &[PathBuf], +) -> (Vec, Vec) { + let mut php_files = Vec::new(); + let mut resource_files = Vec::new(); + visit_workspace_files_gitignore(root, vendor_dir_paths, |path| { + if path.extension().is_some_and(|extension| extension == "php") { + php_files.push(path.to_path_buf()); + } else if crate::resource_navigation::is_resource_path(path) { + resource_files.push(path.to_path_buf()); + } + }); + (php_files, resource_files) +} + +fn visit_workspace_files_gitignore( + root: &Path, + vendor_dir_paths: &[PathBuf], + mut visit: impl FnMut(&Path), +) { use ignore::WalkBuilder; - let mut result = Vec::new(); let vendor_paths_owned: Vec = vendor_dir_paths.to_vec(); let walker = WalkBuilder::new(root) @@ -357,12 +388,10 @@ pub(crate) fn collect_php_files_gitignore( for entry in walker.flatten() { let path = entry.path(); - if path.is_file() && path.extension().is_some_and(|ext| ext == "php") { - result.push(path.to_path_buf()); + if path.is_file() { + visit(path); } } - - result } /// Push a location only if it is not already present (deduplication). diff --git a/src/resource_navigation.rs b/src/resource_navigation.rs index 3091cc411..bf4018bd7 100644 --- a/src/resource_navigation.rs +++ b/src/resource_navigation.rs @@ -5,9 +5,14 @@ //! without knowing the schema of the file that contains them, then delegates //! declaration lookup to the normal PHP class loader. +use std::path::{Path, PathBuf}; +use std::sync::Arc; + use tower_lsp::lsp_types::{Location, Position}; use crate::Backend; +use crate::atom::{AtomMap, atom}; +use crate::symbol_map::{ClassRefContext, SubjectText, SymbolKind, SymbolMap, SymbolSpan}; #[derive(Debug, PartialEq, Eq)] enum ResourceSymbol { @@ -18,6 +23,14 @@ enum ResourceSymbol { }, } +#[derive(Debug, PartialEq, Eq)] +struct ScannedResourceSymbol { + class_fqn: String, + class_start: usize, + class_end: usize, + member: Option<(String, usize, usize)>, +} + /// Whether `uri` names a YAML or XML document that can carry PHP symbols. pub(crate) fn is_resource_document(uri: &str) -> bool { let path = uri @@ -38,6 +51,13 @@ pub(crate) fn is_resource_document(uri: &str) -> bool { .any(|suffix| path.ends_with(suffix)) } +/// Whether a filesystem path is a YAML/XML resource document. +pub(crate) fn is_resource_path(path: &Path) -> bool { + path.file_name() + .and_then(|name| name.to_str()) + .is_some_and(is_resource_document) +} + impl Backend { /// Resolve the fully-qualified PHP class or `Class::member` under the /// cursor in a YAML/XML document. @@ -54,13 +74,118 @@ impl Backend { } => self.class_member_declaration_location(&class_fqn, &member_name), } } + + /// Replace one resource document's synthetic symbol map and reference + /// contributions. + pub(crate) fn update_resource_symbol_index(&self, uri: &str, content: &str) { + let symbol_map = Arc::new(self.resource_symbol_map(content)); + self.symbol_maps + .write() + .insert(uri.to_string(), Arc::clone(&symbol_map)); + self.reindex_references_for_symbol_maps_batch(vec![(uri.to_string(), symbol_map)]); + } + + /// Index resource files discovered during the workspace walk. + pub(crate) fn index_resource_paths_batch(&self, files: &[(String, PathBuf)]) { + let maps: Vec<(String, Arc)> = files + .iter() + .filter_map(|(uri, path)| { + let content = std::fs::read_to_string(path).ok()?; + Some((uri.clone(), Arc::new(self.resource_symbol_map(&content)))) + }) + .collect(); + if maps.is_empty() { + return; + } + + { + let mut symbol_maps = self.symbol_maps.write(); + for (uri, map) in &maps { + symbol_maps.insert(uri.clone(), Arc::clone(map)); + } + } + self.reindex_references_for_symbol_maps_batch(maps); + } + + fn resource_symbol_map(&self, content: &str) -> SymbolMap { + let mut spans = Vec::new(); + for symbol in scan_symbols(content) { + spans.push(SymbolSpan { + start: symbol.class_start as u32, + end: symbol.class_end as u32, + kind: SymbolKind::ClassReference { + name: atom(&symbol.class_fqn), + is_fqn: true, + context: ClassRefContext::Other, + }, + }); + + if let Some((member_name, member_start, member_end)) = symbol.member { + spans.push(SymbolSpan { + start: member_start as u32, + end: member_end as u32, + kind: SymbolKind::MemberAccess { + subject_text: SubjectText::owned(symbol.class_fqn), + member_name: atom(&member_name), + is_static: false, + is_method_call: true, + docblock_ref: crate::symbol_map::DocblockMemberRef::No, + is_array_callable: false, + is_nullsafe: false, + }, + }); + } + } + spans.sort_by_key(|span| span.start); + + let mut member_access_indices = AtomMap::default(); + for (index, span) in spans.iter().enumerate() { + if let SymbolKind::MemberAccess { member_name, .. } = &span.kind { + member_access_indices + .entry(*member_name) + .or_insert_with(Vec::new) + .push(index); + } + } + + SymbolMap { + spans, + member_access_indices, + source_len: u32::try_from(content.len()).unwrap_or(u32::MAX), + ..SymbolMap::default() + } + } } fn symbol_at(content: &str, position: Position) -> Option { let offset = crate::text_position::position_to_offset(content, position) as usize; let previous_offset = offset.checked_sub(1); + + for symbol in scan_symbols(content) { + if contains_cursor( + symbol.class_start, + symbol.class_end, + offset, + previous_offset, + ) { + return Some(ResourceSymbol::Class(symbol.class_fqn)); + } + if let Some((member_name, member_start, member_end)) = symbol.member + && contains_cursor(member_start, member_end, offset, previous_offset) + { + return Some(ResourceSymbol::Member { + class_fqn: symbol.class_fqn, + member_name, + }); + } + } + None +} + +fn scan_symbols(content: &str) -> Vec { let bytes = content.as_bytes(); let mut cursor = 0usize; + let mut symbols = Vec::new(); while cursor < bytes.len() { if !is_name_start(bytes[cursor]) || (cursor > 0 && is_name_char(bytes[cursor - 1])) { @@ -78,29 +203,36 @@ fn symbol_at(content: &str, position: Position) -> Option { if raw_name.contains('\\') && !raw_name.ends_with('\\') { let fqn = normalize_fqn(raw_name); if is_class_fqn(&fqn) { - if contains_cursor(class_start, class_end, offset, previous_offset) { - return Some(ResourceSymbol::Class(fqn)); - } - - if bytes.get(class_end) == Some(&b':') && bytes.get(class_end + 1) == Some(&b':') { + let member = if bytes.get(class_end) == Some(&b':') + && bytes.get(class_end + 1) == Some(&b':') + { let member_start = class_end + 2; let member_end = scan_identifier(bytes, member_start); - if member_end > member_start - && contains_cursor(member_start, member_end, offset, previous_offset) - { - return Some(ResourceSymbol::Member { - class_fqn: fqn, - member_name: content[member_start..member_end].to_string(), - }); + if member_end > member_start { + Some(( + content[member_start..member_end].to_string(), + member_start, + member_end, + )) + } else { + None } - } + } else { + None + }; + symbols.push(ScannedResourceSymbol { + class_fqn: fqn, + class_start, + class_end, + member, + }); } } cursor = class_end; } - None + symbols } fn contains_cursor(start: usize, end: usize, offset: usize, previous: Option) -> bool { diff --git a/src/server.rs b/src/server.rs index 4a9aabe84..260e00783 100644 --- a/src/server.rs +++ b/src/server.rs @@ -574,6 +574,14 @@ impl LanguageServer for Backend { glob_pattern: GlobPattern::String("**/*.php".to_string()), kind: Some(WatchKind::Create | WatchKind::Change | WatchKind::Delete), }, + FileSystemWatcher { + glob_pattern: GlobPattern::String("**/*.{yaml,yml,xml}".to_string()), + kind: Some(WatchKind::Create | WatchKind::Change | WatchKind::Delete), + }, + FileSystemWatcher { + glob_pattern: GlobPattern::String("**/*.{yaml,yml,xml}.dist".to_string()), + kind: Some(WatchKind::Create | WatchKind::Change | WatchKind::Delete), + }, FileSystemWatcher { glob_pattern: GlobPattern::String("**/composer.json".to_string()), kind: Some(WatchKind::Change), @@ -740,9 +748,11 @@ impl LanguageServer for Backend { .write() .insert(uri.clone(), Arc::clone(&text)); - // Resource documents are not PHP source. Their schema-free symbol - // navigation reads the open buffer directly when requested. + // Resource documents are not PHP source. Build a lightweight symbol + // map so navigation, references, rename, and PHP declaration lenses + // all consume the same indexed occurrences. if crate::resource_navigation::is_resource_document(&uri) { + self.update_resource_symbol_index(&uri, &text); self.log(MessageType::INFO, format!("Opened resource file: {}", uri)) .await; return; @@ -828,6 +838,12 @@ impl LanguageServer for Backend { .insert(uri.clone(), Arc::clone(&text)); if crate::resource_navigation::is_resource_document(&uri) { + self.update_resource_symbol_index(&uri, &text); + if self.supports_code_lens_refresh.load(Ordering::Acquire) + && let Some(ref client) = self.client + { + let _ = client.code_lens_refresh().await; + } return; } @@ -936,7 +952,15 @@ impl LanguageServer for Backend { self.blade_injected_vars.write().remove(&uri); } - self.clear_file_maps(&uri); + if crate::resource_navigation::is_resource_document(&uri) { + if let Some(content) = self.get_file_content(&uri) { + self.update_resource_symbol_index(&uri, &content); + } else { + self.clear_file_maps(&uri); + } + } else { + self.clear_file_maps(&uri); + } // Clear diagnostics so stale warnings don't linger after the file is closed self.clear_diagnostics_for_file(&uri).await; @@ -954,7 +978,9 @@ impl LanguageServer for Backend { self.open_files .write() .insert(uri.clone(), Arc::clone(&text)); - if !is_resource { + if is_resource { + self.update_resource_symbol_index(&uri, &text); + } else { self.update_ast(&uri, &text); } } @@ -1014,6 +1040,11 @@ impl LanguageServer for Backend { // (or missing ones) are corrected. if did_work { self.request_diagnostic_refresh().await; + if self.supports_code_lens_refresh.load(Ordering::Acquire) + && let Some(ref client) = self.client + { + let _ = client.code_lens_refresh().await; + } } } diff --git a/tests/integration/definition_resource_files.rs b/tests/integration/definition_resource_files.rs index 99558c8c7..113dfc13c 100644 --- a/tests/integration/definition_resource_files.rs +++ b/tests/integration/definition_resource_files.rs @@ -146,6 +146,156 @@ async fn navigates_class_members_and_yaml_escaped_class_names() { assert_eq!(member_location.range.start.line, 3); } +#[tokio::test] +async fn resource_classes_feed_find_references_and_code_lens() { + let php = concat!(""#; + let (backend, dir) = create_psr4_workspace( + COMPOSER, + &[ + ("src/Domain/Widget.php", php), + ("config/widgets.yaml", yaml), + ("config/widgets.xml", xml), + ], + ); + let php_uri = Url::from_file_path(dir.path().join("src/Domain/Widget.php")).unwrap(); + open_resource(&backend, php_uri.clone(), "php", php).await; + + let references = backend + .references(ReferenceParams { + text_document_position: TextDocumentPositionParams { + text_document: TextDocumentIdentifier { + uri: php_uri.clone(), + }, + position: Position::new(2, 8), + }, + context: ReferenceContext { + include_declaration: false, + }, + work_done_progress_params: WorkDoneProgressParams::default(), + partial_result_params: PartialResultParams::default(), + }) + .await + .expect("reference request should succeed") + .expect("resource references should be found"); + assert_eq!(references.len(), 3); + assert_eq!( + references + .iter() + .filter(|location| location.uri.path().ends_with("/config/widgets.yaml")) + .count(), + 2 + ); + assert_eq!( + references + .iter() + .filter(|location| location.uri.path().ends_with("/config/widgets.xml")) + .count(), + 1 + ); + + let lenses = backend + .code_lens(CodeLensParams { + text_document: TextDocumentIdentifier { uri: php_uri }, + work_done_progress_params: WorkDoneProgressParams::default(), + partial_result_params: PartialResultParams::default(), + }) + .await + .expect("code lens request should succeed") + .expect("class reference lens should be present"); + let lens = lenses + .into_iter() + .find(|lens| lens.range.start.line == 2 && lens.data.is_some()) + .expect("class declaration should have an unresolved reference lens"); + let resolved = backend + .code_lens_resolve(lens) + .await + .expect("class reference lens should resolve"); + assert_eq!( + resolved + .command + .as_ref() + .map(|command| command.title.as_str()), + Some("3 references") + ); +} + +#[tokio::test] +async fn resource_class_members_feed_find_references_and_code_lens() { + let php = concat!( + " Date: Tue, 25 Aug 2026 23:40:31 +0200 Subject: [PATCH 10/10] feat(php): Map transparent proxies to real classes Discover opt-in generated subclasses by path and marker interface, then canonicalize external metadata without changing PHP type resolution. --- config-schema.json | 25 + docs/ARCHITECTURE.md | 1 + docs/CHANGELOG.md | 1 + docs/configuration.md | 18 + src/config.rs | 44 ++ src/indexing/preload.rs | 4 + src/indexing/watch.rs | 26 + src/lib.rs | 10 + src/proxy_metadata.rs | 448 ++++++++++++++++++ src/reference_index.rs | 13 +- src/references/classes.rs | 8 +- src/resource_navigation.rs | 46 +- src/server.rs | 15 + .../integration/definition_resource_files.rs | 85 ++++ 14 files changed, 736 insertions(+), 8 deletions(-) create mode 100644 src/proxy_metadata.rs diff --git a/config-schema.json b/config-schema.json index 0efd65239..faa46b309 100644 --- a/config-schema.json +++ b/config-schema.json @@ -13,6 +13,31 @@ "type": "string", "description": "Override the detected PHP version (e.g. \"8.3\"). When unset, PHPantom infers from composer.json's platform or require.php.", "pattern": "^\\d+\\.\\d+(\\.\\d+)?$" + }, + "proxies": { + "type": "array", + "description": "Generated transparent-proxy discovery rules. Matching subclasses keep their PHP type, while project metadata is attributed to their real parent class.", + "items": { + "type": "object", + "properties": { + "paths": { + "type": "array", + "description": "Workspace-relative PHP files, directories, or glob patterns to scan for generated proxy subclasses.", + "items": { + "type": "string" + } + }, + "marker-interface": { + "type": "string", + "description": "Fully-qualified interface that a generated subclass must directly implement to be treated as a transparent proxy." + } + }, + "required": [ + "paths", + "marker-interface" + ] + }, + "default": [] } } }, diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 91f295fd6..7055d2fb4 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -67,6 +67,7 @@ src/ │ │ # Class & type resolution ├── resolution.rs # Multi-phase class/function lookup across files (find_or_load_class) +├── proxy_metadata.rs # Transparent proxy → real-class relations for metadata consumers ├── class_lookup.rs # Subtype checks (is_subtype_of_typed) and class-lookup helpers ├── inheritance/ # Parent/trait/mixin member merging, generics substitution ├── virtual_members/ # Synthesized members: phpdoc.rs (@method/@property/@mixin) + laravel/ (one file per Eloquent/framework feature) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 5422610fd..65d78deec 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - **Fully-qualified PHP classes navigate from YAML and XML.** Ctrl+Click a class name in any YAML key or value, or any XML attribute or text node, and PHPantom opens its PHP declaration without needing to know that file's schema. `Class::member` references navigate too. The same occurrences feed Find References and declaration CodeLens through the workspace reference index. Unknown and unqualified strings are left alone. Contributed by @sidux. +- **Generated transparent proxies can be mapped back to their real classes.** Configure opt-in proxy paths and a marker interface under `[[php.proxies]]`; metadata read from YAML or XML then bubbles navigation, references, and member links to the real parent class without changing normal PHP type resolution. Contributed by @sidux. - **Reference CodeLens.** PHP declarations show clickable exact reference counts. Declarations with no indexed uses are answered immediately, while semantic member locations are cached in a bounded background index so opening a large file does not fan out into an expensive resolve request per lens. Clients that support CodeLens refresh receive only ready, fully resolved member lenses. Contributed by @sidux. - **`analyze` takes more than one path.** `phpantom_lsp analyze app/ lib/Helper.php tests/` scans the union of everything named, mixing directories and single files freely, so a pre-commit hook or a CI step can hand it exactly the paths that changed instead of running the whole project or invoking the binary once per path. Overlapping arguments are reported once, and a path that does not exist still stops the run with exit code 2. Naming no path scans the entire project, as before. diff --git a/docs/configuration.md b/docs/configuration.md index 3eac6b620..53a4bb0eb 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -52,6 +52,24 @@ The full schema is at [`config-schema.json`](https://github.com/PHPantom-dev/php | --------- | ------ | --------------------------- | ----------- | | `version` | string | Inferred from composer.json | Override the detected PHP version (e.g. `"8.3"`). | +#### `[[php.proxies]]` + +Declare generated transparent-proxy subclasses so metadata found on the +generated class is attributed to its real parent class. PHPantom scans only +the listed workspace-relative files, directories, or globs. A class must +directly implement `marker-interface`; an ordinary subclass in the same path +is left alone. + +```toml +[[php.proxies]] +paths = ["var/cache/*/generated-proxies/*.php"] +marker-interface = 'ProxyManager\Proxy\AccessInterceptorValueHolderInterface' +``` + +This does not replace the proxy class in PHP type resolution. It gives project +metadata features one shared relation to the parent class; YAML/XML navigation +uses that relation directly. + ### `[diagnostics]` | Key | Type | Default | Description | diff --git a/src/config.rs b/src/config.rs index 9d62dbd9b..90622d174 100644 --- a/src/config.rs +++ b/src/config.rs @@ -146,6 +146,23 @@ pub struct PhpConfig { /// Override the detected PHP version (e.g. `"8.3"`). /// When `None`, PHPantom infers from `composer.json`. pub version: Option, + /// Generated transparent-proxy class rules. + /// + /// Each rule scans opt-in workspace-relative paths for subclasses that + /// directly implement a marker interface. Metadata attached to the + /// generated subclass is then attributed to its parent class. + pub proxies: Vec, +} + +/// One `[[php.proxies]]` transparent-proxy discovery rule. +#[derive(Debug, Clone, Default, Deserialize, PartialEq, Eq)] +#[serde(default)] +pub struct PhpProxyConfig { + /// Workspace-relative PHP files, directories, or glob patterns to scan. + pub paths: Vec, + /// Interface that proves a generated subclass is a transparent proxy. + #[serde(rename = "marker-interface")] + pub marker_interface: String, } /// `[diagnostics]` section — toggle individual diagnostic providers. @@ -810,6 +827,7 @@ mod tests { fn default_content_parses_successfully() { let config: Config = toml::from_str(DEFAULT_CONFIG_CONTENT).unwrap(); assert!(config.php.version.is_none()); + assert!(config.php.proxies.is_empty()); assert!(!config.diagnostics.unresolved_member_access_enabled()); assert!(!config.diagnostics.extra_arguments_enabled()); assert!(!config.diagnostics.report_magic_properties_enabled()); @@ -847,6 +865,7 @@ mod tests { let dir = tempfile::tempdir().unwrap(); let config = load_config(dir.path()).unwrap(); assert!(config.php.version.is_none()); + assert!(config.php.proxies.is_empty()); assert!(!config.diagnostics.unresolved_member_access_enabled()); assert!(!config.diagnostics.extra_arguments_enabled()); assert!(!config.diagnostics.report_magic_properties_enabled()); @@ -870,6 +889,7 @@ mod tests { std::fs::write(&path, "").unwrap(); let config = load_config(dir.path()).unwrap(); assert!(config.php.version.is_none()); + assert!(config.php.proxies.is_empty()); assert!(!config.diagnostics.unresolved_member_access_enabled()); assert!(!config.diagnostics.extra_arguments_enabled()); assert!(!config.diagnostics.report_magic_properties_enabled()); @@ -894,6 +914,30 @@ mod tests { assert_eq!(config.php.version.as_deref(), Some("8.3")); } + #[test] + fn parses_transparent_proxy_rules() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join(CONFIG_FILE_NAME); + std::fs::write( + &path, + r#" +[[php.proxies]] +paths = ["var/cache/*/proxies/*.php"] +marker-interface = 'Acme\Proxy\TransparentProxy' +"#, + ) + .unwrap(); + + let config = load_config(dir.path()).unwrap(); + assert_eq!( + config.php.proxies, + vec![PhpProxyConfig { + paths: vec!["var/cache/*/proxies/*.php".to_string()], + marker_interface: "Acme\\Proxy\\TransparentProxy".to_string(), + }] + ); + } + #[test] fn parses_diagnostics_section() { let dir = tempfile::tempdir().unwrap(); diff --git a/src/indexing/preload.rs b/src/indexing/preload.rs index a58f2a60a..8fd1fd8c6 100644 --- a/src/indexing/preload.rs +++ b/src/indexing/preload.rs @@ -284,6 +284,7 @@ impl Backend { let phase1_uri_set: HashSet<&str> = phase1_uris.iter().map(|uri| uri.as_str()).collect(); let (phase2_work, resource_work) = if let Some(root) = workspace_root.clone() { let vendor_dir_paths = self.workspace.vendor_dir_paths.lock().clone(); + let proxy_rules = self.config().php.proxies; self.report_workspace_index_progress(progress, 3, "Scanning workspace files"); let walk_start = std::time::Instant::now(); @@ -302,6 +303,9 @@ impl Backend { let php_work = php_files .into_iter() .filter_map(|path| { + if crate::proxy_metadata::is_configured_proxy_path(&root, &path, &proxy_rules) { + return None; + } let uri = crate::util::path_to_uri(&path); if existing_uris.contains(&uri) || phase1_uri_set.contains(uri.as_str()) { None diff --git a/src/indexing/watch.rs b/src/indexing/watch.rs index 4073e6aeb..c9816aa4b 100644 --- a/src/indexing/watch.rs +++ b/src/indexing/watch.rs @@ -49,6 +49,7 @@ impl Backend { ) -> bool { let mut composer_changed = false; let mut config_changed = false; + let mut proxy_index_rebuild = false; let mut schema_full_rebuild = false; let mut migration_changes: Vec<(PathBuf, FileChangeType)> = Vec::new(); let mut php_changes: Vec<(String, PathBuf, FileChangeType)> = Vec::new(); @@ -56,6 +57,7 @@ impl Backend { let mut migration_discovery = crate::virtual_members::laravel::database_schema::MigrationDiscovery::default(); let is_laravel = self.resolved_class_cache.read().is_laravel(); + let proxy_rules = self.config().php.proxies; let config_path = root.join(crate::config::CONFIG_FILE_NAME); { let open = self.open_files.read(); @@ -136,6 +138,14 @@ impl Backend { continue; }; + // Generated proxies are opt-in metadata inputs, not ordinary + // project classes. Rebuild their small relation index rather + // than parsing them into the workspace symbol maps. + if crate::proxy_metadata::is_configured_proxy_path(root, &file_path, &proxy_rules) { + proxy_index_rebuild = true; + continue; + } + if change.typ == FileChangeType::CHANGED { // `parsed_uris` records the editor URI for open files and // the canonical `file://` URI for lazily loaded ones; @@ -156,6 +166,7 @@ impl Backend { && resource_changes.is_empty() && !composer_changed && !config_changed + && !proxy_index_rebuild && !schema_full_rebuild && migration_changes.is_empty() { @@ -165,6 +176,7 @@ impl Backend { if config_changed { tracing::info!("PHPantom: .phpantom.toml changed, reloading configuration"); self.reload_config(root); + proxy_index_rebuild = true; // Schema/migration settings live in the same file, and the // cheapest correct response to "something in here changed" is // the same full rebuild a config/database.php or schema file @@ -196,6 +208,12 @@ impl Backend { self.rescan_composer_indexes(root); } + if proxy_index_rebuild { + let count = self.rebuild_configured_proxy_index(root); + tracing::info!("PHPantom: indexed {} transparent proxies", count); + self.refresh_indexed_resource_symbols(); + } + if !resource_changes.is_empty() { tracing::info!( "PHPantom: {} watched YAML/XML file(s) changed on disk, refreshing references", @@ -294,6 +312,14 @@ impl Backend { last_modified = modified; tracing::info!("PHPantom: global config changed, reloading configuration"); self.reload_config(&root); + let proxy_backend = self.clone_for_blocking(); + let proxy_root = root.clone(); + crate::server::run_blocking_cancel_safe("reload_php_proxies", move || { + let count = proxy_backend.rebuild_configured_proxy_index(&proxy_root); + proxy_backend.refresh_indexed_resource_symbols(); + count + }) + .await; } } } diff --git a/src/lib.rs b/src/lib.rs index e5c88316e..6aefd47f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -261,6 +261,7 @@ mod phpstan; pub(crate) mod phpstan_ignore; pub(crate) mod process; pub mod progress; +mod proxy_metadata; mod reference_counts; mod reference_index; mod references; @@ -568,6 +569,12 @@ pub struct Backend { /// candidate files, then run their existing semantic checks for aliases, /// inheritance, Laravel declarations, and `self/static/parent`. pub(crate) reference_index: reference_index::ReferenceIndex, + /// Transparent proxy-to-real-class relations for metadata consumers. + /// + /// Generated proxies remain valid PHP subclasses in the type engine, + /// while events, external references, and lenses can be attributed to the + /// class the proxy represents at runtime. + pub(crate) proxy_index: Arc>, /// Skip building [`reference_index`] from `update_ast`. /// /// Set by [`Backend::new_headless`] for the `analyze`/`fix` CLI @@ -1083,6 +1090,7 @@ impl Backend { open_files: Arc::new(RwLock::new(HashMap::new())), symbol_maps: Arc::new(RwLock::new(HashMap::new())), reference_index: reference_index::new_reference_index(), + proxy_index: Arc::new(RwLock::new(proxy_metadata::ProxyIndex::default())), skip_reference_index: false, symbols: SymbolIndex::new(), workspace: WorkspaceEnv::new(), @@ -1194,6 +1202,7 @@ impl Backend { open_files: Arc::new(RwLock::new(HashMap::new())), symbol_maps: Arc::new(RwLock::new(HashMap::new())), reference_index: reference_index::new_reference_index(), + proxy_index: Arc::new(RwLock::new(proxy_metadata::ProxyIndex::default())), skip_reference_index: false, symbols: SymbolIndex::new(), workspace: WorkspaceEnv::new_isolated(), @@ -1848,6 +1857,7 @@ impl Backend { open_files: Arc::clone(&self.open_files), symbol_maps: Arc::clone(&self.symbol_maps), reference_index: Arc::clone(&self.reference_index), + proxy_index: Arc::clone(&self.proxy_index), skip_reference_index: self.skip_reference_index, symbols: self.symbols.clone(), parse_errors: Arc::clone(&self.parse_errors), diff --git a/src/proxy_metadata.rs b/src/proxy_metadata.rs new file mode 100644 index 000000000..e64fa191f --- /dev/null +++ b/src/proxy_metadata.rs @@ -0,0 +1,448 @@ +//! Transparent PHP proxy relations used by project metadata. +//! +//! The type engine still sees generated proxy subclasses as the classes they +//! actually declare. Metadata consumers use this module when a proxy is only +//! a runtime wrapper and annotations, events, references, or lenses should be +//! attributed to the wrapped parent class instead. + +use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet}; +use std::path::{Component, Path, PathBuf}; + +use globset::Glob; +use ignore::WalkBuilder; + +use crate::Backend; +use crate::config::PhpProxyConfig; + +const CONFIG_SOURCE: &str = "php-config"; +const MAX_PROXY_DEPTH: usize = 32; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub(crate) struct ProxyRelation { + pub proxy_fqn: String, + pub target_fqn: String, +} + +#[derive(Debug, Clone, Default)] +pub(crate) struct ProxyIndex { + sources: BTreeMap>, + targets: HashMap, + families: HashMap>, +} + +impl ProxyIndex { + fn replace_source(&mut self, source: String, relations: Vec) { + if relations.is_empty() { + self.sources.remove(&source); + } else { + self.sources.insert(source, relations); + } + self.rebuild_targets(); + } + + fn rebuild_targets(&mut self) { + self.targets.clear(); + for relations in self.sources.values() { + for relation in relations { + let proxy = normalize_class_name(&relation.proxy_fqn); + let target = normalize_class_name(&relation.target_fqn); + if proxy.is_empty() || target.is_empty() || proxy.eq_ignore_ascii_case(&target) { + continue; + } + self.targets.insert( + class_key(&proxy), + ProxyRelation { + proxy_fqn: proxy, + target_fqn: target, + }, + ); + } + } + + let mut families: HashMap> = HashMap::new(); + for relation in self.targets.values() { + if let Some(target) = self.canonical_target(&relation.proxy_fqn) { + families + .entry(class_key(&target)) + .or_default() + .push(relation.proxy_fqn.clone()); + } + } + for proxies in families.values_mut() { + proxies.sort_by_key(|name| name.to_ascii_lowercase()); + proxies.dedup_by(|left, right| left.eq_ignore_ascii_case(right)); + } + self.families = families; + } + + fn canonical_target(&self, class_fqn: &str) -> Option { + let original = normalize_class_name(class_fqn); + let mut current = original.clone(); + let mut seen = HashSet::with_capacity(4); + let mut changed = false; + + for _ in 0..MAX_PROXY_DEPTH { + let key = class_key(¤t); + if !seen.insert(key.clone()) { + return None; + } + let Some(relation) = self.targets.get(&key) else { + return changed.then_some(current); + }; + current.clone_from(&relation.target_fqn); + changed = true; + } + + None + } + + fn class_family(&self, class_fqn: &str) -> Vec { + let canonical = self + .canonical_target(class_fqn) + .unwrap_or_else(|| normalize_class_name(class_fqn)); + let proxies = self.families.get(&class_key(&canonical)); + let mut family = Vec::with_capacity(proxies.map_or(1, |proxies| proxies.len() + 1)); + family.push(canonical); + if let Some(proxies) = proxies { + family.extend(proxies.iter().cloned()); + } + family + } + + fn len(&self) -> usize { + self.targets.len() + } +} + +impl Backend { + /// Replace the proxy relations contributed by one metadata adapter. + /// + /// `source` is stable adapter identity (usually a generated file URI), so + /// refreshing one adapter cannot discard relations found by another. + pub(crate) fn replace_proxy_relations( + &self, + source: impl Into, + relations: Vec, + ) { + self.proxy_index + .write() + .replace_source(source.into(), relations); + } + + /// Return the real class and every transparent proxy that represents it. + pub(crate) fn metadata_class_family(&self, class_fqn: &str) -> Vec { + self.proxy_index.read().class_family(class_fqn) + } + + /// Rebuild relations discovered from `[[php.proxies]]` rules. + pub(crate) fn rebuild_configured_proxy_index(&self, workspace_root: &Path) -> usize { + let rules = self.config().php.proxies; + let mut relations = Vec::new(); + + for rule in &rules { + if rule.marker_interface.trim().is_empty() { + continue; + } + for path in collect_rule_files(workspace_root, rule) { + relations.extend(self.proxy_relations_in_file(&path, rule)); + } + } + + self.replace_proxy_relations(CONFIG_SOURCE, relations); + self.proxy_index.read().len() + } + + fn proxy_relations_in_file(&self, path: &Path, rule: &PhpProxyConfig) -> Vec { + let Ok(content) = std::fs::read_to_string(path) else { + return Vec::new(); + }; + let marker = normalize_class_name(&rule.marker_interface); + + Self::parse_php_versioned_with_namespaces(&content, None) + .into_iter() + .filter_map(|(class, namespace)| { + let implements_marker = class.interfaces.iter().any(|interface| { + normalize_class_name(interface.as_str()).eq_ignore_ascii_case(&marker) + }); + if !implements_marker { + return None; + } + + let target = normalize_class_name(class.parent_class?.as_str()); + if target.is_empty() { + return None; + } + let proxy_fqn = match namespace { + Some(namespace) if !namespace.is_empty() => { + format!("{}\\{}", namespace, class.name) + } + _ => class.name.to_string(), + }; + Some(ProxyRelation { + proxy_fqn, + target_fqn: target, + }) + }) + .collect() + } +} + +/// Whether a changed path belongs to an opt-in proxy discovery rule. +pub(crate) fn is_configured_proxy_path( + workspace_root: &Path, + path: &Path, + rules: &[PhpProxyConfig], +) -> bool { + let Ok(relative) = path.strip_prefix(workspace_root) else { + return false; + }; + rules.iter().any(|rule| { + rule.paths + .iter() + .any(|spec| path_matches_spec(relative, spec)) + }) +} + +fn collect_rule_files(workspace_root: &Path, rule: &PhpProxyConfig) -> Vec { + let mut files = BTreeSet::new(); + for spec in &rule.paths { + let Some(relative) = safe_relative_path(spec) else { + continue; + }; + + if has_glob_meta(spec) { + let Ok(glob) = Glob::new(spec) else { + tracing::warn!("PHPantom: invalid proxy path glob: {}", spec); + continue; + }; + let matcher = glob.compile_matcher(); + let base = workspace_root.join(fixed_glob_prefix(&relative)); + collect_php_files( + &base, + |path| { + path.strip_prefix(workspace_root) + .is_ok_and(|relative| matcher.is_match(relative)) + }, + &mut files, + ); + continue; + } + + let absolute = workspace_root.join(relative); + if absolute.is_file() { + if is_php_file(&absolute) { + files.insert(absolute); + } + } else if absolute.is_dir() { + collect_php_files(&absolute, |_| true, &mut files); + } + } + files.into_iter().collect() +} + +fn collect_php_files(root: &Path, matches: impl Fn(&Path) -> bool, files: &mut BTreeSet) { + if !root.exists() { + return; + } + let walker = WalkBuilder::new(root) + .git_ignore(false) + .git_global(false) + .git_exclude(false) + .hidden(false) + .parents(false) + .ignore(false) + .follow_links(false) + .build(); + + for entry in walker.filter_map(Result::ok) { + let path = entry.path(); + if entry.file_type().is_some_and(|kind| kind.is_file()) + && is_php_file(path) + && matches(path) + { + files.insert(path.to_path_buf()); + } + } +} + +fn path_matches_spec(relative: &Path, spec: &str) -> bool { + let Some(spec_path) = safe_relative_path(spec) else { + return false; + }; + if has_glob_meta(spec) { + return Glob::new(spec) + .ok() + .is_some_and(|glob| glob.compile_matcher().is_match(relative)); + } + relative == spec_path || relative.starts_with(spec_path) +} + +fn safe_relative_path(spec: &str) -> Option { + let path = Path::new(spec.trim()); + if path.as_os_str().is_empty() + || path.is_absolute() + || path.components().any(|component| { + matches!( + component, + Component::ParentDir | Component::RootDir | Component::Prefix(_) + ) + }) + { + return None; + } + Some(path.to_path_buf()) +} + +fn fixed_glob_prefix(path: &Path) -> PathBuf { + path.components() + .take_while(|component| match component { + Component::Normal(part) => !has_glob_meta(&part.to_string_lossy()), + _ => false, + }) + .collect() +} + +fn has_glob_meta(value: &str) -> bool { + value + .bytes() + .any(|byte| matches!(byte, b'*' | b'?' | b'[' | b'{')) +} + +fn is_php_file(path: &Path) -> bool { + path.extension() + .and_then(|extension| extension.to_str()) + .is_some_and(|extension| extension.eq_ignore_ascii_case("php")) +} + +fn normalize_class_name(name: &str) -> String { + name.trim().trim_start_matches('\\').to_string() +} + +fn class_key(name: &str) -> String { + name.to_ascii_lowercase() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn canonicalizes_chains_and_builds_class_families() { + let mut index = ProxyIndex::default(); + index.replace_source( + "generated".to_string(), + vec![ + ProxyRelation { + proxy_fqn: "Generated\\Outer".to_string(), + target_fqn: "Generated\\Inner".to_string(), + }, + ProxyRelation { + proxy_fqn: "Generated\\Inner".to_string(), + target_fqn: "App\\Service".to_string(), + }, + ], + ); + + assert_eq!( + index.canonical_target("generated\\OUTER").as_deref(), + Some("App\\Service") + ); + assert_eq!( + index.class_family("App\\Service"), + vec![ + "App\\Service".to_string(), + "Generated\\Inner".to_string(), + "Generated\\Outer".to_string(), + ] + ); + } + + #[test] + fn isolates_adapter_sources_and_rejects_cycles() { + let mut index = ProxyIndex::default(); + index.replace_source( + "one".to_string(), + vec![ProxyRelation { + proxy_fqn: "Generated\\One".to_string(), + target_fqn: "App\\One".to_string(), + }], + ); + index.replace_source( + "two".to_string(), + vec![ProxyRelation { + proxy_fqn: "Generated\\Two".to_string(), + target_fqn: "App\\Two".to_string(), + }], + ); + index.replace_source("one".to_string(), Vec::new()); + + assert_eq!(index.canonical_target("Generated\\One"), None); + assert_eq!( + index.canonical_target("Generated\\Two").as_deref(), + Some("App\\Two") + ); + + index.replace_source( + "cycle".to_string(), + vec![ + ProxyRelation { + proxy_fqn: "Cycle\\A".to_string(), + target_fqn: "Cycle\\B".to_string(), + }, + ProxyRelation { + proxy_fqn: "Cycle\\B".to_string(), + target_fqn: "Cycle\\A".to_string(), + }, + ], + ); + assert_eq!(index.canonical_target("Cycle\\A"), None); + } + + #[test] + fn scans_only_marked_proxy_subclasses() { + let backend = Backend::new_test(); + let dir = tempfile::tempdir().unwrap(); + let proxy = dir.path().join("Proxy.php"); + std::fs::write( + &proxy, + r#" { if *is_fqn && crate::resource_navigation::is_resource_document(uri) { - return vec![( - ReferenceIndexKey::class_owned(normalize_symbol_name(name)), - true, - )]; + let mut seen = HashSet::new(); + return self + .metadata_class_family(name) + .into_iter() + .filter_map(|name| { + let key = ReferenceIndexKey::class_owned(name); + seen.insert(key.clone()).then_some((key, true)) + }) + .collect(); } let resolved = if *is_fqn { normalize_symbol_name(name) diff --git a/src/references/classes.rs b/src/references/classes.rs index 8a21341cd..1259e884c 100644 --- a/src/references/classes.rs +++ b/src/references/classes.rs @@ -46,7 +46,13 @@ impl Backend { let file_namespace = self.first_file_namespace(file_uri); let file_use_map = std::cell::OnceCell::new(); let class_matches = |resolved: &str| { - class_names_match(strip_fqn_prefix(resolved), target, target_short) + if crate::resource_navigation::is_resource_document(file_uri) { + self.metadata_class_family(resolved) + .iter() + .any(|name| name.eq_ignore_ascii_case(target)) + } else { + class_names_match(strip_fqn_prefix(resolved), target, target_short) + } }; // First pass: resolved-name check to avoid unnecessary content work. diff --git a/src/resource_navigation.rs b/src/resource_navigation.rs index bf4018bd7..5da0ef98c 100644 --- a/src/resource_navigation.rs +++ b/src/resource_navigation.rs @@ -67,11 +67,17 @@ impl Backend { position: Position, ) -> Option { match symbol_at(content, position)? { - ResourceSymbol::Class(fqn) => self.class_declaration_location(&fqn), + ResourceSymbol::Class(fqn) => self + .metadata_class_family(&fqn) + .iter() + .find_map(|target| self.class_declaration_location(target)), ResourceSymbol::Member { class_fqn, member_name, - } => self.class_member_declaration_location(&class_fqn, &member_name), + } => self + .metadata_class_family(&class_fqn) + .iter() + .find_map(|target| self.class_member_declaration_location(target, &member_name)), } } @@ -107,6 +113,35 @@ impl Backend { self.reindex_references_for_symbol_maps_batch(maps); } + /// Rebuild already-indexed resource maps after proxy configuration changes. + pub(crate) fn refresh_indexed_resource_symbols(&self) { + let uris: Vec = self + .symbol_maps + .read() + .keys() + .filter(|uri| is_resource_document(uri)) + .cloned() + .collect(); + let maps: Vec<(String, Arc)> = uris + .into_iter() + .filter_map(|uri| { + let content = self.get_file_content(&uri)?; + Some((uri, Arc::new(self.resource_symbol_map(&content)))) + }) + .collect(); + if maps.is_empty() { + return; + } + + { + let mut symbol_maps = self.symbol_maps.write(); + for (uri, map) in &maps { + symbol_maps.insert(uri.clone(), Arc::clone(map)); + } + } + self.reindex_references_for_symbol_maps_batch(maps); + } + fn resource_symbol_map(&self, content: &str) -> SymbolMap { let mut spans = Vec::new(); for symbol in scan_symbols(content) { @@ -121,11 +156,16 @@ impl Backend { }); if let Some((member_name, member_start, member_end)) = symbol.member { + let canonical_class = self + .metadata_class_family(&symbol.class_fqn) + .into_iter() + .next() + .unwrap_or(symbol.class_fqn); spans.push(SymbolSpan { start: member_start as u32, end: member_end as u32, kind: SymbolKind::MemberAccess { - subject_text: SubjectText::owned(symbol.class_fqn), + subject_text: SubjectText::owned(canonical_class), member_name: atom(&member_name), is_static: false, is_method_call: true, diff --git a/src/server.rs b/src/server.rs index 260e00783..189b25d48 100644 --- a/src/server.rs +++ b/src/server.rs @@ -417,6 +417,21 @@ impl LanguageServer for Backend { } } + // Generated transparent proxies live in opt-in cache/build paths + // that normal project indexing may ignore. Read their declarations + // into the metadata relation index; they do not enter the type + // engine or the workspace class map. + let proxy_backend = self.clone_for_blocking(); + let proxy_root = root.clone(); + let proxy_count = run_blocking_cancel_safe("index_php_proxies", move || { + proxy_backend.rebuild_configured_proxy_index(&proxy_root) + }) + .await + .unwrap_or(0); + if proxy_count > 0 { + tracing::info!("PHPantom: indexed {} transparent proxies", proxy_count); + } + // Laravel-only startup work. The project classification is // set by the init pass above from composer.json, so it has to // run after it: a Symfony workspace must never pay for the diff --git a/tests/integration/definition_resource_files.rs b/tests/integration/definition_resource_files.rs index 113dfc13c..d62212d35 100644 --- a/tests/integration/definition_resource_files.rs +++ b/tests/integration/definition_resource_files.rs @@ -314,3 +314,88 @@ async fn unknown_and_unqualified_names_do_not_navigate() { .is_none() ); } + +#[tokio::test] +async fn transparent_proxy_metadata_navigates_to_the_real_class() { + let php = concat!( + "