branch: elpa/aidermacs
commit b25fa593e34109adf68a9f151e28a4b27811c4b4
Author: Kang Tu <[email protected]>
Commit: GitHub <[email protected]>
Feat: Support multi-lines comment based in-place implementation (#73)
* feat: Add aider-implement-todo function to implement TODOs in current
context
* feat: Add `aider-implement-todo` function description to README
* docs(readme): update aider command description
* merge main
* feat: Add region selection support to aider-implement-todo function
* refactor: Enhance aider-write-unit-test to handle test file and function
contexts
* update prompt for unit-test
---------
Co-authored-by: Kang Tu <[email protected]>
---
aider.el | 60 +++++++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 43 insertions(+), 17 deletions(-)
diff --git a/aider.el b/aider.el
index 0243b6676b..51ec11911f 100644
--- a/aider.el
+++ b/aider.el
@@ -587,26 +587,44 @@ If there are more than 40 files, refuse to add and show
warning message."
(defun aider-write-unit-test ()
"Generate unit test code for current buffer.
Do nothing if current buffer is not visiting a file.
-If current buffer filename contains 'test', do nothing.
-If cursor is on a function, generate unit test for that function.
-Otherwise, generate unit tests for the entire file."
+If current buffer filename contains 'test':
+ - If cursor is inside a test function, implement that test
+ - Otherwise show message asking to place cursor inside a test function
+Otherwise:
+ - If cursor is on a function, generate unit test for that function
+ - Otherwise generate unit tests for the entire file"
(interactive)
(if (not buffer-file-name)
(message "Current buffer is not visiting a file.")
- (if (string-match-p "test" (file-name-nondirectory buffer-file-name))
- (message "Current buffer appears to be a test file.")
- (let* ((function-name (which-function))
- (common-instructions "Keep existing tests if there are. Do not
use Mock if possible. Follow standard unit testing practices.")
- (initial-input
- (if function-name
- (format "Please write unit test code for function '%s'. %s"
- function-name common-instructions)
- (format "Please write unit test code for file '%s'. For each
function %s"
- (file-name-nondirectory buffer-file-name)
common-instructions)))
- (user-command (aider-read-string "Unit test generation
instruction: " initial-input))
- (command (format "/architect %s" user-command)))
- (aider-add-current-file)
- (aider--send-command command t)))))
+ (let ((is-test-file (string-match-p "test" (file-name-nondirectory
buffer-file-name)))
+ (function-name (which-function)))
+ (cond
+ ;; Test file case
+ (is-test-file
+ (if function-name
+ (if (string-match-p "test" function-name)
+ (let* ((initial-input
+ (format "Please implement test function '%s'. Follow
standard unit testing practices and make it a meaningful test. Do not use Mock
if possible."
+ function-name))
+ (user-command (aider-read-string "Test implementation
instruction: " initial-input))
+ (command (format "/architect %s" user-command)))
+ (aider-add-current-file)
+ (aider--send-command command t))
+ (message "Current function '%s' does not appear to be a test
function." function-name))
+ (message "Please place cursor inside a test function to
implement.")))
+ ;; Non-test file case
+ (t
+ (let* ((common-instructions "Keep existing tests if there are. Follow
standard unit testing practices. Do not use Mock if possible.")
+ (initial-input
+ (if function-name
+ (format "Please write unit test code for function '%s'.
%s"
+ function-name common-instructions)
+ (format "Please write unit test code for file '%s'. For each
function %s"
+ (file-name-nondirectory buffer-file-name)
common-instructions)))
+ (user-command (aider-read-string "Unit test generation
instruction: " initial-input))
+ (command (format "/architect %s" user-command)))
+ (aider-add-current-file)
+ (aider--send-command command t)))))))
;;;###autoload
(defun aider-fix-failing-test-under-cursor ()
@@ -636,6 +654,7 @@ ignoring leading whitespace."
;;;###autoload
(defun aider-implement-todo ()
"Implement TODO comments in current context.
+If region is selected, implement that specific region.
If cursor is on a comment line, implement that specific comment.
If cursor is inside a function, implement TODOs for that function.
Otherwise implement TODOs for the entire current file."
@@ -645,8 +664,15 @@ Otherwise implement TODOs for the entire current file."
(let* ((current-line (string-trim (thing-at-point 'line t)))
(is-comment (aider--is-comment-line current-line))
(function-name (which-function))
+ (region-text (when (region-active-p)
+ (buffer-substring-no-properties
+ (region-beginning)
+ (region-end))))
(initial-input
(cond
+ (region-text
+ (format "Please implement this code block: '%s'. It is already
inside current code. Please do in-place implementation. Keep the existing code
structure and implement just this specific block."
+ region-text))
(is-comment
(format "Please implement this comment: '%s'. It is already
inside current code. Please do in-place implementation. Keep the existing code
structure and implement just this specific comment."
current-line))