Skip to content

Commit 89f41ba

Browse files
committed
Allow non-git files in repo context candidates
1 parent 8db1317 commit 89f41ba

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

ai-code-file.el

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ Otherwise, ask AI to generate a build command."
477477
When no region is selected, use the full file path and current function
478478
\(if any). When a region is active, use the file path with line range
479479
in the form filepath#Lstart-Lend."
480+
;; DONE: allow adding non git file / path to existing context candidates, it just use full path
480481
(interactive)
481482
(let* ((current-root (ai-code--git-root))
482483
(all-roots (let ((roots '()))
@@ -495,9 +496,24 @@ in the form filepath#Lstart-Lend."
495496
(when (and root (not (member root roots)))
496497
(push root roots)))))))
497498
(nreverse roots)))
498-
(ordered-roots (if (and current-root (member current-root all-roots))
499-
(cons current-root (remove current-root all-roots))
500-
all-roots))
499+
(existing-roots (let ((roots '()))
500+
(maphash (lambda (root _contexts)
501+
(when (and (stringp root)
502+
(not (member root roots)))
503+
(push root roots)))
504+
ai-code--repo-context-info)
505+
(nreverse roots)))
506+
(candidate-roots (let ((roots (reverse existing-roots)))
507+
(dolist (root (reverse all-roots))
508+
(unless (member root roots)
509+
(push root roots)))
510+
(when (and current-root
511+
(not (member current-root roots)))
512+
(push current-root roots))
513+
roots))
514+
(ordered-roots (if (and current-root (member current-root candidate-roots))
515+
(cons current-root (remove current-root candidate-roots))
516+
candidate-roots))
501517
(repo-root (cond
502518
((null ordered-roots)
503519
(user-error "Not inside a Git repository"))

test/test_ai-code-file.el

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,36 @@ everything is cleaned up afterward."
675675
(ai-code-context-action '(4))
676676
(should completing-read-called))))
677677

678+
(ert-deftest ai-code-test-add-context-allows-non-git-file-for-existing-repo-context ()
679+
"Test `ai-code-add-context' can add a non-git file to an existing repo context."
680+
(let ((ai-code--repo-context-info (make-hash-table :test #'equal))
681+
(repo-root "/tmp/existing-repo/")
682+
(new-file (make-temp-file "ai-code-context-")))
683+
(unwind-protect
684+
(with-temp-buffer
685+
(setq buffer-file-name new-file)
686+
(puthash repo-root
687+
'("/tmp/existing-repo/lib/existing-context.el")
688+
ai-code--repo-context-info)
689+
(cl-letf (((symbol-function 'ai-code--git-root)
690+
(lambda (&optional _dir) nil))
691+
((symbol-function 'walk-windows)
692+
(lambda (&rest _args) nil))
693+
((symbol-function 'completing-read)
694+
(lambda (_prompt collection &rest _args)
695+
(should (equal collection (list repo-root)))
696+
repo-root))
697+
((symbol-function 'derived-mode-p)
698+
(lambda (&rest _args) nil))
699+
((symbol-function 'message)
700+
(lambda (&rest _args) nil)))
701+
(ai-code-add-context)
702+
(should (equal (gethash repo-root ai-code--repo-context-info)
703+
(list new-file
704+
"/tmp/existing-repo/lib/existing-context.el")))))
705+
(when (file-exists-p new-file)
706+
(delete-file new-file)))))
707+
678708
(ert-deftest ai-code-test-build-or-test-project-dispatches-test-project ()
679709
"Test selecting \"Test project\" dispatches to `ai-code-test-project'."
680710
(let ((test-project-called nil)

0 commit comments

Comments
 (0)