diff --git a/ai-code-change.el b/ai-code-change.el index af3b09f..977bfa8 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. @@ -88,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 @@ -98,14 +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-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-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 53ca934..cada94b 100644 --- a/ai-code-discussion.el +++ b/ai-code-discussion.el @@ -91,14 +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-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-location-info (when region-active + (ai-code--get-region-location-info (region-beginning) (region-end)))) (prompt-label (cond ((and clipboard-context @@ -152,6 +146,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 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)))) + region-location-info))) + ;;;###autoload (defun ai-code-investigate-exception (arg) "Generate prompt to investigate exceptions or errors in code.