Skip to main content

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::Imports { from_file, from_line, .. }
46        | graph::Edge::Ambiguous { from_file, from_line, .. }
47        // Never actually reached: 'ctor-di' edges are never pushed into any
48        // structure this helper sorts (see the query-index builder's own
49        // CtorDi arm). Included only for exhaustiveness, as are the four
50        // TS/TSX kinds below, for the same reason.
51        | graph::Edge::CtorDi { from_file, from_line, .. }
52        | graph::Edge::Import { from_file, from_line, .. }
53        | graph::Edge::Call { from_file, from_line, .. }
54        | graph::Edge::JsxUse { from_file, from_line, .. }
55        | graph::Edge::Dispatch { from_file, from_line, .. } => (from_file.as_str(), *from_line),
56    }
57}
58
59pub(super) fn loc_cmp(a: &graph::Edge, b: &graph::Edge) -> std::cmp::Ordering {
60    let (af, al) = edge_loc(a);
61    let (bf, bl) = edge_loc(b);
62    if af == bf {
63        al.cmp(&bl)
64    } else {
65        af.cmp(bf)
66    }
67}
68
69/// Which tier a ROW reports, folded from the tiers of the edges behind it.
70///
71/// One extension edge is enough to call the whole row an extension: that tier
72/// ran C#'s own lookup rule and only failed to check the receiver, so it is the
73/// stronger of the two and a name guess sitting beside it does not weaken it.
74/// Everything else heuristic is a guess, an edge carrying NO tier included -- a
75/// graph written before schema 2 cannot prove it was anything better, and
76/// reporting an unproven edge as the stronger tier is the one direction this
77/// surface must never round.
78pub(super) fn row_tier(heuristic: bool, ext_seen: bool) -> Option<graph::HeuristicTier> {
79    if !heuristic {
80        return None;
81    }
82    Some(if ext_seen {
83        graph::HeuristicTier::Ext
84    } else {
85        graph::HeuristicTier::Guess
86    })
87}
88
89/// One inbound-table row: `file` and `line` of the referencing site, then
90/// `heuristic` (whether the edge was guessed), `tier` (which guess tier said
91/// so) and `source` (the trimmed referencing line). An empty `source` is
92/// omitted from `--json`.
93#[derive(Debug, Clone, PartialEq)]
94pub struct InboundRow {
95    /// The file value.
96    pub file: String,
97    /// The line value.
98    pub line: usize,
99    /// The heuristic value.
100    pub heuristic: bool,
101    /// Which heuristic tier stands behind this row; `None` exactly when
102    /// `heuristic` is false (see [`row_tier`]).
103    pub tier: Option<graph::HeuristicTier>,
104    /// The source value.
105    pub source: String,
106}
107
108/// One outbound-table row: `file`/`line` of the referencing site, `to_file`/`to`
109/// of the target, then `heuristic` and `source` (same omit-when-empty rule as
110/// [`InboundRow`]). `source` is read at the site actually making the reference
111/// -- for an outbound edge that is a line in the def's own file, not the
112/// target's.
113#[derive(Debug, Clone, PartialEq)]
114pub struct OutboundRow {
115    /// The file value.
116    pub file: String,
117    /// The line value.
118    pub line: usize,
119    /// The to file value.
120    pub to_file: String,
121    /// The to value.
122    pub to: String,
123    /// The heuristic value.
124    pub heuristic: bool,
125    /// Which heuristic tier stands behind this row, same rule as
126    /// [`InboundRow::tier`].
127    pub tier: Option<graph::HeuristicTier>,
128    /// The source value.
129    pub source: String,
130}
131
132/// One imports-table row: `file`/`line`, the imported `target` namespace, and
133/// `source`. An imports edge is never a guess, so unlike [`OutboundRow`] this
134/// carries no `heuristic` field.
135#[derive(Debug, Clone, PartialEq)]
136pub struct ImportRow {
137    /// The file value.
138    pub file: String,
139    /// The line value.
140    pub line: usize,
141    /// The target value.
142    pub target: String,
143    /// The source value.
144    pub source: String,
145}
146
147/// One ambiguous-table row: the referencing `file`/`line`, the `origin` and
148/// `raw` text of the reference, and how many candidates it matched.
149#[derive(Debug, Clone, PartialEq)]
150pub struct AmbiguousRow {
151    /// The file value.
152    pub file: String,
153    /// The line value.
154    pub line: usize,
155    /// The origin value.
156    pub origin: String,
157    /// The raw value.
158    pub raw: String,
159    /// The candidate count value.
160    pub candidate_count: usize,
161}
162
163/// A capped table: `total` rows before capping, how many were `dropped`, and
164/// the surviving `rows`.
165#[derive(Debug, Clone, PartialEq)]
166pub struct Table<R> {
167    /// The total value.
168    pub total: usize,
169    /// The dropped value.
170    pub dropped: usize,
171    /// The rows value.
172    pub rows: Vec<R>,
173}
174
175pub(super) fn build_table<R>(
176    mut idxs: Vec<usize>,
177    edges: &[graph::Edge],
178    cap: usize,
179    map_row: fn(&graph::Edge) -> R,
180) -> Table<R> {
181    idxs.sort_by(|&a, &b| loc_cmp(&edges[a], &edges[b]));
182    let total = idxs.len();
183    let (shown, dropped) = cap_rows(idxs, cap);
184    let rows = shown.into_iter().map(|i| map_row(&edges[i])).collect();
185    Table {
186        total,
187        dropped,
188        rows,
189    }
190}
191
192// The per-table, per-kind outbound builder was removed here: the outbound
193// tables now go through `build_outbound_tables`'s shared cap and rank instead
194// (below `RankedOutbound`), the same way the inbound tables stopped going
195// through a per-kind builder. `build_table` above still backs the two
196// ambiguous tables.
197
198pub(super) fn ambiguous_row(e: &graph::Edge) -> AmbiguousRow {
199    match e {
200        graph::Edge::Ambiguous {
201            origin,
202            from_file,
203            from_line,
204            raw,
205            candidate_count,
206            ..
207        } => AmbiguousRow {
208            file: from_file.clone(),
209            line: *from_line,
210            origin: origin.clone(),
211            raw: raw.clone(),
212            candidate_count: *candidate_count,
213        },
214        _ => unreachable!("ambiguous table only ever holds ambiguous edge indices"),
215    }
216}
217
218/// The three inbound tables, one per kind (inherits/uses-type/uses-member).
219#[derive(Debug, Clone, PartialEq)]
220pub struct InboundTables {
221    /// The inherits value.
222    pub inherits: Table<InboundRow>,
223    /// The uses type value.
224    pub uses_type: Table<InboundRow>,
225    /// The uses member value.
226    pub uses_member: Table<InboundRow>,
227}
228
229/// The four outbound tables: three by kind plus imports.
230#[derive(Debug, Clone, PartialEq)]
231pub struct OutboundTables {
232    /// The inherits value.
233    pub inherits: Table<OutboundRow>,
234    /// The uses type value.
235    pub uses_type: Table<OutboundRow>,
236    /// The uses member value.
237    pub uses_member: Table<OutboundRow>,
238    /// The imports value.
239    pub imports: Table<ImportRow>,
240}
241
242/// The inbound and outbound ambiguous-reference tables.
243#[derive(Debug, Clone, PartialEq)]
244pub struct AmbiguousTables {
245    /// The inbound value.
246    pub inbound: Table<AmbiguousRow>,
247    /// The outbound value.
248    pub outbound: Table<AmbiguousRow>,
249}