summaryrefslogtreecommitdiff
path: root/llm-tools-hl.el
diff options
context:
space:
mode:
Diffstat (limited to 'llm-tools-hl.el')
-rw-r--r--llm-tools-hl.el69
1 files changed, 33 insertions, 36 deletions
diff --git a/llm-tools-hl.el b/llm-tools-hl.el
index 0e6d3c8..9b2a9c2 100644
--- a/llm-tools-hl.el
+++ b/llm-tools-hl.el
@@ -159,23 +159,20 @@ Each element is a line number and two-character hash, like `1vx'."
159 159
160(defun llm-tools--hl-validate-insert-has-payload (ops) 160(defun llm-tools--hl-validate-insert-has-payload (ops)
161 "Check that all insert ops have at least one payload line." 161 "Check that all insert ops have at least one payload line."
162 (let (errors) 162 (mapcan (lambda (op)
163 (dolist (op ops) 163 (when (and (memq (hl-verify-op-type op) '(insert-after insert-before))
164 (when (and (memq (hl-verify-op-type op) '(insert-after insert-before)) 164 (null (hl-verify-op-payload op)))
165 (null (hl-verify-op-payload op))) 165 (list (format "Insert operation (%s) has no payload lines following it"
166 (push (format "Insert operation (%s) has no payload lines following it" 166 (hl-verify-op-type op)))))
167 (hl-verify-op-type op)) 167 ops))
168 errors)))
169 (nreverse errors)))
170 168
171(defun llm-tools--hl-validate-delete-no-payload (ops) 169(defun llm-tools--hl-validate-delete-no-payload (ops)
172 "Check that delete ops have no payload lines." 170 "Check that delete ops have no payload lines."
173 (let (errors) 171 (mapcan (lambda (op)
174 (dolist (op ops) 172 (when (and (eq (hl-verify-op-type op) 'delete)
175 (when (and (eq (hl-verify-op-type op) 'delete) 173 (hl-verify-op-payload op))
176 (hl-verify-op-payload op)) 174 (list "Delete operation (-) must not have payload lines")))
177 (push "Delete operation (-) must not have payload lines" errors))) 175 ops))
178 errors))
179 176
180(defun llm-tools--hl-validate-insert-anchor (anchor) 177(defun llm-tools--hl-validate-insert-anchor (anchor)
181 "Return error string if ANCHOR is malformed for insert, else nil." 178 "Return error string if ANCHOR is malformed for insert, else nil."
@@ -201,16 +198,15 @@ Each element is a line number and two-character hash, like `1vx'."
201 198
202(defun llm-tools--hl-validate-anchors (ops) 199(defun llm-tools--hl-validate-anchors (ops)
203 "Check that all anchors are well-formed." 200 "Check that all anchors are well-formed."
204 (let (errors) 201 (mapcan (lambda (op)
205 (dolist (op ops) 202 (pcase (hl-verify-op-type op)
206 (pcase (hl-verify-op-type op) 203 ((or 'insert-after 'insert-before)
207 ((or 'insert-after 'insert-before) 204 (when-let ((err (llm-tools--hl-validate-insert-anchor (hl-verify-op-anchor op))))
208 (let ((err (llm-tools--hl-validate-insert-anchor (hl-verify-op-anchor op)))) 205 (list err)))
209 (when err (push err errors)))) 206 ((or 'delete 'replace)
210 ((or 'delete 'replace) 207 (when-let ((err (llm-tools--hl-validate-range-anchor (hl-verify-op-anchor op))))
211 (let ((err (llm-tools--hl-validate-range-anchor (hl-verify-op-anchor op)))) 208 (list err)))))
212 (when err (push err errors)))))) 209 ops))
213 (nreverse errors)))
214 210
215(defun llm-tools--hl-verify-section (section) 211(defun llm-tools--hl-verify-section (section)
216 "Validate a single SECTION. Returns a list of error strings." 212 "Validate a single SECTION. Returns a list of error strings."
@@ -454,18 +450,19 @@ Returns the modified list of strings."
454 "Apply all operations in PATCH to their respective files. 450 "Apply all operations in PATCH to their respective files.
455Operations within a file are applied sequentially, with line numbers 451Operations within a file are applied sequentially, with line numbers
456adjusted for prior insertions/deletions via a running offset." 452adjusted for prior insertions/deletions via a running offset."
457 (dolist (file-group (llm-tools--hl-group-by-file 453 (mapc (lambda (file-group)
458 (llm-tools--split-patch-sections patch))) 454 (let* ((file (car file-group))
459 (let* ((file (car file-group)) 455 (ops (mapcan (lambda (sec)
460 (ops (mapcan (lambda (sec) 456 (llm-tools--hl-parse-section (cdr sec)))
461 (llm-tools--hl-parse-section (cdr sec))) 457 (cdr file-group)))
462 (cdr file-group))) 458 (content (llm-tools--hl-apply-ops
463 (content (llm-tools--hl-apply-ops 459 ops
464 ops 460 (llm-tools--hl-file-lines file))))
465 (llm-tools--hl-file-lines file)))) 461 (with-temp-buffer
466 (with-temp-buffer 462 (insert (string-join content "\n"))
467 (insert (string-join content "\n")) 463 (write-region (point-min) (point-max) file))))
468 (write-region (point-min) (point-max) file))))) 464 (llm-tools--hl-group-by-file
465 (llm-tools--split-patch-sections patch))))
469 466
470(defun llm-tools--hl-edit (patch) 467(defun llm-tools--hl-edit (patch)
471 "Apply a hashline PATCH to the files it references. 468 "Apply a hashline PATCH to the files it references.