Skip to content

Commit ba75072

Browse files
authored
Refactor: error investigation prompt, run command with comint buffer (#36)
* Refactor error investigation prompt, run command with comint buffer * bump version
1 parent f492680 commit ba75072

File tree

4 files changed

+72
-43
lines changed

4 files changed

+72
-43
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+
- Refactor: error investigation prompt, run command with comint buffer
67
- Chore: Refactor error investigation prompts to improve context handling
78
- Chore: Add clipboard context option to ai-code-send-command function
89
- Chore: add compilation buffer content as error context

ai-code-discussion.el

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -143,36 +143,31 @@ Argument ARG is the prefix argument."
143143
(concat "\n\nCompilation output:\n" compilation-content))
144144
(when (and region-text (not compilation-content))
145145
(concat "\n\nSelected code:\n" region-text)))))
146-
(default-question "How to fix the error in this code? Please analyze the error, explain the root cause, and provide the corrected code to resolve the issue: "))
147-
(if (or arg (not (derived-mode-p 'prog-mode)))
148-
(let* ((prompt (ai-code-read-string "Investigate exception (no context): " default-question))
149-
(final-prompt (concat prompt
150-
context-section
151-
(when function-name (format "\nFunction: %s" function-name))
152-
files-context-string)))
153-
(ai-code--insert-prompt final-prompt))
154-
(let* ((prompt-label
155-
(cond
156-
(compilation-content
157-
"Investigate compilation error: ")
158-
(region-text
159-
(if function-name
160-
(format "Investigate exception in function %s: " function-name)
161-
"Investigate selected exception: "))
162-
(function-name
163-
(format "Investigate exceptions in function %s: " function-name))
164-
(t "Investigate exceptions in code: ")))
165-
(initial-prompt (ai-code-read-string prompt-label default-question))
166-
(final-prompt
167-
(concat initial-prompt
168-
context-section
169-
(when function-name (format "\nFunction: %s" function-name))
170-
files-context-string
171-
(concat "\n\nNote: Please focus on how to fix the error. Your response should include:\n"
172-
"1. A brief explanation of the root cause of the error.\n"
173-
"2. A code snippet with the fix.\n"
174-
"3. An explanation of how the fix addresses the error."))))
175-
(ai-code--insert-prompt final-prompt)))))
146+
(default-question "How to fix the error in this code? Please analyze the error, explain the root cause, and provide the corrected code to resolve the issue: ")
147+
(prompt-label
148+
(cond
149+
(compilation-content
150+
"Investigate compilation error: ")
151+
(full-buffer-context
152+
"Investigate exception in current buffer: ")
153+
(region-text
154+
(if function-name
155+
(format "Investigate exception in function %s: " function-name)
156+
"Investigate selected exception: "))
157+
(function-name
158+
(format "Investigate exceptions in function %s: " function-name))
159+
(t "Investigate exceptions in code: ")))
160+
(initial-prompt (ai-code-read-string prompt-label default-question))
161+
(final-prompt
162+
(concat initial-prompt
163+
context-section
164+
(when function-name (format "\nFunction: %s" function-name))
165+
files-context-string
166+
(concat "\n\nNote: Please focus on how to fix the error. Your response should include:\n"
167+
"1. A brief explanation of the root cause of the error.\n"
168+
"2. A code snippet with the fix.\n"
169+
"3. An explanation of how the fix addresses the error.")))
170+
(ai-code--insert-prompt final-prompt))))
176171

