ein-company: Add completion caching, make caching kernel specific.

This commit is contained in:
John Miller 2018-11-22 09:07:02 -07:00
parent f8ac77df66
commit e280bb8633
4 changed files with 41 additions and 30 deletions

View file

@ -247,6 +247,7 @@
stdin-activep stdin-activep
username username
msg-callbacks msg-callbacks
oinfo-cache
;; FIXME: Use event instead of hook. ;; FIXME: Use event instead of hook.
after-start-hook after-start-hook
after-execute-hook) after-execute-hook)

View file

@ -95,9 +95,10 @@
minor-mode-list) minor-mode-list)
(ein:object-at-point))) (ein:object-at-point)))
(annotation (if ein:allow-company-annotations (annotation (if ein:allow-company-annotations
(ein:aif (gethash arg *ein:oinfo-cache*) (let ((kernel (ein:get-kernel)))
(plist-get it :definition) (ein:aif (gethash arg (ein:$kernel-oinfo-cache kernel))
""))) (plist-get it :definition)
""))))
(doc-buffer (lexical-let ((arg arg)) (doc-buffer (lexical-let ((arg arg))
(cons :async (cons :async
(lambda (cb) (lambda (cb)
@ -108,17 +109,20 @@
(ein:pytools-find-source (ein:get-kernel-or-error) (ein:pytools-find-source (ein:get-kernel-or-error)
obj obj
cb))))) cb)))))
(candidates () (lexical-let ((kernel (ein:get-kernel-or-error)) (candidates () (or
(col (current-column))) (ein:completions--find-cached-completion (ein:object-at-point)
(unless (ein:company-backend--punctuation-check (thing-at-point 'line) col) (ein:$kernel-oinfo-cache (ein:get-kernel-or-error)))
(case ein:completion-backend (lexical-let ((kernel (ein:get-kernel-or-error))
(ein:use-company-jedi-backend (col (current-column)))
(cons :async (lambda (cb) (unless (ein:company-backend--punctuation-check (thing-at-point 'line) col)
(ein:company--complete-jedi cb)))) (case ein:completion-backend
(t (ein:use-company-jedi-backend
(cons :async (cons :async (lambda (cb)
(lambda (cb) (ein:company--complete-jedi cb))))
(ein:company--complete cb)))))))))) (t
(cons :async
(lambda (cb)
(ein:company--complete cb)))))))))))
(defun ein:company-backend--punctuation-check (thing col) (defun ein:company-backend--punctuation-check (thing col)

View file

@ -128,10 +128,13 @@ notebook buffers and connected buffers."
;;; Retrieving Python Object Info ;;; Retrieving Python Object Info
(defvar *ein:oinfo-cache* (make-hash-table :test #'equal)) (defun ein:completions--reset-oinfo-cache (kernel)
(setf (ein:$kernel-oinfo-cache kernel) (make-hash-table :test #'equal)))
(defun ein:completions--reset-oinfo-cache () (defun ein:completions--find-cached-completion (partial oinfo-cache)
(setf *ein:oinfo-cache* (make-hash-table :test #'equal))) (loop for candidate being the hash-keys of oinfo-cache
when (string-prefix-p partial candidate)
collect candidate))
(defun ein:completions--get-oinfo (obj) (defun ein:completions--get-oinfo (obj)
(let ((d (deferred:new #'identity)) (let ((d (deferred:new #'identity))
@ -146,22 +149,23 @@ notebook buffers and connected buffers."
d)) d))
(defun ein:completions--build-oinfo-cache (objs) (defun ein:completions--build-oinfo-cache (objs)
(dolist (o (-non-nil objs)) (let ((kernel (ein:get-kernel)))
(deferred:$ (dolist (o (-non-nil objs))
(deferred:next (deferred:$
(lambda () (deferred:next
(ein:completions--get-oinfo (ein:trim o "\\s-\\|\n\\|\\.")))) (lambda ()
(deferred:nextc it (ein:completions--get-oinfo (ein:trim o "\\s-\\|\n\\|\\."))))
(lambda (output) (deferred:nextc it
(ein:completions--prepare-oinfo output o)))))) (lambda (output)
(ein:completions--prepare-oinfo output o kernel)))))))
(defun ein:completions--prepare-oinfo (output obj) (defun ein:completions--prepare-oinfo (output obj kernel)
(condition-case err (condition-case err
(destructuring-bind (msg-type content _) output (destructuring-bind (msg-type content _) output
(ein:case-equal msg-type (ein:case-equal msg-type
(("stream" "display_data" "pyout" "execute_result") (("stream" "display_data" "pyout" "execute_result")
(ein:aif (plist-get content :text) (ein:aif (plist-get content :text)
(setf (gethash obj *ein:oinfo-cache*) (ein:json-read-from-string it)))) (setf (gethash obj (ein:$kernel-oinfo-cache kernel)) (ein:json-read-from-string it))))
(("error" "pyerr") (("error" "pyerr")
(ein:log 'error "ein:completions--prepare-oinfo: %S" (plist-get content :traceback))))) (ein:log 'error "ein:completions--prepare-oinfo: %S" (plist-get content :traceback)))))
;; It's okay, bad things happen. Not everything in python is going to have a ;; It's okay, bad things happen. Not everything in python is going to have a
@ -170,13 +174,14 @@ notebook buffers and connected buffers."
;; register a debug message in case someone really needs to know what is ;; register a debug message in case someone really needs to know what is
;; happening. ;; happening.
(error (ein:log 'debug "ein:completions--prepare-oinfo: [%s] %s" err obj) (error (ein:log 'debug "ein:completions--prepare-oinfo: [%s] %s" err obj)
(setf (gethash obj *ein:oinfo-cache*) :json-false)))) (setf (gethash obj (ein:$kernel-oinfo-cache kernel)) :json-false))))
;;; Support for Eldoc ;;; Support for Eldoc
(defun ein:completer--get-eldoc-signature () (defun ein:completer--get-eldoc-signature ()
(let ((func (ein:function-at-point))) (let ((func (ein:function-at-point))
(ein:aif (gethash func *ein:oinfo-cache*) (kernel (ein:get-kernel)))
(ein:aif (gethash func (ein:$kernel-oinfo-cache kernel))
(ein:kernel-construct-defstring it) (ein:kernel-construct-defstring it)
(ein:completions--build-oinfo-cache (list func))))) (ein:completions--build-oinfo-cache (list func)))))

View file

@ -77,6 +77,7 @@
:websocket nil :websocket nil
:base-url base-url :base-url base-url
:stdin-activep nil :stdin-activep nil
:oinfo-cache (make-hash-table :test #'equal)
:username "username" :username "username"
:msg-callbacks (make-hash-table :test 'equal))) :msg-callbacks (make-hash-table :test 'equal)))