diff options
| -rw-r--r-- | llm-tools-hl-test.el | 19 | ||||
| -rw-r--r-- | llm-tools-hl.el | 4 |
2 files changed, 21 insertions, 2 deletions
diff --git a/llm-tools-hl-test.el b/llm-tools-hl-test.el index 879a4b4..82117b7 100644 --- a/llm-tools-hl-test.el +++ b/llm-tools-hl-test.el | |||
| @@ -680,3 +680,22 @@ tried to parse a line like \"~payload\" as an operation." | |||
| 680 | (should-not (member "some payload" file-anchors)) | 680 | (should-not (member "some payload" file-anchors)) |
| 681 | (should-not (member "another line" file-anchors)) | 681 | (should-not (member "another line" file-anchors)) |
| 682 | (should-not (member "replacement" file-anchors)))))) | 682 | (should-not (member "replacement" file-anchors)))))) |
| 683 | |||
| 684 | (ert-deftest llm-tools--hl-apply-replace-delete-offset-test () | ||
| 685 | "Replace same-count lines then delete a later line. | ||
| 686 | The replace's offset delta should be 0 (2 payload lines replacing 2 lines). | ||
| 687 | With the buggy formula (- b a 1) this computed to +2 instead, causing the | ||
| 688 | delete to target past the end of the buffer." | ||
| 689 | (let* ((content "A\nB\nC\nD\nE\n") | ||
| 690 | (file (make-temp-file nil nil nil content)) | ||
| 691 | (a2 (llm-tools--hl-file-line-hash file 2)) | ||
| 692 | (a3 (llm-tools--hl-file-line-hash file 3)) | ||
| 693 | (a5 (llm-tools--hl-file-line-hash file 5)) | ||
| 694 | (patch (format "@@ %s\n= %s..%s\n~X\n~Y\n- %s..%s" | ||
| 695 | file a2 a3 a5 a5))) | ||
| 696 | (unwind-protect | ||
| 697 | (progn | ||
| 698 | (llm-tools--hl-apply patch) | ||
| 699 | (let ((lines (llm-tools--hl-file-lines file))) | ||
| 700 | (should (equal lines '("A" "X" "Y" "D"))))) | ||
| 701 | (delete-file file)))) | ||
diff --git a/llm-tools-hl.el b/llm-tools-hl.el index daf9edd..513b3e8 100644 --- a/llm-tools-hl.el +++ b/llm-tools-hl.el | |||
| @@ -442,7 +442,7 @@ FILENAME." | |||
| 442 | (b (+ (llm-tools--hl-anchor-to-line-num (cadr range)) offset))) | 442 | (b (+ (llm-tools--hl-anchor-to-line-num (cadr range)) offset))) |
| 443 | (cons (append (cl-subseq content 0 (1- a)) | 443 | (cons (append (cl-subseq content 0 (1- a)) |
| 444 | (cl-subseq content b)) | 444 | (cl-subseq content b)) |
| 445 | (- offset (- b a 1))))) | 445 | (- offset (1+ (- b a)))))) |
| 446 | 446 | ||
| 447 | (defun llm-tools--hl-apply-replace (range payload content offset) | 447 | (defun llm-tools--hl-apply-replace (range payload content offset) |
| 448 | "Replace lines from (car RANGE) to (cadr RANGE) with PAYLOAD (nil to '(\"\"))" | 448 | "Replace lines from (car RANGE) to (cadr RANGE) with PAYLOAD (nil to '(\"\"))" |
| @@ -452,7 +452,7 @@ FILENAME." | |||
| 452 | (cons (append (cl-subseq content 0 (1- a)) | 452 | (cons (append (cl-subseq content 0 (1- a)) |
| 453 | payload | 453 | payload |
| 454 | (cl-subseq content b)) | 454 | (cl-subseq content b)) |
| 455 | (+ offset (- (length payload) (- b a 1)))))) | 455 | (+ offset (- (length payload) (1+ (- b a))))))) |
| 456 | 456 | ||
| 457 | (defun llm-tools--hl-apply-one-op (op content offset) | 457 | (defun llm-tools--hl-apply-one-op (op content offset) |
| 458 | "Apply a single OP to CONTENT (list of strings) at the given OFFSET. | 458 | "Apply a single OP to CONTENT (list of strings) at the given OFFSET. |
