From 432d56ef45c9369bc87518b3c8d305eeb40e6dd6 Mon Sep 17 00:00:00 2001 From: Vineet Kumar Date: Mon, 25 May 2026 22:43:43 -0400 Subject: consolidate hl-file-read functions --- llm-tools-hl-test.el | 10 +++++----- llm-tools-hl.el | 35 +++++++++++++++++------------------ 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/llm-tools-hl-test.el b/llm-tools-hl-test.el index e796ac8..5b50f14 100644 --- a/llm-tools-hl-test.el +++ b/llm-tools-hl-test.el @@ -234,15 +234,15 @@ export function greet(name) { "Test formatting lines in hashline format." (let ((lines '("hello" "world"))) ;; Without content - (let ((no-content (llm-tools--hl-format-lines lines))) + (let ((no-content (llm-tools--hl-format-lines lines nil t))) (should (= (length (string-lines no-content)) 2)) (should (string-match-p "\\`1.." no-content))) ;; With content - (let ((with-content (llm-tools--hl-format-lines lines nil t))) + (let ((with-content (llm-tools--hl-format-lines lines))) (should (string-match-p "\\`1..|hello" with-content)) (should (string-match-p "2..|world\\'" with-content))) ;; With custom start number - (let ((custom-start (llm-tools--hl-format-lines lines 5))) + (let ((custom-start (llm-tools--hl-format-lines lines 5 t))) (should (string-match-p "\\`5.." custom-start)) (should (string-match-p "6..\\'" custom-start))))) @@ -252,7 +252,7 @@ export function greet(name) { (let* ((content "line1\nline2\nline3\nline4\n") (file (make-temp-file nil nil nil content))) (unwind-protect - (let ((result (llm-tools--hl-file-read-range file 2 3))) + (let ((result (llm-tools--hl-file-read file 2 3))) (should (= (length (string-lines result)) 2)) (should (string-match-p "\\`2.." result)) (should (string-match-p "line2" result)) @@ -265,7 +265,7 @@ export function greet(name) { (let* ((content "foo\nbar\n") (file (make-temp-file nil nil nil content))) (unwind-protect - (let ((hashes (llm-tools--hl-file-read-hashes file))) + (let ((hashes (llm-tools--hl-file-read file nil nil t t))) (should (= (length hashes) 2)) (should (string-match-p "\\`1.." (elt hashes 0))) (should (string-match-p "\\`2.." (elt hashes 1))) diff --git a/llm-tools-hl.el b/llm-tools-hl.el index 9b2a9c2..49e67fb 100644 --- a/llm-tools-hl.el +++ b/llm-tools-hl.el @@ -68,31 +68,30 @@ With optional BEG and END (inclusive, 1-based), return the lines between that ra (cl-subseq lines (1- beg) end) lines)))) -(defun llm-tools--hl-format-lines (lines &optional start include-content?) +(defun llm-tools--hl-format-lines (lines &optional start only-anchors?) "Format LINES in hashline format. START is 1-based (default 1). -If INCLUDE-CONTENT is non-nil, append `|LINE' after each hash." +If ONLY-ANCHORS? is non-nil, do not append `|CONTENT' after each anchor." (string-join (cl-mapcar (lambda (line i) - (if include-content? - (format "%d%2s|%s" i (llm-tools--hl-hash line) line) - (format "%d%2s" i (llm-tools--hl-hash line)))) + (if only-anchors? + (format "%d%2s" i (llm-tools--hl-hash line)) + (format "%d%2s|%s" i (llm-tools--hl-hash line) line))) lines (number-sequence (or start 1) (+ (or start 1) (1- (length lines))))) "\n")) -(defun llm-tools--hl-file-read (file) - "Return FILE contents in hashline format." - (llm-tools--hl-format-lines (llm-tools--hl-file-lines file) nil t)) - -(defun llm-tools--hl-file-read-range (file beg end) - "Return FILE contents in hashline format from lines BEG to END." - (llm-tools--hl-format-lines (llm-tools--hl-file-lines file beg end) beg t)) - -(defun llm-tools--hl-file-read-hashes (file) - "Return FILE contents as a list of hashline strings without line content. -Each element is a line number and two-character hash, like `1vx'." - (string-lines (llm-tools--hl-format-lines (llm-tools--hl-file-lines file)))) +(defun llm-tools--hl-file-read (file &optional beg end only-anchors? as-list?) + "Return FILE contents in hashline format. +With optional BEG and END (inclusive, 1-based), return lines BEG to END. +If ONLY-ANCHORS? is non-nil, do not append `|CONTENT' after each anchor. +If AS-LIST? is non-nil, return a list of hashline strings instead of a +single string." + (let* ((lines (llm-tools--hl-file-lines file beg end)) + (formatted (llm-tools--hl-format-lines lines (or beg 1) only-anchors?))) + (if as-list? + (string-lines formatted) + formatted))) (defun llm-tools--hl-file-line-hash (file n) "Return the hash of the Nth line in FILE." @@ -310,7 +309,7 @@ line in the on-disk file." (mapcar (lambda (section) (let* ((file (car section)) (anchors (delete "BOF" (delete "EOF" (cdr section)))) - (hl (llm-tools--hl-file-read-hashes file))) + (hl (llm-tools--hl-file-read file nil nil t t))) ;; cl-subsetp uses eql by default which compares by identity and not content (cons file (cl-subsetp anchors hl :test #'equal)))) patch-anchors))) -- cgit v1.2.3