[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/phpinspect b25fdfa862 2/3: Make completion of words mor
From: |
ELPA Syncer |
Subject: |
[elpa] externals/phpinspect b25fdfa862 2/3: Make completion of words more context aware |
Date: |
Mon, 2 Sep 2024 18:58:33 -0400 (EDT) |
branch: externals/phpinspect
commit b25fdfa862235ea37d8418a66bb52f10e6c1cc85
Author: Hugo Thunnissen <devel@hugot.nl>
Commit: Hugo Thunnissen <devel@hugot.nl>
Make completion of words more context aware
- Implement completion of scope/static/const keywords in class bodies
- Implement completion of imported type names:
- After "new" keyword
- In function/class declarations
- In property declarations
---
phpinspect-completion.el | 28 ++++++++++++++++++++--
phpinspect-suggest.el | 61 ++++++++++++++++++++++++++++++++++++++++++++++++
phpinspect-type.el | 7 +++++-
test/phpinspect-test.el | 1 +
4 files changed, 94 insertions(+), 3 deletions(-)
diff --git a/phpinspect-completion.el b/phpinspect-completion.el
index ea951d8df1..7db48ae387 100644
--- a/phpinspect-completion.el
+++ b/phpinspect-completion.el
@@ -201,9 +201,19 @@ belonging to a token that conforms with
`phpinspect-attrib-p'"
(list (phpinspect-meta-start subject) (phpinspect-meta-end subject))))
(cl-defmethod phpinspect-comp-strategy-execute
- ((_strat phpinspect-comp-word) (_q phpinspect-completion-query)
+ ((_strat phpinspect-comp-word) (q phpinspect-completion-query)
(rctx phpinspect--resolvecontext))
- (phpinspect-suggest-functions rctx))
+ ;; The "new" case can't be handled in the less sophisticated "suggest" lib.
It
+ ;; is not determinable from the resolvecontext alone.
+ (if (phpinspect-new-p
+ (phpinspect-meta-token
+ (phpinspect-meta-find-left-sibling
+ (phpinspect-completion-subject-at-point
+ (phpinspect-completion-query-buffer q)
+ (phpinspect-completion-query-point q)
+ #'phpinspect-word-p))))
+ (phpinspect-suggest-types-at-point rctx)
+ (phpinspect-suggest-words-at-point rctx)))
(defvar phpinspect-completion-strategies (list (phpinspect-make-comp-attribute)
(phpinspect-make-comp-sigil)
@@ -381,6 +391,13 @@ Returns list of `phpinspect--completion'."
(cl-defmethod phpinspect--make-completion ((completion-candidate
phpinspect-property))
(phpinspect--make-completion (phpi-prop-definition completion-candidate)))
+(cl-defmethod phpinspect--make-completion ((type phpinspect--type))
+ (phpinspect--construct-completion
+ :value (propertize (phpinspect--type-bare-name type) 'face
'font-lock-type-face)
+ :meta (phpinspect--format-type-name type)
+ :target type
+ :kind 'class))
+
(cl-defmethod phpinspect--make-completion
((completion-candidate phpinspect--variable))
(phpinspect--construct-completion
@@ -395,6 +412,13 @@ Returns list of `phpinspect--completion'."
phpinspect--null-type)))
:kind 'variable))
+(cl-defmethod phpinspect--make-completion ((keyword
phpinspect-suggest-keyword))
+ (phpinspect--construct-completion
+ :value (propertize (phpinspect-suggest-keyword-word keyword) 'face
'font-lock-keyword-face)
+ :target keyword
+ :kind 'keyword
+ :meta "keyword"))
+
(define-inline phpinspect--prefix-for-completion (completion)
(inline-letevals (completion)
(inline-quote
diff --git a/phpinspect-suggest.el b/phpinspect-suggest.el
index 24e821a8a3..4bbe806a31 100644
--- a/phpinspect-suggest.el
+++ b/phpinspect-suggest.el
@@ -75,6 +75,67 @@
variable-list)))
+(cl-defstruct (phpinspect-suggest-keyword
+ (:constructor phpinspect-make-suggest-keyword-generated))
+ (word nil
+ :type string))
+
+(defun phpinspect-make-suggest-keyword (word)
+ (phpinspect-make-suggest-keyword-generated :word word))
+
+(defconst phpinspect-class-body-keywords (list "const" "private" "public"
"protected" "static" "function")
+ "Keywords that can be used within a class")
+
+(defconst phpinspect-scope-body-keywords (list "function" "static")
+ "Keywords that can be used within a statement after a scope keyword.")
+
+(defconst phpinspect-static-body-keywords (list "function")
+ "Keywords that can be used within a statement after a static keyword.")
+
+(defun phpinspect-suggest-class-body-keywords ()
+ (mapcar #'phpinspect-make-suggest-keyword phpinspect-class-body-keywords))
+
+(defun phpinspect-suggest-scope-body-keywords ()
+ (mapcar #'phpinspect-make-suggest-keyword phpinspect-scope-body-keywords))
+
+(defun phpinspect-suggest-static-body-keywords ()
+ (mapcar #'phpinspect-make-suggest-keyword phpinspect-static-body-keywords))
+
+(defun phpinspect-suggest-types-at-point (rctx)
+ (let* ((resolver (phpinspect--make-type-resolver-for-resolvecontext rctx))
+ (types (phpinspect-type-resolver--types resolver)))
+
+ (append (mapcar #'cdr types) phpinspect-native-types)))
+
+(defun phpinspect-suggest-words-at-point (rctx)
+ (let ((parent (car (phpinspect--resolvecontext-enclosing-tokens rctx))))
+ (or (cond ((phpinspect-class-p parent)
+ (phpinspect-suggest-class-body-keywords))
+ ((phpinspect-scope-p parent)
+ (append
+ (phpinspect-suggest-scope-body-keywords)
+ (phpinspect-suggest-types-at-point rctx)))
+ ((phpinspect-static-p parent)
+ (phpinspect-suggest-static-body-keywords))
+
+ ((or
+ ;; Inside argument list in function declaration
+ (and (phpinspect-list-p parent)
+ (phpinspect-declaration-p
+ (cadr (phpinspect--resolvecontext-enclosing-tokens
rctx))))
+ ;; Class/function declaration
+ (phpinspect-declaration-p parent))
+ (phpinspect-suggest-types-at-point rctx)))
+
+ ;; Don't complete functions inside function/class declaration
+ (unless (or (phpinspect-declaration-p parent)
+ ;; No use completing function names inside comments
+ (seq-find #'phpinspect-comment-p
+ (phpinspect--resolvecontext-enclosing-tokens
rctx)))
+ (append
+ (phpinspect-suggest-types-at-point rctx)
+ (phpinspect-suggest-functions rctx))))))
+
(defun phpinspect-get-cached-project-typedef-methods (rctx class-fqn &optional
static)
(phpinspect--log "Getting cached project class methods for %s" class-fqn)
(let ((class (phpinspect-rctx-get-typedef rctx class-fqn 'no-enqueue)))
diff --git a/phpinspect-type.el b/phpinspect-type.el
index b5f69db153..458ba2b190 100644
--- a/phpinspect-type.el
+++ b/phpinspect-type.el
@@ -214,6 +214,11 @@ NAMESPACE may be nil, or a string with a namespace FQN."
(last-token
(phpinspect--find-class-token last-token))))))
+(oclosure-define phpinspect-type-resolver
+ "Function that resolves `phpinspect--type' instances to be fully qualified."
+ ;; The types known within the context this resolver was created from.
+ (types :mutable nil :type list))
+
(defun phpinspect--make-type-resolver (types &optional token-tree namespace)
"Little wrapper closure to pass around and resolve types with."
(let* ((inside-class
@@ -221,7 +226,7 @@ NAMESPACE may be nil, or a string with a namespace FQN."
(phpinspect--find-class-token token-tree))))
(inside-class-name
(and inside-class (phpinspect--get-class-name-from-token
inside-class))))
- (lambda (type)
+ (oclosure-lambda (phpinspect-type-resolver (types types)) (type)
(phpinspect--type-resolve
types
namespace
diff --git a/test/phpinspect-test.el b/test/phpinspect-test.el
index 7ea20a616c..48b035c511 100644
--- a/test/phpinspect-test.el
+++ b/test/phpinspect-test.el
@@ -337,6 +337,7 @@ class FlufferUpper
(load-file (concat phpinspect-test-directory "/test-meta.el"))
(load-file (concat phpinspect-test-directory "/test-resolve.el"))
(load-file (concat phpinspect-test-directory "/test-imports.el"))
+(load-file (concat phpinspect-test-directory "/test-suggest.el"))
(provide 'phpinspect-test)