Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 15, 2025

Addresses code duplication feedback from #48 where logic for computing region-end-line, git-relative-path, and region-location-info was duplicated between ai-code-discussion.el and ai-code-change.el.

Changes

  • Added ai-code--get-region-location-info helper function in ai-code-discussion.el that returns (region-end-line git-relative-path region-location-info) given region boundaries
  • Updated ai-code--ask-question-file and ai-code-code-change to use the helper instead of duplicating the logic
  • Added forward declaration in ai-code-change.el

Before:

(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)))

After:

(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))

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
Copilot AI changed the title [WIP] Address feedback on adding region location info in prompts Extract duplicated region location logic into shared helper function Nov 15, 2025
Copilot AI requested a review from tninja November 15, 2025 03:58
Copilot finished work on behalf of tninja November 15, 2025 03:58
@tninja tninja marked this pull request as ready for review November 15, 2025 15:57
@tninja tninja requested a review from Copilot November 16, 2025 23:23
Copilot finished reviewing on behalf of tninja November 16, 2025 23:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors duplicated region location logic by extracting it into a shared helper function ai-code--get-region-location-info. The helper computes the formatted region location string (e.g., "file.el#L10-L20") that was previously duplicated in both ai-code-discussion.el and ai-code-change.el.

  • Adds ai-code--get-region-location-info helper function in ai-code-discussion.el
  • Updates both ai-code--ask-question-file and ai-code-code-change to use the new helper
  • Adds forward declaration in ai-code-change.el

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
ai-code-discussion.el Adds new helper function ai-code--get-region-location-info and refactors ai-code--ask-question-file to use it instead of duplicated logic
ai-code-change.el Adds forward declaration and refactors ai-code-code-change to use the new helper function

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 149 to 160
(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))))
region-location-info)))
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.

The docstring states the function returns 'a list of (region-end-line git-relative-path region-location-info)' but the implementation only returns region-location-info (a string). The docstring should be updated to: 'Returns the formatted region location string (e.g., "file.el#L10-L20") or nil if unavailable.'

Copilot uses AI. Check for mistakes.
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.
@tninja tninja merged commit cdde24a into kang_feat_1 Nov 16, 2025
tninja added a commit that referenced this pull request Nov 17, 2025
…50)

* Add region location info with git relative path in prompts

* Update ai-code-discussion.el

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update ai-code-change.el

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update ai-code-change.el

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* 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>

---------

Co-authored-by: Kang Tu <kang_tu@apple.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants