Skip to main content

aft_parse/parser/
outline_facts.rs

1//! Syntax facts shared by the parser cache and outline renderer.
2
3use crate::symbols::{Range, Symbol};
4use serde::Serialize;
5
6/// A single entry in the outline tree.
7///
8/// Top-level symbols have an empty `members` vec. Classes/structs contain
9/// their methods and nested types in `members`, forming a recursive tree.
10#[derive(Debug, Clone, Serialize)]
11pub struct OutlineEntry {
12    pub name: String,
13    pub kind: String,
14    pub range: Range,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub signature: Option<String>,
17    pub exported: bool,
18    pub members: Vec<OutlineEntry>,
19}
20
21pub fn symbol_to_entry(sym: &Symbol) -> OutlineEntry {
22    OutlineEntry {
23        name: sym.name.clone(),
24        kind: serde_json::to_value(&sym.kind)
25            .ok()
26            .and_then(|v| v.as_str().map(String::from))
27            .unwrap_or_else(|| format!("{:?}", sym.kind).to_lowercase()),
28        range: sym.range.clone(),
29        signature: sym.signature.clone(),
30        exported: sym.exported,
31        members: Vec::new(),
32    }
33}
34
35#[derive(Clone)]
36pub struct TestSummary {
37    pub name: String,
38    pub range: Range,
39    pub items: usize,
40    pub unknown_reason: Option<&'static str>,
41}
42
43#[derive(Clone)]
44pub struct TestRegion {
45    pub summary: TestSummary,
46    pub included_path: Option<String>,
47    pub implicit_include: bool,
48    pub module_scope: Vec<String>,
49    pub standalone: bool,
50}
51
52/// What the multi-file structure map needs from one parse of a file beyond
53/// its symbols: the test regions it collapses and, for Rust, the methods
54/// declared in each trait it previews. Both depend only on the file's own
55/// text, so they are stored next to the symbols in the shared symbol cache
56/// under the same freshness key, and a warm outline renders the file without
57/// reading or parsing it. Test includes are resolved at render time, because
58/// they depend on other files and on the caller's read policy.
59pub struct OutlineParseFacts {
60    pub test_regions: Vec<TestRegion>,
61    pub trait_previews: Vec<(Range, Vec<OutlineEntry>)>,
62}
63
64impl OutlineParseFacts {
65    /// Rough resident size, for the symbol cache's memory estimate.
66    pub fn estimated_bytes(&self) -> u64 {
67        fn entry_bytes(entry: &OutlineEntry) -> u64 {
68            (std::mem::size_of::<OutlineEntry>()
69                + entry.name.len()
70                + entry.kind.len()
71                + entry.signature.as_ref().map_or(0, String::len)) as u64
72                + entry.members.iter().map(entry_bytes).sum::<u64>()
73        }
74        let regions = self
75            .test_regions
76            .iter()
77            .map(|region| {
78                (std::mem::size_of::<TestRegion>()
79                    + region.summary.name.len()
80                    + region.included_path.as_ref().map_or(0, String::len)
81                    + region.module_scope.iter().map(String::len).sum::<usize>())
82                    as u64
83            })
84            .sum::<u64>();
85        let previews = self
86            .trait_previews
87            .iter()
88            .map(|(_, methods)| {
89                std::mem::size_of::<(Range, Vec<OutlineEntry>)>() as u64
90                    + methods.iter().map(entry_bytes).sum::<u64>()
91            })
92            .sum::<u64>();
93        std::mem::size_of::<Self>() as u64 + regions + previews
94    }
95}