From 0b3f0a363d107fb39cc226583111214f454e668f Mon Sep 17 00:00:00 2001 From: Vineet Kumar Date: Fri, 5 Jun 2026 17:33:34 -0400 Subject: fix offset bug in hashline operations --- llm-tools-hl-test.el | 19 +++++++++++++++++++ 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." (should-not (member "some payload" file-anchors)) (should-not (member "another line" file-anchors)) (should-not (member "replacement" file-anchors)))))) + +(ert-deftest llm-tools--hl-apply-replace-delete-offset-test () + "Replace same-count lines then delete a later line. +The replace's offset delta should be 0 (2 payload lines replacing 2 lines). +With the buggy formula (- b a 1) this computed to +2 instead, causing the +delete to target past the end of the buffer." + (let* ((content "A\nB\nC\nD\nE\n") + (file (make-temp-file nil nil nil content)) + (a2 (llm-tools--hl-file-line-hash file 2)) + (a3 (llm-tools--hl-file-line-hash file 3)) + (a5 (llm-tools--hl-file-line-hash file 5)) + (patch (format "@@ %s\n= %s..%s\n~X\n~Y\n- %s..%s" + file a2 a3 a5 a5))) + (unwind-protect + (progn + (llm-tools--hl-apply patch) + (let ((lines (llm-tools--hl-file-lines file))) + (should (equal lines '("A" "X" "Y" "D"))))) + (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." (b (+ (llm-tools--hl-anchor-to-line-num (cadr range)) offset))) (cons (append (cl-subseq content 0 (1- a)) (cl-subseq content b)) - (- offset (- b a 1))))) + (- offset (1+ (- b a)))))) (defun llm-tools--hl-apply-replace (range payload content offset) "Replace lines from (car RANGE) to (cadr RANGE) with PAYLOAD (nil to '(\"\"))" @@ -452,7 +452,7 @@ FILENAME." (cons (append (cl-subseq content 0 (1- a)) payload (cl-subseq content b)) - (+ offset (- (length payload) (- b a 1)))))) + (+ offset (- (length payload) (1+ (- b a))))))) (defun llm-tools--hl-apply-one-op (op content offset) "Apply a single OP to CONTENT (list of strings) at the given OFFSET. -- cgit v1.2.3