Skip to content

Commit

Permalink
Implement ede-php-autoload-complete for full completions
Browse files Browse the repository at this point in the history
  • Loading branch information
xendk committed Apr 17, 2017
1 parent 40f508b commit 67b4dc8
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 5 deletions.
9 changes: 9 additions & 0 deletions ede-php-autoload.el
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@ Generate this class name using the class loader information.
FILE-NAME must be absolute or relative to the project root."
(ede-php-autoload-get-class-name-for-file (oref this class-loader) file-name))

(defmethod ede-php-autoload-complete ((this ede-php-autoload-project) prefix)
"Get completion suggestions for the type PREFIX.
PREFIX is the beginning of a fully-qualified name.
The result is a list of completion suggestions for this
prefix."
(ede-php-autoload-complete (oref this class-loader) prefix))

(defmethod ede-php-autoload-complete-type-name ((this ede-php-autoload-project) prefix)
"Get completion suggestions for the type PREFIX.
Expand Down
27 changes: 24 additions & 3 deletions ede-php-autoload/class-loader/core.el
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,34 @@ Generate this class name using the class loader information.
FILE-NAME must be absolute or relative to the project root."
(error "Method `ede-php-autoload-find-class-def-file' must be overriden"))

(defmethod ede-php-autoload-complete-type-name ((this ede-php-autoload-class-loader) prefix)
"Get completion suggestions for the type PREFIX.
(defmethod ede-php-autoload-complete ((this ede-php-autoload-class-loader) prefix)
"Get completion suggestions for the PREFIX.
PREFIX is the beginning of a fully-qualified name.
The result is a list of completion suggestions for this
prefix. Completions are not guaranteed to give full class names,
prefix."
(let* ((split-prefix (split-string prefix "\\\\"))
(ns (mapconcat 'identity (butlast split-prefix) "\\"))
(completions (ede-php-autoload-complete-type-name this prefix)))
;; Try to detect if we got toplevel namespace returned (which can
;; contain multiple components), in which case we should not
;; prepend the base namespace. This is error prone, and just a
;; stop gap until ede-php-autoload-complete-type-name handles
;; namespace sub-completion.
(if (cl-loop for completion in completions
always (string-prefix-p prefix completion t))
completions
(cl-loop for completion in completions
collect (concat ns "\\" completion)))))

(defmethod ede-php-autoload-complete-type-name ((this ede-php-autoload-class-loader) prefix)
"Get type completion suggestions for the type PREFIX.
PREFIX is the beginning of a fully-qualified name.
The result is a list of type completion suggestions for this
prefix. Type completions are not guaranteed to give full class names,
this can only suggest the next namespace."
'())

Expand Down
48 changes: 46 additions & 2 deletions features/ede-php-autoload-completion.feature
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ Feature: Class name completion
| Psr0Split\Ns1\ |
| Psr0Split\Ns2\ |
| Psr0Fallback\ |
Then completions for query "Psr0" should be:
| name |
| Psr0Ns\ |
| Psr0Split\Ns1\ |
| Psr0Split\Ns2\ |
| Psr0Fallback\ |

Scenario: Complete PSR-0 namespace with slashes
Given I visit "src/main.php" in project "without-composer"
Expand All @@ -19,6 +25,14 @@ Feature: Class name completion
| name |
| TheClass1 |
| TheClass2 |
Then completions for query "Psr0Ns\T" should be:
| name |
| Psr0Ns\TheClass |
| Psr0Ns\TheSubdir\ |
And completions for query "Psr0Ns\TheSubdir\" should be:
| name |
| Psr0Ns\TheSubdir\TheClass1 |
| Psr0Ns\TheSubdir\TheClass2 |
Scenario: Complete PSR-0 namespace with underscores
Given I visit "src/main.php" in project "without-composer"
Expand All @@ -30,6 +44,14 @@ Feature: Class name completion
| name |
| Psr0Ns_TheSubdir_TheClass1 |
| Psr0Ns_TheSubdir_TheClass2 |
Then completions for query "Psr0Ns_T" should be:
| name |
| Psr0Ns_TheClass |
| Psr0Ns_TheSubdir\ |
And completions for query "Psr0Ns_TheSubdir_" should be:
| name |
| Psr0Ns_TheSubdir_TheClass1 |
| Psr0Ns_TheSubdir_TheClass2 |
Scenario: Complete PSR-4 namespaces
Given I visit "src/main.php" in project "without-composer"
Expand All @@ -47,20 +69,42 @@ Feature: Class name completion
| name |
| TheClass1 |
| TheClass2 |
Then completions for query "Psr4" should be:
| name |
| Psr4Ns\ |
| Psr4Split\Ns1\ |
| Psr4Split\Ns2\ |
| Psr4Fallback\ |
And completions for query "Psr4Ns\T" should be:
| name |
| Psr4Ns\TheClass |
| Psr4Ns\TheSubdir\ |
And completions for query "Psr4Ns\TheSubdir\" should be:
| name |
| Psr4Ns\TheSubdir\TheClass1 |
| Psr4Ns\TheSubdir\TheClass2 |
Scenario: Complete PSR-4 multi-dir namespaces
Given I visit "src/main.php" in project "without-composer"
Then type completions for query "MultiDirNs\T" should be:
| name |
| name |
| TheClass1 |
| TheClass2 |
Then completions for query "MultiDirNs\T" should be:
| name |
| MultiDirNs\TheClass1 |
| MultiDirNs\TheClass2 |
Scenario: Complete classmap namespaces
Given I visit "src/main.php" in project "without-composer"
Then type completions for query "ClassMapNs\" should be:
| name |
| ClassMapNs\MyClass |
Then completions for query "ClassMapNs\" should be:
| name |
| ClassMapNs\MyClass |
Scenario: Complete non-existing dir
Given I visit "src/main.php" in project "without-composer"
Then type completions for query "NonExisting\" should be nil
Then completions for query "NonExisting\" should be nil
11 changes: 11 additions & 0 deletions features/step-definitions/ede-php-autoload-steps.el
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,14 @@
(Then "^type completions for query \"\\(.+\\)\" should be nil"
(lambda (query)
(should (null (ede-php-autoload-complete-type-name (ede-current-project) query)))))

(Then "^completions for query \"\\(.+\\)\" should be:"
(lambda (query suggestion-table)
(let ((suggestions (cl-loop for suggestion in (cdr suggestion-table)
collect (car suggestion))))
(should (equal (ede-php-autoload-complete (ede-current-project) query)
suggestions)))))

(Then "^completions for query \"\\(.+\\)\" should be nil"
(lambda (query)
(should (null (ede-php-autoload-complete (ede-current-project) query)))))

0 comments on commit 67b4dc8

Please sign in to comment.