Skip to content

Commit cdde24a

Browse files
Copilottninja
andauthored
Extract duplicated region location logic into shared helper function (#49)
* Initial plan * Extract duplicated region location logic into shared helper function Co-authored-by: tninja <714625+tninja@users.noreply.github.com> * Remove unused variables and simplify region location info extraction * Fix docstring to correctly describe return value of region info function --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tninja <714625+tninja@users.noreply.github.com> Co-authored-by: tninja <tninja@gmail.com>
1 parent 4966d75 commit cdde24a

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

ai-code-change.el

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
(declare-function ai-code--insert-prompt "ai-code-prompt-mode")
2222
(declare-function ai-code--get-clipboard-text "ai-code-interface")
2323
(declare-function ai-code--get-git-relative-paths "ai-code-discussion")
24+
(declare-function ai-code--get-region-location-info "ai-code-discussion")
2425

2526
(defun ai-code--is-comment-line (line)
2627
"Check if LINE is a comment line based on current buffer's comment syntax.
@@ -88,7 +89,6 @@ If a region is selected, change that specific region.
8889
Otherwise, change the function under cursor.
8990
If nothing is selected and no function context, prompts for general code change.
9091
Inserts the prompt into the AI prompt file and optionally sends to AI.
91-
9292
Argument ARG is the prefix argument."
9393
(interactive "P")
9494
(unless buffer-file-name
@@ -98,14 +98,8 @@ Argument ARG is the prefix argument."
9898
(region-active (region-active-p))
9999
(region-text (when region-active
100100
(buffer-substring-no-properties (region-beginning) (region-end))))
101-
(region-start-line (when region-active
102-
(line-number-at-pos (region-beginning))))
103-
(region-end-line (when region-active
104-
(line-number-at-pos (region-end))))
105-
(git-relative-path (when (and region-active buffer-file-name)
106-
(car (ai-code--get-git-relative-paths (list buffer-file-name)))))
107-
(region-location-info (when (and region-active git-relative-path region-start-line region-end-line)
108-
(format "%s#L%d-L%d" git-relative-path region-start-line region-end-line)))
101+
(region-location-info (when region-active
102+
(ai-code--get-region-location-info (region-beginning) (region-end))))
109103
(prompt-label
110104
(cond
111105
((and clipboard-context

ai-code-discussion.el

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,8 @@ CLIPBOARD-CONTEXT is optional clipboard text to append as context."
9191
(region-active (region-active-p))
9292
(region-text (when region-active
9393
(buffer-substring-no-properties (region-beginning) (region-end))))
94-
(region-start-line (when region-active
95-
(line-number-at-pos (region-beginning))))
96-
(region-end-line (when region-active
97-
(line-number-at-pos (region-end))))
98-
(git-relative-path (when (and region-active buffer-file-name)
99-
(car (ai-code--get-git-relative-paths (list buffer-file-name)))))
100-
(region-location-info (when (and region-active git-relative-path region-start-line region-end-line)
101-
(format "%s#L%d-L%d" git-relative-path region-start-line region-end-line)))
94+
(region-location-info (when region-active
95+
(ai-code--get-region-location-info (region-beginning) (region-end))))
10296
(prompt-label
10397
(cond
10498
((and clipboard-context
@@ -152,6 +146,19 @@ Returns a list of relative paths from the git repository root."
152146
(file-relative-name file-path git-root))
153147
file-paths)))))
154148

149+
(defun ai-code--get-region-location-info (region-beginning region-end)
150+
"Compute region location information for the active region.
151+
Returns region-location-info
152+
REGION-BEGINNING and REGION-END are the region boundaries.
153+
Returns nil if region is not active or required information is unavailable."
154+
(when (and region-beginning region-end buffer-file-name)
155+
(let* ((region-end-line (line-number-at-pos region-end))
156+
(region-start-line (line-number-at-pos region-beginning))
157+
(git-relative-path (car (ai-code--get-git-relative-paths (list buffer-file-name))))
158+
(region-location-info (when (and git-relative-path region-start-line region-end-line)
159+
(format "%s#L%d-L%d" git-relative-path region-start-line region-end-line))))
160+
region-location-info)))
161+
155162
;;;###autoload
156163
(defun ai-code-investigate-exception (arg)
157164
"Generate prompt to investigate exceptions or errors in code.

0 commit comments

Comments
 (0)