sem-core 0.3.21

Entity-level semantic diff engine. Extracts functions, classes, and methods from 20 languages via tree-sitter and diffs at the entity level.
Documentation
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use crate::model::entity::{build_entity_id, SemanticEntity};
use crate::parser::plugin::SemanticParserPlugin;
use crate::utils::hash::content_hash;

pub struct JsonParserPlugin;

impl SemanticParserPlugin for JsonParserPlugin {
    fn id(&self) -> &str {
        "json"
    }

    fn extensions(&self) -> &[&str] {
        &[".json"]
    }

    fn extract_entities(&self, content: &str, file_path: &str) -> Vec<SemanticEntity> {
        // Extract top-level properties from JSON objects, plus depth-2 children
        // for "object" entities (e.g. scripts, dependencies in package.json).
        // We scan the source text directly to get accurate line positions,
        // which weave needs for entity-level merge reconstruction.
        let trimmed = content.trim();
        if !trimmed.starts_with('{') {
            return Vec::new();
        }

        let lines: Vec<&str> = content.lines().collect();
        let entries = find_top_level_entries(content);
        let closing = find_closing_brace_line(&lines);

        let mut entities = Vec::new();
        for (i, entry) in entries.iter().enumerate() {
            let end_line = if i + 1 < entries.len() {
                let next_start = entries[i + 1].start_line;
                trim_trailing_blanks(&lines, entry.start_line, next_start)
            } else {
                trim_trailing_blanks(&lines, entry.start_line, closing)
            };

            let entity_content = lines[entry.start_line - 1..end_line]
                .join("\n");

            let value_content = extract_value_content(&entity_content);
            let structural_hash = Some(content_hash(value_content));

            let parent_id = build_entity_id(file_path, &entry.entity_type, &entry.pointer, None);

            entities.push(SemanticEntity {
                id: parent_id.clone(),
                file_path: file_path.to_string(),
                entity_type: entry.entity_type.clone(),
                name: entry.key.clone(),
                parent_id: None,
                content_hash: content_hash(&entity_content),
                structural_hash,
                content: entity_content.clone(),
                start_line: entry.start_line,
                end_line,
                metadata: None,
            });

            // Extract depth-2 children from "object" entities
            if entry.entity_type == "object" {
                let nested = find_nested_object_entries(&entity_content, entry.start_line);
                for (j, nentry) in nested.iter().enumerate() {
                    let child_end = if j + 1 < nested.len() {
                        trim_trailing_blanks(&lines, nentry.start_line, nested[j + 1].start_line)
                    } else {
                        trim_trailing_blanks(&lines, nentry.start_line, end_line)
                    };

                    let child_content = lines[nentry.start_line - 1..child_end].join("\n");
                    let child_value = extract_value_content(&child_content);

                    entities.push(SemanticEntity {
                        id: build_entity_id(file_path, &nentry.entity_type, &nentry.key, Some(&parent_id)),
                        file_path: file_path.to_string(),
                        entity_type: nentry.entity_type.clone(),
                        name: nentry.key.clone(),
                        parent_id: Some(parent_id.clone()),
                        content_hash: content_hash(&child_content),
                        structural_hash: Some(content_hash(child_value)),
                        content: child_content,
                        start_line: nentry.start_line,
                        end_line: child_end,
                        metadata: None,
                    });
                }
            }
        }

        entities
    }
}

struct JsonEntry {
    key: String,
    pointer: String,
    entity_type: String,
    start_line: usize, // 1-based
}

