-
Notifications
You must be signed in to change notification settings - Fork 9
Extract duplicated region location logic into shared helper function #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Co-authored-by: tninja <714625+tninja@users.noreply.github.com>
There was a problem hiding this 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-infohelper function inai-code-discussion.el - Updates both
ai-code--ask-question-fileandai-code-code-changeto 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.
| (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))) |
Copilot
AI
Nov 16, 2025
There was a problem hiding this comment.
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.'
| 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. |
Copilot
AI
Nov 16, 2025
There was a problem hiding this comment.
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.
| 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. |
…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>
Addresses code duplication feedback from #48 where logic for computing
region-end-line,git-relative-path, andregion-location-infowas duplicated betweenai-code-discussion.elandai-code-change.el.Changes
ai-code--get-region-location-infohelper function inai-code-discussion.elthat returns(region-end-line git-relative-path region-location-info)given region boundariesai-code--ask-question-fileandai-code-code-changeto use the helper instead of duplicating the logicai-code-change.elBefore:
After:
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.