|
2 | 2 | /// |
3 | 3 | /// This module contains the logic for constructing LSP `CompletionItem`s from |
4 | 4 | /// resolved `ClassInfo`, filtered by the `AccessKind` (arrow, double-colon, |
5 | | -/// or parent double-colon). |
| 5 | +/// or parent double-colon), as well as class name completion when no member |
| 6 | +/// access operator is present. |
| 7 | +use std::collections::{HashMap, HashSet}; |
| 8 | + |
6 | 9 | use tower_lsp::lsp_types::*; |
7 | 10 |
|
8 | 11 | use crate::Backend; |
@@ -248,4 +251,189 @@ impl Backend { |
248 | 251 |
|
249 | 252 | items |
250 | 253 | } |
| 254 | + |
| 255 | + // ─── Class name completion ────────────────────────────────────────── |
| 256 | + |
| 257 | + /// Extract the partial identifier (class name fragment) that the user |
| 258 | + /// is currently typing at the given cursor position. |
| 259 | + /// |
| 260 | + /// Walks backward from the cursor through alphanumeric characters, |
| 261 | + /// underscores, and backslashes (namespace separators). Returns |
| 262 | + /// `None` if the resulting text starts with `$` (variable context) |
| 263 | + /// or is empty. |
| 264 | + pub fn extract_partial_class_name(content: &str, position: Position) -> Option<String> { |
| 265 | + let lines: Vec<&str> = content.lines().collect(); |
| 266 | + if position.line as usize >= lines.len() { |
| 267 | + return None; |
| 268 | + } |
| 269 | + |
| 270 | + let line = lines[position.line as usize]; |
| 271 | + let chars: Vec<char> = line.chars().collect(); |
| 272 | + let col = (position.character as usize).min(chars.len()); |
| 273 | + |
| 274 | + // Walk backwards through identifier characters (including `\`) |
| 275 | + let mut i = col; |
| 276 | + while i > 0 |
| 277 | + && (chars[i - 1].is_alphanumeric() || chars[i - 1] == '_' || chars[i - 1] == '\\') |
| 278 | + { |
| 279 | + i -= 1; |
| 280 | + } |
| 281 | + |
| 282 | + if i == col { |
| 283 | + // Nothing typed — no partial identifier |
| 284 | + return None; |
| 285 | + } |
| 286 | + |
| 287 | + // If preceded by `$`, this is a variable, not a class name |
| 288 | + if i > 0 && chars[i - 1] == '$' { |
| 289 | + return None; |
| 290 | + } |
| 291 | + |
| 292 | + // If preceded by `->` or `::`, member completion handles this |
| 293 | + if i >= 2 && chars[i - 2] == '-' && chars[i - 1] == '>' { |
| 294 | + return None; |
| 295 | + } |
| 296 | + if i >= 2 && chars[i - 2] == ':' && chars[i - 1] == ':' { |
| 297 | + return None; |
| 298 | + } |
| 299 | + |
| 300 | + let partial: String = chars[i..col].iter().collect(); |
| 301 | + if partial.is_empty() { |
| 302 | + return None; |
| 303 | + } |
| 304 | + |
| 305 | + Some(partial) |
| 306 | + } |
| 307 | + |
| 308 | + /// Build completion items for class names from all known sources. |
| 309 | + /// |
| 310 | + /// Sources (in priority order): |
| 311 | + /// 1. Classes imported via `use` statements in the current file |
| 312 | + /// 2. Classes in the same namespace (from the ast_map) |
| 313 | + /// 3. Built-in PHP classes from embedded stubs |
| 314 | + /// 4. Classes from the Composer classmap (`autoload_classmap.php`) |
| 315 | + /// 5. Classes from the class_index (discovered during parsing) |
| 316 | + /// |
| 317 | + /// Each item uses the short class name as `label` and the |
| 318 | + /// fully-qualified name as `detail`. Items are deduplicated by FQN. |
| 319 | + pub(crate) fn build_class_name_completions( |
| 320 | + &self, |
| 321 | + file_use_map: &HashMap<String, String>, |
| 322 | + file_namespace: &Option<String>, |
| 323 | + ) -> Vec<CompletionItem> { |
| 324 | + let mut seen_fqns: HashSet<String> = HashSet::new(); |
| 325 | + let mut items: Vec<CompletionItem> = Vec::new(); |
| 326 | + |
| 327 | + // ── 1. Use-imported classes (highest priority) ────────────── |
| 328 | + for (short_name, fqn) in file_use_map { |
| 329 | + if !seen_fqns.insert(fqn.clone()) { |
| 330 | + continue; |
| 331 | + } |
| 332 | + items.push(CompletionItem { |
| 333 | + label: short_name.clone(), |
| 334 | + kind: Some(CompletionItemKind::CLASS), |
| 335 | + detail: Some(fqn.clone()), |
| 336 | + insert_text: Some(short_name.clone()), |
| 337 | + filter_text: Some(short_name.clone()), |
| 338 | + sort_text: Some(format!("0_{}", short_name.to_lowercase())), |
| 339 | + ..CompletionItem::default() |
| 340 | + }); |
| 341 | + } |
| 342 | + |
| 343 | + // ── 2. Same-namespace classes (from ast_map) ──────────────── |
| 344 | + if let Some(ns) = file_namespace |
| 345 | + && let Ok(nmap) = self.namespace_map.lock() |
| 346 | + { |
| 347 | + // Find all URIs that share the same namespace |
| 348 | + let same_ns_uris: Vec<String> = nmap |
| 349 | + .iter() |
| 350 | + .filter_map(|(uri, opt_ns)| { |
| 351 | + if opt_ns.as_deref() == Some(ns.as_str()) { |
| 352 | + Some(uri.clone()) |
| 353 | + } else { |
| 354 | + None |
| 355 | + } |
| 356 | + }) |
| 357 | + .collect(); |
| 358 | + drop(nmap); |
| 359 | + |
| 360 | + if let Ok(amap) = self.ast_map.lock() { |
| 361 | + for uri in &same_ns_uris { |
| 362 | + if let Some(classes) = amap.get(uri) { |
| 363 | + for cls in classes { |
| 364 | + let fqn = format!("{}\\{}", ns, cls.name); |
| 365 | + if !seen_fqns.insert(fqn.clone()) { |
| 366 | + continue; |
| 367 | + } |
| 368 | + items.push(CompletionItem { |
| 369 | + label: cls.name.clone(), |
| 370 | + kind: Some(CompletionItemKind::CLASS), |
| 371 | + detail: Some(fqn), |
| 372 | + insert_text: Some(cls.name.clone()), |
| 373 | + filter_text: Some(cls.name.clone()), |
| 374 | + sort_text: Some(format!("1_{}", cls.name.to_lowercase())), |
| 375 | + ..CompletionItem::default() |
| 376 | + }); |
| 377 | + } |
| 378 | + } |
| 379 | + } |
| 380 | + } |
| 381 | + } |
| 382 | + |
| 383 | + // ── 3. Built-in PHP classes from stubs ────────────────────── |
| 384 | + for &name in self.stub_index.keys() { |
| 385 | + if !seen_fqns.insert(name.to_string()) { |
| 386 | + continue; |
| 387 | + } |
| 388 | + items.push(CompletionItem { |
| 389 | + label: name.to_string(), |
| 390 | + kind: Some(CompletionItemKind::CLASS), |
| 391 | + detail: Some(name.to_string()), |
| 392 | + insert_text: Some(name.to_string()), |
| 393 | + filter_text: Some(name.to_string()), |
| 394 | + sort_text: Some(format!("2_{}", name.to_lowercase())), |
| 395 | + ..CompletionItem::default() |
| 396 | + }); |
| 397 | + } |
| 398 | + |
| 399 | + // ── 4. Composer classmap ──────────────────────────────────── |
| 400 | + if let Ok(cmap) = self.classmap.lock() { |
| 401 | + for fqn in cmap.keys() { |
| 402 | + if !seen_fqns.insert(fqn.clone()) { |
| 403 | + continue; |
| 404 | + } |
| 405 | + let short_name = fqn.rsplit('\\').next().unwrap_or(fqn); |
| 406 | + items.push(CompletionItem { |
| 407 | + label: short_name.to_string(), |
| 408 | + kind: Some(CompletionItemKind::CLASS), |
| 409 | + detail: Some(fqn.clone()), |
| 410 | + insert_text: Some(short_name.to_string()), |
| 411 | + filter_text: Some(short_name.to_string()), |
| 412 | + sort_text: Some(format!("3_{}", short_name.to_lowercase())), |
| 413 | + ..CompletionItem::default() |
| 414 | + }); |
| 415 | + } |
| 416 | + } |
| 417 | + |
| 418 | + // ── 5. class_index (discovered classes) ───────────────────── |
| 419 | + if let Ok(idx) = self.class_index.lock() { |
| 420 | + for fqn in idx.keys() { |
| 421 | + if !seen_fqns.insert(fqn.clone()) { |
| 422 | + continue; |
| 423 | + } |
| 424 | + let short_name = fqn.rsplit('\\').next().unwrap_or(fqn); |
| 425 | + items.push(CompletionItem { |
| 426 | + label: short_name.to_string(), |
| 427 | + kind: Some(CompletionItemKind::CLASS), |
| 428 | + detail: Some(fqn.clone()), |
| 429 | + insert_text: Some(short_name.to_string()), |
| 430 | + filter_text: Some(short_name.to_string()), |
| 431 | + sort_text: Some(format!("3_{}", short_name.to_lowercase())), |
| 432 | + ..CompletionItem::default() |
| 433 | + }); |
| 434 | + } |
| 435 | + } |
| 436 | + |
| 437 | + items |
| 438 | + } |
251 | 439 | } |
0 commit comments