/// Scan the source text to find each top-level key in the root JSON object.
/// Returns entries with accurate start_line positions.
fn find_top_level_entries(content: &str) -> Vec<JsonEntry> {
    let mut entries = Vec::new();
    let mut depth = 0;
    let mut in_string = false;
    let mut escape_next = false;
    let mut line_num: usize = 1;

    // State for tracking when we find a key at depth 1
    let mut current_key: Option<String> = None;
    let mut key_start = false;
    let mut key_buf = String::new();
    let mut reading_key = false;

    for ch in content.chars() {
        if ch == '\n' {
            line_num += 1;
            continue;
        }

        if escape_next {
            if reading_key {
                key_buf.push(ch);
            }
            escape_next = false;
            continue;
        }

        if ch == '\\' && in_string {
            if reading_key {
                key_buf.push(ch);
            }
            escape_next = true;
            continue;
        }

        if in_string {
            if ch == '"' {
                in_string = false;
                if reading_key {
                    reading_key = false;
                    current_key = Some(key_buf.clone());
                    key_buf.clear();
                }
            } else if reading_key {
                key_buf.push(ch);
            }
            continue;
        }

        match ch {
            '"' => {
                in_string = true;
                // At depth 1, a string could be a key (before ':') or value (after ':')
                if depth == 1 && current_key.is_none() && !key_start {
                    reading_key = true;
                    key_buf.clear();
                }
            }
            ':' => {
                if depth == 1 {
                    if let Some(ref key) = current_key {
                        // Found a key: value pair at depth 1
                        let escaped_key = key.replace('~', "~0").replace('/', "~1");
                        let pointer = format!("/{escaped_key}");
                        entries.push(JsonEntry {
                            key: key.clone(),
                            pointer,
                            entity_type: String::new(), // filled in below
                            start_line: line_num,
                        });
                        key_start = true;
                    }
                }
            }
            '{' | '[' => {
                depth += 1;
                if depth == 2 && key_start {
                    // The value for this key is an object/array
                    if let Some(entry) = entries.last_mut() {
                        entry.entity_type = "object".to_string();
                    }
                }
            }
            '}' | ']' => {
                depth -= 1;
            }
            ',' => {
                if depth == 1 {
                    // End of a top-level entry
                    if let Some(entry) = entries.last_mut() {
                        if entry.entity_type.is_empty() {
                            entry.entity_type = "property".to_string();
                        }
                    }
                    current_key = None;
                    key_start = false;
                }
            }
            _ => {}
        }
    }

    // Handle last entry (no trailing comma)
    if let Some(entry) = entries.last_mut() {
        if entry.entity_type.is_empty() {
            entry.entity_type = "property".to_string();
        }
    }

    entries
}

/// Find keys inside a depth-1 object value within an entity's content.
/// Returns entries with absolute line numbers computed from `base_line`.
fn find_nested_object_entries(entity_content: &str, base_line: usize) -> Vec<JsonEntry> {
    let mut entries = Vec::new();
    let mut in_string = false;
    let mut escape_next = false;
    let mut line_num: usize = 0; // 0-based offset from base_line
    let mut found_outer_colon = false;
    let mut found_value_start = false;
    let mut value_depth: usize = 0;
    let mut current_key: Option<String> = None;
    let mut reading_key = false;
    let mut key_buf = String::new();
    let mut key_start = false;

    for ch in entity_content.chars() {
        if ch == '\n' {
            line_num += 1;
            continue;
        }

        if escape_next {
            if reading_key {
                key_buf.push(ch);
            }
            escape_next = false;
            continue;
        }

        if ch == '\\' && in_string {
            if reading_key {
                key_buf.push(ch);
            }
            escape_next = true;
            continue;
        }

        if in_string {
            if ch == '"' {
                in_string = false;
                if reading_key {
                    reading_key = false;
                    current_key = Some(key_buf.clone());
                    key_buf.clear();
                }
            } else if reading_key {
                key_buf.push(ch);
            }
            continue;
        }

        if !found_value_start {
            match ch {
                '"' => {
                    in_string = true;
                }
                ':' => {
                    found_outer_colon = true;
                }
                '{' if found_outer_colon => {
                    found_value_start = true;
                    value_depth = 1;
                }
                _ => {}
            }
            continue;
        }

        match ch {
            '"' => {
                in_string = true;
                if value_depth == 1 && current_key.is_none() && !key_start {
                    reading_key = true;
                    key_buf.clear();
                }
            }
            ':' => {
                if value_depth == 1 {
                    if let Some(ref key) = current_key {
                        entries.push(JsonEntry {
                            key: key.clone(),
                            pointer: String::new(),
                            entity_type: "property".to_string(),
                            start_line: base_line + line_num,
                        });
                        key_start = true;
                    }
                }
            }
            '{' | '[' => {
                value_depth += 1;
            }
            '}' | ']' => {
                value_depth -= 1;
                if value_depth == 0 {
                    break;
                }
            }
            ',' => {
                if value_depth == 1 {
                    current_key = None;
                    key_start = false;
                }
            }
            _ => {}
        }
    }

    entries
}

/// Extract just the value portion of a `"key": value` entity content string,
/// stripping the key name so that renamed keys with identical values share the
/// same structural_hash and are detected as renames rather than delete + add.
fn extract_value_content(content: &str) -> &str {
    let mut in_string = false;
    let mut escape_next = false;
    for (i, ch) in content.char_indices() {
        if escape_next {
            escape_next = false;
            continue;
        }
        if ch == '\\' && in_string {
            escape_next = true;
            continue;
        }
        if ch == '"' {
            in_string = !in_string;
        }
        if ch == ':' && !in_string {
            let rest = content[i + 1..].trim();
            return rest.trim_end_matches(',').trim();
        }
    }
    content
}

