Skip to content

Commit a718218

Browse files
tninjaCopilot
andauthored
Feat: Add context collection list management functions (#45)
* Add Git repository context storage and management functions * Use # for line and function separators in context refs and add context action command * Enhance code implementation prompts with region location details * Add repository context tracking and display in AI prompts * Add repository context info to AI code change prompt * Include repository context info in AI code questions prompt * addressing feedback * bump version and update HISTORY * Update ai-code-file.el Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update ai-code-discussion.el Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update transient_menu.png to a new version * Rewrite README Key Features for clarity and conciseness * Reorganize Typical Workflows section in README.org --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 7d7516f commit a718218

File tree

7 files changed

+178
-46
lines changed

7 files changed

+178
-46
lines changed

HISTORY.org

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
** Main branch change
55

6+
- Feat: Add context collection list management functions
67
- Feat: Add note taking functions, useful during discussing with AI
78
- Fix function detection for TODO comments preceding method definitions
89
- Addressing [[https://github.com/tninja/ai-code-interface.el/issues/40][Wrong function detection]], suggested by @Silex

README.org

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,16 @@ An Emacs interface for AI-assisted software development. *The purpose is to prov
5050

5151
You can add other backends by customizing the `ai-code-backends` variable.
5252

53-
*** Main Entry Point
54-
Press `C-c a` to open the main transient menu, which provides access to all major features.
53+
** Key Features
54+
55+
- *Transient-Driven Hub (`C-c a`)*: One keystroke opens a contextual transient menu that groups every capability (CLI control, code actions, agile workflows, utilities) so you never need to memorize scattered keybindings.
56+
- *AI CLI Session Management*: Start (`a`), resume (`R`), or jump back into (`z`) the active AI CLI buffer, instantly swap backends (`s`), upgrade them (`u`), edit backend configs (`g`), and run prompts against the current file (`|`).
57+
- *Context-Aware Code Actions*: The menu exposes dedicated entries for changing code (`c`), implementing TODOs (`i`), asking questions (`q`), explaining code (`x`), sending free-form commands (`<SPC>`), and refreshing AI context (`@`). Each command automatically captures the surrounding function, region, or clipboard contents (via `C-u`) to keep prompts precise.
58+
- *Agile Development Workflows*: Use the refactoring navigator (`r`), the guided TDD cycle (`t`), and the pull/review diff helper (`v`) to keep AI-assisted work aligned with agile best practices. Prompt authoring is first-class through quick access to the prompt file (`p`), block sending (`b`), and AI-assisted shell/file execution (`!`).
59+
- *Productivity & Debugging Utilities*: Initialize project navigation assets (`.`), investigate exceptions (`e`), auto-fix Flycheck issues in scope (`f`), copy or open file paths formatted for prompts (`k`, `o`), generate MCP inspector commands (`m`), and capture session notes straight into Org (`n`).
60+
- *Seamless Prompt Management*: Open `.ai.code.prompt.org`, send regions with `ai-code-prompt-send-block`, and reuse prompt snippets via `yasnippet` to keep conversations organized.
61+
- *Interactive Chat & Context Tools*: Dedicated buffers hold long-running chats, automatically enriched with file paths, diffs, and history from Magit or Git commands for richer AI responses.
62+
- *AI-Assisted Bash Commands*: From Dired, shell, eshell, or vterm, run `C-c a !` and type natural-language commands prefixed with `:` (e.g., `:count lines of python code recursively`); the tool generates the shell command for review and executes it in a compile buffer.
5563

5664
*** Typical Workflows
5765
- *Changing Code*: Position the cursor on a function or select a region of code. Press `C-c a`, then `c` (`ai-code-code-change`). Describe the change you want to make in the prompt. The AI will receive the context of the function or region and your instruction.
@@ -60,43 +68,12 @@ An Emacs interface for AI-assisted software development. *The purpose is to prov
6068
- *Refactoring a Function*: With the cursor in a function, press `C-c a`, then `r` (`ai-code-refactor-book-method`). Select a refactoring technique from the list, provide any required input (e.g., a new method name), and the prompt will be generated.
6169
- *Reviewing a Pull Request*: Press `C-c a`, then `v` (`ai-code-pull-or-review-diff-file`). Choose to generate a diff between two branches. The diff will be created in a new buffer, and you'll be prompted to start a review.
6270

63-
*** The Prompt File
64-
Use `M-x ai-code-open-prompt-file` to open or create `.ai.code.prompt.org` at the root of your Git repository. This file is an Org-mode buffer where you can write, edit, and manage your prompts. Press `C-c C-c` (`ai-code-prompt-send-block`) on a block of text to send it directly to the AI.
65-
66-
** Key Features
67-
68-
- *Interactive AI Chat*: Start an interactive session with an AI assistant in a dedicated Emacs buffer.
69-
- *Context-Aware Prompting*: Automatically include relevant context in your prompts, such as file paths, selected code regions, or the current function.
70-
- *Code Manipulation*:
71-
- *Change Code*: Request changes to the current function or a selected region.
72-
- *Implement TODOs*: Automatically generate code to fulfill `TODO` comments.
73-
- *Explain Code*: Get explanations for a symbol, line, function, file, or selected region.
74-
- *Fix Errors*: Use `Flycheck` integration to automatically generate fixes for reported errors.
75-
- *Agile Development*:
76-
- *TDD Cycle*: A guided workflow for the Red-Green-Refactor cycle of Test-Driven Development.
77-
- *Refactoring*: Apply common refactoring techniques (e.g., Extract Method, Rename Variable) with AI assistance.
78-
- *Git & Magit Integration*:
79-
- *Review Diffs*: Generate and review diffs for commits, branches, or staged changes.
80-
- *Analyze History*: Use `git blame` and `git log` output to get AI-powered insights into code evolution and contributor patterns.
81-
- *Transient Menus*: Access AI commands directly from the Magit interface.
82-
- *Prompt Management*:
83-
- *Prompt File*: Use a dedicated `.ai.code.prompt.org` file to manage and reuse prompts.
84-
- *Snippets*: Leverage `yasnippet` for a library of predefined, reusable prompts.
85-
- *Usability Enhancements*:
86-
- *Transient Menu*: A clean, organized menu (`C-c a`) for easy command discovery.
87-
- *Helm/Completing-Read*: Enhanced input completion with history.
88-
- *Clipboard Utilities*: Easily copy file paths (formatted for AI prompts) and open clipboard paths in Dired.
89-
- *AI assisted bash command generation and running*
90-
- C-c a ! in a dired buffer, or shell / eshell / vterm buffer, and start type command lead with : character, eg, :count lines of python code recursively, it will generate the full command with ai for review, and then run it in a compile buffer.
91-
- *MCP debugging feature*
92-
- C-c a m to automatically generate mcp inspector command for python MCP project.
93-
- *Project Initialization*
94-
- C-c a . to initialize .projectile and create gtags for the project. For easy code navigation.
95-
9671
** Add a new AI coding CLI backend
9772

9873
- [[https://github.com/tninja/ai-code-interface.el/pull/2][This PR]] add the github-copilot-cli (require claude-code.el). It can be an example to add basic support for other AI coding CLI.
9974

75+
- Open a issue, Post information about the new AI coding CLI backend (eg. cursor CLI?), at least providing the command line name. You can also include the version upgrade command, how to resume, where the configuration files are located, and so on. We can ask GitHub Copilot to add support features based on the issue.
76+
10077
** [[https://github.com/tninja/aider.el/blob/main/appendix.org#be-careful-about-ai-generated-code][Why Agile development with AI?]]
10178

10279
** Dependencies

ai-code-change.el

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
(declare-function ai-code--get-clipboard-text "ai-code-interface")
2323
(declare-function ai-code--get-git-relative-paths "ai-code-discussion")
2424
(declare-function ai-code--get-region-location-info "ai-code-discussion")
25+
(declare-function ai-code--format-repo-context-info "ai-code-file")
2526

2627
(defun ai-code--is-comment-line (line)
2728
"Check if LINE is a comment line based on current buffer's comment syntax.
@@ -121,6 +122,7 @@ Argument ARG is the prefix argument."
121122
(t "Change code: ")))
122123
(initial-prompt (ai-code-read-string prompt-label ""))
123124
(files-context-string (ai-code--get-context-files-string))
125+
(repo-context-string (ai-code--format-repo-context-info))
124126
(final-prompt
125127
(concat initial-prompt
126128
(when region-text
@@ -133,6 +135,7 @@ Argument ARG is the prefix argument."
133135
region-text))
134136
(when function-name (format "\nFunction: %s" function-name))
135137
files-context-string
138+
repo-context-string
136139
(when (and clipboard-context
137140
(string-match-p "\\S-" clipboard-context))
138141
(concat "\n\nClipboard context:\n" clipboard-context))
@@ -169,14 +172,28 @@ Argument ARG is the prefix argument."
169172
(region-end))))
170173
(region-start-line (when region-active
171174
(line-number-at-pos (region-beginning))))
175+
(region-location-info (when region-active
176+
(ai-code--get-region-location-info
177+
(region-beginning)
178+
(region-end))))
179+
(region-location-line (when region-text
180+
(or (and region-location-info
181+
(format "Selected region: %s"
182+
region-location-info))
183+
(when region-start-line
184+
(format "Selected region starting on line %d"
185+
region-start-line)))))
172186
(files-context-string (ai-code--get-context-files-string))
173187
(initial-input
174188
(if arg
175189
;; With prefix argument: implement after comment, not in-place
176190
(cond
177191
(region-text
178-
(format "Please implement code after this requirement comment block starting on line %d: '%s'. Leave the comment as-is and add the implementation code after it. Keep the existing code structure and add the implementation after this specific block.%s%s"
179-
region-start-line region-text function-context files-context-string))
192+
(format (concat
193+
"Please implement code after this requirement comment block in the selected region. "
194+
"Leave the comment as-is and add the implementation code after it. "
195+
"Keep the existing code structure and add the implementation after this specific block.\n%s\n%s%s%s")
196+
region-location-line region-text function-context files-context-string))
180197
(is-comment
181198
(format "Please implement code after this requirement comment on line %d: '%s'. Leave the comment as-is and add the implementation code after it. Keep the existing code structure and add the implementation after this specific comment.%s%s"
182199
current-line-number current-line function-context files-context-string))
@@ -189,8 +206,11 @@ Argument ARG is the prefix argument."
189206
;; Without prefix argument: replace in-place (original behavior)
190207
(cond
191208
(region-text
192-
(format "Please implement this requirement comment block starting on line %d in-place: '%s'. It is already inside current code. Please replace it with implementation. Keep the existing code structure and implement just this specific block.%s%s"
193-
region-start-line region-text function-context files-context-string))
209+
(format (concat
210+
"Please implement this requirement comment block in-place within the selected region. "
211+
"It is already inside current code. Please replace it with implementation. "
212+
"Keep the existing code structure and implement just this specific block.\n%s\n%s%s%s")
213+
region-location-line region-text function-context files-context-string))
194214
(is-comment
195215
(format "Please implement this requirement comment on line %d in-place: '%s'. It is already inside current code. Please replace it with implementation. Keep the existing code structure and implement just this specific comment.%s%s"
196216
current-line-number current-line function-context files-context-string))

ai-code-discussion.el

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
(declare-function ai-code--insert-prompt "ai-code-prompt-mode")
1919
(declare-function ai-code--get-clipboard-text "ai-code-interface")
2020
(declare-function ai-code-call-gptel-sync "ai-code-prompt-mode")
21+
(declare-function magit-toplevel "magit" (&optional dir))
22+
(declare-function ai-code--format-repo-context-info "ai-code-file")
23+
24+
(defvar ai-code--repo-context-info)
2125

2226
;;;###autoload
2327
(defun ai-code-ask-question (arg)
@@ -71,12 +75,14 @@ CLIPBOARD-CONTEXT is optional clipboard text to append as context."
7175
(file-at-point (format "Question about %s: " (file-name-nondirectory file-at-point)))
7276
(t "General question about directory: ")))
7377
(question (ai-code-read-string prompt-label ""))
78+
(repo-context-string (ai-code--format-repo-context-info))
7479
(final-prompt (concat question
75-
files-context-string
76-
(when (and clipboard-context
77-
(string-match-p "\\S-" clipboard-context))
78-
(concat "\n\nClipboard context:\n" clipboard-context))
79-
"\nNote: This is a question only - please do not modify the code.")))
80+
files-context-string
81+
repo-context-string
82+
(when (and clipboard-context
83+
(string-match-p "\\S-" clipboard-context))
84+
(concat "\n\nClipboard context:\n" clipboard-context))
85+
"\nNote: This is a question only - please do not modify the code.")))
8086
(ai-code--insert-prompt final-prompt)))
8187

