gnodet-bot commented on code in PR #26750:
URL: https://github.com/apache/camel/pull/26750#discussion_r4073157031


##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ai/AuthoringTools.java:
##########
@@ -364,6 +383,67 @@ public static JsonObject validate(ToolContext ctx, Path 
dir, String file, String
     /** How long a write waits for the running integration's reload record 
before answering without it. */
     static final long RELOAD_WAIT_MILLIS = 8000;
 
+    /**
+     * Replaces one snippet of a file and writes the result through {@link 
#writeFile}, so a change to an existing file
+     * does not rewrite every line of it: a model that re-emits a whole file 
corrupts the lines it did not mean to touch
+     * (CAMEL-24909). The snippet must occur exactly once; the answer says 
what was replaced.
+     */
+    public static JsonObject editFile(ToolContext ctx, Path dir, String file, 
String find, String replace) {
+        Path path = resolveFile(dir, file);
+        if (!Files.isRegularFile(path)) {
+            throw new ToolExecutionException(file + " does not exist: write 
the whole file with camel_write_file");
+        }
+        String content;
+        try {
+            content = Files.readString(path, StandardCharsets.UTF_8);
+        } catch (IOException e) {
+            throw new ToolExecutionException("Failed to read " + path + ": " + 
e.getMessage());
+        }
+        if (find.isEmpty()) {
+            throw new ToolExecutionException("find is required: the text to 
replace, as it stands in the file");
+        }
+        int first = content.indexOf(find);
+        if (first < 0) {
+            JsonObject result = new JsonObject();
+            result.put("status", "not-found");
+            result.put("file", file);
+            result.put("message", "The text to find is not in the file as 
given; read it with camel_get_files and copy"
+                                  + " the lines exactly, indentation 
included");
+            return result;
+        }
+        int second = content.indexOf(find, first + find.length());
+        if (second >= 0) {
+            JsonObject result = new JsonObject();
+            result.put("status", "ambiguous");
+            result.put("file", file);
+            result.put("occurrences", count(content, find));
+            result.put("message", "The text to find occurs more than once: 
include the lines around it so it names one"
+                                  + " place, or write the whole file with 
camel_write_file");
+            return result;
+        }
+        int line = (int) content.substring(0, first).lines().count() + (first 
> 0 && content.charAt(first - 1) == '\n' ? 1 : 0);
+        JsonObject result = writeFile(ctx, dir, file, content.substring(0, 
first) + replace
+                                                      + 
content.substring(first + find.length()),
+                true);
+        if (!"invalid".equals(result.getString("status"))) {
+            result.put("status", "edited");
+            result.put("editedAtLine", Math.max(1, line));
+            result.put("replacedLines", find.isEmpty() ? 0 : (int) 
find.lines().count());

Review Comment:
   🔍 **Dead code in `find.isEmpty()` guard (low):** `find` was already 
validated non-empty at line 402 — reaching this point guarantees `find` is 
non-empty, so the ternary `find.isEmpty() ? 0 :` can never be true. The branch 
is unreachable and misleading (it implies `find` could be empty here).
   
   ```suggestion
               result.put("replacedLines", (int) find.lines().count());
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to