Only attempt to complete at point if jupyter-current-client is non-nil

This commit is contained in:
Nathaniel Nicandro 2018-09-29 21:22:02 -05:00
parent 3cd573f5c3
commit 327bbede75

View file

@ -1739,72 +1739,87 @@ Run FUN when the completions are available."
(defun jupyter-completion-at-point () (defun jupyter-completion-at-point ()
"Function to add to `completion-at-point-functions'." "Function to add to `completion-at-point-functions'."
(let ((prefix (jupyter-completion-prefix)) req) (when jupyter-current-client
(when jupyter-completion--company-timer (let ((prefix (jupyter-completion-prefix)) req)
(cancel-timer jupyter-completion--company-timer)) (when jupyter-completion--company-timer
(when prefix (cancel-timer jupyter-completion--company-timer))
(when (consp prefix) (when prefix
(setq prefix (car prefix)) (when (consp prefix)
(when (and (bound-and-true-p company-mode) (setq prefix (car prefix))
(not company-candidates) (when (and (bound-and-true-p company-mode)
(< (length prefix) company-minimum-prefix-length)) (not company-candidates)
;; Trigger completion similar to `company' when (< (length prefix) company-minimum-prefix-length))
;; `jupyter-completion-prefix' returns a cons cell. ;; Trigger completion similar to `company' when
(setq jupyter-completion--company-timer ;; `jupyter-completion-prefix' returns a cons cell.
;; NOTE: When we reach here `company-idle-delay' is `now' since (setq jupyter-completion--company-timer
;; we are already inside a company completion so we can't use ;; NOTE: When we reach here `company-idle-delay' is `now' since
;; it, just use a sensible time value instead. ;; we are already inside a company completion so we can't use
(run-with-timer 0.1 nil 'company-manual-begin)))) ;; it, just use a sensible time value instead.
;; Prefetch candidates (run-with-idle-timer
(when (or (not jupyter-completion-last-prefix) 0.01 nil
(not jupyter-completion-cache) (lambda ()
(and (let ((company-minimum-prefix-length 0)
;; Only when there is no whitespace before point since some (this-command 'company-manual-begin))
;; kernel's would give a list of all completions on every (unless company-candidates
;; space. (company-auto-begin))
(not (eq (char-syntax (char-before)) ? )) ;; Only call frontends when there are
(or ;; completions from the kernel for
;; This case happens when completing things like foo.| ;; syntax based auto completion
(string= jupyter-completion-last-prefix "") (when jupyter-completion-cache
;; The obvious condition... (company-post-command))))))))
(not (string-prefix-p jupyter-completion-last-prefix prefix)))) ;; Prefetch candidates
;; Invalidate the cache when completing argument lists (when (or (not jupyter-completion-last-prefix)
(and (not (string= prefix "")) (not jupyter-completion-cache)
(eq (aref prefix (1- (length prefix))) ?\())) (and
(setq ;; FIXME: We would like to manually get
jupyter-completion-last-prefix prefix ;; completions for empty prefixes because
req (jupyter-completion-prefetch ;; of things like from foo import |
(lambda (msg) (setq jupyter-completion-cache ;; Only when there is no whitespace before point since some
(cons 'fetched msg)))))) ;; kernel's would give a list of all completions on every
(list ;; space.
(- (point) (length prefix)) (point) ;; (not (eq (char-syntax (char-before)) ? ))
(completion-table-dynamic (or
(lambda (_) ;; This case happens when completing things like foo.|
(when (and req (not (jupyter-request-idle-received-p req)) (string= jupyter-completion-last-prefix "")
(not (eq (jupyter-message-type ;; The obvious condition...
(jupyter-request-last-message req)) (not (string-prefix-p jupyter-completion-last-prefix prefix))))
:complete-reply))) ;; Invalidate the cache when completing argument lists
(jupyter-wait-until-received :complete-reply req)) (and (not (string= prefix ""))
(when (eq (car jupyter-completion-cache) 'fetched) (eq (aref prefix (1- (length prefix))) ?\()))
(cl-destructuring-bind (&key status (setq
matches metadata jupyter-completion-last-prefix prefix
&allow-other-keys) req (jupyter-completion-prefetch
(jupyter-message-content (cdr jupyter-completion-cache)) (lambda (msg) (setq jupyter-completion-cache
(setq jupyter-completion-cache (cons 'fetched msg))))))
(when (equal status "ok") (list
(jupyter-completion-construct-candidates (- (point) (length prefix)) (point)
matches metadata))))) (completion-table-dynamic
jupyter-completion-cache)) (lambda (_)
:exit-function (when (and req (not (jupyter-request-idle-received-p req))
#'jupyter-completion--post-completion (not (eq (jupyter-message-type
:company-location (jupyter-request-last-message req))
(lambda (arg) (get-text-property 0 'location arg)) :complete-reply)))
:annotation-function (jupyter-wait-until-received :complete-reply req))
(lambda (arg) (get-text-property 0 'annot arg)) (when (eq (car jupyter-completion-cache) 'fetched)
:company-docsig (cl-destructuring-bind (&key status
(lambda (arg) (get-text-property 0 'docsig arg)) matches metadata
:company-doc-buffer &allow-other-keys)
#'jupyter-completion--company-doc-buffer)))) (jupyter-message-content (cdr jupyter-completion-cache))
(setq jupyter-completion-cache
(when (equal status "ok")
(jupyter-completion-construct-candidates
matches metadata)))))
jupyter-completion-cache))
:exit-function
#'jupyter-completion--post-completion
:company-location
(lambda (arg) (get-text-property 0 'location arg))
:annotation-function
(lambda (arg) (get-text-property 0 'annot arg))
:company-docsig
(lambda (arg) (get-text-property 0 'docsig arg))
:company-doc-buffer
#'jupyter-completion--company-doc-buffer)))))
(defun jupyter-completion--company-doc-buffer (arg) (defun jupyter-completion--company-doc-buffer (arg)
(let* ((inhibit-read-only t) (let* ((inhibit-read-only t)