From 9e12ba5eeebdee0530de12ca57c8a1fd237998ef Mon Sep 17 00:00:00 2001
From: Mikael Kermorgant <mikael.kermorgant@gmail.com>
Date: Wed, 22 Aug 2018 22:01:10 +0300
Subject: [PATCH] toggle completion caching whether number of candidates
 reaches limit

if the number of suggested candidates reaches the limit, the desired
candidate might be missing.

If that occurs, caching should not be used when typing more characters
of the completed string, as phpactor will be invoked with a longer
prefix, and have more chances to return the desired candidate.
---
 company-phpactor.el | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/company-phpactor.el b/company-phpactor.el
index 378d8a8..4b33a33 100644
--- a/company-phpactor.el
+++ b/company-phpactor.el
@@ -32,6 +32,11 @@
 (require 'company)
 (require 'phpactor)
 
+; this should have the same value as phpactor's completion.completor.class.limit value
+; by default, phpactor sets this to 100
+(defvar company-phpactor--completion-limit 100)
+(defvar company-phpactor--ignore-cache nil)
+
 (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.
@@ -54,6 +59,7 @@ Here we create a temporary syntax table in order to add $ to symbols."
 (defun company-phpactor--get-candidates ()
   "Build a list of candidates with text-properties extracted from phpactor's output."
   (let ((suggestions (company-phpactor--get-suggestions)) candidate)
+    (setq company-phpactor--ignore-cache (= (length suggestions) company-phpactor--completion-limit))
     (mapcar
      (lambda (suggestion)
        (setq candidate (plist-get suggestion :name))
@@ -80,6 +86,7 @@ Here we create a temporary syntax table in order to add $ to symbols."
   (save-restriction
     (widen)
     (cl-case command
+      (no-cache company-phpactor--ignore-cache)
       (post-completion (company-phpactor--post-completion arg))
       (annotation (company-phpactor--annotation arg))
       (interactive (company-begin-backend 'company-phpactor))