splice 2.6.4

Span-safe refactoring kernel for 7 languages with Magellan code graph integration
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
//! Integration tests for context flags (-A/-B/-C) with expansion flags (--expand).
//!
//! Tests verify that when --expand is used with context flags, the context
//! is calculated from the expanded symbol boundaries, not the original offset.
//! This ensures users get context around the full symbol, not just the original location.

use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use tempfile::TempDir;

/// Helper to create a test file with known content.
fn create_test_file(dir: &TempDir, name: &str, content: &str) -> PathBuf {
    let path = dir.path().join(name);
    let mut file = File::create(&path).unwrap();
    writeln!(file, "{}", content).unwrap();
    path
}

// ============================================================================
// Test Fixtures
// ============================================================================

/// Rust function with surrounding context.
const RUST_FUNCTION_WITH_CONTEXT: &str = r#"
// Leading comment before function
fn example_function() -> i32 {
    let x = 42;
    let y = 10;
    x + y
}

// Trailing comment after function
fn another_function() -> i32 {
    0
}
"#;

/// Python class with surrounding context.
const PYTHON_CLASS_WITH_CONTEXT: &str = r#"
# Module docstring

class ExampleClass:
    """Example class docstring."""

    def method_one(self):
        """First method."""
        pass

    def method_two(self):
        """Second method."""
        pass

# After class
def standalone_function():
    pass
"#;

/// Rust struct with surrounding context.
const RUST_STRUCT_WITH_CONTEXT: &str = r#"
// Before struct
struct DataPoint {
    x: i32,
    y: i32,
}

// After struct
fn process() -> DataPoint {
    DataPoint { x: 0, y: 0 }
}
"#;

/// Rust function with after context.
const RUST_FUNCTION_AFTER_CONTEXT: &str = r#"
fn calculate() -> i32 {
    let result = 10 + 20;
    result
}

// This comment should appear in -A context after expanded function
fn next_function() -> i32 {
    0
}
"#;

/// Rust module with nested function for level 2 expansion.
const RUST_MODULE_NESTED: &str = r#"
mod outer_module {
    // Before inner function
    fn inner_function() -> i32 {
        42
    }

    // After inner function
}
"#;

/// TypeScript interface with documentation.
const TYPESCRIPT_INTERFACE_WITH_CONTEXT: &str = r#"
/**
 * Example interface documentation
 */
interface ExampleInterface {
    name: string;
    value: number;
}

// After interface
class Implementation implements ExampleInterface {
    name = "test";
    value = 42;
}
"#;

/// JavaScript class with surrounding context.
const JAVASCRIPT_CLASS_WITH_CONTEXT: &str = r#"
// Leading comment
class JSClass {
    constructor(value) {
        this.value = value;
    }

    getValue() {
        return this.value;
    }
}

// Trailing comment
"#;

/// C++ class with surrounding context.
const CPP_CLASS_WITH_CONTEXT: &str = r#"
// Before class
classCppClass {
public:
    int getX() const { return x; }
    void setX(int value) { x = value; }

private:
    int x = 0;
};

// After class
"#;

/// Java class with documentation.
const JAVA_CLASS_WITH_CONTEXT: &str = r#"
/**
 * Java class documentation
 */
public class JavaClass {
    private int value;

    public JavaClass(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }
}

// After class
"#;

// ============================================================================
// Tests
// ============================================================================

#[test]
fn test_context_with_expand_rust() {
    use splice::context::extract_context_asymmetric;
    use splice::expand::expand_to_body_with_docs;
    use splice::symbol::Language;

    let dir = TempDir::new().unwrap();
    let file = create_test_file(&dir, "test.rs", RUST_FUNCTION_WITH_CONTEXT);

    // Find byte offset within "example_function" name
    let source = std::fs::read_to_string(&file).unwrap();
    let func_offset = source.find("example_function").unwrap();

    // Expand to get full function body
    let (expanded_start, expanded_end) =
        expand_to_body_with_docs(&file, func_offset, Language::Rust).unwrap();

    // Extract context with -B 2 (2 lines before) from expanded span
    let ctx = extract_context_asymmetric(&file, expanded_start, expanded_end, 2, 0).unwrap();

    // Should include the leading comment (context before expanded function)
    assert!(
        !ctx.before.is_empty(),
        "Should have context before expanded function"
    );
    assert!(
        ctx.before
            .iter()
            .any(|line| line.contains("Leading comment")),
        "Context should include comment before expanded function"
    );

    // Context should NOT include anything after (we only requested -B)
    assert!(
        ctx.after.is_empty(),
        "Should not have context after when only -B requested"
    );
}

