From 930961bd85701ab8906b8d81095e46d3635a951d Mon Sep 17 00:00:00 2001 From: asepharyana Date: Wed, 15 Jul 2026 05:57:40 +0700 Subject: [PATCH] feat: Tambahkan file baru ke mention_index saat tool write membuatnya Ketika write tool membuat file baru (bukan overwrite), path file sekarang di-push ke mention_index untuk autocomplete @file mention. - Capture file existence status sebelum write - Push ke mention_index hanya untuk genuinely new files - Add 2 tests: one untuk new file push, one untuk overwrite non-duplication Co-Authored-By: Claude Sonnet 5 --- src/tool/fs/write.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/tool/fs/write.rs b/src/tool/fs/write.rs index 500d4b0..bd3114c 100644 --- a/src/tool/fs/write.rs +++ b/src/tool/fs/write.rs @@ -60,12 +60,16 @@ impl Tool for Write { let check_matches = check_graduated_checks(&rel, &content, &ctx.graduated_checks); let path = resolve_path(&ctx.workspaces, &rel)?; let old_content = fs::read_to_string(&path).ok(); + let existed_before = path.exists(); if let Some(parent) = path.parent() { fs::create_dir_all(parent) .map_err(|e| anyhow!("failed to create parent directories for '{rel}': {e}"))?; } fs::write(&path, &content) .map_err(|e| anyhow!("failed to write '{rel}': {e}"))?; + if !existed_before { + ctx.mention_index.push(rel.clone()); + } // Notify the LSP server of the on-disk change so diagnostics stay in // sync. Never fails the write itself: a lock failure or LSP error is // folded into the returned message instead of propagated as an Err. @@ -144,4 +148,25 @@ mod tests { assert!(result.contains("wrote")); fs::remove_dir_all(&workspace).ok(); } + + #[test] + fn write_creating_a_new_file_appends_to_the_mention_index() { + let workspace = temp_workspace(); + let ctx = test_ctx(workspace.clone()); + let args = json!({"path": "brand_new.txt", "content": "hi\n", "reason": "test mention index"}); + Write.run(&ctx, &args).unwrap(); + assert_eq!(ctx.mention_index.snapshot(), vec!["brand_new.txt".to_string()]); + fs::remove_dir_all(&workspace).ok(); + } + + #[test] + fn write_overwriting_a_file_does_not_duplicate_the_mention_index_entry() { + let workspace = temp_workspace(); + fs::write(workspace.join("existing.txt"), "old\n").unwrap(); + let ctx = test_ctx(workspace.clone()); + let args = json!({"path": "existing.txt", "content": "new\n", "reason": "test no duplicate"}); + Write.run(&ctx, &args).unwrap(); + assert!(ctx.mention_index.snapshot().is_empty()); + fs::remove_dir_all(&workspace).ok(); + } }