branch: elpa/aidermacs commit 5f9b7fa1fe955b15246472091793bedf558601b4 Author: Kang Tu <tni...@gmail.com> Commit: GitHub <nore...@github.com>
Feat: easily refactor the function under cursor (#22) * increase log history to 10k * refactor: Modify buffer switching to use custom function * feat: Add syntax highlighting from source buffer to aider comint mode fix: Remove non-existent font-lock-keywords-keywords function call refactor: Simplify font-lock handling in aider-run-aider function refactor: Improve syntax highlighting for aider comint buffer fix: Remove unnecessary trailing parenthesis in aider.el refactor: Enhance syntax highlighting for aider buffer with source buffer's mode refactor: Improve syntax highlighting inheritance in aider-run-aider function refactor: Remove unnecessary blank lines in aider.el docs: Convert comments in aider-run-aider to English refactor: Simplify aider-run-aider function and extract source highlighting logic * feat: Add autoload cookies to interactive functions in aider.el * refactor: Conditionally inherit syntax highlighting for prog-mode buffers * move font-lock code into when block * feat: Add syntax highlighting inheritance message in aider buffer * feat: Add initial-input support to helm string reading functions * feat: Add aider-function-refactor for refactoring function under cursor * re-indent, adjust menu item. update README * fix typo --- README.org | 3 +++ aider-helm.el | 34 +++++++++++++++++++--------------- aider.el | 15 +++++++++++++++ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/README.org b/README.org index e465b25b73..6f39284a56 100644 --- a/README.org +++ b/README.org @@ -22,6 +22,9 @@ - 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. +- Refactor function under cursor + - (aider-function-refactor): Ask aider to refactor the function under cursor given your instruction. + - 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 diff --git a/aider-helm.el b/aider-helm.el index 322eb108f5..520dae3980 100644 --- a/aider-helm.el +++ b/aider-helm.el @@ -3,36 +3,40 @@ (require 'helm) (require 'cl-lib) ; For `cl-subseq` -(defun helm-read-string-with-history (prompt history-file-name) +(defun helm-read-string-with-history (prompt history-file-name &optional initial-input) "Read a string with Helm completion using specified history file. PROMPT is the prompt string. -HISTORY-FILE-NAME is the base name for history file." +HISTORY-FILE-NAME is the base name for history file. +INITIAL-INPUT is optional initial input string." ;; Load history from file (let* ((history-file (expand-file-name history-file-name user-emacs-directory)) (history (when (file-exists-p history-file) - (with-temp-buffer - (insert-file-contents history-file) - (delete-dups (read (buffer-string)))))) + (with-temp-buffer + (insert-file-contents history-file) + (delete-dups (read (buffer-string)))))) ;; Read input with helm (input (helm-comp-read - prompt - history - :must-match nil - :name "Helm Read String" - :fuzzy t))) + prompt + history + :must-match nil + :name "Helm Read String" + :fuzzy t + :initial-input initial-input))) ;; Add to history if non-empty and save (unless (string-empty-p input) (push input history) (with-temp-file history-file (let ((history-entries (cl-subseq history - 0 (min (length history) - 10000)))) ; Keep last 10000 entries + 0 (min (length history) + 10000)))) ; Keep last 10000 entries (insert (prin1-to-string history-entries))))) input)) -(defun aider-helm-read-string (prompt) - "Read a string with Helm completion for aider, showing historical inputs." - (helm-read-string-with-history prompt "aider-helm-read-string-history.el")) +(defun aider-helm-read-string (prompt &optional initial-input) + "Read a string with Helm completion for aider, showing historical inputs. +PROMPT is the prompt string. +INITIAL-INPUT is optional initial input string." + (helm-read-string-with-history prompt "aider-helm-read-string-history.el" initial-input)) (defalias 'aider-read-string 'aider-helm-read-string) diff --git a/aider.el b/aider.el index f27fb32985..f1081a2261 100644 --- a/aider.el +++ b/aider.el @@ -77,6 +77,7 @@ This function can be customized or redefined by the user." ["Code change" ("c" "Code Change" aider-code-change) ("t" "Architect Discuss and Change" aider-architect-discussion) + ("R" "Refactor Function Under Cursor" aider-function-refactor) ("r" "Refactor Code in Selected Region" aider-region-refactor) ("m" "Show last commit with magit" aider-magit-show-last-commit) ("u" "Undo Last Change" aider-undo-last-change) @@ -335,6 +336,20 @@ If Magit is not installed, report that it is required." (format "/architect \"for the following code block, %s: %s\"\n" user-command processed-region-text)))) +;;;###autoload +(defun aider-function-refactor () + "Get the function name under cursor and send refactor command to aider. +The command will be formatted as \"/architect\" followed by refactoring instructions +for the specified function." + (interactive) + (if-let ((function-name (which-function))) + (let* ((initial-input (format "refactor %s: " function-name)) + (user-command (aider-read-string "Enter refactor instruction: " initial-input)) + (command (format "/architect %s" user-command))) + (aider-add-current-file) + (aider--send-command command t)) + (message "No function found at cursor position."))) + ;;;###autoload (defun aider-region-refactor () "Get a command from the user and send it to the corresponding aider comint buffer based on the selected region.