From b09f28fff9220bac684920aaff968877723ad34d Mon Sep 17 00:00:00 2001 From: Kentaro Ohkouchi Date: Mon, 19 Aug 2019 17:37:09 +0900 Subject: [PATCH 1/6] Use :async to candidates --- company-phpactor.el | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/company-phpactor.el b/company-phpactor.el index b1cb66e..370224c 100644 --- a/company-phpactor.el +++ b/company-phpactor.el @@ -75,6 +75,10 @@ Here we create a temporary syntax table in order to add $ to symbols." "Show additional info (ARG) from phpactor as lateral annotation." (message (concat " " (get-text-property 0 'annotation arg)))) +(defun company-phpactor--get-candidates-async (callback) + "Get completion candidates asynchronously." + (funcall callback (company-phpactor--get-candidates))) + ;;;###autoload (defun company-phpactor (command &optional arg &rest ignored) "`company-mode' completion backend for Phpactor." @@ -87,7 +91,9 @@ Here we create a temporary syntax table in order to add $ to symbols." (`annotation (company-phpactor--annotation arg)) (`interactive (company-begin-backend 'company-phpactor)) (`prefix (company-phpactor--grab-symbol)) - (`candidates (company-phpactor--get-candidates)))))) + (`candidates + (cons :async (lambda (callback) + (company-phpactor--get-candidates-async callback)))))))) (provide 'company-phpactor) ;;; company-phpactor.el ends here From e154a4ccd2997cf1f330f6135cebb4263f5804ce Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Tue, 20 Aug 2019 21:18:29 +0900 Subject: [PATCH 2/6] Add require async package and impl phpactor--rpc-async --- phpactor.el | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/phpactor.el b/phpactor.el index fadf597..3088528 100644 --- a/phpactor.el +++ b/phpactor.el @@ -7,7 +7,7 @@ ;; Created: 8 Apr 2018 ;; Version: 0.1.0 ;; Keywords: tools, php -;; Package-Requires: ((emacs "24.4") (cl-lib "0.5") (f "0.17") (php-runtime "0.2") (composer "0.1")) +;; Package-Requires: ((emacs "24.4") (cl-lib "0.5") (f "0.17") (php-runtime "0.2") (composer "0.1") (async "1.9.3")) ;; URL: https://github.com/emacs-php/phpactor.el ;; License: GPL-3.0-or-later @@ -52,6 +52,7 @@ (require 'ring) (require 'subr-x) (require 'composer) +(require 'async) ;; Custom variables ;;;###autoload @@ -223,6 +224,20 @@ have to ensure a compatible version of phpactor is used." (call-process-region (point-min) (point-max) phpactor-executable nil output nil "rpc" (format "--working-dir=%s" default-directory)) (phpactor--parse-json output)))) +(defun phpactor--rpc-async (action arguments callback) + "Async execute Phpactor `ACTION' subcommand with `ARGUMENTS' and calling `CALLBACK' after process." + (declare (indent 2)) + (phpactor--add-history 'phpactor--rpc-async (list action arguments)) + (let* ((json (phpactor--serialize-json (list :action action + :parameters arguments))) + (coding-system-for-write 'utf-8) + (executable phpactor-executable) + (proc (async-start-process + "phpactor-async" executable callback + "rpc" (format "--working-dir=%s" (phpactor-get-working-dir))))) + (process-send-string proc json) + (process-send-eof proc))) + (defun phpactor--parse-json (buffer) "Read JSON string from BUFFER." (with-current-buffer buffer From 11b03939997f0dc4d9138cf54ffa9a9450324d24 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Tue, 20 Aug 2019 21:18:45 +0900 Subject: [PATCH 3/6] Add company-phpactor-request-async option --- company-phpactor.el | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/company-phpactor.el b/company-phpactor.el index 370224c..c747211 100644 --- a/company-phpactor.el +++ b/company-phpactor.el @@ -32,6 +32,17 @@ (require 'company) (require 'phpactor) +(defgroup company-phpactor nil + "Company backend for Phpactor." + :prefix "company-phpactor-" + :group 'company + :group 'phpactor) + +(defcustom company-phpactor-request-async t + "When non-NIL, asynchronous recuest to Phpactor." + :type 'boolean + :group 'company-phpactor) + (defun company-phpactor--grab-symbol () "If point is at the end of a symbol, return it. Otherwise, if point is not inside a symbol, return an empty string. From 537ca19dc5e31460dda13d72a499872b6581dac1 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Tue, 20 Aug 2019 21:20:20 +0900 Subject: [PATCH 4/6] Inject suggestions from the argument --- company-phpactor.el | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/company-phpactor.el b/company-phpactor.el index c747211..d6a1664 100644 --- a/company-phpactor.el +++ b/company-phpactor.el @@ -62,9 +62,10 @@ Here we create a temporary syntax table in order to add $ to symbols." (let ((response (phpactor--rpc "complete" (phpactor--command-argments :source :offset)))) (plist-get (plist-get (plist-get response :parameters) :value) :suggestions))) -(defun company-phpactor--get-candidates () +(defun company-phpactor--get-candidates (&optional suggestions) "Build a list of candidates with text-properties extracted from phpactor's output." - (let ((suggestions (company-phpactor--get-suggestions)) candidate) + (let ((suggestions (or suggestions (company-phpactor--get-suggestions))) + candidate) (mapcar (lambda (suggestion) (setq candidate (plist-get suggestion :name)) From 7914b2456d24001a4f09b0a9a8337bc0b63c4576 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Tue, 20 Aug 2019 21:30:17 +0900 Subject: [PATCH 5/6] Asynchronous function call with phpactor--rpc-async --- company-phpactor.el | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/company-phpactor.el b/company-phpactor.el index d6a1664..b079c13 100644 --- a/company-phpactor.el +++ b/company-phpactor.el @@ -62,10 +62,9 @@ Here we create a temporary syntax table in order to add $ to symbols." (let ((response (phpactor--rpc "complete" (phpactor--command-argments :source :offset)))) (plist-get (plist-get (plist-get response :parameters) :value) :suggestions))) -(defun company-phpactor--get-candidates (&optional suggestions) - "Build a list of candidates with text-properties extracted from phpactor's output." - (let ((suggestions (or suggestions (company-phpactor--get-suggestions))) - candidate) +(defun company-phpactor--get-candidates (suggestions) + "Build a list of candidates with text-properties extracted from phpactor's output `SUGGESTIONS'." + (let (candidate) (mapcar (lambda (suggestion) (setq candidate (plist-get suggestion :name)) @@ -88,8 +87,15 @@ Here we create a temporary syntax table in order to add $ to symbols." (message (concat " " (get-text-property 0 'annotation arg)))) (defun company-phpactor--get-candidates-async (callback) - "Get completion candidates asynchronously." - (funcall callback (company-phpactor--get-candidates))) + "Get completion candidates asynchronously calling `CALLBACK' by Phpactor." + (if (not company-phpactor-request-async) + (funcall callback (company-phpactor--get-candidates (company-phpactor--get-suggestions))) + (phpactor--rpc-async "complete" (phpactor--command-argments :source :offset) + (lambda (proc) + (let* ((response (phpactor--parse-json (process-buffer proc))) + (suggestions + (plist-get (plist-get (plist-get response :parameters) :value) :suggestions))) + (funcall callback (company-phpactor--get-candidates suggestions))))))) ;;;###autoload (defun company-phpactor (command &optional arg &rest ignored) @@ -103,9 +109,7 @@ Here we create a temporary syntax table in order to add $ to symbols." (`annotation (company-phpactor--annotation arg)) (`interactive (company-begin-backend 'company-phpactor)) (`prefix (company-phpactor--grab-symbol)) - (`candidates - (cons :async (lambda (callback) - (company-phpactor--get-candidates-async callback)))))))) + (`candidates (cons :async #'company-phpactor--get-candidates-async)))))) (provide 'company-phpactor) ;;; company-phpactor.el ends here From 49dfc4700e06bf936fd489b2e993ae3683a30fd0 Mon Sep 17 00:00:00 2001 From: USAMI Kenta Date: Thu, 22 Aug 2019 19:23:02 +0900 Subject: [PATCH 6/6] Explicitly set the current directory (default-directory) before starting the process --- phpactor.el | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/phpactor.el b/phpactor.el index 3088528..e608137 100644 --- a/phpactor.el +++ b/phpactor.el @@ -231,10 +231,11 @@ have to ensure a compatible version of phpactor is used." (let* ((json (phpactor--serialize-json (list :action action :parameters arguments))) (coding-system-for-write 'utf-8) + (default-directory (phpactor-get-working-dir)) (executable phpactor-executable) (proc (async-start-process "phpactor-async" executable callback - "rpc" (format "--working-dir=%s" (phpactor-get-working-dir))))) + "rpc" (format "--working-dir=%s" default-directory)))) (process-send-string proc json) (process-send-eof proc)))