Skip to content

Commit ae5c11e

Browse files
committed
Rewrite: improve start region selection
1 parent 79e05a7 commit ae5c11e

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

eca-rewrite.el

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@
116116
(let ((secs (/ (float ms) 1000)))
117117
(format "%.2f s" secs)))
118118

119+
(defun eca-rewrite--normalize-start-region (start)
120+
"Normalize START region.
121+
If chars from START until bol are all spaces return bol,
122+
otherwise START."
123+
(save-excursion
124+
(goto-char start)
125+
(let ((bol (line-beginning-position)))
126+
(if (string-match-p "\\`[ \t]*\\'"
127+
(buffer-substring bol start))
128+
bol
129+
start))))
130+
119131
(defun eca-rewrite--overlay-menu-str (ov label)
120132
"Return the before-string for overlay OV.
121133
LABEL is the base text prefix."
@@ -484,7 +496,7 @@ PROMPT is the instructions prompt for the LLM."
484496
(eca-assert-session-running (eca-session))
485497
(let* ((existing-ov (eca-rewrite--overlay-at-point))
486498
(start (cond (existing-ov (overlay-start existing-ov))
487-
((use-region-p) (region-beginning))
499+
((use-region-p) (eca-rewrite--normalize-start-region (region-beginning)))
488500
(t (or (car (bounds-of-thing-at-point 'defun))
489501
(car (bounds-of-thing-at-point 'paragraph))
490502
(user-error "No region selected or existing-ov rewrite overlay")))))

0 commit comments

Comments
 (0)