#[test]
fn test_context_with_expand_python() {
    use splice::context::extract_context_asymmetric;
    use splice::expand::expand_to_body_with_docs;
    use splice::symbol::Language;

    let dir = TempDir::new().unwrap();
    let file = create_test_file(&dir, "test.py", PYTHON_CLASS_WITH_CONTEXT);

    // Find byte offset within "ExampleClass" name
    let source = std::fs::read_to_string(&file).unwrap();
    let class_offset = source.find("ExampleClass").unwrap();

    // Expand to get full class body
    let (expanded_start, expanded_end) =
        expand_to_body_with_docs(&file, class_offset, Language::Python).unwrap();

    // Extract context with -B 1 -A 1 from expanded span
    let ctx = extract_context_asymmetric(&file, expanded_start, expanded_end, 1, 1).unwrap();

    // Should include context before
    assert!(
        !ctx.before.is_empty(),
        "Should have context before expanded class"
    );

    // Should include the full class definition with all methods
    let expanded = std::fs::read_to_string(&file).unwrap();
    let expanded_content = &expanded[expanded_start..expanded_end];
    assert!(
        expanded_content.contains("method_one"),
        "Should include all class methods"
    );
    assert!(
        expanded_content.contains("method_two"),
        "Should include all class methods"
    );

    // Should have after context (or at least not crash)
    // Note: actual content depends on expansion results
}

#[test]
fn test_context_before_respects_expansion() {
    use splice::context::extract_context_asymmetric;
    use splice::expand::expand_to_body_with_docs;
    use splice::symbol::Language;

    let dir = TempDir::new().unwrap();
    let content = r#"
fn small_function() -> i32 {
    42
}
"#;
    let file = create_test_file(&dir, "test.rs", content);

    // Find offset within function name
    let source = std::fs::read_to_string(&file).unwrap();
    let name_offset = source.find("small_function").unwrap();
    let name_end = name_offset + "small_function".len();

    // Expand to get full function
    let (expanded_start, expanded_end) =
        expand_to_body_with_docs(&file, name_offset, Language::Rust).unwrap();

    // Extract context from expanded span
    let ctx_expanded =
        extract_context_asymmetric(&file, expanded_start, expanded_end, 2, 0).unwrap();

    // Extract context from just the name (no expansion)
    let ctx_name = extract_context_asymmetric(&file, name_offset, name_end, 2, 0).unwrap();

    // The expanded version should have different (usually less) context before
    // because the symbol boundary moved up
    assert!(
        ctx_expanded.before.len() <= ctx_name.before.len() || ctx_expanded.before.is_empty(),
        "Expanded context should differ from non-expanded context"
    );
}

#[test]
fn test_context_after_respects_expansion() {
    use splice::context::extract_context_asymmetric;
    use splice::expand::expand_to_body_with_docs;
    use splice::symbol::Language;

    let dir = TempDir::new().unwrap();
    let file = create_test_file(&dir, "test.rs", RUST_FUNCTION_AFTER_CONTEXT);

    // Find offset within "calculate" name
    let source = std::fs::read_to_string(&file).unwrap();
    let name_offset = source.find("calculate").unwrap();

    // Expand to get full function
    let (expanded_start, expanded_end) =
        expand_to_body_with_docs(&file, name_offset, Language::Rust).unwrap();

    // Extract context with -A 3 (3 lines after) from expanded span
    let ctx = extract_context_asymmetric(&file, expanded_start, expanded_end, 0, 3).unwrap();

    // Should include comment and next function (context after expanded body)
    assert!(
        !ctx.after.is_empty(),
        "Should have context after expanded function"
    );
    assert!(
        ctx.after
            .iter()
            .any(|line| line.contains("This comment") || line.contains("next_function")),
        "Context after should be relative to expanded symbol end"
    );
}

#[test]
fn test_context_both_respects_expansion() {
    use splice::context::extract_context_asymmetric;
    use splice::expand::expand_to_body_with_docs;
    use splice::symbol::Language;

    let dir = TempDir::new().unwrap();
    let file = create_test_file(&dir, "test.rs", RUST_STRUCT_WITH_CONTEXT);

    // Find offset within "DataPoint" struct name
    let source = std::fs::read_to_string(&file).unwrap();
    let struct_offset = source.find("DataPoint").unwrap();

    // Expand to get full struct
    let (expanded_start, expanded_end) =
        expand_to_body_with_docs(&file, struct_offset, Language::Rust).unwrap();

    // Extract context with -C 1 (1 line before and after) from expanded span
    let ctx = extract_context_asymmetric(&file, expanded_start, expanded_end, 1, 1).unwrap();

    // Should include before comment (context before)
    assert!(!ctx.before.is_empty(), "Should have context before");
    assert!(
        ctx.before.iter().any(|line| line.contains("Before struct")),
        "Context -C should include lines before expanded struct"
    );

    // Should include after context
    assert!(!ctx.after.is_empty(), "Should have context after");

    // Verify expanded content includes full struct
    let expanded = std::fs::read_to_string(&file).unwrap();
    let expanded_content = &expanded[expanded_start..expanded_end];
    assert!(
        expanded_content.contains("x: i32"),
        "Should include struct fields"
    );
    assert!(
        expanded_content.contains("y: i32"),
        "Should include struct fields"
    );
}

