hierarchy (file:line per instance, peek), find (every declaration by name, refs + unresolved mentions), outline, read, check, rename, port, edit_item, format. Name lookup reaches every declaration the symbol tables hold (tools/declared.rs); rename of a generate-block label is refused (engine gap filed as topo §2.9 MCP-14). Text-only results, engine sentences made root-relative. Tests ported or deleted per the README; QA lane in tests/qa_m7.rs. Docs, smoke gate, release notes (0.58.0 unreleased) updated. Co-Authored-By: Claude Fable 5.1 <[email protected]>
231 lines
8.6 KiB
Rust
231 lines
8.6 KiB
Rust
//! `outline unit:` — what a unit offers the code that uses it: a module's
|
|
//! or interface's parameters and ports in declaration order (plus an
|
|
//! interface's modports), a package's or class's members — without the
|
|
//! agent reading the file that declares it.
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use vlog_parser::ast::{Item, ItemKind};
|
|
use vlog_parser::span::TextRange;
|
|
use vlog_refactor::{instantiation_plan, InstantiationPlan};
|
|
use vlog_semantic::{Def, ScopeId, Semantics, Symbol, SymbolKind};
|
|
|
|
use super::read_item::child_items;
|
|
use crate::project::{Analysis, Project};
|
|
use crate::render;
|
|
|
|
/// A unit found in the index: where it is, and what it is.
|
|
struct Found {
|
|
path: PathBuf,
|
|
def: Def,
|
|
}
|
|
|
|
/// The unit's `file:line`, then its interface, one fact per line.
|
|
pub fn unit_outline(project: &mut Project, name: &str) -> Result<Vec<String>, String> {
|
|
let mut found = candidates(project, name);
|
|
if found.is_empty() && project.ensure_walked_for_miss() {
|
|
found = candidates(project, name);
|
|
}
|
|
let found = pick(project, found, name)?;
|
|
let analysis = project.analysis(&found.path)?;
|
|
let head = format!("{}:{}", project.relative(&found.path), found.def.lines.name);
|
|
let short = name.rsplit("::").next().unwrap_or(name);
|
|
match found.def.kind {
|
|
SymbolKind::Module | SymbolKind::Interface => instantiable(&analysis, &found, short, head),
|
|
_ => Ok(members(project, &analysis, &found, short, head)),
|
|
}
|
|
}
|
|
|
|
/// Every indexed unit declaration of `name` (`pkg::cls` for a class).
|
|
fn candidates(project: &Project, name: &str) -> Vec<Found> {
|
|
let index = project.index();
|
|
let unit_kinds =
|
|
[SymbolKind::Module, SymbolKind::Interface, SymbolKind::Package, SymbolKind::Class];
|
|
let mut defs: Vec<Def> = match name.split_once("::") {
|
|
Some((package, class)) => index.member(package, class).to_vec(),
|
|
None => index.units(name).to_vec(),
|
|
};
|
|
defs.retain(|def| unit_kinds.contains(&def.kind));
|
|
defs.into_iter()
|
|
.filter_map(|def| Some(Found { path: index.path(def.file)?.to_path_buf(), def }))
|
|
.collect()
|
|
}
|
|
|
|
/// The one declaration among `found`, or a refusal that says why there is
|
|
/// not exactly one.
|
|
fn pick(project: &Project, mut found: Vec<Found>, name: &str) -> Result<Found, String> {
|
|
match found.len() {
|
|
0 => {
|
|
let similar = super::names::similar_units(project.index(), name, 10);
|
|
let near = match similar.is_empty() {
|
|
true => String::new(),
|
|
false => format!("; similar: {}", similar.join(", ")),
|
|
};
|
|
Err(format!("no module, interface, package or class named `{name}`{near}."))
|
|
}
|
|
1 => Ok(found.remove(0)),
|
|
_ => {
|
|
let sites: Vec<String> = found
|
|
.iter()
|
|
.map(|f| format!("{}:{}", project.relative(&f.path), f.def.lines.name))
|
|
.collect();
|
|
Err(format!(
|
|
"`{name}` is declared {} times ({}); outline one `file` instead.",
|
|
sites.len(),
|
|
sites.join(", ")
|
|
))
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A module's or interface's parameters, ports and modports.
|
|
fn instantiable(
|
|
analysis: &Analysis,
|
|
found: &Found,
|
|
name: &str,
|
|
head: String,
|
|
) -> Result<Vec<String>, String> {
|
|
let plan = instantiation_plan(name, &analysis.text, None)?;
|
|
let scope = unit_symbol(&analysis.semantics, found, name).and_then(|s| s.child_scope);
|
|
let mut lines = vec![head];
|
|
lines.extend(plan.params.iter().map(|p| match p.default.trim() {
|
|
"" => format!("param {}", p.name),
|
|
default => format!("param {} = {}", p.name, render::one_line(default)),
|
|
}));
|
|
lines.extend(port_lines(&plan, analysis, scope));
|
|
if let (SymbolKind::Interface, Some(scope)) = (found.def.kind, scope) {
|
|
lines.extend(modports_of(analysis, scope));
|
|
}
|
|
render::warnings(&mut lines, &plan.warnings);
|
|
Ok(lines)
|
|
}
|
|
|
|
/// An interface's modports, each as its declaration reads on one line,
|
|
/// led by the keyword.
|
|
fn modports_of(analysis: &Analysis, scope: ScopeId) -> Vec<String> {
|
|
analysis
|
|
.semantics
|
|
.symbols()
|
|
.iter()
|
|
.filter(|s| s.scope == scope && s.kind == SymbolKind::Modport)
|
|
.map(|s| render::one_line(analysis.slice(s.decl_span.start, s.decl_span.end)))
|
|
.map(|written| match written.starts_with("modport") {
|
|
true => written,
|
|
false => format!("modport {written}"),
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// The unit's own symbol in the file's analysis: the declaration at the
|
|
/// indexed name, else the first of its kind spelling the name.
|
|
fn unit_symbol<'s>(semantics: &'s Semantics, found: &Found, name: &str) -> Option<&'s Symbol> {
|
|
let mut candidates = semantics
|
|
.symbols_named(name)
|
|
.filter_map(|id| semantics.symbol(id))
|
|
.filter(|s| s.kind == found.def.kind);
|
|
let first = candidates.next()?;
|
|
Some(
|
|
std::iter::once(first)
|
|
.chain(candidates)
|
|
.find(|s| s.name_span == found.def.name_span)
|
|
.unwrap_or(first),
|
|
)
|
|
}
|
|
|
|
/// Each port as `dir type [range] name [unpacked]`, the empty parts left
|
|
/// out.
|
|
fn port_lines(
|
|
plan: &InstantiationPlan,
|
|
analysis: &Analysis,
|
|
scope: Option<ScopeId>,
|
|
) -> Vec<String> {
|
|
plan.ports
|
|
.iter()
|
|
.map(|port| {
|
|
let unpacked =
|
|
scope.map_or(String::new(), |scope| unpacked(analysis, scope, &port.name));
|
|
let parts = [port.dir.as_str(), port.ty.trim(), port.name.as_str(), unpacked.as_str()];
|
|
let parts: Vec<&str> = parts.into_iter().filter(|part| !part.is_empty()).collect();
|
|
render::one_line(&parts.join(" "))
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// The `[..]` groups written right after an ANSI port's name. A non-ANSI
|
|
/// port (completed in the body) answers nothing here.
|
|
fn unpacked(analysis: &Analysis, scope: ScopeId, name: &str) -> String {
|
|
let Some(port) = analysis.semantics.symbols_named(name).find_map(|id| {
|
|
let s = analysis.semantics.symbol(id)?;
|
|
(s.scope == scope && s.kind == SymbolKind::Port && s.detail_span.is_none()).then_some(s)
|
|
}) else {
|
|
return String::new();
|
|
};
|
|
let mut rest = analysis.slice(port.name_span.end, port.decl_span.end).trim_start();
|
|
let mut out = String::new();
|
|
while rest.starts_with('[') {
|
|
let mut depth = 0;
|
|
let Some(end) = rest.char_indices().find_map(|(at, c)| {
|
|
depth += match c {
|
|
'[' => 1,
|
|
']' => -1,
|
|
_ => 0,
|
|
};
|
|
(depth == 0).then_some(at)
|
|
}) else {
|
|
break;
|
|
};
|
|
out.push_str(&render::one_line(&rest[..=end]));
|
|
rest = rest[end + 1..].trim_start();
|
|
}
|
|
out
|
|
}
|
|
|
|
/// A package's or class's members, in declaration order, as `kind name
|
|
/// LINE`, and a class's base. `name` is the unit's own name, without a
|
|
/// package prefix — which two classes in two packages may share, so both
|
|
/// answers are read off this declaration: a member counts only when it is
|
|
/// declared inside `found.def.decl_span`, and the base is what this
|
|
/// declaration's own `extends` writes.
|
|
fn members(
|
|
project: &Project,
|
|
analysis: &Analysis,
|
|
found: &Found,
|
|
name: &str,
|
|
head: String,
|
|
) -> Vec<String> {
|
|
let index = project.index();
|
|
let within = found.def.decl_span;
|
|
let mut rows: Vec<(u32, &'static str, String)> = index
|
|
.members_of(name)
|
|
.flat_map(|(member, defs)| {
|
|
defs.iter()
|
|
.filter(|def| index.path(def.file) == Some(found.path.as_path()))
|
|
.filter(|def| {
|
|
within.start <= def.name_span.start && def.name_span.end <= within.end
|
|
})
|
|
.map(move |def| (def.lines.name, render::kind(def.kind), member.to_string()))
|
|
})
|
|
.collect();
|
|
rows.sort();
|
|
let mut lines = vec![head];
|
|
if let Some(base) = class_base(&analysis.tree.items, found.def.name_span) {
|
|
lines.push(format!("extends {base}"));
|
|
}
|
|
lines.extend(rows.iter().map(|(line, kind, member)| format!("{kind} {member} {line}")));
|
|
lines
|
|
}
|
|
|
|
/// The base the class declared at `name_span` names in its `extends`, read
|
|
/// off the file's own tree; `None` for a class without one (or a package).
|
|
fn class_base(items: &[Item], name_span: TextRange) -> Option<String> {
|
|
let item = items
|
|
.iter()
|
|
.find(|item| item.span.start <= name_span.start && name_span.end <= item.span.end)?;
|
|
if let ItemKind::Class(class) = &item.kind {
|
|
if class.name_span == name_span {
|
|
return class.extends.as_ref().map(|extends| extends.name.clone());
|
|
}
|
|
}
|
|
child_items(&item.kind).into_iter().find_map(|items| class_base(items, name_span))
|
|
}
|