//! Finding class declarations in a parse tree — one walk, two readers. //! //! `type_hierarchy.rs` wants every class in a file so it can build a hierarchy //! item for the one the cursor names; the workspace index wants the same walk //! so it can record who extends whom as it builds //! (`docs/features/incremental-index/README.md`). One walk here, so the census //! the index holds and the declaration the hierarchy shows can never disagree. //! //! **Lifted from `lucisv_server::type_hierarchy` on 2026-08-27**, unchanged by //! the move. use vlog_parser::ast::{ClassDecl, Item, ItemKind}; use vlog_parser::TextRange; /// One class declaration: the node, and the extent of the whole /// `class ... endclass` it was written as. #[derive(Clone, Copy)] pub struct Decl<'a> { /// The declaration node, for its name and its `extends` clause. pub class: &'a ClassDecl, /// Code extent of the declaration, attributes included — what a type /// hierarchy item's `range` is. pub span: TextRange, } /// Every class declaration in `items`, wherever a class may be written: at file /// scope, inside a `package`, inside a `module`, and nested inside another /// class. pub fn declarations(items: &[Item]) -> Vec> { let mut out = Vec::new(); collect(items, &mut out); out } /// The base class one declaration names, as written — `None` when the header /// has no `extends`. /// /// The name is the bare base: `extends base #(8)` reads `base`, because the /// parser keeps the parameter overrides on their own field, so a parameterized /// base needs no unwrapping here. pub fn extends_name(class: &ClassDecl) -> Option<&str> { class.extends.as_ref().map(|extends| extends.name.as_str()) } fn collect<'a>(items: &'a [Item], out: &mut Vec>) { for item in items { match &item.kind { ItemKind::Class(class) => { out.push(Decl { class, span: item.span }); collect(&class.items, out); } ItemKind::Package { items, .. } => collect(items, out), ItemKind::Module(module) => collect(&module.items, out), _ => {} } } } #[cfg(test)] mod tests { use super::*; use vlog_parser::parse_source_tolerant; /// Every class name a file declares, in walk order, with its base. fn declared(text: &str) -> Vec<(String, String)> { let (tree, _) = parse_source_tolerant(text); declarations(&tree.items) .iter() .map(|decl| { (decl.class.name.clone(), extends_name(decl.class).unwrap_or("-").to_string()) }) .collect() } #[test] fn a_class_is_found_wherever_it_may_be_written() { let text = "\ class top extends base; class nested extends top; endclass endclass package p; class in_pkg extends top; endclass endpackage module m; class in_mod; endclass endmodule "; assert_eq!( declared(text), [ ("top".to_string(), "base".to_string()), ("nested".to_string(), "top".to_string()), ("in_pkg".to_string(), "top".to_string()), ("in_mod".to_string(), "-".to_string()), ] ); } #[test] fn a_parameterized_base_names_the_bare_class() { assert_eq!( declared("class d extends b #(8);\nendclass\n"), [("d".to_string(), "b".to_string())] ); } #[test] fn a_declarations_extent_contains_its_own_name() { let text = "class first;\nendclass\n\nclass second;\nendclass\n"; let (tree, _) = parse_source_tolerant(text); let decls = declarations(&tree.items); assert_eq!(decls.len(), 2); for decl in &decls { assert!(decl.span.start <= decl.class.name_span.start); assert!(decl.class.name_span.end <= decl.span.end); } } }