branch: elpa/gptel
commit ebdf6aad99eec3bdaff8b4786dcca9b9ac00bd19
Author: Karthik Chikmagalur <[email protected]>
Commit: Karthik Chikmagalur <[email protected]>

    gptel-context: Refactor context region collection
    
    * gptel-context.el (gptel-context--insert-buffer-string,
    gptel-context--collect-regions): Split the insert-buffer-string
    function into two pieces, one to collect the context regions and
    one to insert them.  This makes the feature easier to test.
    
    * test: Add tests for context collection.
---
 gptel-context.el | 33 +++++++++++++++++++--------------
 test             |  2 +-
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/gptel-context.el b/gptel-context.el
index 6756d76943f..bfd4602bb42 100644
--- a/gptel-context.el
+++ b/gptel-context.el
@@ -486,18 +486,12 @@ Ignore overlays, buffers and files that are not live or 
readable."
         ((pred buffer-live-p) (push (list entry) res)))) ;Just a buffer
     res))
 
-(defun gptel-context--insert-buffer-string (buffer context-data &optional 
header)
-  "Insert at point a context string from CONTEXT-DATA in BUFFER.
-
-CONTEXT-DATA is a plist with keys :overlays, :lines and :bounds to
-include specific overlays, line ranges or position bounds instead of the
-entire buffer.  See `gptel-context'.
+(defun gptel-context--collect-regions (buffer context-data)
+  "Collect BUFFER regions from CONTEXT-DATA specification.
 
-HEADER is an optional header to insert before the contents."
-  (let ((is-top-snippet t)
-        (previous-line 1)
-        regions)
-    ;; Collect all regions into a unified list of (start . end) pairs
+CONTEXT-DATA is a plist with keys :overlays, :lines and :bounds.
+Returns a sorted list of (START . END) position pairs."
+  (let (regions)
     (with-current-buffer buffer
       (without-restriction
         ;; Collect overlays
@@ -522,10 +516,21 @@ HEADER is an optional header to insert before the 
contents."
                                (forward-line (cdr pair))
                                (point)))
                   regions)))))
-
     ;; TODO: Update sort for Emacs 28+ calling convention
-    ;; Sort by start position.  In-place, but assign to be sure.
-    (setq regions (sort regions (lambda (a b) (< (car a) (car b)))))
+    ;; Sort by start position.
+    (sort regions (lambda (a b) (< (car a) (car b))))))
+
+(defun gptel-context--insert-buffer-string (buffer context-data &optional 
header)
+  "Insert at point a context string from CONTEXT-DATA in BUFFER.
+
+CONTEXT-DATA is a plist with keys :overlays, :lines and :bounds to
+include specific overlays, line ranges or position bounds instead of the
+entire buffer.  See `gptel-context'.
+
+HEADER is an optional header to insert before the contents."
+  (let ((is-top-snippet t)
+        (previous-line 1)
+        (regions (gptel-context--collect-regions buffer context-data)))
 
     ;; Insert header
     (insert (or header (format "In buffer `%s`:\n\n```"(buffer-name buffer)))
diff --git a/test b/test
index 5f5275c32ed..ac510d33bb3 160000
--- a/test
+++ b/test
@@ -1 +1 @@
-Subproject commit 5f5275c32ed240a03f38d9edef26d920d4535a41
+Subproject commit ac510d33bb3f976d193534e0037d8d119de53981

Reply via email to