3232(require 'company )
3333(require 'phpactor )
3434
35+ (defgroup company-phpactor nil
36+ " Company backend for Phpactor."
37+ :prefix " company-phpactor-"
38+ :group 'company
39+ :group 'phpactor )
40+
41+ (defcustom company-phpactor-request-async t
42+ " When non-NIL, asynchronous recuest to Phpactor."
43+ :type 'boolean
44+ :group 'company-phpactor )
45+
3546(defun company-phpactor--grab-symbol ()
3647 " If point is at the end of a symbol, return it.
3748Otherwise, if point is not inside a symbol, return an empty string.
@@ -51,9 +62,9 @@ Here we create a temporary syntax table in order to add $ to symbols."
5162 (let ((response (phpactor--rpc " complete" (phpactor--command-argments :source :offset ))))
5263 (plist-get (plist-get (plist-get response :parameters ) :value ) :suggestions )))
5364
54- (defun company-phpactor--get-candidates ()
55- " Build a list of candidates with text-properties extracted from phpactor's output."
56- (let ((suggestions (company-phpactor--get-suggestions)) candidate)
65+ (defun company-phpactor--get-candidates (suggestions )
66+ " Build a list of candidates with text-properties extracted from phpactor's output `SUGGESTIONS' ."
67+ (let (candidate)
5768 (mapcar
5869 (lambda (suggestion )
5970 (setq candidate (plist-get suggestion :name ))
@@ -75,6 +86,17 @@ Here we create a temporary syntax table in order to add $ to symbols."
7586 " Show additional info (ARG) from phpactor as lateral annotation."
7687 (message (concat " " (get-text-property 0 'annotation arg))))
7788
89+ (defun company-phpactor--get-candidates-async (callback )
90+ " Get completion candidates asynchronously calling `CALLBACK' by Phpactor."
91+ (if (not company-phpactor-request-async)
92+ (funcall callback (company-phpactor--get-candidates (company-phpactor--get-suggestions)))
93+ (phpactor--rpc-async " complete" (phpactor--command-argments :source :offset )
94+ (lambda (proc )
95+ (let* ((response (phpactor--parse-json (process-buffer proc)))
96+ (suggestions
97+ (plist-get (plist-get (plist-get response :parameters ) :value ) :suggestions )))
98+ (funcall callback (company-phpactor--get-candidates suggestions)))))))
99+
78100;;;### autoload
79101(defun company-phpactor (command &optional arg &rest ignored )
80102 " `company-mode' completion backend for Phpactor."
@@ -87,7 +109,7 @@ Here we create a temporary syntax table in order to add $ to symbols."
87109 (`annotation (company-phpactor--annotation arg))
88110 (`interactive (company-begin-backend 'company-phpactor ))
89111 (`prefix (company-phpactor--grab-symbol))
90- (`candidates (company-phpactor--get-candidates))))))
112+ (`candidates (cons :async # ' company-phpactor--get-candidates-async ))))))
91113
92114(provide 'company-phpactor )
93115; ;; company-phpactor.el ends here
0 commit comments