branch: elpa/aidermacs commit e391bbe222fc7a9d63546a058067321137559880 Author: Kang Tu <tni...@gmail.com> Commit: GitHub <nore...@github.com>
Feat: Add function to explain code in selected region * feat: Add function to explain code in selected region * add aider-region-explain, support send large text (> 1024 chars) to aider buffer * better comment --------- Co-authored-by: Kang Tu <kang...@apple.com> --- README.org | 11 ++++++++--- aider.el | 44 ++++++++++++++++++++++++++++++++++++++++---- transient_menu.png | Bin 121113 -> 58345 bytes 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/README.org b/README.org index 4701442037..e8ac7950b6 100644 --- a/README.org +++ b/README.org @@ -17,9 +17,14 @@ - Git Repository-Specific aider Sessions in Emacs: Automatically identify the Git repository of the current file and create a new aider session for it. Multiple aider sessions can exist for different Git repositories. -- Batch add files from dired buffer (aider-batch-add-dired-marked-files): Add multiple Dired marked files to the Aider buffer. These files can be searched from repo with aider-repo-find-name-dired command - -- Region-Based Refactor Support (aider-region-refactor): You can select a region (e.g., a code block) in a file and ask aider to refactor it. +- More ways to add files to aider buffer + - Add current buffer file: (aider-add-current-file) + - Add all buffers in current window: (aider-add-files-in-current-window) + - Batch add files from dired buffer (aider-batch-add-dired-marked-files): Add multiple Dired marked files to the Aider buffer. + +- Region-Based Explain / Refactor Support + - Explain: (aider-region-explain): You can select a region (e.g., a code block) in a file and ask aider to explain it. + - Refactor: (aider-region-refactor): Ask aider to refactor it given your input instruction - And More: Add your own Elisp functions to support your use case. You can certainly ask aider / aider.el to do that. diff --git a/aider.el b/aider.el index 94b8e39ec5..e8ef5a403f 100644 --- a/aider.el +++ b/aider.el @@ -36,7 +36,8 @@ This function can be customized or redefined by the user." (let* ((input (read-string prompt initial-input)) (processed-input (replace-regexp-in-string "\n" "\\\\n" input))) - (concat processed-input "\n"))) + processed-input + )) (defalias 'aider-read-string 'aider-plain-read-string) @@ -62,12 +63,13 @@ This function can be customized or redefined by the user." ] ["Code change" ("c" "Code Change" aider-code-change) - ("r" "Region Code Refactor" aider-region-refactor) + ("r" "Refactor Code in Selected Region" aider-region-refactor) ("u" "Undo Last Change" aider-undo-last-change) ] ["Discussion" ("q" "Ask Question" aider-ask-question) ("t" "Architect Discussion" aider-architect-discussion) + ("e" "Explain Code in Selected Region" aider-region-explain) ("d" "Debug Exception" aider-debug-exception) ] ["Other" @@ -130,6 +132,20 @@ If not in a git repository, an error is raised." (interactive) (aider--send-command "/reset")) +;; Function to send large text (> 1024 chars) to the Aider buffer +(defun comint-send-large-string (buffer text) + "Send large TEXT to the comint buffer in chunks of 1000 characters." + (let ((chunk-size 1000) + (pos 0) + (process (get-buffer-process buffer))) + (while (< pos (length text)) + (process-send-string + process + (substring text pos (min (+ pos chunk-size) (length text)))) + (sleep-for 0.1) + (message "Sent command to aider buffer: %s" (substring text pos (min (+ pos chunk-size) (length text)))) + (setq pos (+ pos chunk-size))))) + ;; Shared helper function to send commands to corresponding aider buffer (defun aider--send-command (command &optional switch-to-buffer) "Send COMMAND to the corresponding aider comint buffer after performing necessary checks. @@ -144,9 +160,9 @@ COMMAND should be a string representing the command to send." (unless (string-suffix-p "\n" command) (setq command (concat command "\n"))) ;; Send the command to the aider process - (comint-send-string aider-buffer command) + (comint-send-large-string aider-buffer command) ;; Provide feedback to the user - (message "Sent command to aider buffer: %s" (string-trim command)) + ;; (message "Sent command to aider buffer: %s" (string-trim command)) (when switch-to-buffer (aider-switch-to-buffer))) (message "No active process found in buffer %s." (aider-buffer-name)))) @@ -237,6 +253,26 @@ The command will be formatted as \"/architect \" followed by the user command an ) (message "No region selected."))) +;; New function to explain the code in the selected region +(defun aider-region-explain () + "Get a command from the user and send it to the corresponding aider comint buffer based on the selected region. +The command will be formatted as \"/ask \" followed by the text from the selected region." + (interactive) + (if (use-region-p) + (let* ((region-text (buffer-substring-no-properties (region-beginning) (region-end))) + (function-name (which-function)) + (processed-region-text (replace-regexp-in-string "\n" "\\\\n" region-text)) + (command (if function-name + (format "/ask in function %s, explain the following code block: %s" + function-name + processed-region-text) + (format "/ask explain the following code block: %s" + processed-region-text) + ))) + (aider-add-current-file) + (aider--send-command command t)) + (message "No region selected."))) + (defun aider-send-command-with-prefix (prefix command) "Send COMMAND to the Aider buffer prefixed with PREFIX." (aider-add-current-file) diff --git a/transient_menu.png b/transient_menu.png index 22610cf5d0..dfd15846b0 100644 Binary files a/transient_menu.png and b/transient_menu.png differ