Skip to content

Commit 16c9e10

Browse files
committed
Add function to get function name related to comment context
1 parent e9f4829 commit 16c9e10

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

HISTORY.org

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- feat: Improve aider-implement-todo
1111
- aider-implement-todo (C-a i) on blank line will ask user and insert TODO comment.
1212
- After finishing code generation, mark TODO as DONE, instead of delete it
13+
- you can add TODO requirement ahead of a function or class, and use aider-implement-todo to make the change for them, addressing https://github.com/tninja/ai-code-interface.el/issues/40
1314

1415
** v0.13.1
1516

aider-code-change.el

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,46 @@ ignoring leading whitespace."
159159
(aider--is-comment-line line))
160160
lines)))))
161161

162+
(defun aider--get-function-name-for-comment ()
163+
"Return function name relevant to comment at point.
164+
If comment precedes a function definition or is inside a function body,
165+
use that function; otherwise fall back to `which-function`."
166+
(let* ((current-func (which-function))
167+
(resolved-func
168+
(save-excursion
169+
(cl-labels ((line-text ()
170+
(buffer-substring-no-properties
171+
(line-beginning-position)
172+
(line-end-position))))
173+
(forward-line 1)
174+
(cl-block resolve
175+
(let ((text (line-text)))
176+
(when (or (eobp) (string-blank-p text))
177+
(cl-return-from resolve nil))
178+
(while (aider--is-comment-line text)
179+
(forward-line 1)
180+
(setq text (line-text))
181+
(when (or (eobp) (string-blank-p text))
182+
(cl-return-from resolve nil)))
183+
(let ((next-func (which-function)))
184+
(cl-loop with lookahead = 5
185+
while (and (> lookahead 0)
186+
(or (null next-func)
187+
(string= next-func current-func)))
188+
do (forward-line 1)
189+
(setq lookahead (1- lookahead))
190+
(setq text (line-text))
191+
(when (string-blank-p text)
192+
(cl-return-from resolve nil))
193+
(unless (aider--is-comment-line text)
194+
(setq next-func (which-function)))
195+
finally return (cond
196+
((not current-func) next-func)
197+
((not next-func) current-func)
198+
((not (string= next-func current-func)) next-func)
199+
(t current-func))))))))))
200+
resolved-func))
201+
162202
(defun aider--extract-comment-content (comment-text)
163203
"Extract the actual content from COMMENT-TEXT, removing comment markers."
164204
(when comment-start
@@ -304,7 +344,9 @@ The keyword and its definition are configured in `aider-todo-keyword-pair`."
304344
(let* ((current-line (string-trim (thing-at-point 'line t)))
305345
(current-line-number (line-number-at-pos (point)))
306346
(is-comment (aider--is-comment-line current-line))
307-
(function-name (which-function))
347+
(function-name (if is-comment
348+
(aider--get-function-name-for-comment)
349+
(which-function)))
308350
(function-context (when function-name (format "\nFunction: %s" function-name)))
309351
(region-active (region-active-p))
310352
(region-text (when region-active

0 commit comments

Comments
 (0)