From e47d7b233e4530a7d39a3eadf05e04f62791c606 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 15 Nov 2025 03:51:53 +0000 Subject: [PATCH 1/4] Initial plan From c1bc96bbb6c4b37e2f7fa21f777a6e188cc42ff3 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 15 Nov 2025 03:55:49 +0000 Subject: [PATCH 2/4] Extract duplicated region location logic into shared helper function Co-authored-by: tninja <714625+tninja@users.noreply.github.com> --- ai-code-change.el | 12 ++++++------ ai-code-discussion.el | 24 ++++++++++++++++++------ 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/ai-code-change.el b/ai-code-change.el index af3b09f..0801a4e 100644 --- a/ai-code-change.el +++ b/ai-code-change.el @@ -21,6 +21,7 @@ (declare-function ai-code--insert-prompt "ai-code-prompt-mode") (declare-function ai-code--get-clipboard-text "ai-code-interface") (declare-function ai-code--get-git-relative-paths "ai-code-discussion") +(declare-function ai-code--get-region-location-info "ai-code-discussion") (defun ai-code--is-comment-line (line) "Check if LINE is a comment line based on current buffer's comment syntax. @@ -100,12 +101,11 @@ Argument ARG is the prefix argument." (buffer-substring-no-properties (region-beginning) (region-end)))) (region-start-line (when region-active (line-number-at-pos (region-beginning)))) - (region-end-line (when region-active - (line-number-at-pos (region-end)))) - (git-relative-path (when (and region-active buffer-file-name) - (car (ai-code--get-git-relative-paths (list buffer-file-name))))) - (region-location-info (when (and region-active git-relative-path region-start-line region-end-line) - (format "%s#L%d-L%d" git-relative-path region-start-line region-end-line))) + (region-info (when region-active + (ai-code--get-region-location-info (region-beginning) (region-end)))) + (region-end-line (nth 0 region-info)) + (git-relative-path (nth 1 region-info)) + (region-location-info (nth 2 region-info)) (prompt-label (cond ((and clipboard-context diff --git a/ai-code-discussion.el b/ai-code-discussion.el index 53ca934..3fa345c 100644 --- a/ai-code-discussion.el +++ b/ai-code-discussion.el @@ -93,12 +93,11 @@ CLIPBOARD-CONTEXT is optional clipboard text to append as context." (buffer-substring-no-properties (region-beginning) (region-end)))) (region-start-line (when region-active (line-number-at-pos (region-beginning)))) - (region-end-line (when region-active - (line-number-at-pos (region-end)))) - (git-relative-path (when (and region-active buffer-file-name) - (car (ai-code--get-git-relative-paths (list buffer-file-name))))) - (region-location-info (when (and region-active git-relative-path region-start-line region-end-line) - (format "%s#L%d-L%d" git-relative-path region-start-line region-end-line))) + (region-info (when region-active + (ai-code--get-region-location-info (region-beginning) (region-end)))) + (region-end-line (nth 0 region-info)) + (git-relative-path (nth 1 region-info)) + (region-location-info (nth 2 region-info)) (prompt-label (cond ((and clipboard-context @@ -152,6 +151,19 @@ Returns a list of relative paths from the git repository root." (file-relative-name file-path git-root)) file-paths))))) +(defun ai-code--get-region-location-info (region-beginning region-end) + "Compute region location information for the active region. +Returns a list of (region-end-line git-relative-path region-location-info). +REGION-BEGINNING and REGION-END are the region boundaries. +Returns nil if region is not active or required information is unavailable." + (when (and region-beginning region-end buffer-file-name) + (let* ((region-end-line (line-number-at-pos region-end)) + (region-start-line (line-number-at-pos region-beginning)) + (git-relative-path (car (ai-code--get-git-relative-paths (list buffer-file-name)))) + (region-location-info (when (and git-relative-path region-start-line region-end-line) + (format "%s#L%d-L%d" git-relative-path region-start-line region-end-line)))) + (list region-end-line git-relative-path region-location-info)))) + ;;;###autoload (defun ai-code-investigate-exception (arg) "Generate prompt to investigate exceptions or errors in code. From 0a1d9d7e04b75b0c28cdccaeae5edada65cec5c3 Mon Sep 17 00:00:00 2001 From: tninja Date: Sun, 16 Nov 2025 15:22:58 -0800 Subject: [PATCH 3/4] Remove unused variables and simplify region location info extraction --- ai-code-change.el | 10 ++-------- ai-code-discussion.el | 11 +++-------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/ai-code-change.el b/ai-code-change.el index 0801a4e..977bfa8 100644 --- a/ai-code-change.el +++ b/ai-code-change.el @@ -89,7 +89,6 @@ If a region is selected, change that specific region. Otherwise, change the function under cursor. If nothing is selected and no function context, prompts for general code change. Inserts the prompt into the AI prompt file and optionally sends to AI. - Argument ARG is the prefix argument." (interactive "P") (unless buffer-file-name @@ -99,13 +98,8 @@ Argument ARG is the prefix argument." (region-active (region-active-p)) (region-text (when region-active (buffer-substring-no-properties (region-beginning) (region-end)))) - (region-start-line (when region-active - (line-number-at-pos (region-beginning)))) - (region-info (when region-active - (ai-code--get-region-location-info (region-beginning) (region-end)))) - (region-end-line (nth 0 region-info)) - (git-relative-path (nth 1 region-info)) - (region-location-info (nth 2 region-info)) + (region-location-info (when region-active + (ai-code--get-region-location-info (region-beginning) (region-end)))) (prompt-label (cond ((and clipboard-context diff --git a/ai-code-discussion.el b/ai-code-discussion.el index 3fa345c..3b330b4 100644 --- a/ai-code-discussion.el +++ b/ai-code-discussion.el @@ -91,13 +91,8 @@ CLIPBOARD-CONTEXT is optional clipboard text to append as context." (region-active (region-active-p)) (region-text (when region-active (buffer-substring-no-properties (region-beginning) (region-end)))) - (region-start-line (when region-active - (line-number-at-pos (region-beginning)))) - (region-info (when region-active - (ai-code--get-region-location-info (region-beginning) (region-end)))) - (region-end-line (nth 0 region-info)) - (git-relative-path (nth 1 region-info)) - (region-location-info (nth 2 region-info)) + (region-location-info (when region-active + (ai-code--get-region-location-info (region-beginning) (region-end)))) (prompt-label (cond ((and clipboard-context @@ -162,7 +157,7 @@ Returns nil if region is not active or required information is unavailable." (git-relative-path (car (ai-code--get-git-relative-paths (list buffer-file-name)))) (region-location-info (when (and git-relative-path region-start-line region-end-line) (format "%s#L%d-L%d" git-relative-path region-start-line region-end-line)))) - (list region-end-line git-relative-path region-location-info)))) + region-location-info))) ;;;###autoload (defun ai-code-investigate-exception (arg) From 0fbbeae2e5032c0823b18108796ccd15451b5f78 Mon Sep 17 00:00:00 2001 From: tninja Date: Sun, 16 Nov 2025 15:26:45 -0800 Subject: [PATCH 4/4] Fix docstring to correctly describe return value of region info function --- ai-code-discussion.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ai-code-discussion.el b/ai-code-discussion.el index 3b330b4..cada94b 100644 --- a/ai-code-discussion.el +++ b/ai-code-discussion.el @@ -148,7 +148,7 @@ Returns a list of relative paths from the git repository root." (defun ai-code--get-region-location-info (region-beginning region-end) "Compute region location information for the active region. -Returns a list of (region-end-line git-relative-path region-location-info). +Returns region-location-info REGION-BEGINNING and REGION-END are the region boundaries. Returns nil if region is not active or required information is unavailable." (when (and region-beginning region-end buffer-file-name)