textDocument/references now reads declaration-to-occurrence postings merged in WorkspaceIndex from compact per-file reference facts, with open buffers overlaying live semantics — 0.062 ms/request over 820 files vs 21.7 ms for the old Workspace::sources() walk (~350x). The walk stays as rename's engine and the equivalence oracle in tests. FACTS_FORMAT 1->2 discards v1 caches whole. Cursor lookups (symbol_at/reference_at/token_at) and macro-define lookup move to binary-searched sorted tables; include definition inspects only the cursor's line. Ambiguity refusals preserved throughout. External dev: codex. Lead-reviewed, gates re-run, probe reproduced. Co-Authored-By: Claude Fable 5 <[email protected]>
716 lines
30 KiB
Rust
716 lines
30 KiB
Rust
//! `textDocument/hover`.
|
|
//!
|
|
//! Four things can be under the cursor, tried in this order: a macro usage, a
|
|
//! reference resolved in this file, a reference the workspace answered, and a
|
|
//! declaration itself. The order and the rendering rules are in
|
|
//! `docs/features/lucisv-server/README.md`.
|
|
//!
|
|
//! A macro card also shows what the hovered use expands to in one step, when
|
|
//! that is more than the body it already prints — `crate::expand` and
|
|
//! `docs/features/macro-expansion/README.md`.
|
|
|
|
use lsp_types::{Hover, HoverContents, MarkupContent, MarkupKind, Uri};
|
|
use vlog_parser::defines::Define;
|
|
use vlog_parser::token::Token;
|
|
use vlog_parser::TextRange;
|
|
use vlog_semantic::{Definition, FileId, Symbol};
|
|
|
|
use crate::expand::{self, Expansion};
|
|
use crate::locate::{display_name, line_number};
|
|
use crate::state::{Document, Workspace};
|
|
use crate::symbols::kind_label;
|
|
|
|
/// Hover text for the cursor at `offset`.
|
|
pub fn hover(ws: &Workspace, uri: &Uri, offset: u32) -> Option<Hover> {
|
|
let doc = ws.document(uri)?;
|
|
macro_hover(ws, doc, offset).or_else(|| symbol_hover(ws, doc, offset))
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// `define expansion
|
|
// ----------------------------------------------------------------------
|
|
|
|
/// A macro usage under the cursor: the file's own definition wins, then the
|
|
/// one it reaches through its `` `include``s, then the workspace's — **all**
|
|
/// of the workspace's, because outside the include graph picking one would be
|
|
/// a guess.
|
|
fn macro_hover(ws: &Workspace, doc: &Document, offset: u32) -> Option<Hover> {
|
|
let token = doc.token_at(offset).filter(|token| token.is_macro())?;
|
|
let name = macro_name(token)?;
|
|
|
|
if let Some(define) = doc.defines.latest(name) {
|
|
return Some(markdown(doc, define_text(name, define, None, Some(&token.text)), token.span));
|
|
}
|
|
|
|
let candidates = narrowed_to_the_include_graph(ws, doc, ws.index().find_define(name));
|
|
if candidates.is_empty() {
|
|
return Some(markdown(doc, format!("`` `{name} `` — no definition found"), token.span));
|
|
}
|
|
// The expansion follows the definition the card shows, so it is offered
|
|
// only where the card shows exactly one: with several candidates listed,
|
|
// expanding one of them would be picking a winner the lookup refused to
|
|
// pick.
|
|
let call = (candidates.len() == 1).then_some(token.text.as_str());
|
|
let mut parts = Vec::with_capacity(candidates.len());
|
|
for (file, define) in &candidates {
|
|
let where_ =
|
|
ws.index().path(*file).map(|path| match line_number(ws, path, define.span.start) {
|
|
Some(line) => format!("{}:{line}", display_name(path)),
|
|
None => display_name(path),
|
|
});
|
|
parts.push(define_text(name, define, where_, call));
|
|
}
|
|
let joined = if parts.len() == 1 {
|
|
parts.remove(0)
|
|
} else {
|
|
format!(
|
|
"`` `{name} `` is defined {} times\n\n---\n\n{}",
|
|
parts.len(),
|
|
parts.join("\n\n---\n\n")
|
|
)
|
|
};
|
|
Some(markdown(doc, joined, token.span))
|
|
}
|
|
|
|
/// The one definition this file actually *reaches*, when there is exactly
|
|
/// one, and every candidate otherwise.
|
|
///
|
|
/// The include graph is the only thing that can order two definitions of one
|
|
/// macro name, so where it has an unambiguous answer the hover shows that
|
|
/// answer alone. Where it does not — nothing reachable defines the name, or
|
|
/// several reachable files do — the honest all-candidates list from before
|
|
/// this feature stands: an empty include graph must never make hover worse
|
|
/// than it was (`docs/features/include-nav/README.md`).
|
|
fn narrowed_to_the_include_graph<'a>(
|
|
ws: &Workspace,
|
|
doc: &Document,
|
|
candidates: Vec<(FileId, &'a Define)>,
|
|
) -> Vec<(FileId, &'a Define)> {
|
|
if candidates.len() < 2 {
|
|
return candidates;
|
|
}
|
|
let reachable = crate::includes::reachable_from(ws, doc);
|
|
if reachable.is_empty() {
|
|
return candidates;
|
|
}
|
|
let narrowed: Vec<(FileId, &Define)> = candidates
|
|
.iter()
|
|
.filter(|(file, _)| ws.index().path(*file).is_some_and(|p| reachable.contains(p)))
|
|
.cloned()
|
|
.collect();
|
|
match narrowed.len() {
|
|
1 => narrowed,
|
|
_ => candidates,
|
|
}
|
|
}
|
|
|
|
/// The macro's name: the identifier after the backtick, argument list excluded.
|
|
fn macro_name(token: &Token) -> Option<&str> {
|
|
let rest = token.text.strip_prefix('`')?;
|
|
let end = rest.find(|c: char| !c.is_ascii_alphanumeric() && c != '_' && c != '$');
|
|
let name = match end {
|
|
Some(end) => &rest[..end],
|
|
None => rest,
|
|
};
|
|
if name.is_empty() {
|
|
None
|
|
} else {
|
|
Some(name)
|
|
}
|
|
}
|
|
|
|
/// One definition, rendered: its signature, its body, where it came from, and
|
|
/// — when `call` is the macro use under the cursor — what that use expands to
|
|
/// in one step.
|
|
fn define_text(name: &str, define: &Define, origin: Option<String>, call: Option<&str>) -> String {
|
|
let signature = match &define.params {
|
|
Some(params) => format!("`define {name}({})", params.join(", ")),
|
|
None => format!("`define {name}"),
|
|
};
|
|
let mut out = String::new();
|
|
if let Some(origin) = origin {
|
|
out.push_str(&format!("defined in `{origin}`\n\n"));
|
|
}
|
|
out.push_str("```systemverilog\n");
|
|
out.push_str(&signature);
|
|
if !define.body.is_empty() {
|
|
out.push(' ');
|
|
out.push_str(&define.body);
|
|
}
|
|
out.push_str("\n```");
|
|
if let Some(expansion) = call.and_then(|call| expand::single_step(call, define)) {
|
|
out.push_str(&expansion_text(&expansion));
|
|
}
|
|
out
|
|
}
|
|
|
|
/// The "expands to" half of a macro hover card: this call's arguments
|
|
/// substituted into the body, fenced exactly like the definition above it.
|
|
///
|
|
/// Two footnotes, both there so the card cannot be misread as a preprocessor's
|
|
/// output: what a remaining macro use means, and where a long body stopped.
|
|
fn expansion_text(expansion: &Expansion) -> String {
|
|
let mut out = String::from("\n\n**expands to**\n\n```systemverilog\n");
|
|
out.push_str(&expansion.text);
|
|
out.push_str("\n```");
|
|
if expand::has_macro_use(&expansion.text) {
|
|
out.push_str("\n\n*one step: a nested macro use is left as written*");
|
|
}
|
|
if expansion.truncated {
|
|
out.push_str(&format!("\n\n*truncated at {} characters*", expand::CAP));
|
|
}
|
|
out
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// declarations and references
|
|
// ----------------------------------------------------------------------
|
|
|
|
/// A reference or a declaration under the cursor.
|
|
fn symbol_hover(ws: &Workspace, doc: &Document, offset: u32) -> Option<Hover> {
|
|
let range = hovered_name_span(doc, offset)?;
|
|
match doc.external.definition_of(&doc.semantics, offset)? {
|
|
Definition::Local(symbol) => Some(markdown(doc, local_text(doc, symbol), range)),
|
|
Definition::External(defs) => {
|
|
let mut parts = Vec::with_capacity(defs.len());
|
|
for def in defs {
|
|
let label = kind_label(def.kind);
|
|
let where_ = ws
|
|
.index()
|
|
.path(def.file)
|
|
.map(|path| match line_number(ws, path, def.name_span.start) {
|
|
Some(line) => format!("{}:{line}", display_name(path)),
|
|
None => display_name(path),
|
|
})
|
|
.unwrap_or_else(|| "the workspace".to_string());
|
|
parts.push(format!("**{label}** — declared in `{where_}`"));
|
|
}
|
|
Some(markdown(doc, parts.join("\n\n"), range))
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A local declaration: what it is, and the lines it was written on.
|
|
fn local_text(doc: &Document, symbol: &Symbol) -> String {
|
|
let snippet = doc.positions().whole_lines(symbol.decl_span);
|
|
format!(
|
|
"**{}** `{}`\n\n```systemverilog\n{}\n```",
|
|
kind_label(symbol.kind),
|
|
symbol.name,
|
|
snippet.trim_end()
|
|
)
|
|
}
|
|
|
|
/// The span the hover card should highlight: the name under the cursor.
|
|
fn hovered_name_span(doc: &Document, offset: u32) -> Option<TextRange> {
|
|
if let Some(reference) = doc.semantics.reference_at(offset) {
|
|
return Some(reference.name_span);
|
|
}
|
|
doc.semantics.symbol_at(offset).and_then(|id| doc.semantics.symbol(id)).map(|s| s.name_span)
|
|
}
|
|
|
|
/// A markdown hover card highlighting `span` in the hovered document.
|
|
fn markdown(doc: &Document, value: String, span: TextRange) -> Hover {
|
|
Hover {
|
|
contents: HoverContents::Markup(MarkupContent { kind: MarkupKind::Markdown, value }),
|
|
range: Some(doc.positions().range(span)),
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::uri::path_to_uri;
|
|
use std::fs;
|
|
use std::path::PathBuf;
|
|
use std::str::FromStr;
|
|
|
|
fn document(text: &str) -> (Workspace, Uri) {
|
|
let uri = Uri::from_str("file:///rtl/m.sv").expect("uri");
|
|
let mut ws = Workspace::default();
|
|
ws.set_document(uri.clone(), text.to_string());
|
|
(ws, uri)
|
|
}
|
|
|
|
fn at(text: &str, needle: &str) -> u32 {
|
|
text.find(needle).expect("needle present") as u32
|
|
}
|
|
|
|
fn text_of(hover: &Hover) -> String {
|
|
match &hover.contents {
|
|
HoverContents::Markup(m) => m.value.clone(),
|
|
other => panic!("expected markup, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
fn temp_dir(tag: &str) -> PathBuf {
|
|
let dir =
|
|
std::env::temp_dir().join(format!("lucisv_server_hover_{tag}_{}", std::process::id()));
|
|
let _ = fs::remove_dir_all(&dir);
|
|
fs::create_dir_all(&dir).expect("temp dir");
|
|
dir
|
|
}
|
|
|
|
#[test]
|
|
fn hovering_a_macro_usage_shows_its_body() {
|
|
let text = "`define WIDTH 8\nmodule m;\n wire [`WIDTH-1:0] a;\nendmodule\n";
|
|
let (ws, uri) = document(text);
|
|
let hover = hover(&ws, &uri, at(text, "[`WIDTH") + 2).expect("hover");
|
|
let body = text_of(&hover);
|
|
// The fence tag is load-bearing: it is the language id the extension
|
|
// registers its TextMate grammar for, and the only reason a hover card
|
|
// renders highlighted. Pinned here so a rename cannot pass silently.
|
|
assert!(body.contains("```systemverilog\n`define WIDTH 8\n```"), "got {body}");
|
|
}
|
|
|
|
#[test]
|
|
fn hovering_a_function_like_macro_shows_its_signature() {
|
|
let text = "`define MAX(a, b) ((a) > (b) ? (a) : (b))\nmodule m;\n wire x = `MAX(1, 2);\nendmodule\n";
|
|
let (ws, uri) = document(text);
|
|
let hover = hover(&ws, &uri, at(text, "`MAX(1")).expect("hover");
|
|
let body = text_of(&hover);
|
|
assert!(body.contains("`define MAX(a, b)"), "got {body}");
|
|
assert!(body.contains("? (a) : (b)"), "got {body}");
|
|
}
|
|
|
|
#[test]
|
|
fn hovering_a_call_shows_what_it_expands_to_in_one_step() {
|
|
let text = "`define MAX(a, b) ((a) > (b) ? (a) : (b))\nmodule m;\n wire x = `MAX(p, q+1);\nendmodule\n";
|
|
let (ws, uri) = document(text);
|
|
let body = text_of(&hover(&ws, &uri, at(text, "`MAX(p")).expect("hover"));
|
|
// The definition stays, and the expansion is added under it — same
|
|
// fence tag, since that is what colours a hover card (0.4.1).
|
|
assert!(body.contains("```systemverilog\n`define MAX(a, b) ((a) > (b) ? (a) : (b))\n```"));
|
|
assert!(
|
|
body.contains("**expands to**\n\n```systemverilog\n((p) > (q+1) ? (p) : (q+1))\n```"),
|
|
"got {body}"
|
|
);
|
|
assert!(!body.contains("one step:"), "nothing left unexpanded here: got {body}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_plain_object_like_macro_gets_no_second_section() {
|
|
// Its expansion *is* its body, which the card already shows.
|
|
let text = "`define WIDTH 8\nmodule m;\n wire [`WIDTH-1:0] a;\nendmodule\n";
|
|
let (ws, uri) = document(text);
|
|
let body = text_of(&hover(&ws, &uri, at(text, "[`WIDTH") + 2).expect("hover"));
|
|
assert!(!body.contains("expands to"), "got {body}");
|
|
}
|
|
|
|
#[test]
|
|
fn an_object_like_macro_that_calls_another_shows_where_one_step_stopped() {
|
|
let text =
|
|
"`define BASE 4\n`define W `BASE + 1\nmodule m;\n wire [`W-1:0] a;\nendmodule\n";
|
|
let (ws, uri) = document(text);
|
|
let body = text_of(&hover(&ws, &uri, at(text, "[`W-") + 1).expect("hover"));
|
|
assert!(body.contains("**expands to**\n\n```systemverilog\n`BASE + 1\n```"), "got {body}");
|
|
assert!(body.contains("*one step: a nested macro use is left as written*"), "got {body}");
|
|
assert!(!body.contains("`define BASE 4"), "one step, not two: got {body}");
|
|
}
|
|
|
|
#[test]
|
|
fn a_call_whose_argument_count_is_wrong_keeps_todays_card_and_guesses_nothing() {
|
|
let text = "`define MAX(a, b) ((a) > (b) ? (a) : (b))\nmodule m;\n wire x = `MAX(p);\nendmodule\n";
|
|
let (ws, uri) = document(text);
|
|
let body = text_of(&hover(&ws, &uri, at(text, "`MAX(p")).expect("hover"));
|
|
assert!(body.contains("`define MAX(a, b)"), "the card is unchanged: got {body}");
|
|
assert!(!body.contains("expands to"), "got {body}");
|
|
}
|
|
|
|
#[test]
|
|
fn an_argument_may_carry_commas_and_strings_of_its_own() {
|
|
let text = "`define SHOW(a, b) $display(a, b)\nmodule m;\n initial `SHOW(\"x, y\", f(1, 2));\nendmodule\n";
|
|
let (ws, uri) = document(text);
|
|
let body = text_of(&hover(&ws, &uri, at(text, "`SHOW(\"")).expect("hover"));
|
|
assert!(
|
|
body.contains("```systemverilog\n$display(\"x, y\", f(1, 2))\n```"),
|
|
"the commas inside the string and the call did not split it: got {body}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_long_body_is_truncated_with_a_note() {
|
|
let filler = "z".repeat(crate::expand::CAP + 100);
|
|
let text =
|
|
format!("`define BIG(a) a + {filler}\nmodule m;\n wire x = `BIG(1);\nendmodule\n");
|
|
let (ws, uri) = document(&text);
|
|
let body = text_of(&hover(&ws, &uri, at(&text, "`BIG(1")).expect("hover"));
|
|
assert!(
|
|
body.contains(" …\n```"),
|
|
"cut with an ellipsis: got {}",
|
|
&body[..200.min(body.len())]
|
|
);
|
|
assert!(
|
|
body.contains(&format!("*truncated at {} characters*", crate::expand::CAP)),
|
|
"and says so"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn the_expansion_follows_the_definition_the_card_narrowed_to() {
|
|
// M10's include narrowing picks `a.svh`; the expansion must be that
|
|
// definition's, not the other file's.
|
|
let text = "`include \"a.svh\"\nmodule top;\n wire x = `M(7);\nendmodule\n";
|
|
let (dir, ws, uri) = included(
|
|
"expandreach",
|
|
&[
|
|
("a.svh", "`define M(x) x + 1\n"),
|
|
("b.svh", "`define M(x) x + 2\n"),
|
|
("top.sv", text),
|
|
],
|
|
);
|
|
let body = text_of(&hover(&ws, &uri, at(text, "`M(7")).expect("hover"));
|
|
assert!(body.contains("a.svh:1"), "got {body}");
|
|
assert!(body.contains("**expands to**\n\n```systemverilog\n7 + 1\n```"), "got {body}");
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn several_candidate_definitions_expand_nothing() {
|
|
let text = "module top;\n wire x = `M(7);\nendmodule\n";
|
|
let (dir, ws, uri) = included(
|
|
"expandambiguous",
|
|
&[
|
|
("a.svh", "`define M(x) x + 1\n"),
|
|
("b.svh", "`define M(x) x + 2\n"),
|
|
("top.sv", text),
|
|
],
|
|
);
|
|
let body = text_of(&hover(&ws, &uri, at(text, "`M(7")).expect("hover"));
|
|
assert!(body.contains("is defined 2 times"), "got {body}");
|
|
assert!(!body.contains("expands to"), "no winner to expand: got {body}");
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn an_undefined_macro_says_so_rather_than_returning_nothing() {
|
|
let text = "module m;\n wire x = `NOPE;\nendmodule\n";
|
|
let (ws, uri) = document(text);
|
|
let hover = hover(&ws, &uri, at(text, "`NOPE")).expect("hover");
|
|
assert!(text_of(&hover).contains("no definition found"));
|
|
}
|
|
|
|
#[test]
|
|
fn a_workspace_define_is_found_when_this_file_does_not_define_it() {
|
|
let dir = temp_dir("define");
|
|
fs::write(dir.join("defs.svh"), "`define WIDTH 8\n").expect("write");
|
|
let top_path = dir.join("top.sv");
|
|
let top_text = "module top;\n wire [`WIDTH-1:0] a;\nendmodule\n";
|
|
fs::write(&top_path, top_text).expect("write");
|
|
|
|
let mut ws = Workspace::default();
|
|
ws.open_whole_workspace(vec![dir.clone()]);
|
|
let uri = path_to_uri(&top_path).expect("uri");
|
|
ws.set_document(uri.clone(), top_text.to_string());
|
|
|
|
let hover = hover(&ws, &uri, at(top_text, "`WIDTH")).expect("hover");
|
|
let body = text_of(&hover);
|
|
assert!(body.contains("`define WIDTH 8"), "got {body}");
|
|
assert!(body.contains("defs.svh:1"), "the file and line: got {body}");
|
|
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn several_workspace_definitions_are_all_listed() {
|
|
let dir = temp_dir("dupdefine");
|
|
fs::write(dir.join("a.svh"), "`define W 8\n").expect("write");
|
|
fs::write(dir.join("b.svh"), "`define W 16\n").expect("write");
|
|
let top_path = dir.join("top.sv");
|
|
let top_text = "module top;\n wire [`W-1:0] a;\nendmodule\n";
|
|
fs::write(&top_path, top_text).expect("write");
|
|
|
|
let mut ws = Workspace::default();
|
|
ws.open_whole_workspace(vec![dir.clone()]);
|
|
let uri = path_to_uri(&top_path).expect("uri");
|
|
ws.set_document(uri.clone(), top_text.to_string());
|
|
|
|
let body = text_of(&hover(&ws, &uri, at(top_text, "`W-")).expect("hover"));
|
|
assert!(body.contains("is defined 2 times"), "got {body}");
|
|
assert!(body.contains("`define W 8"), "got {body}");
|
|
assert!(body.contains("`define W 16"), "got {body}");
|
|
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
/// A workspace whose files are `files`, with `top.sv` opened.
|
|
fn included(tag: &str, files: &[(&str, &str)]) -> (PathBuf, Workspace, Uri) {
|
|
let dir = temp_dir(tag);
|
|
for (name, text) in files {
|
|
fs::write(dir.join(name), text).expect("write");
|
|
}
|
|
let mut ws = Workspace::default();
|
|
ws.open_whole_workspace(vec![dir.clone()]);
|
|
let top = dir.join("top.sv");
|
|
let uri = path_to_uri(&top).expect("uri");
|
|
ws.set_document(uri.clone(), fs::read_to_string(&top).expect("read"));
|
|
(dir, ws, uri)
|
|
}
|
|
|
|
/// A `top.sv` using `` `W ``, with whatever include lines precede it.
|
|
fn top_using_w(includes: &str) -> String {
|
|
format!("{includes}module top;\n wire [`W-1:0] a;\nendmodule\n")
|
|
}
|
|
|
|
#[test]
|
|
fn one_hop_of_include_picks_the_definition_the_file_actually_reaches() {
|
|
// Two files define `W`; only one of them is included. Before the
|
|
// include graph this hover listed both and refused to choose.
|
|
let (dir, ws, uri) = included(
|
|
"reach1",
|
|
&[
|
|
("a.svh", "`define W 8\n"),
|
|
("b.svh", "`define W 16\n"),
|
|
("top.sv", &top_using_w("`include \"a.svh\"\n")),
|
|
],
|
|
);
|
|
let body = text_of(
|
|
&hover(&ws, &uri, at(&top_using_w("`include \"a.svh\"\n"), "`W-")).expect("hover"),
|
|
);
|
|
assert!(body.contains("`define W 8"), "the reachable one: got {body}");
|
|
assert!(!body.contains("`define W 16"), "and not the other: got {body}");
|
|
assert!(body.contains("a.svh:1"), "named with its origin: got {body}");
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn two_hops_of_include_reach_just_as_far() {
|
|
let text = top_using_w("`include \"mid.svh\"\n");
|
|
let (dir, ws, uri) = included(
|
|
"reach2",
|
|
&[
|
|
("mid.svh", "`include \"deep.svh\"\n"),
|
|
("deep.svh", "`define W 8\n"),
|
|
("elsewhere.svh", "`define W 16\n"),
|
|
("top.sv", &text),
|
|
],
|
|
);
|
|
let body = text_of(&hover(&ws, &uri, at(&text, "`W-")).expect("hover"));
|
|
assert!(body.contains("`define W 8") && body.contains("deep.svh:1"), "got {body}");
|
|
assert!(!body.contains("`define W 16"), "got {body}");
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn an_include_cycle_terminates_and_both_files_stay_reachable() {
|
|
let text = top_using_w("`include \"a.svh\"\n");
|
|
let (dir, ws, uri) = included(
|
|
"reachcycle",
|
|
&[
|
|
("a.svh", "`include \"b.svh\"\n"),
|
|
("b.svh", "`include \"a.svh\"\n`define W 8\n"),
|
|
("elsewhere.svh", "`define W 16\n"),
|
|
("top.sv", &text),
|
|
],
|
|
);
|
|
let body = text_of(&hover(&ws, &uri, at(&text, "`W-")).expect("hover"));
|
|
assert!(body.contains("`define W 8") && body.contains("b.svh:2"), "got {body}");
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn a_definition_nothing_includes_is_still_shown_when_it_is_all_there_is() {
|
|
// The fallback that must not regress: an unreachable definition is
|
|
// better than none, and a project that does not use `` `include`` at
|
|
// all sees exactly what it saw before this feature.
|
|
let text = top_using_w("");
|
|
let (dir, ws, uri) =
|
|
included("reachnone", &[("far.svh", "`define W 8\n"), ("top.sv", &text)]);
|
|
let body = text_of(&hover(&ws, &uri, at(&text, "`W-")).expect("hover"));
|
|
assert!(body.contains("`define W 8") && body.contains("far.svh:1"), "got {body}");
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn two_definitions_neither_of_them_reachable_are_still_both_listed() {
|
|
let text = top_using_w("`include \"empty.svh\"\n");
|
|
let (dir, ws, uri) = included(
|
|
"reachambiguous",
|
|
&[
|
|
("empty.svh", "// nothing here\n"),
|
|
("a.svh", "`define W 8\n"),
|
|
("b.svh", "`define W 16\n"),
|
|
("top.sv", &text),
|
|
],
|
|
);
|
|
let body = text_of(&hover(&ws, &uri, at(&text, "`W-")).expect("hover"));
|
|
assert!(body.contains("is defined 2 times"), "the honest answer: got {body}");
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn two_reachable_definitions_are_listed_rather_than_guessed_between() {
|
|
// The graph orders nothing here — both files are included — so the
|
|
// all-candidates answer stands.
|
|
let text = top_using_w("`include \"a.svh\"\n`include \"b.svh\"\n");
|
|
let (dir, ws, uri) = included(
|
|
"reachboth",
|
|
&[("a.svh", "`define W 8\n"), ("b.svh", "`define W 16\n"), ("top.sv", &text)],
|
|
);
|
|
let body = text_of(&hover(&ws, &uri, at(&text, "`W-")).expect("hover"));
|
|
assert!(body.contains("is defined 2 times"), "got {body}");
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn hovering_a_use_shows_its_declaration() {
|
|
let text = "module m;\n localparam WIDTH = 8;\n wire [WIDTH-1:0] a;\nendmodule\n";
|
|
let (ws, uri) = document(text);
|
|
let body = text_of(&hover(&ws, &uri, at(text, "[WIDTH") + 1).expect("hover"));
|
|
assert!(body.contains("**parameter**"), "got {body}");
|
|
// Fenced with the grammar's language id — see the macro hover test.
|
|
assert!(
|
|
body.contains("```systemverilog\n localparam WIDTH = 8;\n```"),
|
|
"the decl snippet: got {body}"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn hovering_a_declaration_shows_its_own_info() {
|
|
let text = "module top;\n wire a;\nendmodule\n";
|
|
let (ws, uri) = document(text);
|
|
let body = text_of(&hover(&ws, &uri, at(text, "top")).expect("hover"));
|
|
assert!(body.contains("**module** `top`"), "got {body}");
|
|
}
|
|
|
|
#[test]
|
|
fn hovering_a_cross_file_module_names_its_file() {
|
|
let dir = temp_dir("xhover");
|
|
fs::write(dir.join("sub.sv"), "module sub (input clk);\nendmodule\n").expect("write");
|
|
let top_path = dir.join("top.sv");
|
|
let top_text = "module top;\n sub u (.clk(1'b0));\nendmodule\n";
|
|
fs::write(&top_path, top_text).expect("write");
|
|
|
|
let mut ws = Workspace::default();
|
|
ws.open_whole_workspace(vec![dir.clone()]);
|
|
let uri = path_to_uri(&top_path).expect("uri");
|
|
ws.set_document(uri.clone(), top_text.to_string());
|
|
|
|
let body = text_of(&hover(&ws, &uri, at(top_text, "sub u")).expect("hover"));
|
|
assert!(body.contains("**module**"), "got {body}");
|
|
assert!(body.contains("sub.sv:1"), "got {body}");
|
|
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
#[test]
|
|
fn hovering_whitespace_returns_nothing() {
|
|
let text = "module m;\n\n wire a;\nendmodule\n";
|
|
let (ws, uri) = document(text);
|
|
assert!(hover(&ws, &uri, at(text, "\n\n") + 1).is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn a_macro_name_stops_at_its_argument_list() {
|
|
let text = "`define M(a) a\nmodule m;\n wire x = `M(1);\nendmodule\n";
|
|
let (ws, uri) = document(text);
|
|
let doc = ws.document(&uri).expect("open");
|
|
let token = doc.tokens.iter().find(|t| t.is_macro()).expect("a macro usage token");
|
|
assert_eq!(macro_name(token), Some("M"));
|
|
}
|
|
}
|
|
|
|
/// QA (M10 include-nav): a defect pinned rather than fixed — see the QA log
|
|
/// in `docs/features/include-nav/README.md` for the writeup and the fix
|
|
/// options weighed there.
|
|
#[cfg(test)]
|
|
mod qa_findings {
|
|
use super::*;
|
|
use crate::settings::ClientSettings;
|
|
use crate::uri::path_to_uri;
|
|
use std::fs;
|
|
|
|
/// The angled form is correctly wired into the narrowing overlay: not a
|
|
/// finding, recorded as a permanent regression test since the six tests
|
|
/// the dev shipped in this file only exercise the quoted form.
|
|
#[test]
|
|
fn an_angled_include_narrows_hover_same_as_a_quoted_one() {
|
|
let dir = std::env::temp_dir().join(format!("lucisv_qa_hover_sys_{}", std::process::id()));
|
|
let _ = fs::remove_dir_all(&dir);
|
|
fs::create_dir_all(dir.join("inc")).expect("mkdir");
|
|
fs::write(dir.join("inc/uvm_macros.svh"), "`define W 8\n").expect("write");
|
|
fs::write(dir.join("elsewhere.svh"), "`define W 16\n").expect("write");
|
|
let top_path = dir.join("top.sv");
|
|
let top_text = "`include <uvm_macros.svh>\nmodule top;\n wire [`W-1:0] a;\nendmodule\n";
|
|
fs::write(&top_path, top_text).expect("write");
|
|
fs::write(dir.join("files.f"), "+incdir+inc\ntop.sv\ninc/uvm_macros.svh\nelsewhere.svh\n")
|
|
.expect("write");
|
|
|
|
let mut ws = Workspace::default();
|
|
ws.set_client_settings(ClientSettings {
|
|
filelist: Some(dir.join("files.f").display().to_string()),
|
|
..Default::default()
|
|
});
|
|
ws.open_whole_workspace(vec![dir.clone()]);
|
|
let uri = path_to_uri(&top_path).expect("uri");
|
|
ws.set_document(uri.clone(), top_text.to_string());
|
|
|
|
let h = hover(&ws, &uri, top_text.find("`W-").expect("needle") as u32).expect("hover");
|
|
let body = match &h.contents {
|
|
HoverContents::Markup(m) => m.value.clone(),
|
|
_ => panic!("markup"),
|
|
};
|
|
assert!(body.contains("`define W 8") && body.contains("uvm_macros.svh:1"), "got {body}");
|
|
assert!(!body.contains("`define W 16"), "got {body}");
|
|
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
|
|
/// QA finding (M10, HIGH), fixed 2026-08-25. On a case-insensitive
|
|
/// filesystem (the default on macOS — this machine — and on Windows)
|
|
/// `Path::is_file` succeeds for a case-mismatched candidate, and
|
|
/// `normalize` only folds `.`/`..`, so `IncludeGraph::resolve` used to
|
|
/// return the path *as written* in the `` `include ``. `WorkspaceIndex`
|
|
/// meanwhile records the file under the case that was read off disk, so
|
|
/// the two spellings were different `PathBuf`s and
|
|
/// `narrowed_to_the_include_graph`'s `reachable.contains(p)` check
|
|
/// silently missed a file that **is** genuinely reachable.
|
|
///
|
|
/// Below, `top.sv` includes both `defs.svh` (case-mismatched: the file is
|
|
/// `Defs.svh`) and `other.svh` (spelled correctly), and both are equally
|
|
/// reachable in truth. Two reachable definitions must fall back to the
|
|
/// honest "defined 2 times" answer — the same thing
|
|
/// `two_reachable_definitions_are_listed_rather_than_guessed_between`
|
|
/// above pins for the case-matched spelling. Before the fix this narrowed
|
|
/// to the correctly-cased file alone and showed it with full confidence,
|
|
/// which is "may only narrow, never widen" failing in the one direction
|
|
/// that matters. `IncludeGraph::as_the_index_spells_it` now re-spells a
|
|
/// resolved path to the index's own when exactly one in-scope file
|
|
/// matches it case-insensitively.
|
|
///
|
|
/// Case-gated: only macOS/Windows default to a case-insensitive
|
|
/// filesystem, so only there does the premise (`is_file` succeeding for
|
|
/// the wrong case) hold.
|
|
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
|
#[test]
|
|
fn a_case_mismatched_include_still_counts_as_reachable() {
|
|
let dir = std::env::temp_dir().join(format!("lucisv_qa_hover_case_{}", std::process::id()));
|
|
let _ = fs::remove_dir_all(&dir);
|
|
fs::create_dir_all(&dir).expect("mkdir");
|
|
fs::write(dir.join("Defs.svh"), "`define W 8\n").expect("write");
|
|
fs::write(dir.join("other.svh"), "`define W 16\n").expect("write");
|
|
let top_path = dir.join("top.sv");
|
|
let top_text = "`include \"defs.svh\"\n`include \"other.svh\"\nmodule top;\n \
|
|
wire [`W-1:0] a;\nendmodule\n";
|
|
fs::write(&top_path, top_text).expect("write");
|
|
|
|
let mut ws = Workspace::default();
|
|
ws.open_whole_workspace(vec![dir.clone()]);
|
|
let uri = path_to_uri(&top_path).expect("uri");
|
|
ws.set_document(uri.clone(), top_text.to_string());
|
|
|
|
let h = hover(&ws, &uri, top_text.find("`W-").expect("needle") as u32).expect("hover");
|
|
let body = match &h.contents {
|
|
HoverContents::Markup(m) => m.value.clone(),
|
|
_ => panic!("markup"),
|
|
};
|
|
// Both files are reachable, so neither may be dropped and neither may
|
|
// be presented as the answer.
|
|
assert!(body.contains("is defined 2 times"), "got {body}");
|
|
assert!(body.contains("`define W 8") && body.contains("Defs.svh:1"), "got {body}");
|
|
assert!(body.contains("`define W 16") && body.contains("other.svh:1"), "got {body}");
|
|
|
|
let _ = fs::remove_dir_all(&dir);
|
|
}
|
|
}
|