branch: externals/phpinspect
commit 1ec0e0cfa2d07de4f4d6a6b5434e0ac206fb62c8
Author: Hugo Thunnissen <de...@hugot.nl>
Commit: Hugo Thunnissen <de...@hugot.nl>

    Limit token lookback range and start completion from the first non-blank 
character
    
    When editing files where tokens occur sparingly, like in HTML templates, 
looking
    back for the last token that occured is very expensive and never useful.
---
 phpinspect-bmap.el | 13 ++++++++++---
 phpinspect.el      | 12 ++++++++++--
 2 files changed, 20 insertions(+), 5 deletions(-)

diff --git a/phpinspect-bmap.el b/phpinspect-bmap.el
index de92c2567d..39aef72609 100644
--- a/phpinspect-bmap.el
+++ b/phpinspect-bmap.el
@@ -319,11 +319,18 @@
   (and (listp token)
        (keywordp (car token))))
 
-(cl-defmethod phpinspect-bmap-last-token-before-point ((bmap phpinspect-bmap) 
point)
+(cl-defmethod phpinspect-bmap-last-token-before-point ((bmap phpinspect-bmap) 
point &optional limit)
+  "Search backward in BMAP for last token ending before POINT.
+
+LIMIT is the maximum number of positions to check backward before
+giving up. If not provided, this is 10."
+  (unless limit (setq limit 10))
   (let* ((ends (phpinspect-bmap-ends bmap))
-         (ending))
+         (ending)
+         (point-limit (- point limit)))
     (unless (hash-table-empty-p ends)
-      (while (not (or (<= point 0) (setq ending 
(phpinspect-bmap-tokens-ending-at bmap point))))
+      (while (not (or (<= point 0) (<= point point-limit)
+                      (setq ending (phpinspect-bmap-tokens-ending-at bmap 
point))))
         (setq point (- point 1)))
       (car (last ending)))))
 
diff --git a/phpinspect.el b/phpinspect.el
index 481796dbc5..55f73b652d 100644
--- a/phpinspect.el
+++ b/phpinspect.el
@@ -273,6 +273,14 @@ ARG-LIST. ARG-LIST should be a list token as returned by
                        (phpinspect--make-type :name (car (last arg))))
             nil)))))
 
+(defun phpinspect--determine-completion-point ()
+  "Find first point backwards that could contain any kind of
+context for completion."
+  (save-excursion
+    (re-search-backward "[^[:blank:]\n]")
+    (forward-char)
+    (point)))
+
 (defun phpinspect-eldoc-function ()
   "An `eldoc-documentation-function` implementation for PHP files.
 
@@ -284,7 +292,7 @@ TODO:
 "
   (catch 'phpinspect-parse-interrupted
     (let* ((token-map (phpinspect-buffer-parse-map phpinspect-current-buffer))
-           (resolvecontext (phpinspect-get-resolvecontext token-map (point)))
+           (resolvecontext (phpinspect-get-resolvecontext token-map 
(phpinspect--determine-completion-point)))
            (parent-token (car (phpinspect--resolvecontext-enclosing-tokens
                                resolvecontext)))
            (enclosing-token (cadr (phpinspect--resolvecontext-enclosing-tokens
@@ -1052,7 +1060,7 @@ static variables and static methods."
 (defun phpinspect--suggest-at-point ()
   (phpinspect--log "Entering suggest at point. Point: %d" (point))
   (let* ((bmap (phpinspect-buffer-parse-map phpinspect-current-buffer))
-         (resolvecontext (phpinspect-get-resolvecontext bmap (point)))
+         (resolvecontext (phpinspect-get-resolvecontext bmap 
(phpinspect--determine-completion-point)))
          (last-tokens (last (phpinspect--resolvecontext-subject 
resolvecontext) 2)))
     (phpinspect--log "Subject: %s" (phpinspect--resolvecontext-subject
                                     resolvecontext))

Reply via email to