177172
;;;###autoload
178173
(defun ai-code-explain ()

ai-code-file.el

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
(require 'magit)
1313
(require 'dired)
14+
(require 'comint)
15+
(require 'subr-x)
1416

1517
(require 'ai-code-input)
1618
(require 'ai-code-prompt-mode)
@@ -99,10 +101,45 @@ If the clipboard contains a directory path, open it directly in dired in another
99101
(defvar ai-code-run-file-history nil
100102
"History list for ai-code-run-current-file commands.")
101103

104+
(defun ai-code--run-command-in-comint (command run-directory display-name)
105+
"Run COMMAND in a comint buffer using RUN-DIRECTORY and DISPLAY-NAME.
106+
COMMAND is parsed into a program and its SWITCHES using `split-string-and-unquote'.
107+
RUN-DIRECTORY is used as the default directory for the process.
108+
DISPLAY-NAME is the buffer name applied to the comint session."
109+
(let* ((command-parts (split-string-and-unquote command))
110+
(program (car command-parts))
111+
(switches (cdr command-parts)))
112+
(unless program
113+
(user-error "Command cannot be empty"))
114+
(let* ((origin-window (selected-window))
115+
(origin-buffer (window-buffer origin-window))
116+
(effective-directory (or run-directory default-directory))
117+
(comint-buffer-name (format "*%s*" (file-name-nondirectory program))))
118+
(when-let ((existing (get-buffer display-name)))
119+
(unless (yes-or-no-p (format "Kill existing session %s? " display-name))
120+
(user-error "Aborted running command: %s" command))
121+
(when-let ((proc (get-buffer-process existing)))
122+
(delete-process proc))
123+
(kill-buffer existing))
124+
(let ((default-directory effective-directory))
125+
(comint-run program switches))
126+
(when (window-live-p origin-window)
127+
(set-window-buffer origin-window origin-buffer))
128+
(when-let ((buffer (get-buffer comint-buffer-name)))
129+
(with-current-buffer buffer
130+
(rename-buffer display-name t)
131+
(setq-local default-directory effective-directory))
132+
(let ((session-window
133+
(display-buffer buffer '((display-buffer-pop-up-window)
134+
(inhibit-same-window . t)))))
135+
(when (window-live-p session-window)
136+
(select-window session-window))))
137+
(get-buffer display-name))))
138+
102139
;;;###autoload
103140
(defun ai-code-run-current-file ()
104141
"Generate command to run current script file (.py, .js, .ts, or .sh).
105-
Let user modify the command before running it in a compile buffer.
142+
Let user modify the command before running it in a comint buffer.
106143
Maintains a dedicated history list for this command."
107144
(interactive)
108145
(let* ((current-file (buffer-file-name))
@@ -128,17 +165,13 @@ Maintains a dedicated history list for this command."
128165
(user-error "Current buffer is not visiting a file"))
129166
(unless default-command
130167
(user-error "Current file is not a .py, .js, .ts, or .sh file"))
131-
(let ((command (read-string (format "Run command for %s: " file-name)
132-
default-command
133-
'ai-code-run-file-history)))
134-
(let* ((default-directory (file-name-directory current-file))
135-
(buffer-name (format "*ai-code-run-current-file: %s*"
136-
(file-name-base file-name))))
137-
(compilation-start
138-
command
139-
nil
140-
(lambda (_mode)
141-
(generate-new-buffer-name buffer-name)))))))
168+
(let ((command (read-string (format "Run command for %s: " file-name)
169+
default-command
170+
'ai-code-run-file-history)))
171+
(let* ((run-directory (file-name-directory current-file))
172+
(display-name (format "*ai-code-run-current-file: %s*"
173+
(file-name-base file-name))))
174+
(ai-code--run-command-in-comint command run-directory display-name)))))
142175

143176
;;;###autoload
144177
(defun ai-code-apply-prompt-on-current-file ()

ai-code-interface.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
;;; ai-code-interface.el --- AI code interface for editing AI prompt files -*- lexical-binding: t; -*-
22

33
;; Author: Kang Tu <tninja@gmail.com>
4-
;; Version: 0.49
4+
;; Version: 0.50
55
;; Package-Requires: ((emacs "26.1") (transient "0.8.0") (magit "2.1.0"))
66

77
;; SPDX-License-Identifier: Apache-2.0

0 commit comments

Comments
 (0)