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

    Don't complete words within comments
---
 phpinspect-buffer.el     |  4 +++-
 phpinspect-completion.el | 41 ++++++++++++++++++++++++++---------------
 phpinspect-meta.el       |  7 +++++++
 test/test-completion.el  | 25 +++++++++++++++++++++++++
 4 files changed, 61 insertions(+), 16 deletions(-)

diff --git a/phpinspect-buffer.el b/phpinspect-buffer.el
index 2d4e54d209..4827be6c5f 100644
--- a/phpinspect-buffer.el
+++ b/phpinspect-buffer.el
@@ -584,7 +584,9 @@ continuing execution."
   (phpinspect-edtrack-register-edit
    (phpinspect-buffer-edit-tracker buffer) start end pre-change-length))
 
-(cl-defmethod phpinspect-buffer-tokens-enclosing-point ((buffer 
phpinspect-buffer) point)
+(defun phpinspect-buffer-tokens-enclosing-point (buffer point)
+  "Return token metadata objects for tokens enclosing POINT in BUFFER."
+  (cl-assert (phpinspect-buffer-p buffer))
   (phpinspect-bmap-tokens-overlapping (phpinspect-buffer-map buffer) point))
 
 (cl-defmethod phpinspect-buffer-token-meta ((buffer phpinspect-buffer) token)
diff --git a/phpinspect-completion.el b/phpinspect-completion.el
index b9fa8efd68..1ca321e025 100644
--- a/phpinspect-completion.el
+++ b/phpinspect-completion.el
@@ -198,22 +198,33 @@ belonging to a token that conforms with 
`phpinspect-attrib-p'"
 (cl-defmethod phpinspect-comp-strategy-supports
   ((_strat phpinspect-comp-word) (q phpinspect-completion-query)
    (rctx phpinspect--resolvecontext))
-  (or
-   ;; Complete word being typed
-   (when-let ((subject (phpinspect-completion-subject-at-point
-                      (phpinspect-completion-query-buffer q)
-                      (phpinspect-completion-query-point q)
-                      #'phpinspect-word-p)))
+  "Determine suitability of comp-word strategy to resolve Q.
+
+Words can be type names, function names and keywords.  This
+strategy is a bit special in the sense that it needs to carefully
+consider whether or not it is useful to complete words at point.
+Completing words in a comment for example, is usually not useful."
+  ;; Don't complete words when editing comments
+  (unless (seq-find (phpinspect-meta-token-predicate #'phpinspect-comment-p)
+                    (phpinspect-buffer-tokens-enclosing-point
+                     (phpinspect-completion-query-buffer q)
+                     (phpinspect-completion-query-point q)))
+    (or
+     ;; Complete word being typed
+     (when-let ((subject (phpinspect-completion-subject-at-point
+                          (phpinspect-completion-query-buffer q)
+                          (phpinspect-completion-query-point q)
+                          #'phpinspect-word-p)))
        (list (phpinspect-meta-start subject) (phpinspect-meta-end subject)))
 
-   ;; Point is right after a "new" token. Complete "newable" types.
-   (when-let ((token-before (phpinspect-bmap-last-token-before-point
-                             (phpinspect-buffer-map
-                              (phpinspect-completion-query-buffer q))
-                             (phpinspect-completion-query-point q)))
-              ((phpinspect-new-p (phpinspect-meta-token token-before))))
-     (list (phpinspect-completion-query-point q)
-           (phpinspect-completion-query-point q)))
+     ;; Point is right after a "new" token. Complete "newable" types.
+     (when-let ((token-before (phpinspect-bmap-last-token-before-point
+                               (phpinspect-buffer-map
+                                (phpinspect-completion-query-buffer q))
+                               (phpinspect-completion-query-point q)))
+                ((phpinspect-new-p (phpinspect-meta-token token-before))))
+       (list (phpinspect-completion-query-point q)
+             (phpinspect-completion-query-point q)))
 
    ;; Point is after a scope, static, declaration, const or other keyword that
    ;; can precede a type or another word.
@@ -224,7 +235,7 @@ belonging to a token that conforms with 
`phpinspect-attrib-p'"
           (car (phpinspect--resolvecontext-subject rctx))))
 
         (list (phpinspect-completion-query-point q)
-              (phpinspect-completion-query-point q)))))
+              (phpinspect-completion-query-point q))))))
 
 (cl-defmethod phpinspect-comp-strategy-execute
   ((_strat phpinspect-comp-word) (q phpinspect-completion-query)
diff --git a/phpinspect-meta.el b/phpinspect-meta.el
index 49bdce9ba1..f0d0765c94 100644
--- a/phpinspect-meta.el
+++ b/phpinspect-meta.el
@@ -282,6 +282,13 @@
 (cl-defmethod phpinspect-meta-first-child ((meta (head meta)))
   (phpinspect-meta-find-child-after meta (- (phpinspect-meta-start meta) 1)))
 
+(defun phpinspect-meta-token-predicate (token-predicate)
+  "Wrap TOKEN-PREDICATE in a closure that operates on metadata.
+
+The returned closure takes a metadata object as argument and then
+calls TOKEN-PREDICATE on its token
+slot (`phpinspect-meta-token')."
+  (lambda (meta) (funcall token-predicate (phpinspect-meta-token meta))))
 
 (defun phpinspect-meta-string (meta)
   (if meta
diff --git a/test/test-completion.el b/test/test-completion.el
index 2d04544978..1fb1d3472b 100644
--- a/test/test-completion.el
+++ b/test/test-completion.el
@@ -120,3 +120,28 @@ public function a() {} ")
            (strings (phpinspect--completion-list-strings completions)))
 
       (should-not strings))))
+
+(ert-deftest phpinspect-dont-complete-within-comments ()
+  (let ((cases (list "function a() { new // "
+                     "/* "
+                     "// ")))
+    (dolist (prefix cases)
+      (with-temp-buffer
+        (insert "<?php
+
+namespace App;
+
+use \\DateTime;
+
+class A { ")
+        (insert prefix)
+
+        (phpinspect-claim-buffer
+         (current-buffer) (phpinspect--make-dummy-composer-project-with-code))
+
+        (let* ((query (phpinspect--get-completion-query))
+                  (completions (phpinspect-completion-query-execute query))
+               (strings (phpinspect--completion-list-strings completions)))
+
+          (should-not (phpinspect--completion-list-has-candidates completions))
+          (should-not strings))))))

Reply via email to