Skip to content

Add hook to customize @ file completion#518

Open
nohzafk wants to merge 1 commit into
xenodium:mainfrom
nohzafk:main
Open

Add hook to customize @ file completion#518
nohzafk wants to merge 1 commit into
xenodium:mainfrom
nohzafk:main

Conversation

@nohzafk
Copy link
Copy Markdown

@nohzafk nohzafk commented Apr 12, 2026

Problem

@ completion is hardcoded to CAPF/projectile, preventing fuzzy or alternative backends.

Without corfu/company it faills through to Completions buffer with prefix mathching.

Solution

  • add agent-shell-file-completion-function defcustom to handle @ completion; if it returns non-nil, CAPF is skipped.
  • Default behavior remains unchanged (CAPF still used when the custom function returns nil).

My Hack

Because I dont' use courfu/company, currently I use this hack to make agent-shell completion to use with another backend, it would be nice that agent-shell provide an option for user to provide custom function

;; Fuzzy file completion for @ references using consult-snapfile.
    ;; Replaces prefix-based CAPF with minibuffer fuzzy search via vertico.
    (defun backbone/agent-shell-fuzzy-insert-file ()
      "Insert a file reference using consult-snapfile fuzzy matching.
On cancel, inserts bare @ for manual typing."
      (interactive)
      (if (not (fboundp 'consult-snapfile--async-source))
          (progn (self-insert-command 1 ?@)
                 (completion-at-point))
        (let* ((root (consult-snapfile--project-root))
               (default-directory root)
               (prompt (format "@ File [%s]: " (abbreviate-file-name root)))
               (selected (condition-case nil
                             (consult--read
                              #'consult-snapfile--async-source
                              :prompt prompt
                              :sort nil
                              :require-match t
                              :category 'file
                              :history 'file-name-history)
                           (quit nil))))
          (if selected
              (insert "@" (substring-no-properties selected) " ")
            (insert "@")))))

    (keymap-set agent-shell-mode-map "@" #'backbone/agent-shell-fuzzy-insert-file)

    ;; Route / command CAPF completion through minibuffer for fuzzy matching.
    (defun backbone/agent-shell-setup-fuzzy-completion ()
      "Use consult-completion-in-region for CAPF in agent-shell."
      (setq-local completion-in-region-function
                  #'consult-completion-in-region))
    (add-hook 'agent-shell-mode-hook #'backbone/agent-shell-setup-fuzzy-completion)

Checklist

  • I agree to communicate (PR description and comments) with the author myself (not AI-generated).
  • I've reviewed all code in PR myself and will vouch for its quality.
  • I've read and followed the Contributing guidelines.
  • I've filed a feature request/discussion for a new feature.
  • I've added tests where applicable.
  • I've updated documentation where necessary.
  • I've run M-x checkdoc and M-x byte-compile-file.

@xenodium
Copy link
Copy Markdown
Owner

xenodium commented May 19, 2026

Thanks for the PR. Sorry about the delay. Working my way through the backlog from #500.

How about we make this a little more versatile with something like?

(defcustom agent-shell-file-completion-function
  #'completion-at-point
  "Function called when @ is typed at a word boundary.
The function takes no arguments and is responsible for performing
the completion (e.g. calling `completion-at-point' or invoking a
custom completion UI).  Set to nil to disable @ completion."
  :type '(choice (const :tag "Disabled" nil)
                 function)
  :group 'agent-shell)

(cond
 ((eq (char-before) ?@)
  (when agent-shell-file-completion-function
    (funcall agent-shell-file-completion-function)))
 ((and (eq (char-before) ?/)
       (agent-shell--command-completion-at-point))
  (completion-at-point)))

ps. Don't you need a similar thing for completing commands?

@nohzafk
Copy link
Copy Markdown
Author

nohzafk commented May 23, 2026

Yes, I might need similar things for command completion. However, I suggest we add another custom variable for command completion that is symmetric to file completion.

@xenodium
Copy link
Copy Markdown
Owner

However, I suggest we add another custom variable for command completion that is symmetric to file completion.

SGTM!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants