agent-file-tools 0.20.0

Agent File Tools — tree-sitter powered code analysis for AI agents
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
//! Handler for the `extract_function` command: extract a range of code into
//! a new function with auto-detected parameters and return value.
//!
//! Follows the edit_symbol.rs pattern: validate → parse → compute → dry_run
//! check → auto_backup → write_format_validate → respond.

use std::path::Path;

use tree_sitter::Parser;

use crate::context::AppContext;
use crate::edit;
use crate::extract::{
    detect_free_variables, detect_return_value, generate_call_site, generate_extracted_function,
    ReturnKind,
};
use crate::indent::detect_indent;
use crate::parser::{detect_language, grammar_for, LangId};
use crate::protocol::{RawRequest, Response};

/// Handle an `extract_function` request.
///
/// Params:
///   - `file` (string, required) — target file path
///   - `name` (string, required) — name for the new function
///   - `start_line` (u32, required) — first line of the range to extract (1-based)
///   - `end_line` (u32, required) — last line (exclusive, 1-based) of the range to extract
///   - `dry_run` (bool, optional) — if true, return diff without writing
///
/// Returns on success:
///   `{ file, name, parameters, return_type, extracted_range, call_site_range, syntax_valid, backup_id }`
///
/// Error codes:
///   - `unsupported_language` — file is not TS/JS/TSX/Python
///   - `this_reference_in_range` — range contains `this`/`self`
pub fn handle_extract_function(req: &RawRequest, ctx: &AppContext) -> Response {
    // --- Extract params ---
    let file = match req.params.get("file").and_then(|v| v.as_str()) {
        Some(f) => f,
        None => {
            return Response::error(
                &req.id,
                "invalid_request",
                "extract_function: missing required param 'file'",
            );
        }
    };

    let name = match req.params.get("name").and_then(|v| v.as_str()) {
        Some(n) => n,
        None => {
            return Response::error(
                &req.id,
                "invalid_request",
                "extract_function: missing required param 'name'",
            );
        }
    };

    let start_line_1based = match req.params.get("start_line").and_then(|v| v.as_u64()) {
        Some(l) if l >= 1 => l as u32,
        Some(_) => {
            return Response::error(
                &req.id,
                "invalid_request",
                "extract_function: 'start_line' must be >= 1 (1-based)",
            );
        }
        None => {
            return Response::error(
                &req.id,
                "invalid_request",
                "extract_function: missing required param 'start_line'",
            );
        }
    };
    let start_line = start_line_1based - 1;

    let end_line_1based = match req.params.get("end_line").and_then(|v| v.as_u64()) {
        Some(l) if l >= 1 => l as u32,
        Some(_) => {
            return Response::error(
                &req.id,
                "invalid_request",
                "extract_function: 'end_line' must be >= 1 (1-based)",
            );
        }
        None => {
            return Response::error(
                &req.id,
                "invalid_request",
                "extract_function: missing required param 'end_line'",
            );
        }
    };
    let end_line = end_line_1based - 1;

    if start_line >= end_line {
        return Response::error(
            &req.id,
            "invalid_request",
            format!(
                "extract_function: start_line ({}) must be less than end_line ({})",
                start_line, end_line
            ),
        );
    }

    // --- Validate file ---
    let path = match ctx.validate_path(&req.id, Path::new(file)) {
        Ok(path) => path,
        Err(resp) => return resp,
    };
    if !path.exists() {
        return Response::error(
            &req.id,
            "file_not_found",
            format!("extract_function: file not found: {}", file),
        );
    }

    // --- Language guard (D101) ---
    let lang = match detect_language(&path) {
        Some(l) => l,
        None => {
            return Response::error(
                &req.id,
                "unsupported_language",
                "extract_function: unsupported file type",
            );
        }
    };

    if !matches!(
        lang,
        LangId::TypeScript | LangId::Tsx | LangId::JavaScript | LangId::Python
    ) {
        return Response::error(
            &req.id,
            "unsupported_language",
            format!(
                "extract_function: only TypeScript/JavaScript/Python files are supported, got {:?}",
                lang
            ),
        );
    }

    // --- Read and parse ---
    let source = match std::fs::read_to_string(&path) {
        Ok(s) => s,
        Err(e) => {
            return Response::error(
                &req.id,
                "file_not_found",
                format!("extract_function: {}: {}", file, e),
            );
        }
    };

    let grammar = grammar_for(lang);
    let mut parser = Parser::new();
    if parser.set_language(&grammar).is_err() {
        return Response::error(
            &req.id,
            "parse_error",
            "extract_function: failed to initialize parser",
        );
    }
    let tree = match parser.parse(source.as_bytes(), None) {
        Some(t) => t,
        None => {
            return Response::error(
                &req.id,
                "parse_error",
                "extract_function: failed to parse file",
            );
        }
    };

    // --- Convert line range to byte range ---
    let start_byte = edit::line_col_to_byte(&source, start_line, 0);
    let end_byte = edit::line_col_to_byte(&source, end_line, 0);

    if start_byte >= source.len() {
        return Response::error(
            &req.id,
            "invalid_request",
            format!(
                "extract_function: start_line {} is beyond end of file",
                start_line
            ),
        );
    }

    // --- Detect free variables ---
    let free_vars = detect_free_variables(&source, &tree, start_byte, end_byte, lang);

    // Check for this/self
    if free_vars.has_this_or_self {
        let keyword = match lang {
            LangId::Python => "self",
            _ => "this",
        };
        return Response::error(
            &req.id,
            "this_reference_in_range",
            format!(
                "extract_function: selected range contains '{}' reference. Consider extracting as a method instead, or move the {} usage outside the extracted range.",
                keyword, keyword
            ),
        );
    }

    // --- Find enclosing function for return value detection ---
    let root = tree.root_node();
    let enclosing_fn = find_enclosing_function_node(&root, start_byte, lang);
    let enclosing_fn_end_byte = enclosing_fn.map(|n| n.end_byte());

    // --- Detect return value ---
    let return_kind = detect_return_value(
        &source,
        &tree,
        start_byte,
        end_byte,
        enclosing_fn_end_byte,
        lang,
    );

    // --- Detect indentation ---
    let indent_style = detect_indent(&source, lang);

    // Determine base indent (indentation of the line where the enclosing function starts,
    // or no indent if at module level)
    let base_indent = if let Some(fn_node) = enclosing_fn {
        let fn_start_line = fn_node.start_position().row;
        get_line_indent(&source, fn_start_line as usize)
    } else {
        String::new()
    };

    // Determine the indent of the extracted range (for the call site)
    let range_indent = get_line_indent(&source, start_line as usize);

    // --- Extract body text ---
    let body_text = &source[start_byte..end_byte];
    let body_text = body_text.trim_end_matches('\n');

    // --- Generate function and call site ---
    let extracted_fn = generate_extracted_function(
        name,
        &free_vars.parameters,
        &return_kind,
        body_text,
        &base_indent,
        lang,
        indent_style,
    );

    let call_site = generate_call_site(
        name,
        &free_vars.parameters,
        &return_kind,
        &range_indent,
        lang,
    );

    // --- Compute new file content ---
    // Insert the extracted function before the enclosing function (or at the range position
    // if there's no enclosing function).
    //
    // For TS/JS, when the enclosing function is wrapped in `export` (or
    // `export default`), the parser reports the function_declaration as
    // a child of an export_statement. If we use fn_node.start_byte() as
    // the insertion point, the `export` keyword stays attached to the
    // start of the file content, and the extracted function gets inserted
    // BETWEEN `export ` and `function`, producing:
    //   `export function newFn(...) {} \n\n function originalFn(...) {}`
    // The export keyword silently jumps from the original function to the
    // extracted one. Fix: if the parent is an export_statement, insert
    // before the export_statement instead.
    let insert_pos = if let Some(fn_node) = enclosing_fn {
        let mut anchor = fn_node;
        if matches!(lang, LangId::TypeScript | LangId::Tsx | LangId::JavaScript) {
            if let Some(parent) = fn_node.parent() {
                if parent.kind() == "export_statement" {
                    anchor = parent;
                }
            }
        }
        anchor.start_byte()
    } else {
        start_byte
    };

    let new_source = build_new_source(
        &source,
        insert_pos,
        start_byte,
        end_byte,
        &extracted_fn,
        &call_site,
    );

    // --- Return type string for the response ---
    let return_type = match &return_kind {
        ReturnKind::Expression(_) => "expression",
        ReturnKind::Variable(_) => "variable",
        ReturnKind::Void => "void",
    };

    // --- Dry-run check ---
    if edit::is_dry_run(&req.params) {
        let dr = edit::dry_run_diff(&source, &new_source, &path);
        return Response::success(
            &req.id,
            serde_json::json!({
                "ok": true,
                "dry_run": true,
                "diff": dr.diff,
                "syntax_valid": dr.syntax_valid,
                "parameters": free_vars.parameters,
                "return_type": return_type,
            }),
        );
    }

    // --- Auto-backup before mutation ---
    let backup_id = match edit::auto_backup(
        ctx,
        req.session(),
        &path,
        &format!("extract_function: {}", name),
    ) {
        Ok(id) => id,
        Err(e) => {
            return Response::error(&req.id, e.code(), e.to_string());
        }
    };

    // --- Write, format, validate ---
    let mut write_result =
        match edit::write_format_validate(&path, &new_source, &ctx.config(), &req.params) {
            Ok(r) => r,
            Err(e) => {
                return Response::error(&req.id, e.code(), e.to_string());
            }
        };

    if let Ok(final_content) = std::fs::read_to_string(&path) {
        write_result.lsp_outcome = ctx.lsp_post_write(&path, &final_content, &req.params);
    }

    let param_count = free_vars.parameters.len();
    log::debug!(
        "extract_function: {} from {}:{}-{} ({} params)",
        name,
        file,
        start_line,
        end_line,
        param_count
    );

    // --- Build response ---
    let syntax_valid = write_result.syntax_valid.unwrap_or(true);

    let mut result = serde_json::json!({
        "file": file,
        "name": name,
        "parameters": free_vars.parameters,
        "return_type": return_type,
        "syntax_valid": syntax_valid,
        "formatted": write_result.formatted,
    });

    if let Some(ref reason) = write_result.format_skipped_reason {
        result["format_skipped_reason"] = serde_json::json!(reason);
    }

    if write_result.validate_requested {
        result["validation_errors"] = serde_json::json!(write_result.validation_errors);
    }
    if let Some(ref reason) = write_result.validate_skipped_reason {
        result["validate_skipped_reason"] = serde_json::json!(reason);
    }

    if let Some(ref id) = backup_id {
        result["backup_id"] = serde_json::json!(id);
    }

    write_result.append_lsp_diagnostics_to(&mut result);
    Response::success(&req.id, result)
}

/// Find the enclosing function node for a byte position.
fn find_enclosing_function_node<'a>(
    root: &'a tree_sitter::Node<'a>,
    byte_pos: usize,
    lang: LangId,
) -> Option<tree_sitter::Node<'a>> {
    let fn_kinds: &[&str] = match lang {
        LangId::TypeScript | LangId::Tsx | LangId::JavaScript => &[
            "function_declaration",
            "method_definition",
            "arrow_function",
            "lexical_declaration",
        ],
        LangId::Python => &["function_definition"],
        _ => &[],
    };

    find_deepest_ancestor(root, byte_pos, fn_kinds)
}

/// Find the deepest ancestor node of given kinds containing byte_pos.
fn find_deepest_ancestor<'a>(
    node: &tree_sitter::Node<'a>,
    byte_pos: usize,
    kinds: &[&str],
) -> Option<tree_sitter::Node<'a>> {
    let mut result: Option<tree_sitter::Node<'a>> = None;
    if kinds.contains(&node.kind()) && node.start_byte() <= byte_pos && byte_pos < node.end_byte() {
        result = Some(*node);
    }

    let child_count = node.child_count();
    for i in 0..child_count {
        if let Some(child) = node.child(i as u32) {
            if child.start_byte() <= byte_pos && byte_pos < child.end_byte() {
                if let Some(deeper) = find_deepest_ancestor(&child, byte_pos, kinds) {
                    result = Some(deeper);
                }
            }
        }
    }

    result
}

/// Get the leading whitespace of a source line.
fn get_line_indent(source: &str, line: usize) -> String {
    source
        .lines()
        .nth(line)
        .map(|l| {
            let trimmed = l.trim_start();
            l[..l.len() - trimmed.len()].to_string()
        })
        .unwrap_or_default()
}

/// Build the new source with the extracted function inserted and the range replaced.
fn build_new_source(
    source: &str,
    insert_pos: usize,
    range_start: usize,
    range_end: usize,
    extracted_fn: &str,
    call_site: &str,
) -> String {
    let mut result = String::with_capacity(source.len() + extracted_fn.len() + 64);

    // Everything before the insertion point
    result.push_str(&source[..insert_pos]);

    // The extracted function + blank line
    result.push_str(extracted_fn);
    result.push_str("\n\n");

    // Everything between insert point and the range start (the original function
    // declaration up to where extraction begins)
    result.push_str(&source[insert_pos..range_start]);

    // The call site replacing the original range
    result.push_str(call_site);
    result.push('\n');

    // Everything after the range
    result.push_str(&source[range_end..]);

    result
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::protocol::RawRequest;

    fn make_request(id: &str, command: &str, params: serde_json::Value) -> RawRequest {
        RawRequest {
            id: id.to_string(),
            command: command.to_string(),
            params,
            lsp_hints: None,
            session_id: None,
        }
    }

    // --- Param validation ---

    #[test]
    fn extract_function_missing_file() {
        let req = make_request("1", "extract_function", serde_json::json!({}));
        let ctx = crate::context::AppContext::new(
            Box::new(crate::parser::TreeSitterProvider::new()),
            crate::config::Config::default(),
        );
        let resp = handle_extract_function(&req, &ctx);
        let json = serde_json::to_value(&resp).unwrap();
        assert_eq!(json["success"], false);
        assert_eq!(json["code"], "invalid_request");
        let msg = json["message"].as_str().unwrap();
        assert!(
            msg.contains("file"),
            "message should mention 'file': {}",
            msg
        );
    }

    #[test]
    fn extract_function_missing_name() {
        let req = make_request(
            "2",
            "extract_function",
            serde_json::json!({"file": "/tmp/test.ts"}),
        );
        let ctx = crate::context::AppContext::new(
            Box::new(crate::parser::TreeSitterProvider::new()),
            crate::config::Config::default(),
        );
        let resp = handle_extract_function(&req, &ctx);
        let json = serde_json::to_value(&resp).unwrap();
        assert_eq!(json["success"], false);
        assert_eq!(json["code"], "invalid_request");
        let msg = json["message"].as_str().unwrap();
        assert!(
            msg.contains("name"),
            "message should mention 'name': {}",
            msg
        );
    }

    #[test]
    fn extract_function_missing_start_line() {
        let req = make_request(
            "3",
            "extract_function",
            serde_json::json!({"file": "/tmp/test.ts", "name": "foo"}),
        );
        let ctx = crate::context::AppContext::new(
            Box::new(crate::parser::TreeSitterProvider::new()),
            crate::config::Config::default(),
        );
        let resp = handle_extract_function(&req, &ctx);
        let json = serde_json::to_value(&resp).unwrap();
        assert_eq!(json["success"], false);
        assert_eq!(json["code"], "invalid_request");
    }

    #[test]
    fn extract_function_unsupported_language() {
        // Create a temp .rs file (Rust is not supported for extract_function)
        let dir = std::env::temp_dir().join("aft_test_extract");
        std::fs::create_dir_all(&dir).ok();
        let file = dir.join("test.rs");
        std::fs::write(&file, "fn main() {}").unwrap();

        let req = make_request(
            "4",
            "extract_function",
            serde_json::json!({
                "file": file.display().to_string(),
                "name": "foo",
                "start_line": 1,
                "end_line": 2,
            }),
        );
        let ctx = crate::context::AppContext::new(
            Box::new(crate::parser::TreeSitterProvider::new()),
            crate::config::Config::default(),
        );
        let resp = handle_extract_function(&req, &ctx);
        let json = serde_json::to_value(&resp).unwrap();
        assert_eq!(json["success"], false);
        assert_eq!(json["code"], "unsupported_language");

        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn extract_function_invalid_line_range() {
        let dir = std::env::temp_dir().join("aft_test_extract_range");
        std::fs::create_dir_all(&dir).ok();
        let file = dir.join("test.ts");
        std::fs::write(&file, "const x = 1;\n").unwrap();

        let req = make_request(
            "5",
            "extract_function",
            serde_json::json!({
                "file": file.display().to_string(),
                "name": "foo",
                "start_line": 6,
                "end_line": 4,
            }),
        );
        let ctx = crate::context::AppContext::new(
            Box::new(crate::parser::TreeSitterProvider::new()),
            crate::config::Config::default(),
        );
        let resp = handle_extract_function(&req, &ctx);
        let json = serde_json::to_value(&resp).unwrap();
        assert_eq!(json["success"], false);
        assert_eq!(json["code"], "invalid_request");

        std::fs::remove_dir_all(&dir).ok();
    }

    #[test]
    fn extract_function_this_reference_error() {
        let fixture = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("tests/fixtures/extract_function/sample_this.ts");

        let req = make_request(
            "6",
            "extract_function",
            serde_json::json!({
                "file": fixture.display().to_string(),
                "name": "extracted",
                "start_line": 5,
                "end_line": 8,
            }),
        );
        let ctx = crate::context::AppContext::new(
            Box::new(crate::parser::TreeSitterProvider::new()),
            crate::config::Config::default(),
        );
        let resp = handle_extract_function(&req, &ctx);
        let json = serde_json::to_value(&resp).unwrap();
        assert_eq!(json["success"], false);
        assert_eq!(json["code"], "this_reference_in_range");
    }

    #[test]
    fn extract_function_dry_run_returns_diff() {
        let fixture = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("tests/fixtures/extract_function/sample.ts");

        let req = make_request(
            "7",
            "extract_function",
            serde_json::json!({
                "file": fixture.display().to_string(),
                "name": "computeResult",
                "start_line": 15,
                "end_line": 17,
                "dry_run": true,
            }),
        );
        let ctx = crate::context::AppContext::new(
            Box::new(crate::parser::TreeSitterProvider::new()),
            crate::config::Config::default(),
        );
        let resp = handle_extract_function(&req, &ctx);
        let json = serde_json::to_value(&resp).unwrap();
        assert_eq!(json["success"], true);
        assert_eq!(json["dry_run"], true);
        assert!(json["diff"].as_str().is_some(), "should have diff");
        assert!(json["parameters"].is_array(), "should have parameters");
    }
}