Function: eca-chat-completion-at-point

eca-chat-completion-at-point is a natively compiled function defined in eca-chat-context.el.

Signature

(eca-chat-completion-at-point)

Documentation

Complete at point in the chat.

Source Code

;; Defined in /nix/store/jklqzw6pzz6j4jrbv8wy0iwgi7jlrbld-emacs-packages-deps/share/emacs/site-lisp/elpa/eca-20260928.1827/eca-chat-context.el
(defun eca-chat-completion-at-point ()
  "Complete at point in the chat."
  (when-let ((type (eca-chat--completion-type-at-point)))
    (let* ((bounds-start (pcase type
                           ('prompts (1+ (line-beginning-position)))
                           ('files-from-prompt (or (eca-chat--completion-prefix-end eca-chat-filepath-prefix)
                                                   (point)))
                           (_ (or (eca-chat--completion-prefix-end eca-chat-context-prefix)
                                  (point)))))
           (candidates-fn (lambda ()
                            (eca-api-catch 'input
                                (eca-api-while-no-input
                                  (pcase type
                                    ((or 'contexts-from-prompt
                                         'contexts-from-new-context)
                                     (let* ((query (eca-chat--find-typed-query eca-chat-context-prefix))
                                            (roots (eca--session-workspace-folders (eca-session)))
                                            (server-items (eca-chat--completion-cached-items
                                                           'eca-chat--context-completion-cache query
                                                           (lambda ()
                                                             (eca-api-request-while-no-input
                                                              (eca-session)
                                                              :method "chat/queryContext"
                                                              :params (list :chatId eca-chat--id
                                                                            :query query
                                                                            :contexts (vconcat (mapcar #'eca-chat--refine-context
                                                                                                       eca-chat--context)))))
                                                           :contexts
                                                           (lambda (context)
                                                             (eca-chat--context-to-completion query roots context))))
                                            (buffer-items (eca-chat--buffer-completion-items)))
                                       (append server-items buffer-items)))

                                    ('files-from-prompt
                                     (let ((query (eca-chat--find-typed-query eca-chat-filepath-prefix))
                                           (roots (eca--session-workspace-folders (eca-session))))
                                       (eca-chat--completion-cached-items
                                        'eca-chat--file-completion-cache query
                                        (lambda ()
                                          (eca-api-request-while-no-input
                                           (eca-session)
                                           :method "chat/queryFiles"
                                           :params (list :chatId eca-chat--id
                                                         :query query)))
                                        :files
                                        (lambda (file)
                                          (eca-chat--file-to-completion query roots file)))))

                                    ('prompts
                                     (let ((query (buffer-substring-no-properties
                                                   (1+ (line-beginning-position)) (point))))
                                       (eca-chat--completion-cached-items
                                        'eca-chat--command-completion-cache query
                                        (lambda ()
                                          (eca-api-request-while-no-input
                                           (eca-session)
                                           :method "chat/queryCommands"
                                           :params (list :chatId eca-chat--id
                                                         :query query)))
                                        :commands
                                        #'eca-chat--command-to-completion)))

                                    (_ nil)))
                              (:interrupted nil)
                              (`,res res))))
           (exit-fn (pcase type
                      ('contexts-from-new-context #'eca-chat--completion-context-from-new-context-exit-function)
                      ('contexts-from-prompt #'eca-chat--completion-context-from-prompt-exit-function)
                      ('files-from-prompt #'eca-chat--completion-file-from-prompt-exit-function)
                      ('prompts #'eca-chat--completion-prompt-exit-function)
                      (_ nil)))
           (annotation-fn (pcase type
                            ((or 'contexts-from-prompt
                                 'contexts-from-new-context) (-partial #'eca-chat--completion-context-annotate (eca--session-workspace-folders (eca-session))))
                            ('files-from-prompt (-partial #'eca-chat--completion-file-annotate (eca--session-workspace-folders (eca-session))))
                            ('prompts #'eca-chat--completion-prompts-annotate))))
      (list
       bounds-start
       (point)
       (lambda (probe pred action)
         (cond
          ((eq action 'metadata)
           '(metadata (category . eca-capf)
                      (display-sort-function . identity)
                      (cycle-sort-function . identity)))
          ((eq (car-safe action) 'boundaries) nil)
          (t
           (complete-with-action action (funcall candidates-fn) probe pred))))
       :company-kind #'eca-chat--completion-item-label-kind
       :company-require-match 'never
       :annotation-function annotation-fn
       :exit-function exit-fn))))