Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions agent-shell-project.el
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,31 @@
(declare-function projectile-project-name "projectile")
(declare-function projectile-current-project-files "projectile")

(defvar agent-shell-list-files-depth 1
"Maximum directory depth for file completion outside a project.
Set to 0 to disable the fallback entirely.")

(defun agent-shell--should-descend (max-depth root dir)
"Return non-nil if DIR should be descended into within MAX-DEPTH levels of ROOT."
(let* ((rel (string-remove-prefix root dir))
(current-depth (length (seq-filter
(lambda (s) (not (string-empty-p s)))
(file-name-split rel)))))
(< current-depth max-depth)))

(defun agent-shell--directory-files (dir max-depth)
"List files under DIR, up to MAX-DEPTH levels."
(let ((root (file-name-as-directory (expand-file-name dir))))
(directory-files-recursively
root
directory-files-no-dot-files-regexp
nil
(apply-partially #'agent-shell--should-descend max-depth root)
nil)))

(defun agent-shell--project-files ()
"Get project files using projectile or project.el."
"Get project files using `projectile', `project.el'.
Fall back to `agent-shell-cwd' if not in a detected project."
(cond
((and (boundp 'projectile-mode)
projectile-mode
Expand All @@ -52,7 +75,8 @@
(mapcar (lambda (f)
(string-remove-prefix root f))
(project-files proj))))
(t nil)))
((> agent-shell-list-files-depth 0)
(agent-shell--directory-files (agent-shell-cwd) agent-shell-list-files-depth))))

(defcustom agent-shell-cwd-function nil
"When non-nil, a function called to determine the shell's CWD.
Expand Down