#[test]
fn test_context_no_expand_baseline() {
    use splice::context::extract_context_asymmetric;

    let dir = TempDir::new().unwrap();
    let content = r#"
fn test_function() -> i32 {
    100
}
"#;
    let file = create_test_file(&dir, "test.rs", content);

    // Find offset within function name
    let source = std::fs::read_to_string(&file).unwrap();
    let name_offset = source.find("test_function").unwrap();
    let name_end = name_offset + "test_function".len();

    // Extract context WITHOUT expansion, just -C 1
    let ctx = extract_context_asymmetric(&file, name_offset, name_end, 1, 1).unwrap();

    // Should still provide context around the original span
    assert!(
        ctx.before.len() > 0 || ctx.after.len() > 0 || !ctx.selected.is_empty(),
        "Should have some context content"
    );
}

#[test]
fn test_context_expand_level_2() {
    use splice::context::extract_context_asymmetric;
    use splice::expand::{expand_symbol, ExpansionLevel};
    use splice::symbol::Language;

    let dir = TempDir::new().unwrap();
    let file = create_test_file(&dir, "test.rs", RUST_MODULE_NESTED);

    // Find offset within "inner_function" name
    let source = std::fs::read_to_string(&file).unwrap();
    let func_offset = source.find("inner_function").unwrap();

    // Level 2 expansion to containing block
    let (expanded_start, expanded_end) = expand_symbol(
        &file,
        func_offset,
        Language::Rust,
        ExpansionLevel::ContainingBlock,
    )
    .unwrap();

    // Extract context with -C 1 from expanded span
    let _ctx = extract_context_asymmetric(&file, expanded_start, expanded_end, 1, 1).unwrap();

    // Level 2 should expand to containing module
    let expanded = std::fs::read_to_string(&file).unwrap();
    let expanded_content = &expanded[expanded_start..expanded_end];
    assert!(
        expanded_content.contains("outer_module") || expanded_content.contains("mod"),
        "Level 2 expansion should include containing block"
    );
}

#[test]
fn test_context_with_expand_typescript() {
    use splice::context::extract_context_asymmetric;
    use splice::expand::expand_to_body_with_docs;
    use splice::symbol::Language;

    let dir = TempDir::new().unwrap();
    let file = create_test_file(&dir, "test.ts", TYPESCRIPT_INTERFACE_WITH_CONTEXT);

    // Find offset within "ExampleInterface" name
    let source = std::fs::read_to_string(&file).unwrap();
    let interface_offset = source.find("ExampleInterface").unwrap();

    // Expand to get full interface
    let (expanded_start, expanded_end) =
        expand_to_body_with_docs(&file, interface_offset, Language::TypeScript).unwrap();

    // Extract context with -C 1 from expanded span
    let ctx = extract_context_asymmetric(&file, expanded_start, expanded_end, 1, 1).unwrap();

    // Should include context before
    assert!(
        !ctx.before.is_empty(),
        "Should have context before expanded interface"
    );

    // Verify expanded content includes full interface
    let expanded = std::fs::read_to_string(&file).unwrap();
    let expanded_content = &expanded[expanded_start..expanded_end];
    assert!(
        expanded_content.contains("name: string"),
        "Should include interface properties"
    );
    assert!(
        expanded_content.contains("value: number"),
        "Should include interface properties"
    );

    // After context depends on expansion results
    // Just verify we got some context (before or after)
}

