Skip to content

Commit a2d0a0a

Browse files
tninjaKang Tu
andauthored
Chore: Improve ai-code-implement-todo inside org-mode file (#325)
* Support any Org headline, not just TODO keywords, but not DONE * Move append-summary feature to `ai-code--insert-prompt` * follow up need to be ai-code-prompt mode * addressing comments --------- Co-authored-by: Kang Tu <kang_tu@apple.com>
1 parent 7a23d39 commit a2d0a0a

4 files changed

Lines changed: 328 additions & 22 deletions

File tree

ai-code-change.el

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,8 @@ Returns (TEXT START-POS END-POS) if TODO found, nil otherwise."
147147
(let ((heading-line (buffer-substring-no-properties
148148
(line-beginning-position)
149149
(line-end-position))))
150-
(when (and (or (org-get-todo-state)
151-
(ai-code--implement-todo--org-todo-headline-p heading-line))
152-
(not (org-entry-is-done-p)))
150+
(when (and (not (org-entry-is-done-p))
151+
(not (string-match-p "^\\*+ DONE " heading-line)))
153152
(list heading-line
154153
(line-beginning-position)
155154
(line-end-position))))))))))
@@ -285,6 +284,8 @@ Otherwise implement comments for the entire current file.
285284
Argument ARG is the prefix argument.
286285
Optional DEFAULT-ACTION skips the action prompt when non-nil."
287286
;; DONE: I want to implement the idea inside https://github.com/tninja/ai-code-interface.el/issues/316, it could to either code change or ask question, given user's input with completing-read selection. The difference of this org-mode section TODO, with the existing comment todo is, it won't replace the TODO section with implementation. It just use the section headline and content inside this section as part of prompt, and send to AI.
287+
;; DONE: for this command triggered from org-mode file buffer. We want to let user choose if they want to add the condense result summary as a section (org headline), at the end of current section under cursor. If user choose yes. The prompt should let AI know this and where to add (maybe let it know current file, headline / cursor position maybe), so that it can add result summary.
288+
;; DONE: This result summary looks good. Is it possible to move this feature to the function just before sending prompt to AI, so that it can be applied to other code change or question asking command as well? The key point is to let user choose if they want to have this summary added to the file, and where to add, so that the prompt can include this requirement and context.
288289
(interactive "P")
289290
(if (not buffer-file-name)
290291
(user-error "Error: buffer-file-name must be available")
@@ -363,12 +364,9 @@ The plist contains `:heading-line', `:content', and `:line-number'."
363364
(let* ((line-number (line-number-at-pos (point)))
364365
(heading-line (buffer-substring-no-properties
365366
(line-beginning-position)
366-
(line-end-position)))
367-
(todo-state (org-get-todo-state))
368-
(todo-prefix-p
369-
(ai-code--implement-todo--org-todo-headline-p heading-line)))
370-
(when (and (or todo-state todo-prefix-p)
371-
(not (org-entry-is-done-p)))
367+
(line-end-position))))
368+
(when (and (not (org-entry-is-done-p))
369+
(not (string-match-p "^\\*+ DONE " heading-line)))
372370
(let* ((content-start (save-excursion
373371
(forward-line 1)
374372
(point)))
@@ -402,6 +400,7 @@ The plist contains `:heading-line', `:content', and `:line-number'."
402400
ARG is the prefix argument for clipboard context.
403401
Optional DEFAULT-ACTION skips the completing-read prompt when non-nil."
404402
;; DONE: ask user with completing-read before build up prompt, candidate should be 1. Code change; 2. Ask question. Given selection, add suffix to them respectively to indicate AI to make code change, or do not make any code change
403+
;; DONE: currently ai-code-implement-todo work on the org-mode TODO headline. But I want it be able to work on any org-mode headline, no matter it has TODO keyword or not. Please also update the ai-code-code-change and @ai-code-discussion.el#ai-code-ask-question, for the org headline detection code (currently only detect org TODO headline) to be consistent with this function.
405404
(let* ((clipboard-context (when arg (ai-code--get-clipboard-text)))
406405
(current-line (string-trim (thing-at-point 'line t)))
407406
(current-line-number (line-number-at-pos (point)))
@@ -440,7 +439,7 @@ Optional DEFAULT-ACTION skips the completing-read prompt when non-nil."
440439
(ai-code--is-comment-block region-text)))
441440
;; Validate scenario before prompting user
442441
(_ (unless (or org-todo-section-info region-text is-comment)
443-
(user-error "Current line is not a TODO comment or Org TODO headline and cannot proceed with `ai-code-implement-todo'. Please select a TODO comment (not DONE), an Org TODO headline, a region of comments, or activate on a blank line")))
442+
(user-error "Current line is not a TODO comment or Org headline and cannot proceed with `ai-code-implement-todo'. Please select a TODO comment (not DONE), an Org headline (not DONE), a region of comments, or activate on a blank line")))
444443
(_ (unless region-comment-block-p
445444
(user-error "Selected region must be a comment block")))
446445
(action-intent (or default-action
@@ -452,32 +451,32 @@ Optional DEFAULT-ACTION skips the completing-read prompt when non-nil."
452451
(cond
453452
((and ask-question-p org-todo-section-info)
454453
(if (and clipboard-context (string-match-p "\\S-" clipboard-context))
455-
"Question about Org TODO headline (clipboard context): "
456-
"Question about Org TODO headline: "))
454+
"Question about Org headline (clipboard context): "
455+
"Question about Org headline: "))
457456
(ask-question-p
458457
(if (and clipboard-context (string-match-p "\\S-" clipboard-context))
459458
"Question about TODO comment (clipboard context): "
460459
"Question about TODO comment: "))
461460
((and org-todo-section-info
462461
clipboard-context
463462
(string-match-p "\\S-" clipboard-context))
464-
"TODO implementation instruction for Org TODO headline (clipboard context): ")
463+
"Implementation instruction for Org headline (clipboard context): ")
465464
((and clipboard-context
466465
(string-match-p "\\S-" clipboard-context))
467466
(cond
468467
(region-text "TODO implementation instruction (clipboard context): ")
469468
(is-comment "TODO implementation instruction (clipboard context): ")
470469
(function-name (format "TODO implementation instruction for function %s (clipboard context): " function-name))
471470
(t "TODO implementation instruction (clipboard context): ")))
472-
(org-todo-section-info "TODO implementation instruction for Org TODO headline: ")
471+
(org-todo-section-info "Implementation instruction for Org headline: ")
473472
(region-text "TODO implementation instruction: ")
474473
(is-comment "TODO implementation instruction: ")
475474
(function-name (format "TODO implementation instruction for function %s: " function-name))
476475
(t "TODO implementation instruction: ")))
477476
(initial-input
478477
(cond
479478
((and ask-question-p org-todo-section-info)
480-
(format "Regarding this Org TODO headline on line %d:\n%s%s%s"
479+
(format "Regarding this Org headline on line %d:\n%s%s%s"
481480
org-line-number org-section-block function-context files-context-string))
482481
((and ask-question-p region-text)
483482
(format "Regarding this TODO comment block in the selected region:\n%s\n%s%s%s"
@@ -486,7 +485,7 @@ Optional DEFAULT-ACTION skips the completing-read prompt when non-nil."
486485
(format "Regarding this TODO comment on line %d: '%s'%s%s"
487486
current-line-number current-line function-context files-context-string))
488487
(org-todo-section-info
489-
(format "Please implement code for this Org TODO headline first. After implementing, keep the Org TODO headline in place and use the headline and content as prompt context.\nLine %d:\n%s%s%s"
488+
(format "Please implement code for this Org headline first. After implementing, keep the Org headline in place and use the headline and content as prompt context.\nLine %d:\n%s%s%s"
490489
org-line-number org-section-block function-context
491490
files-context-string))
492491
(region-text

ai-code-prompt-mode.el

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,15 @@ If PROMPT-TEXT is a command (starts with /), execute it directly instead."
421421
(if (and (string-prefix-p "/" processed-prompt)
422422
(not (string-match-p " " processed-prompt)))
423423
(ai-code--execute-command processed-prompt)
424-
(ai-code--write-prompt-to-file-and-send 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)))))
425433

426434
;; Define the AI Prompt Mode (derived from org-mode)
427435
;;;###autoload

0 commit comments

Comments
 (0)