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
use crate::models::{EntityKind, ParsedEntity, ReferenceIntent};
use crate::pipeline::parser::comments::{extract_comments, extract_decorators};
use crate::pipeline::parser::context::{ClassContext, compute_fqn_and_context};
use crate::pipeline::parser::languages::{
cpp, csharp, java, javascript, kotlin, markdown, python, typescript,
};
use crate::pipeline::parser::utils::{extract_decorator_references, extract_type_references};
use super::captures::CaptureState;
/// `true` for every `CSharp*` entity kind — the gate for the C# FQN prefix
/// branch below.
fn is_csharp_kind(kind: &EntityKind) -> bool {
matches!(
kind,
EntityKind::CSharpClass
| EntityKind::CSharpInterface
| EntityKind::CSharpStruct
| EntityKind::CSharpRecord
| EntityKind::CSharpEnum
| EntityKind::CSharpMethod
| EntityKind::CSharpConstructor
| EntityKind::CSharpProperty
| EntityKind::CSharpField
| EntityKind::CSharpConstant
| EntityKind::CSharpDelegate
| EntityKind::CSharpEvent
| EntityKind::CSharpIndexer
| EntityKind::CSharpOperator
| EntityKind::CSharpNamespace
| EntityKind::CSharpLocalFunction
)
}
#[expect(
clippy::too_many_arguments,
reason = "entity enrichment requires state, source, language, and callbacks from extraction context"
)]
#[expect(
clippy::too_many_lines,
reason = "function is verbose but correct — extraction deferred"
)]
#[expect(
clippy::cognitive_complexity,
reason = "function is verbose but correct — extraction deferred"
)]
pub(crate) fn enrich_and_create_entity<'a>(
state: &mut CaptureState<'a>,
source_bytes: &[u8],
lang_name: &str,
file_path: &str,
repo_name: &str,
class_contexts: &[ClassContext],
package_prefix: &Option<String>,
entities: &mut Vec<ParsedEntity>,
covered_ranges: &mut Vec<(usize, usize)>,
) {
let name = state.name.take();
let kind = state.kind.take();
let mut signature = state.signature.take();
let start_line = state.start_line;
let end_line;
let entity_node = state.entity_node.take();
let mut reference_intents = std::mem::take(&mut state.reference_intents);
let mut enclosing_class_fqn: Option<String> = None;
if let (Some(mut name), Some(kind)) = (name, kind) {
// Extract docstring and inline comments dynamically from the entity node
let (docstring, inline_comments) = if let Some(node) = entity_node {
extract_comments(node, source_bytes, lang_name, &kind, class_contexts)
} else {
(None, Vec::new())
};
// Extract decorators/annotations from the entity node
let mut decorators = if let Some(node) = entity_node {
extract_decorators(node, source_bytes, lang_name)
} else {
Vec::new()
};
// Phase 5: For Python entities, extract decorator names for display
if lang_name == "python"
&& let Some(entity_n) = entity_node
{
python::extract_decorator_names_python(entity_n, source_bytes, &mut decorators);
}
// Determine FQN and enclosing class based on context
let (mut fqn, mut enclosing_class) =
compute_fqn_and_context(&name, &kind, start_line, lang_name, class_contexts);
// Prefix FQN with Java package name if available
if lang_name == "java"
&& let Some(pkg) = package_prefix
{
fqn = format!("{}.{}", pkg, fqn);
}
if matches!(
kind,
EntityKind::CppMethod
| EntityKind::CFunction
| EntityKind::CppClass
| EntityKind::CStruct
| EntityKind::CppNamespace
| EntityKind::MacroDefinition
) && let Some(node) = entity_node
&& let Some(cpp_fqn) = cpp::build_cpp_fqn(node, source_bytes)
&& !cpp_fqn.is_empty()
{
enclosing_class = Some(cpp_fqn.clone());
fqn = format!("{}::{}", cpp_fqn, name);
}
// C#: namespace-qualified FQN — the file-scoped namespace pre-pass
// supplies the outermost scope, then an ancestor walk collects block
// namespaces and containing types (plan §3.2). Namespace entities
// anchor at their own name and are skipped by the prefix logic.
if is_csharp_kind(&kind)
&& let Some(node) = entity_node
{
let is_namespace_decl = kind == EntityKind::CSharpNamespace
&& matches!(
node.kind(),
"namespace_declaration" | "file_scoped_namespace_declaration"
);
if is_namespace_decl {
if let Some(anc) = csharp::build_csharp_fqn_prefix(node, source_bytes) {
fqn = format!("{}.{}", anc, name);
enclosing_class_fqn = Some(anc);
}
} else {
let mut parts: Vec<String> = Vec::new();
if lang_name == "csharp"
&& let Some(pkg) = package_prefix
{
parts.push(pkg.clone());
}
if let Some(anc) = csharp::build_csharp_fqn_prefix(node, source_bytes) {
parts.push(anc);
}
if !parts.is_empty() {
let prefix = parts.join(".");
fqn = format!("{}.{}", prefix, name);
enclosing_class_fqn = Some(prefix);
}
}
}
// derive the document's display name from repo + file path, and the FQN from the file path so entities don't collide.
if lang_name == "markdown" {
if matches!(kind, EntityKind::MarkdownDocument) {
name = format!("{}::{}", repo_name, file_path);
}
fqn = match kind {
EntityKind::MarkdownDocument => file_path.to_string(),
EntityKind::MarkdownSection => {
if let Some(node) = entity_node {
let chain = markdown::build_markdown_fqn(node, source_bytes);
format!("{}::{}", file_path, chain)
} else {
format!("{}::{}", file_path, name)
}
}
_ => fqn,
};
}
// For classes, also extract extends/implements from AST
if matches!(
kind,
EntityKind::Class
| EntityKind::Interface
| EntityKind::KotlinClass
| EntityKind::KotlinInterface
| EntityKind::KotlinObject
| EntityKind::KotlinEnum
| EntityKind::CSharpClass
| EntityKind::CSharpInterface
| EntityKind::CSharpStruct
| EntityKind::CSharpRecord
| EntityKind::CSharpEnum
) && let Some(class_node) = entity_node
{
if lang_name == "javascript" {
javascript::extract_class_inheritance_js(
class_node,
source_bytes,
&mut reference_intents,
);
// Extract decorator references for JavaScript (e.g., @Component, @Injectable)
// Decorators may be in the parent node (export_statement) rather than class_declaration
let decorator_node = class_node
.parent()
.filter(|p| p.kind() == "export_statement")
.unwrap_or(class_node);
extract_decorator_references(decorator_node, source_bytes, &mut reference_intents);
} else if lang_name == "typescript" {
typescript::extract_class_inheritance(
class_node,
source_bytes,
&mut reference_intents,
);
// Extract decorator references (e.g., @Component, @NgModule)
// Decorators may be in the parent node (export_statement) rather than class_declaration
let decorator_node = class_node
.parent()
.filter(|p| p.kind() == "export_statement")
.unwrap_or(class_node);
extract_decorator_references(decorator_node, source_bytes, &mut reference_intents);
// Extract type references (e.g., constructor parameters, property types)
extract_type_references(class_node, source_bytes, &mut reference_intents);
} else if lang_name == "java" {
// Extract extends/implements from Java class/interface declarations
java::extract_class_inheritance_java(
class_node,
source_bytes,
&mut reference_intents,
);
// Extract annotation references (e.g., @Component, @Autowired)
java::extract_annotation_references(
class_node,
source_bytes,
&mut reference_intents,
);
// Extract type references (e.g., constructor parameters, field types)
extract_type_references(class_node, source_bytes, &mut reference_intents);
} else if lang_name == "kotlin" {
// Extract extends/implements from Kotlin class/interface declarations
kotlin::extract_class_inheritance_kotlin(
class_node,
source_bytes,
&mut reference_intents,
);
// Extract annotation references (e.g., @Component, @Composable)
kotlin::extract_annotation_references(
class_node,
source_bytes,
&mut reference_intents,
);
// Extract type references (e.g., constructor parameters, property types)
kotlin::extract_type_references(class_node, source_bytes, &mut reference_intents);
} else if lang_name == "csharp" {
// Extract extends/implements from the C# base_list using the
// §3.3 heuristic (first entry extends unless `^I[A-Z]`, …)
csharp::extract_class_inheritance_csharp(
class_node,
source_bytes,
&mut reference_intents,
);
// Extract attribute references (e.g., [Obsolete], [Authorize]) —
// attributes live in attribute_list children, not modifiers.
csharp::extract_attribute_references(
class_node,
source_bytes,
&mut reference_intents,
);
// Extract type references (e.g., property types, generic args)
csharp::extract_type_references_csharp(
class_node,
source_bytes,
&mut reference_intents,
);
}
}
// Calculate end_line from entity_node if available
if let Some(node) = entity_node {
end_line = node.end_position().row + 1;
} else {
// If no entity_node, use start_line as a fallback
end_line = start_line;
}
// Extract C++ signature from entity_node if not captured by query
if signature.is_none()
&& matches!(kind, EntityKind::CppMethod | EntityKind::CFunction)
&& let Some(node) = entity_node
{
signature = cpp::extract_cpp_signature(node, source_bytes);
}
let mut entity = ParsedEntity::new(
name,
kind,
fqn,
signature,
docstring,
lang_name,
file_path,
start_line,
end_line,
enclosing_class,
repo_name,
);
entity.reference_intents = reference_intents;
// Filter self-references (TypeReference/ValueReference to own name).
// A type or value reference whose target is the enclosing entity itself
// would produce a self-loop edge in Neo4j; useless visually and semantically.
entity.reference_intents.retain(|intent| match intent {
ReferenceIntent::TypeReference { type_name, .. } => type_name != entity.name.as_str(),
ReferenceIntent::ValueReference { value_name, .. } => {
value_name != entity.name.as_str()
}
_ => true,
});
entity.inline_comments = inline_comments;
entity.decorators = decorators;
// C#: persist the FQN of the enclosing scope (namespace + containing
// types) so the CONTAINS auto-link in Neo4j can match by FQN instead
// of by bare name (mirrors the Rust post-pass).
entity.enclosing_class_fqn = enclosing_class_fqn;
// Extract text below header/document entity for each markdown entity
if lang_name == "markdown"
&& let Some(node) = entity_node
{
let body = match entity.kind {
EntityKind::MarkdownDocument => {
markdown::extract_document_intro(node, source_bytes)
}
EntityKind::MarkdownSection => {
node.utf8_text(source_bytes).unwrap_or("").to_string()
}
_ => String::new(),
};
let header = format!("[{}] {}", entity.kind, entity.name);
let location = format!("File: {}:{}", entity.file_path, entity.start_line);
entity.embed_text = format!("{header}\n{location}\n\n{body}");
}
// Extract alias module path for JS require() aliases
if lang_name == "javascript"
&& entity.kind == EntityKind::Constant
&& let Some(node) = entity_node
{
entity.alias_module_path = javascript::extract_require_module_path(node, source_bytes);
}
// Track byte range of this entity for orphan detection
// Must be done for ALL entities to keep indices aligned with the entities vector
if let Some(node) = entity_node {
covered_ranges.push((node.start_byte(), node.end_byte()));
} else {
// If we don't have a node, use a dummy range that won't match any orphans
covered_ranges.push((usize::MAX, usize::MAX));
}
entities.push(entity);
}
}