Skip to content

Commit f846453

Browse files
authored
Merge pull request #133 from emacs-php/use-async-v2
Use :async to company-phpactor, Version 2
2 parents c3a41f2 + 49dfc47 commit f846453

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

company-phpactor.el

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@
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.
3748
Otherwise, 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

phpactor.el

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
;; Created: 8 Apr 2018
88
;; Version: 0.1.0
99
;; Keywords: tools, php
10-
;; Package-Requires: ((emacs "24.4") (cl-lib "0.5") (f "0.17") (php-runtime "0.2") (composer "0.1"))
10+
;; Package-Requires: ((emacs "24.4") (cl-lib "0.5") (f "0.17") (php-runtime "0.2") (composer "0.1") (async "1.9.3"))
1111
;; URL: https://github.com/emacs-php/phpactor.el
1212
;; License: GPL-3.0-or-later
1313

@@ -52,6 +52,7 @@
5252
(require 'ring)
5353
(require 'subr-x)
5454
(require 'composer)
55+
(require 'async)
5556

5657
;; Custom variables
5758
;;;###autoload
@@ -223,6 +224,21 @@ have to ensure a compatible version of phpactor is used."
223224
(call-process-region (point-min) (point-max) phpactor-executable nil output nil "rpc" (format "--working-dir=%s" default-directory))
224225
(phpactor--parse-json output))))
225226

227+
(defun phpactor--rpc-async (action arguments callback)
228+
"Async execute Phpactor `ACTION' subcommand with `ARGUMENTS' and calling `CALLBACK' after process."
229+
(declare (indent 2))
230+
(phpactor--add-history 'phpactor--rpc-async (list action arguments))
231+
(let* ((json (phpactor--serialize-json (list :action action
232+
:parameters arguments)))
233+
(coding-system-for-write 'utf-8)
234+
(default-directory (phpactor-get-working-dir))
235+
(executable phpactor-executable)
236+
(proc (async-start-process
237+
"phpactor-async" executable callback
238+
"rpc" (format "--working-dir=%s" default-directory))))
239+
(process-send-string proc json)
240+
(process-send-eof proc)))
241+
226242
(defun phpactor--parse-json (buffer)
227243
"Read JSON string from BUFFER."
228244
(with-current-buffer buffer

0 commit comments

Comments
 (0)