/// Find the line number (1-based) of the closing `}` of the root object.
fn find_closing_brace_line(lines: &[&str]) -> usize {
    for (i, line) in lines.iter().enumerate().rev() {
        if line.trim() == "}" {
            return i + 1;
        }
    }
    lines.len()
}

/// Walk backwards from next_start to skip trailing blank lines and commas,
/// returning the end_line (1-based, inclusive) for the current entry.
fn trim_trailing_blanks(lines: &[&str], start: usize, next_start: usize) -> usize {
    let mut end = next_start - 1;
    while end > start {
        let trimmed = lines[end - 1].trim();
        if trimmed.is_empty() || trimmed == "," {
            end -= 1;
        } else {
            break;
        }
    }
    end
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::change::ChangeType;
    use crate::model::identity::match_entities;

    #[test]
    fn test_json_line_positions() {
        let content = r#"{
  "name": "my-app",
  "version": "1.0.0",
  "scripts": {
    "build": "tsc",
    "test": "jest"
  },
  "description": "a test app"
}
"#;
        let plugin = JsonParserPlugin;
        let entities = plugin.extract_entities(content, "package.json");

        assert_eq!(entities.len(), 6);

        assert_eq!(entities[0].name, "name");
        assert_eq!(entities[0].start_line, 2);
        assert_eq!(entities[0].end_line, 2);
        assert!(entities[0].parent_id.is_none());

        assert_eq!(entities[1].name, "version");
        assert_eq!(entities[1].start_line, 3);
        assert_eq!(entities[1].end_line, 3);

        assert_eq!(entities[2].name, "scripts");
        assert_eq!(entities[2].entity_type, "object");
        assert_eq!(entities[2].start_line, 4);
        assert_eq!(entities[2].end_line, 7);

        // Depth-2 children of "scripts"
        assert_eq!(entities[3].name, "build");
        assert_eq!(entities[3].start_line, 5);
        assert_eq!(entities[3].end_line, 5);
        assert_eq!(entities[3].parent_id.as_deref(), Some(&entities[2].id as &str));

        assert_eq!(entities[4].name, "test");
        assert_eq!(entities[4].start_line, 6);
        assert_eq!(entities[4].end_line, 6);
        assert_eq!(entities[4].parent_id.as_deref(), Some(&entities[2].id as &str));

        assert_eq!(entities[5].name, "description");
        assert_eq!(entities[5].start_line, 8);
        assert_eq!(entities[5].end_line, 8);
    }

    #[test]
    fn test_rename_detected_end_to_end() {
        let before_content = "{\n  \"timeout\": 30\n}\n";
        let after_content = "{\n  \"request_timeout\": 30\n}\n";
        let plugin = JsonParserPlugin;
        let before = plugin.extract_entities(before_content, "config.json");
        let after = plugin.extract_entities(after_content, "config.json");
        let result = match_entities(&before, &after, "config.json", None, None, None);
        assert_eq!(result.changes.len(), 1);
        assert_eq!(result.changes[0].change_type, ChangeType::Renamed);
        assert_eq!(result.changes[0].entity_name, "request_timeout");
    }

    #[test]
    fn test_renamed_scalar_property_shares_structural_hash() {
        let before_content = "{\n  \"timeout\": 30\n}\n";
        let after_content = "{\n  \"request_timeout\": 30\n}\n";
        let plugin = JsonParserPlugin;
        let before = plugin.extract_entities(before_content, "config.json");
        let after = plugin.extract_entities(after_content, "config.json");
        assert_eq!(before.len(), 1);
        assert_eq!(after.len(), 1);
        // content_hash differs (key name is part of content)
        assert_ne!(before[0].content_hash, after[0].content_hash);
        // structural_hash matches (same value)
        assert_eq!(before[0].structural_hash, after[0].structural_hash);
    }

    #[test]
    fn test_renamed_object_property_shares_structural_hash() {
        let before_content = "{\n  \"config\": {\n    \"port\": 8080\n  }\n}\n";
        let after_content = "{\n  \"settings\": {\n    \"port\": 8080\n  }\n}\n";
        let plugin = JsonParserPlugin;
        let before = plugin.extract_entities(before_content, "config.json");
        let after = plugin.extract_entities(after_content, "config.json");
        // 1 parent + 1 child ("port")
        assert_eq!(before.len(), 2);
        assert_eq!(after.len(), 2);
        assert_ne!(before[0].content_hash, after[0].content_hash);
        assert_eq!(before[0].structural_hash, after[0].structural_hash);
    }
}