8288
(defun ai-code--ask-question-file (clipboard-context)
@@ -118,6 +124,7 @@ CLIPBOARD-CONTEXT is optional clipboard text to append as context."
118124
(t "General question: ")))
119125
(question (ai-code-read-string prompt-label ""))
120126
(files-context-string (ai-code--get-context-files-string))
127+
(repo-context-string (ai-code--format-repo-context-info))
121128
(final-prompt
122129
(concat question
123130
(when region-text
@@ -128,8 +135,9 @@ CLIPBOARD-CONTEXT is optional clipboard text to append as context."
128135
(when function-name
129136
(format "\nFunction: %s" function-name))
130137
files-context-string
138+
repo-context-string
131139
(when (and clipboard-context
132-
(string-match-p "\\S-" clipboard-context))
140+
(string-match-p "\\S-" clipboard-context))
133141
(concat "\n\nClipboard context:\n" clipboard-context))
134142
(if region-text
135143
"\nNote: This is a question about the selected region - please do not modify the code."
@@ -187,6 +195,7 @@ Argument ARG is the prefix argument."
187195
(buffer-substring-no-properties (point-min) (point-max))))
188196
(function-name (which-function))
189197
(files-context-string (ai-code--get-context-files-string))
198+
(repo-context-string (ai-code--format-repo-context-info))
190199
(context-section
191200
(if full-buffer-context
192201
(concat "\n\nContext:\n" full-buffer-context)
@@ -225,6 +234,7 @@ Argument ARG is the prefix argument."
225234
context-section
226235
(when function-name (format "\nFunction: %s" function-name))
227236
files-context-string
237+
repo-context-string
228238
(concat "\n\nNote: Please focus on how to fix the error. Your response should include:\n"
229239
"1. A brief explanation of the root cause of the error.\n"
230240
"2. A code snippet with the fix.\n"

0 commit comments

Comments
 (0)