Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions ai-code-change.el
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Copy link

Copilot AI Nov 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] An unintentional blank line was removed from the docstring. While this doesn't affect functionality, it's unrelated to the refactoring purpose and should be preserved to maintain consistency with the original formatting.

Suggested change
Inserts the prompt into the AI prompt file and optionally sends to AI.
Inserts the prompt into the AI prompt file and optionally sends to AI.

Copilot uses AI. Check for mistakes.

Argument ARG is the prefix argument."
(interactive "P")
(unless buffer-file-name
Expand All @@ -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
Expand Down
23 changes: 15 additions & 8 deletions ai-code-discussion.el
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down