commit 31372203feb19e452730b722c6202f11dd9039d2
parent fc2771f952230a3246d5265879ed280302075b06
Author: Vineet Kumar <git@vineetk.net>
Date: Mon, 25 May 2026 20:11:05 -0400
split hl-validate-anchors to be not monolithic
Diffstat:
1 file changed, 29 insertions(+), 20 deletions(-)
diff --git a/llm-tools-hl.el b/llm-tools-hl.el
@@ -177,30 +177,39 @@ Each element is a line number and two-character hash, like `1vx'."
(push "Delete operation (-) must not have payload lines" errors)))
errors))
+(defun llm-tools--hl-validate-insert-anchor (anchor)
+ "Return error string if ANCHOR is malformed for insert, else nil."
+ (when (and anchor
+ (not (string= anchor "EOF"))
+ (not (string= anchor "BOF")))
+ (pcase anchor
+ ((pred (lambda (a)
+ (or (< (length a) 3)
+ (not (string-match-p "^[0-9]" a)))))
+ (format "Invalid anchor %S (expected LINEHASH like '5ff' or EOF/BOF)" anchor))
+ (_ nil))))
+
+(defun llm-tools--hl-validate-range-anchor (anchor)
+ "Return error string if ANCHOR (like \"3gv..6be\") is malformed, else nil."
+ (pcase (split-string anchor "\\.\\.")
+ (`(,a ,b)
+ (when (or (< (length a) 3) (not (string-match-p "^[0-9]" a))
+ (< (length b) 3) (not (string-match-p "^[0-9]" b)))
+ (format "Invalid anchor %S in range (expected numeric line anchor like '5ff')" anchor)))
+ (_
+ (format "Invalid range %S (expected A..B)" anchor))))
+
(defun llm-tools--hl-validate-anchors (ops)
"Check that all anchors are well-formed."
(let (errors)
(dolist (op ops)
- (let ((anchor (hl-verify-op-anchor op)))
- (pcase (hl-verify-op-type op)
- ((or 'insert-after 'insert-before)
- (when (and anchor
- (not (string= anchor "EOF"))
- (not (string= anchor "BOF")))
- (if (or (< (length anchor) 3)
- (not (string-match-p "^[0-9]" anchor)))
- (push (format "Invalid anchor %S (expected LINEHASH like '5ff' or EOF/BOF)" anchor)
- errors))))
- ((or 'delete 'replace)
- (let ((parts (split-string anchor "\\.\\.")))
- (if (not (= (length parts) 2))
- (push (format "Invalid range %S (expected A..B)" anchor) errors)
- (dolist (part parts)
- (when (or (< (length part) 3)
- (not (string-match-p "^[0-9]" part)))
- (push (format "Invalid anchor %S in range (expected numeric line anchor like '5ff')" part)
- errors))))))))
- errors)
+ (pcase (hl-verify-op-type op)
+ ((or 'insert-after 'insert-before)
+ (let ((err (llm-tools--hl-validate-insert-anchor (hl-verify-op-anchor op))))
+ (when err (push err errors))))
+ ((or 'delete 'replace)
+ (let ((err (llm-tools--hl-validate-range-anchor (hl-verify-op-anchor op))))
+ (when err (push err errors))))))
(nreverse errors)))
(defun llm-tools--hl-verify-section (section)