Skip to content

Commit c01101f

Browse files
tninjaKang TuCopilot
authored
Chore: Add region location info with git relative path in prompts (#48)
* 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> --------- Co-authored-by: Kang Tu <kang_tu@apple.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 7cfca45 commit c01101f

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

ai-code-change.el

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
(declare-function ai-code-read-string "ai-code-input")
2121
(declare-function ai-code--insert-prompt "ai-code-prompt-mode")
2222
(declare-function ai-code--get-clipboard-text "ai-code-interface")
23+
(declare-function ai-code--get-git-relative-paths "ai-code-discussion")
2324

2425
(defun ai-code--is-comment-line (line)
2526
"Check if LINE is a comment line based on current buffer's comment syntax.
@@ -99,6 +100,12 @@ Argument ARG is the prefix argument."
99100
(buffer-substring-no-properties (region-beginning) (region-end))))
100101
(region-start-line (when region-active
101102
(line-number-at-pos (region-beginning))))
103+
(region-end-line (when region-active
104+
(line-number-at-pos (region-end))))
105+
(git-relative-path (when (and region-active buffer-file-name)
106+
(car (ai-code--get-git-relative-paths (list buffer-file-name)))))
107+
(region-location-info (when (and region-active git-relative-path region-start-line region-end-line)
108+
(format "%s#L%d-L%d" git-relative-path region-start-line region-end-line)))
102109
(prompt-label
103110
(cond
104111
((and clipboard-context
@@ -123,13 +130,21 @@ Argument ARG is the prefix argument."
123130
(final-prompt
124131
(concat initial-prompt
125132
(when region-text
126-
(format "\nCode from line %d:\n%s" region-start-line region-text))
133+
(concat "\nSelected region:\n"
134+
(cond
135+
(region-location-info
136+
(concat region-location-info "\n"))
137+
(region-start-line
138+
(format "Start line: %d\n" region-start-line)))
139+
region-text))
127140
(when function-name (format "\nFunction: %s" function-name))
128141
files-context-string
129142
(when (and clipboard-context
130143
(string-match-p "\\S-" clipboard-context))
131144
(concat "\n\nClipboard context:\n" clipboard-context))
132-
"\nNote: Please make the code change described above.")))
145+
(if region-text
146+
"\nNote: Please apply the code change to the selected region specified above."
147+
"\nNote: Please make the code change described above."))))
133148
(ai-code--insert-prompt final-prompt)))
134149

135150
;;;###autoload

ai-code-discussion.el

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ CLIPBOARD-CONTEXT is optional clipboard text to append as context."
9191
(region-active (region-active-p))
9292
(region-text (when region-active
9393
(buffer-substring-no-properties (region-beginning) (region-end))))
94+
(region-start-line (when region-active
95+
(line-number-at-pos (region-beginning))))
96+
(region-end-line (when region-active
97+
(line-number-at-pos (region-end))))
98+
(git-relative-path (when (and region-active buffer-file-name)
99+
(car (ai-code--get-git-relative-paths (list buffer-file-name)))))
100+
(region-location-info (when (and region-active git-relative-path region-start-line region-end-line)
101+
(format "%s#L%d-L%d" git-relative-path region-start-line region-end-line)))
94102
(prompt-label
95103
(cond
96104
((and clipboard-context
@@ -119,14 +127,19 @@ CLIPBOARD-CONTEXT is optional clipboard text to append as context."
119127
(final-prompt
120128
(concat question
121129
(when region-text
122-
(concat "\n" region-text))
130+
(concat "\nSelected region:\n"
131+
(when region-location-info
132+
(concat region-location-info "\n"))
133+
region-text))
123134
(when function-name
124135
(format "\nFunction: %s" function-name))
125136
files-context-string
126137
(when (and clipboard-context
127138
(string-match-p "\\S-" clipboard-context))
128139
(concat "\n\nClipboard context:\n" clipboard-context))
129-
"\nNote: This is a question only - please do not modify the code.")))
140+
(if region-text
141+
"\nNote: This is a question about the selected region - please do not modify the code."
142+
"\nNote: This is a question only - please do not modify the code."))))
130143
(ai-code--insert-prompt final-prompt)))
131144

132145
(defun ai-code--get-git-relative-paths (file-paths)

0 commit comments

Comments
 (0)