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
//! Data format parsers (JSON, YAML, TOML)
use crate::indexer::SymbolChunk;
/// Extract chunks from JSON source code
pub(super) fn extract_json_chunks(source: &str) -> Vec<SymbolChunk> {
// Parse JSON and create chunks for top-level keys
// This provides better granularity than treating the whole file as one chunk
// First, try to parse as valid JSON
let value: serde_json::Value = match serde_json::from_str(source) {
Ok(v) => v,
Err(_) => return Vec::new(), // Invalid JSON, fall back to module chunking
};
let mut chunks = Vec::new();
// Only chunk if it's an object with reasonable number of keys
if let serde_json::Value::Object(map) = value {
// For package.json, always chunk scripts, dependencies, devDependencies
let important_keys = [
"scripts",
"dependencies",
"devDependencies",
"config",
"exports",
];
// If it has important keys or many keys, chunk it
let has_important = important_keys.iter().any(|k| map.contains_key(*k));
if !has_important && map.len() <= 3 {
return Vec::new(); // Too simple, use module fallback
}
// For each top-level key, create a chunk
let lines: Vec<&str> = source.lines().collect();
let mut current_line = 1;
for (key, _value) in map.iter() {
// Find the line where this key appears
let key_pattern = format!("\"{}\"", key);
let mut start_line = current_line;
let mut end_line = current_line;
let mut found = false;
let mut brace_depth = 0;
let mut in_string = false;
let mut escape_next = false;
for (i, line) in lines.iter().enumerate().skip(current_line - 1) {
let line_num = i + 1;
// Look for the key
if !found && line.contains(&key_pattern) {
start_line = line_num;
found = true;
}
if found {
// Track brace depth to find the end of this value
for ch in line.chars() {
if escape_next {
escape_next = false;
continue;
}
match ch {
'\\' if in_string => escape_next = true,
'"' if !in_string => in_string = true,
'"' if in_string => in_string = false,
'{' | '[' if !in_string => brace_depth += 1,
'}' | ']' if !in_string => {
brace_depth -= 1;
if brace_depth == 0 {
end_line = line_num;
current_line = line_num + 1;
break;
}
}
',' if !in_string && brace_depth == 0 => {
// Simple value ends at comma
end_line = line_num;
current_line = line_num + 1;
break;
}
_ => {}
}
}
if end_line > start_line {
break;
}
}
}
if found && end_line >= start_line {
chunks.push(SymbolChunk {
symbol_name: Some(key.clone()),
kind: "json_key".to_string(),
signature: None,
docstring: None,
start_line: start_line as i32,
end_line: end_line as i32,
metadata: None,
});
}
}
}
chunks
}
/// Extract chunks from YAML source code
pub(super) fn extract_yaml_chunks(source: &str) -> Vec<SymbolChunk> {
// YAML chunking: create chunks for top-level keys and nested sections
let mut chunks = Vec::new();
let lines: Vec<&str> = source.lines().collect();
let mut i = 0;
while i < lines.len() {
let line = lines[i];
// Skip empty lines and comments
if line.trim().is_empty() || line.trim().starts_with('#') {
i += 1;
continue;
}
// Check if this is a top-level key (no leading spaces)
if !line.starts_with(' ') && !line.starts_with('\t') && line.contains(':') {
// Found a top-level key
let key = line.split(':').next().unwrap_or("").trim();
if key.is_empty() {
i += 1;
continue;
}
let start_line = i + 1;
let mut end_line = start_line;
// Find where this section ends (next top-level key or EOF)
let mut j = i + 1;
while j < lines.len() {
let next_line = lines[j];
// Check if we hit another top-level key
if !next_line.starts_with(' ')
&& !next_line.starts_with('\t')
&& !next_line.trim().is_empty()
&& !next_line.trim().starts_with('#')
&& next_line.contains(':')
{
break;
}
end_line = j + 1;
j += 1;
}
chunks.push(SymbolChunk {
symbol_name: Some(key.to_string()),
kind: "yaml_key".to_string(),
signature: None,
docstring: None,
start_line: start_line as i32,
end_line: end_line as i32,
metadata: None,
});
i = j;
} else {
i += 1;
}
}
chunks
}
/// Extract chunks from TOML source code
pub(super) fn extract_toml_chunks(source: &str) -> Vec<SymbolChunk> {
// TOML chunking: create chunks for sections [section] and top-level keys
let mut chunks = Vec::new();
let lines: Vec<&str> = source.lines().collect();
let mut i = 0;
while i < lines.len() {
let line = lines[i];
let trimmed = line.trim();
// Skip empty lines and comments
if trimmed.is_empty() || trimmed.starts_with('#') {
i += 1;
continue;
}
// Check for section headers [section] or [[array]]
if trimmed.starts_with('[') {
let section_name = trimmed
.trim_start_matches('[')
.trim_start_matches('[')
.trim_end_matches(']')
.trim_end_matches(']')
.trim();
if section_name.is_empty() {
i += 1;
continue;
}
let start_line = i + 1;
let mut end_line = start_line;
// Find where this section ends (next section or EOF)
let mut j = i + 1;
while j < lines.len() {
let next_line = lines[j].trim();
// Check if we hit another section
if next_line.starts_with('[') {
break;
}
end_line = j + 1;
j += 1;
}
chunks.push(SymbolChunk {
symbol_name: Some(section_name.to_string()),
kind: "toml_section".to_string(),
signature: None,
docstring: None,
start_line: start_line as i32,
end_line: end_line as i32,
metadata: None,
});
i = j;
} else {
i += 1;
}
}
// If no sections found but file has content, look for top-level keys
if chunks.is_empty() && !lines.is_empty() {
// Simple approach: chunk by top-level keys
let mut i = 0;
while i < lines.len() {
let line = lines[i];
let trimmed = line.trim();
if !trimmed.is_empty() && !trimmed.starts_with('#') && trimmed.contains('=') {
let key = trimmed.split('=').next().unwrap_or("").trim();
if !key.is_empty() {
chunks.push(SymbolChunk {
symbol_name: Some(key.to_string()),
kind: "toml_key".to_string(),
signature: None,
docstring: None,
start_line: (i + 1) as i32,
end_line: (i + 1) as i32,
metadata: None,
});
}
}
i += 1;
}
}
chunks
}