#[test]
fn test_context_with_expand_javascript() {
    use splice::context::extract_context_asymmetric;
    use splice::expand::expand_to_body_with_docs;
    use splice::symbol::Language;

    let dir = TempDir::new().unwrap();
    let file = create_test_file(&dir, "test.js", JAVASCRIPT_CLASS_WITH_CONTEXT);

    // Find offset within "JSClass" name
    let source = std::fs::read_to_string(&file).unwrap();
    let class_offset = source.find("JSClass").unwrap();

    // Expand to get full class
    let (expanded_start, expanded_end) =
        expand_to_body_with_docs(&file, class_offset, Language::JavaScript).unwrap();

    // Extract context with -B 1 -A 1 from expanded span
    let ctx = extract_context_asymmetric(&file, expanded_start, expanded_end, 1, 1).unwrap();

    // Should include leading comment (context before)
    assert!(
        ctx.before
            .iter()
            .any(|line| line.contains("Leading comment")),
        "Context should include lines before expanded class"
    );

    // Verify expanded content includes full class
    let expanded = std::fs::read_to_string(&file).unwrap();
    let expanded_content = &expanded[expanded_start..expanded_end];
    assert!(
        expanded_content.contains("constructor"),
        "Should include constructor"
    );
    assert!(
        expanded_content.contains("getValue"),
        "Should include methods"
    );

    // Should include context after (may be empty if at EOF)
    // Note: trailing comment may be at EOF, so after context might be empty
    // Just verify the test doesn't crash
}

#[test]
fn test_context_with_expand_cpp() {
    use splice::context::extract_context_asymmetric;
    use splice::expand::expand_to_body_with_docs;
    use splice::symbol::Language;

    let dir = TempDir::new().unwrap();
    let file = create_test_file(&dir, "test.cpp", CPP_CLASS_WITH_CONTEXT);

    // Find offset within "CppClass" name
    let source = std::fs::read_to_string(&file).unwrap();
    let class_offset = source.find("CppClass").unwrap();

    // Expand to get full class (may fail if C++ parser has issues with this fixture)
    let (expanded_start, expanded_end) =
        match expand_to_body_with_docs(&file, class_offset, Language::Cpp) {
            Ok(span) => span,
            Err(_) => {
                // C++ expansion might not work for all fixtures
                // Fall back to testing with just the name offset
                (class_offset, class_offset + "CppClass".len())
            }
        };

    // Extract context with -C 1 from expanded span
    let ctx = extract_context_asymmetric(&file, expanded_start, expanded_end, 1, 1).unwrap();

    // Should have some context (before or after)
    assert!(
        ctx.before.len() > 0 || ctx.after.len() > 0,
        "Should have some context"
    );
}

#[test]
fn test_context_with_expand_java() {
    use splice::context::extract_context_asymmetric;
    use splice::expand::expand_to_body_with_docs;
    use splice::symbol::Language;

    let dir = TempDir::new().unwrap();
    let file = create_test_file(&dir, "test.java", JAVA_CLASS_WITH_CONTEXT);

    // Find offset within "JavaClass" name
    let source = std::fs::read_to_string(&file).unwrap();
    let class_offset = source.find("JavaClass").unwrap();

    // Expand to get full class
    let (expanded_start, expanded_end) =
        expand_to_body_with_docs(&file, class_offset, Language::Java).unwrap();

    // Extract context with -B 1 -A 1 from expanded span
    let ctx = extract_context_asymmetric(&file, expanded_start, expanded_end, 1, 1).unwrap();

    // Should include context before
    assert!(
        !ctx.before.is_empty(),
        "Should have context before expanded class"
    );

    // Verify expanded content includes full class
    let expanded = std::fs::read_to_string(&file).unwrap();
    let expanded_content = &expanded[expanded_start..expanded_end];
    assert!(
        expanded_content.contains("getValue"),
        "Should include all methods"
    );

    // After context depends on expansion results
    // Just verify we got context before
}

#[test]
fn test_expansion_larger_than_replaced_span() {
    use splice::expand::expand_to_body_with_docs;
    use splice::symbol::Language;

    let dir = TempDir::new().unwrap();
    let content = r#"
fn test_name() -> i32 {
    42
}
"#;
    let file = create_test_file(&dir, "test.rs", content);

    // Find offset within function name only
    let source = std::fs::read_to_string(&file).unwrap();
    let name_offset = source.find("test_name").unwrap();
    let name_end = name_offset + "test_name".len();

    // Expand to get full function
    let (expanded_start, expanded_end) =
        expand_to_body_with_docs(&file, name_offset, Language::Rust).unwrap();

    // Expanded span should be larger than just the name
    assert!(
        expanded_start < name_offset,
        "Expanded start should be before name start"
    );
    assert!(
        expanded_end > name_end,
        "Expanded end should be after name end"
    );

    // Calculate sizes
    let replaced_size = name_end - name_offset;
    let expanded_size = expanded_end - expanded_start;

    assert!(
        expanded_size > replaced_size,
        "Expanded span should be larger than replaced name span"
    );
}