devscout_rs/query/refs_tables.rs
1use crate::graph;
2
3// The three reference kinds and their fixed order. Not iterated generically
4// here -- each kind gets its own struct field (`inherits`/`uses_type`/
5// `uses_member`) throughout this module rather than a keyed collection. This
6// constant exists only as an anchor for the kind list and its order.
7#[allow(dead_code)]
8const REF_KINDS: [&str; 3] = ["inherits", "uses-type", "uses-member"];
9
10/// Default per-table row cap for the outbound and ambiguous tables. The three
11/// inbound tables share [`INBOUND_CAP`] instead.
12pub const DEFAULT_CAP: usize = 50;
13/// Default cap shared across all three inbound kinds -- one cap total, not one
14/// per table.
15pub const INBOUND_CAP: usize = 30;
16/// Default cap for the `--out` view: one cap shared across all four outbound
17/// kinds (inherits/uses-type/uses-member/imports), the mirror of
18/// [`INBOUND_CAP`]. `--all` lifts this cap.
19pub const OUTBOUND_CAP: usize = 30;
20/// Maximum length of a displayed source line, in UTF-16 code units.
21/// Truncation counts code units (see `clip_source`) so the cut is stable
22/// regardless of any astral characters on the line.
23pub const SOURCE_MAX: usize = 120;
24// ============================================================================
25// build_refs_model.
26// ============================================================================
27
28pub(super) fn cap_rows<T>(mut rows: Vec<T>, cap: usize) -> (Vec<T>, usize) {
29 if rows.len() <= cap {
30 (rows, 0)
31 } else {
32 let dropped = rows.len() - cap;
33 rows.truncate(cap);
34 (rows, dropped)
35 }
36}
37
38// The (from_file, from_line) an edge originates at, the key location sorts use.
39// See the module header re: `str::cmp` ordering.
40pub(super) fn edge_loc(e: &graph::Edge) -> (&str, usize) {
41 match e {
42 graph::Edge::Inherits { from_file, from_line, .. }
43 | graph::Edge::UsesType { from_file, from_line, .. }
44 | graph::Edge::UsesMember { from_file, from_line, .. }
45 | graph::Edge::Implements { from_file, from_line, .. }
46 | graph::Edge::Overrides { from_file, from_line, .. }
47 | graph::Edge::Imports { from_file, from_line, .. }
48 | graph::Edge::Ambiguous { from_file, from_line, .. }
49 // Never actually reached: 'ctor-di' edges are never pushed into any
50 // structure this helper sorts (see the query-index builder's own
51 // CtorDi arm). Included only for exhaustiveness, as are the four
52 // TS/TSX kinds below, for the same reason.
53 | graph::Edge::CtorDi { from_file, from_line, .. }
54 | graph::Edge::Import { from_file, from_line, .. }
55 | graph::Edge::Call { from_file, from_line, .. }
56 | graph::Edge::JsxUse { from_file, from_line, .. }
57 | graph::Edge::Dispatch { from_file, from_line, .. } => (from_file.as_str(), *from_line),
58 }
59}
60
61pub(super) fn loc_cmp(a: &graph::Edge, b: &graph::Edge) -> std::cmp::Ordering {
62 let (af, al) = edge_loc(a);
63 let (bf, bl) = edge_loc(b);
64 if af == bf {
65 al.cmp(&bl)
66 } else {
67 af.cmp(bf)
68 }
69}
70
71/// Which tier a ROW reports, folded from the tiers of the edges behind it.
72///
73/// One extension edge is enough to call the whole row an extension: that tier
74/// ran C#'s own lookup rule and only failed to check the receiver, so it is the
75/// stronger of the two and a name guess sitting beside it does not weaken it.
76/// Everything else heuristic is a guess, an edge carrying NO tier included -- a
77/// graph written before schema 2 cannot prove it was anything better, and
78/// reporting an unproven edge as the stronger tier is the one direction this
79/// surface must never round.
80pub(super) fn row_tier(heuristic: bool, ext_seen: bool) -> Option<graph::HeuristicTier> {
81 if !heuristic {
82 return None;
83 }
84 Some(if ext_seen {
85 graph::HeuristicTier::Ext
86 } else {
87 graph::HeuristicTier::Guess
88 })
89}
90
91/// One inbound-table row: `file` and `line` of the referencing site, then
92/// `heuristic` (whether the edge was guessed), `tier` (which guess tier said
93/// so) and `source` (the trimmed referencing line). An empty `source` is
94/// omitted from `--json`.
95#[derive(Debug, Clone, PartialEq)]
96pub struct InboundRow {
97 /// The file value.
98 pub file: String,
99 /// The line value.
100 pub line: usize,
101 /// The heuristic value.
102 pub heuristic: bool,
103 /// Which heuristic tier stands behind this row; `None` exactly when
104 /// `heuristic` is false (see [`row_tier`]).
105 pub tier: Option<graph::HeuristicTier>,
106 /// The source value.
107 pub source: String,
108}
109
110/// One outbound-table row: `file`/`line` of the referencing site, `to_file`/`to`
111/// of the target, then `heuristic` and `source` (same omit-when-empty rule as
112/// [`InboundRow`]). `source` is read at the site actually making the reference
113/// -- for an outbound edge that is a line in the def's own file, not the
114/// target's.
115#[derive(Debug, Clone, PartialEq)]
116pub struct OutboundRow {
117 /// The file value.
118 pub file: String,
119 /// The line value.
120 pub line: usize,
121 /// The to file value.
122 pub to_file: String,
123 /// The to value.
124 pub to: String,
125 /// The heuristic value.
126 pub heuristic: bool,
127 /// Which heuristic tier stands behind this row, same rule as
128 /// [`InboundRow::tier`].
129 pub tier: Option<graph::HeuristicTier>,
130 /// The source value.
131 pub source: String,
132}
133
134/// One imports-table row: `file`/`line`, the imported `target` namespace, and
135/// `source`. An imports edge is never a guess, so unlike [`OutboundRow`] this
136/// carries no `heuristic` field.
137#[derive(Debug, Clone, PartialEq)]
138pub struct ImportRow {
139 /// The file value.
140 pub file: String,
141 /// The line value.
142 pub line: usize,
143 /// The target value.
144 pub target: String,
145 /// The source value.
146 pub source: String,
147}
148
149/// One ambiguous-table row: the referencing `file`/`line`, the `origin` and
150/// `raw` text of the reference, and how many candidates it matched.
151#[derive(Debug, Clone, PartialEq)]
152pub struct AmbiguousRow {
153 /// The file value.
154 pub file: String,
155 /// The line value.
156 pub line: usize,
157 /// The origin value.
158 pub origin: String,
159 /// The raw value.
160 pub raw: String,
161 /// The candidate count value.
162 pub candidate_count: usize,
163}
164
165/// A capped table: `total` rows before capping, how many were `dropped`, and
166/// the surviving `rows`.
167#[derive(Debug, Clone, PartialEq)]
168pub struct Table<R> {
169 /// The total value.
170 pub total: usize,
171 /// The dropped value.
172 pub dropped: usize,
173 /// The rows value.
174 pub rows: Vec<R>,
175}
176
177pub(super) fn build_table<R>(
178 mut idxs: Vec<usize>,
179 edges: &[graph::Edge],
180 cap: usize,
181 map_row: fn(&graph::Edge) -> R,
182) -> Table<R> {
183 idxs.sort_by(|&a, &b| loc_cmp(&edges[a], &edges[b]));
184 let total = idxs.len();
185 let (shown, dropped) = cap_rows(idxs, cap);
186 let rows = shown.into_iter().map(|i| map_row(&edges[i])).collect();
187 Table {
188 total,
189 dropped,
190 rows,
191 }
192}
193
194// The per-table, per-kind outbound builder was removed here: the outbound
195// tables now go through `build_outbound_tables`'s shared cap and rank instead
196// (below `RankedOutbound`), the same way the inbound tables stopped going
197// through a per-kind builder. `build_table` above still backs the two
198// ambiguous tables.
199
200pub(super) fn ambiguous_row(e: &graph::Edge) -> AmbiguousRow {
201 match e {
202 graph::Edge::Ambiguous {
203 origin,
204 from_file,
205 from_line,
206 raw,
207 candidate_count,
208 ..
209 } => AmbiguousRow {
210 file: from_file.clone(),
211 line: *from_line,
212 origin: origin.clone(),
213 raw: raw.clone(),
214 candidate_count: *candidate_count,
215 },
216 _ => unreachable!("ambiguous table only ever holds ambiguous edge indices"),
217 }
218}
219
220/// The five inbound tables, one per kind.
221///
222/// `implements`/`overrides` are never heuristic, and a renderer shows
223/// either only when its own total is non-zero -- unlike the first three,
224/// which always show their header -- so a symbol untouched by dispatch
225/// edges renders byte-identical to before this pair existed.
226#[derive(Debug, Clone, PartialEq)]
227pub struct InboundTables {
228 /// The inherits value.
229 pub inherits: Table<InboundRow>,
230 /// The uses type value.
231 pub uses_type: Table<InboundRow>,
232 /// The uses member value.
233 pub uses_member: Table<InboundRow>,
234 /// The implements value.
235 pub implements: Table<InboundRow>,
236 /// The overrides value.
237 pub overrides: Table<InboundRow>,
238}
239
240/// The six outbound tables: three by kind, `implements`/`overrides`, plus
241/// imports.
242#[derive(Debug, Clone, PartialEq)]
243pub struct OutboundTables {
244 /// The inherits value.
245 pub inherits: Table<OutboundRow>,
246 /// The uses type value.
247 pub uses_type: Table<OutboundRow>,
248 /// The uses member value.
249 pub uses_member: Table<OutboundRow>,
250 /// The implements value.
251 pub implements: Table<OutboundRow>,
252 /// The overrides value.
253 pub overrides: Table<OutboundRow>,
254 /// The imports value.
255 pub imports: Table<ImportRow>,
256}
257
258/// The inbound and outbound ambiguous-reference tables.
259#[derive(Debug, Clone, PartialEq)]
260pub struct AmbiguousTables {
261 /// The inbound value.
262 pub inbound: Table<AmbiguousRow>,
263 /// The outbound value.
264 pub outbound: Table<AmbiguousRow>,
265}