Enhance CLI tool with usage rendering and indented output formatting. Update user prompt for improved clarity and adjust verbosity settings. Modify file listing to return JSON strings for consistent output.

This commit is contained in:
sebseb7
2025-08-11 19:51:06 +02:00
parent f21aa45065
commit 31b8b5c719
3 changed files with 101 additions and 35 deletions

View File

@@ -428,6 +428,38 @@ function find_context(lines, context, start, eof) {
return [start, 0];
}
// Special handling for full-file replacement patterns
// If context is large and starts with deletion lines, try to match at beginning
if (context.length > 3) {
// Try exact match at start
let [new_index, fuzz] = find_context_core(lines, context, 0);
if (new_index !== -1) {
return [new_index, fuzz];
}
// Try fuzzy match at start (allowing for whitespace differences)
let match = true;
let local_fuzz = 0;
const compare_length = Math.min(context.length, lines.length);
for (let j = 0; j < compare_length; j++) {
if (j < lines.length && j < context.length) {
if (lines[j] !== context[j]) {
if (lines[j].trim() === context[j].trim()) {
local_fuzz += 10;
} else if (lines[j].replace(/\s+$/, "") === context[j].replace(/\s+$/, "")) {
local_fuzz += 1;
} else {
// Allow some mismatch for full-file replacements
local_fuzz += 100;
}
}
}
}
if (local_fuzz < context.length * 50) { // Allow up to 50 fuzz per line
return [0, local_fuzz];
}
}
if (eof) {
let [new_index, fuzz] = find_context_core(lines, context, Math.max(0, lines.length - context.length));
if (new_index !== -1) {
@@ -480,11 +512,6 @@ function peek_next_section(lines, index) {
}
s = s.substring(1);
// Handle the case where we're at the beginning and have content
if (index === orig_index + 1 && old.length === 0 && (del_lines.length > 0 || ins_lines.length > 0)) {
// This is the first content line, start collecting
}
if (mode === "keep" && last_mode !== mode && (ins_lines.length > 0 || del_lines.length > 0)) {
const chunk_orig_index = old.length - del_lines.length;
chunks.push(
@@ -622,12 +649,12 @@ function text_to_patch(text, orig, chroot = null) {
}
// Debug logging
console.log("Lines count:", lines.length);
console.log("First line:", JSON.stringify(lines[0]));
//console.log("Lines count:", lines.length);
//console.log("First line:", JSON.stringify(lines[0]));
if (lines.length > 0) {
console.log("Last line:", JSON.stringify(lines[lines.length - 1]));
console.log("First line normalized:", JSON.stringify(Parser._norm(lines[0])));
console.log("Last line normalized:", JSON.stringify(Parser._norm(lines[lines.length - 1])));
//console.log("Last line:", JSON.stringify(lines[lines.length - 1]));
//console.log("First line normalized:", JSON.stringify(Parser._norm(lines[0])));
//console.log("Last line normalized:", JSON.stringify(Parser._norm(lines[lines.length - 1])));
}
if (
@@ -719,13 +746,12 @@ function process_patch(text, open_fn, write_fn, remove_fn, chroot = null) {
if (!text.startsWith("*** Begin Patch")) {
throw new DiffError("Patch text must start with *** Begin Patch");
}
// Load update/delete targets and also attempt to load add targets
// so existing files are detected during parsing
const paths = [
...identify_files_needed(text, chroot),
...identify_files_added(text, chroot),
];
const orig = load_files(paths, open_fn);
// Load ONLY update/delete targets - do NOT load add targets
// because add targets are expected to not exist yet
const updateDeletePaths = identify_files_needed(text, chroot);
const orig = load_files(updateDeletePaths, open_fn);
const [patch, _fuzz] = text_to_patch(text, orig, chroot);
const commit = patch_to_commit(patch, orig, chroot);
apply_commit(commit, write_fn, remove_fn, chroot);
@@ -776,8 +802,6 @@ export default {
};
export async function run(args) {
console.log('patch_files:', args);
try {
const result = process_patch(
args.patch,
@@ -788,9 +812,6 @@ export async function run(args) {
);
return result;
} catch (error) {
if (error instanceof DiffError) {
throw new Error(`Patch error: ${error.message}`);
}
throw error;
return `Patch error: ${error.message}`
}
}