Skip to content

Commit 6be122f

Browse files
author
Kang Tu
committed
addressing comments
1 parent e95a805 commit 6be122f

2 files changed

Lines changed: 47 additions & 25 deletions

File tree

ai-code-prompt-mode.el

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -415,21 +415,21 @@ that root, otherwise return the absolute path."
415415
(defun ai-code--insert-prompt (prompt-text)
416416
"Preprocess and insert PROMPT-TEXT into the AI prompt file.
417417
If PROMPT-TEXT is a command (starts with /), execute it directly instead."
418-
(let* ((processed-prompt (if ai-code-prompt-preprocess-filepaths
419-
(ai-code--preprocess-prompt-text prompt-text)
420-
prompt-text))
421-
(append-summary-p (and (derived-mode-p 'ai-code-prompt-mode)
422-
(org-at-heading-p)
423-
(y-or-n-p "Append result summary to current section? ")))
424-
(final-prompt (if append-summary-p
425-
(concat processed-prompt
426-
(format "\n\nAfter completing, append a concise result summary as a sub-heading at the end of the current section in file %s near line %d."
427-
buffer-file-name (line-number-at-pos)))
428-
processed-prompt)))
429-
(if (and (string-prefix-p "/" final-prompt)
430-
(not (string-match-p " " final-prompt)))
431-
(ai-code--execute-command final-prompt)
432-
(ai-code--write-prompt-to-file-and-send final-prompt))))
418+
(let ((processed-prompt (if ai-code-prompt-preprocess-filepaths
419+
(ai-code--preprocess-prompt-text prompt-text)
420+
prompt-text)))
421+
(if (and (string-prefix-p "/" processed-prompt)
422+
(not (string-match-p " " processed-prompt)))
423+
(ai-code--execute-command processed-prompt)
424+
(let* ((append-summary-p (and (derived-mode-p 'ai-code-prompt-mode)
425+
(org-at-heading-p)
426+
(y-or-n-p "Append result summary to current section? ")))
427+
(final-prompt (if append-summary-p
428+
(concat processed-prompt
429+
(format "\n\nAfter completing, append a concise result summary as a sub-heading at the end of the current section in file %s near line %d."
430+
buffer-file-name (line-number-at-pos)))
431+
processed-prompt)))
432+
(ai-code--write-prompt-to-file-and-send final-prompt)))))
433433

434434
;; Define the AI Prompt Mode (derived from org-mode)
435435
;;;###autoload

test/test_ai-code-change.el

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -909,13 +909,12 @@ is between the function definition and its body."
909909
(should-not (string-match-p "summary" captured-prompt))))))
910910

911911
(ert-deftest ai-code-test-insert-prompt-org-heading-append-summary-yes ()
912-
"Test that `ai-code--insert-prompt' appends summary instruction on org heading when user confirms."
912+
"Test that `ai-code--insert-prompt' appends summary instruction on prompt-mode heading when user confirms."
913913
(with-temp-buffer
914-
(require 'org)
915-
(setq buffer-file-name "/tmp/project/plan.org")
914+
(setq buffer-file-name "/tmp/project/.ai.code.files/.ai.code.prompt.org")
916915
(insert "* TODO Build search feature\n")
917916
(insert "Design the API first.\n")
918-
(org-mode)
917+
(ai-code-prompt-mode)
919918
(goto-char (point-min))
920919

921920
(let (captured-prompt
@@ -929,16 +928,15 @@ is between the function definition and its body."
929928
(should (stringp captured-prompt))
930929
(should (string-match-p "Test prompt text" captured-prompt))
931930
(should (string-match-p "summary" captured-prompt))
932-
(should (string-match-p "plan\\.org" captured-prompt))))))
931+
(should (string-match-p "\\.ai\\.code\\.prompt\\.org" captured-prompt))))))
933932

934933
(ert-deftest ai-code-test-insert-prompt-org-heading-append-summary-no ()
935934
"Test that `ai-code--insert-prompt' does NOT append summary when user declines."
936935
(with-temp-buffer
937-
(require 'org)
938-
(setq buffer-file-name "/tmp/project/plan.org")
936+
(setq buffer-file-name "/tmp/project/.ai.code.files/.ai.code.prompt.org")
939937
(insert "* TODO Build search feature\n")
940938
(insert "Design the API first.\n")
941-
(org-mode)
939+
(ai-code-prompt-mode)
942940
(goto-char (point-min))
943941

944942
(let (captured-prompt
@@ -953,8 +951,8 @@ is between the function definition and its body."
953951
(should (string-match-p "Test prompt text" captured-prompt))
954952
(should-not (string-match-p "summary" captured-prompt))))))
955953

956-
(ert-deftest ai-code-test-insert-prompt-non-org-no-summary-asked ()
957-
"Test that `ai-code--insert-prompt' does NOT ask about summary in non-org buffer."
954+
(ert-deftest ai-code-test-insert-prompt-non-prompt-mode-no-summary-asked ()
955+
"Test that `ai-code--insert-prompt' does NOT ask about summary in non-prompt-mode buffer."
958956
(with-temp-buffer
959957
(setq buffer-file-name "/tmp/project/test.el")
960958
(setq-local comment-start ";")
@@ -976,6 +974,30 @@ is between the function definition and its body."
976974
(should (string-match-p "Test prompt text" captured-prompt))
977975
(should-not (string-match-p "summary" captured-prompt))))))
978976

977+
(ert-deftest ai-code-test-insert-prompt-slash-command-on-heading-executes ()
978+
"Test that slash commands execute directly even when on a prompt-mode heading."
979+
(with-temp-buffer
980+
(setq buffer-file-name "/tmp/project/.ai.code.files/.ai.code.prompt.org")
981+
(insert "* TODO Build search feature\n")
982+
(ai-code-prompt-mode)
983+
(goto-char (point-min))
984+
985+
(let (y-or-n-called command-executed
986+
(ai-code-prompt-preprocess-filepaths nil))
987+
(cl-letf (((symbol-function 'y-or-n-p)
988+
(lambda (_)
989+
(setq y-or-n-called t)
990+
t))
991+
((symbol-function 'ai-code--execute-command)
992+
(lambda (_cmd) (setq command-executed t)))
993+
((symbol-function 'ai-code--write-prompt-to-file-and-send)
994+
(lambda (_) (error "Should not reach write-prompt"))))
995+
996+
(ai-code--insert-prompt "/status")
997+
998+
(should command-executed)
999+
(should-not y-or-n-called)))))
1000+
9791001
(provide 'test_ai-code-change)
9801002

9811003
;;; test_ai-code-change.el ends here

0 commit comments

Comments
 (0)