1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//! Symbol context aggregation for comprehensive metadata display
use crate::relationship::RelationshipMetadata;
use crate::{Symbol, Visibility};
use bitflags::bitflags;
use serde::Serialize;
use std::fmt;
/// Comprehensive context for a symbol including all relationships
#[derive(Debug, Clone, Serialize)]
pub struct SymbolContext {
/// The symbol itself with all its metadata
pub symbol: Symbol,
/// Resolved file path for easy navigation
pub file_path: String,
/// All relationships this symbol has
pub relationships: SymbolRelationships,
}
/// Container for all types of symbol relationships
#[derive(Debug, Clone, Default, Serialize)]
pub struct SymbolRelationships {
/// What traits this type implements
pub implements: Option<Vec<Symbol>>,
/// What types implement this trait
pub implemented_by: Option<Vec<Symbol>>,
/// What base class(es) this class extends
pub extends: Option<Vec<Symbol>>,
/// What classes extend this base class
pub extended_by: Option<Vec<Symbol>>,
/// What types this symbol uses
pub uses: Option<Vec<Symbol>>,
/// What symbols use this type
pub used_by: Option<Vec<Symbol>>,
/// What methods/fields this symbol defines
pub defines: Option<Vec<Symbol>>,
/// What this symbol calls (with relationship metadata including call site location)
pub calls: Option<Vec<(Symbol, Option<RelationshipMetadata>)>>,
/// What calls this symbol (with relationship metadata including call site location)
pub called_by: Option<Vec<(Symbol, Option<RelationshipMetadata>)>>,
}
bitflags! {
/// Flags to control what context to include
pub struct ContextIncludes: u8 {
const IMPLEMENTATIONS = 0b00000001;
const DEFINITIONS = 0b00000010;
const CALLS = 0b00000100;
const CALLERS = 0b00001000;
const EXTENDS = 0b00010000;
const USES = 0b00100000;
const ALL = 0b00111111;
}
}
impl fmt::Display for SymbolContext {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let formatted = self.format_full("");
if formatted.ends_with('\n') {
write!(f, "{}", &formatted[..formatted.len() - 1])
} else {
write!(f, "{formatted}")
}
}
}
impl SymbolContext {
/// Format just the location line
pub fn format_location(&self) -> String {
format!(
"{} at {}",
self.symbol.name,
Self::symbol_location(&self.symbol)
)
}
/// Format location with type info
pub fn format_location_with_type(&self) -> String {
format!(
"{:?} {} at {} [symbol_id:{}]",
self.symbol.kind,
self.symbol.name,
Self::symbol_location(&self.symbol),
self.symbol.id.value()
)
}
/// Format comprehensive output
pub fn format_full(&self, indent: &str) -> String {
let mut output = String::new();
self.append_header(&mut output, indent);
self.append_metadata(&mut output, indent);
self.append_relationships(&mut output, indent);
output
}
fn append_header(&self, output: &mut String, indent: &str) {
output.push_str(&format!(
"{}{} ({:?}) at {} [symbol_id:{}]\n",
indent,
self.symbol.name,
self.symbol.kind,
Self::symbol_location(&self.symbol),
self.symbol.id.value()
));
}
fn append_metadata(&self, output: &mut String, indent: &str) {
// Module path
if let Some(module) = self.symbol.as_module_path() {
output.push_str(&format!("{indent}Module: {module}\n"));
}
if let Some(sig) = self.symbol.as_signature() {
output.push_str(&format!("{indent}Signature:\n"));
Self::write_multiline(output, sig, indent, 2);
}
// Visibility for appropriate symbols
if !matches!(self.symbol.visibility, Visibility::Private) {
output.push_str(&format!(
"{}Visibility: {:?}\n",
indent, self.symbol.visibility
));
}
// Documentation preview
if let Some(doc) = self.symbol.as_doc_comment() {
let preview: Vec<&str> = doc.lines().take(2).collect();
if !preview.is_empty() {
output.push_str(&format!("{}Doc: {}", indent, preview.join(" ")));
if doc.lines().count() > 2 {
output.push_str("...");
}
output.push('\n');
}
}
}
fn append_relationships(&self, output: &mut String, indent: &str) {
// Implementations
if let Some(impls) = &self.relationships.implements {
if !impls.is_empty() {
output.push_str(&format!("{indent}Implements:\n"));
for symbol in impls {
output.push_str(&format!(
"{} - {} ({:?}) at {}\n",
indent,
symbol.name,
symbol.kind,
SymbolContext::symbol_location(symbol)
));
}
}
}
// Implemented by
if let Some(impl_by) = &self.relationships.implemented_by {
if !impl_by.is_empty() {
output.push_str(&format!(
"{}Implemented by {} symbol(s):\n",
indent,
impl_by.len()
));
for impl_type in impl_by {
output.push_str(&format!(
"{} - {} ({:?}) at {}\n",
indent,
impl_type.name,
impl_type.kind,
SymbolContext::symbol_location(impl_type)
));
}
}
}
// Extends (what base class this extends)
if let Some(extends) = &self.relationships.extends {
if !extends.is_empty() {
output.push_str(&format!("{indent}Extends:\n"));
for base_class in extends {
output.push_str(&format!(
"{} - {} ({:?}) at {}\n",
indent,
base_class.name,
base_class.kind,
SymbolContext::symbol_location(base_class)
));
}
}
}
// Extended by (what classes extend this base class)
if let Some(extended_by) = &self.relationships.extended_by {
if !extended_by.is_empty() {
output.push_str(&format!(
"{}Extended by {} class(es):\n",
indent,
extended_by.len()
));
for derived_class in extended_by {
output.push_str(&format!(
"{} - {} ({:?}) at {}\n",
indent,
derived_class.name,
derived_class.kind,
SymbolContext::symbol_location(derived_class)
));
}
}
}
// Uses (what types this symbol uses)
if let Some(uses) = &self.relationships.uses {
if !uses.is_empty() {
output.push_str(&format!("{indent}Uses {} type(s):\n", uses.len()));
for used_type in uses {
output.push_str(&format!(
"{} - {} ({:?}) at {}\n",
indent,
used_type.name,
used_type.kind,
SymbolContext::symbol_location(used_type)
));
}
}
}
// Used by (what symbols use this type)
if let Some(used_by) = &self.relationships.used_by {
if !used_by.is_empty() {
output.push_str(&format!("{}Used by {} symbol(s):\n", indent, used_by.len()));
for using_symbol in used_by {
output.push_str(&format!(
"{} - {} ({:?}) at {}\n",
indent,
using_symbol.name,
using_symbol.kind,
SymbolContext::symbol_location(using_symbol)
));
}
}
}
// Methods defined
if let Some(defines) = &self.relationships.defines {
if !defines.is_empty() {
output.push_str(&format!("{}Defines {} symbol(s):\n", indent, defines.len()));
for defined in defines {
output.push_str(&format!(
"{} - {} ({:?}) at {}",
indent,
defined.name,
defined.kind,
SymbolContext::symbol_location(defined)
));
if let Some(sig) = defined.as_signature() {
output.push('\n');
Self::write_multiline(output, sig, indent, 4);
}
output.push('\n');
}
}
}
// Calls
if let Some(calls) = &self.relationships.calls {
if !calls.is_empty() {
output.push_str(&format!("{}Calls {} function(s):\n", indent, calls.len()));
for (called, metadata) in calls {
// Use call site location from metadata if available, otherwise definition location
let location = if let Some(meta) = metadata {
if let Some(line) = meta.line {
format!("{}:{}", called.file_path, line.saturating_add(1))
} else {
Self::symbol_location(called)
}
} else {
Self::symbol_location(called)
};
output.push_str(&format!(
"{} - {} ({:?}) at {} [symbol_id:{}]",
indent,
called.name,
called.kind,
location,
called.id.value()
));
// Show receiver info if available
if let Some(meta) = metadata {
if let Some(context) = &meta.context {
if !context.is_empty() {
output.push_str(&format!(" [{context}]"));
}
}
}
output.push('\n');
}
}
}
// Called by
if let Some(callers) = &self.relationships.called_by {
if !callers.is_empty() {
output.push_str(&format!(
"{}Called by {} function(s):\n",
indent,
callers.len()
));
for (caller, metadata) in callers {
// Use call site location from metadata if available, otherwise definition location
let location = if let Some(meta) = metadata {
if let Some(line) = meta.line {
format!("{}:{}", caller.file_path, line.saturating_add(1))
} else {
Self::symbol_location(caller)
}
} else {
Self::symbol_location(caller)
};
output.push_str(&format!(
"{} - {} ({:?}) at {} [symbol_id:{}]",
indent,
caller.name,
caller.kind,
location,
caller.id.value()
));
// Show receiver info if available
if let Some(meta) = metadata {
if let Some(context) = &meta.context {
if !context.is_empty() {
output.push_str(&format!(" [{context}]"));
}
}
}
output.push('\n');
}
}
}
}
pub(crate) fn symbol_location(symbol: &Symbol) -> String {
let start = symbol.range.start_line.saturating_add(1);
let end = symbol.range.end_line.saturating_add(1);
if start == end {
format!("{}:{start}", symbol.file_path)
} else {
format!("{}:{start}-{end}", symbol.file_path)
}
}
}
impl SymbolContext {
fn write_multiline(output: &mut String, text: &str, indent: &str, extra_spaces: usize) {
let padding = format!("{indent}{:width$}", "", width = extra_spaces);
for line in text.lines() {
output.push_str(&padding);
output.push_str(line);
output.push('\n');
}
}
}