From f153e5cd54781b4c2d8599d71ade2519b0b0f698 Mon Sep 17 00:00:00 2001 From: Thomas Fini Hansen Date: Mon, 10 Apr 2017 19:45:45 +0200 Subject: [PATCH 1/8] Add (failing) test for classes in subdirs --- features/ede-php-autoload-completion.feature | 12 +++++++++--- .../src/Psr0Ns/TheSubdir/TheClass1.php | 6 ++++++ .../src/Psr0Ns/TheSubdir/TheClass2.php | 6 ++++++ .../src/Psr4Ns/TheSubdir/TheClass1.php | 6 ++++++ .../src/Psr4Ns/TheSubdir/TheClass2.php | 6 ++++++ 5 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 test/projects/without-composer/src/Psr0Ns/TheSubdir/TheClass1.php create mode 100644 test/projects/without-composer/src/Psr0Ns/TheSubdir/TheClass2.php create mode 100644 test/projects/without-composer/src/Psr4Ns/TheSubdir/TheClass1.php create mode 100644 test/projects/without-composer/src/Psr4Ns/TheSubdir/TheClass2.php diff --git a/features/ede-php-autoload-completion.feature b/features/ede-php-autoload-completion.feature index 974ec11..bff3e98 100644 --- a/features/ede-php-autoload-completion.feature +++ b/features/ede-php-autoload-completion.feature @@ -8,19 +8,25 @@ Feature: Class name completion Scenario: Complete PSR-0 namespace with slashes Given I visit "src/main.php" in project "without-composer" Then completions for query "Psr0Ns\T" should be: - | TheClass | + | TheClass | TheSubdir | + Then completions for query "Psr0Ns\TheSubdir\" should be: + | TheClass1 | TheClass2 | Scenario: Complete PSR-0 namespace with underscores Given I visit "src/main.php" in project "without-composer" Then completions for query "Psr0Ns_T" should be: - | Psr0Ns_TheClass | + | Psr0Ns_TheClass | Psr0Ns_TheSubdir | + Then completions for query "Psr0Ns_TheSubdir_" should be: + | Psr0Ns_TheSubdir_TheClass1 | Psr0Ns_TheSubdir_TheClass2 | Scenario: Complete PSR-4 namespaces Given I visit "src/main.php" in project "without-composer" Then completions for query "Psr4" should be: | Psr4Ns | Psr4Split\Ns1 | Psr4Split\Ns2 | And completions for query "Psr4Ns\T" should be: - | TheClass | + | TheClass | TheSubdir | + Then completions for query "Psr4Ns\TheSubdir\" should be: + | TheClass1 | TheClass2 | Scenario: Complete PSR-4 multi-dir namespaces Given I visit "src/main.php" in project "without-composer" diff --git a/test/projects/without-composer/src/Psr0Ns/TheSubdir/TheClass1.php b/test/projects/without-composer/src/Psr0Ns/TheSubdir/TheClass1.php new file mode 100644 index 0000000..40e5bc8 --- /dev/null +++ b/test/projects/without-composer/src/Psr0Ns/TheSubdir/TheClass1.php @@ -0,0 +1,6 @@ + Date: Mon, 10 Apr 2017 20:00:42 +0200 Subject: [PATCH 2/8] Filter out dot files per default --- ede-php-autoload/class-loader/core.el | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/ede-php-autoload/class-loader/core.el b/ede-php-autoload/class-loader/core.el index b795d4e..3dd28f9 100644 --- a/ede-php-autoload/class-loader/core.el +++ b/ede-php-autoload/class-loader/core.el @@ -98,7 +98,9 @@ Basically, it returns PROJECT-ROOT/{NS-DIRECTORIES}/RELATIVE-PATH/{PREFIX}*" files (append files (directory-files full-dir nil - (concat "^" (regexp-quote prefix)))))) + (concat "^" (if (string= prefix "") + "[^.]" + (regexp-quote prefix))))))) files)) (defun ede-php-autoload--ensure-list (list-or-element) From a6d4fd329dff45446a2ec185a3d199fb2776e7f3 Mon Sep 17 00:00:00 2001 From: Thomas Fini Hansen Date: Mon, 10 Apr 2017 21:12:19 +0200 Subject: [PATCH 3/8] Convert feature tables --- features/ede-php-autoload-completion.feature | 45 ++++++++++++++----- .../ede-php-autoload-steps.el | 6 ++- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/features/ede-php-autoload-completion.feature b/features/ede-php-autoload-completion.feature index bff3e98..33f9569 100644 --- a/features/ede-php-autoload-completion.feature +++ b/features/ede-php-autoload-completion.feature @@ -3,39 +3,60 @@ Feature: Class name completion Scenario: Complete PSR-0 namespaces Given I visit "src/main.php" in project "without-composer" Then completions for query "Psr0" should be: - | Psr0Ns | Psr0Split\Ns1 | Psr0Split\Ns2 | + | name | + | Psr0Ns | + | Psr0Split\Ns1 | + | Psr0Split\Ns2 | Scenario: Complete PSR-0 namespace with slashes Given I visit "src/main.php" in project "without-composer" Then completions for query "Psr0Ns\T" should be: - | TheClass | TheSubdir | - Then completions for query "Psr0Ns\TheSubdir\" should be: - | TheClass1 | TheClass2 | + | name | + | TheClass | + | TheSubdir | + And completions for query "Psr0Ns\TheSubdir\" should be: + | name | + | TheClass1 | + | TheClass2 | Scenario: Complete PSR-0 namespace with underscores Given I visit "src/main.php" in project "without-composer" Then completions for query "Psr0Ns_T" should be: - | Psr0Ns_TheClass | Psr0Ns_TheSubdir | - Then completions for query "Psr0Ns_TheSubdir_" should be: - | Psr0Ns_TheSubdir_TheClass1 | Psr0Ns_TheSubdir_TheClass2 | + | 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" Then completions for query "Psr4" should be: - | Psr4Ns | Psr4Split\Ns1 | Psr4Split\Ns2 | + | name | + | Psr4Ns | + | Psr4Split\Ns1 | + | Psr4Split\Ns2 | And completions for query "Psr4Ns\T" should be: - | TheClass | TheSubdir | - Then completions for query "Psr4Ns\TheSubdir\" should be: - | TheClass1 | TheClass2 | + | name | + | TheClass | + | TheSubdir | + And completions for query "Psr4Ns\TheSubdir\" should be: + | name | + | TheClass1 | + | TheClass2 | Scenario: Complete PSR-4 multi-dir namespaces Given I visit "src/main.php" in project "without-composer" Then completions for query "MultiDirNs\T" should be: - | TheClass1 | TheClass2 | + | name | + | TheClass1 | + | TheClass2 | Scenario: Complete classmap namespaces Given I visit "src/main.php" in project "without-composer" Then completions for query "ClassMapNs\" should be: + | name | | ClassMapNs\MyClass | Scenario: Complete non-existing dir diff --git a/features/step-definitions/ede-php-autoload-steps.el b/features/step-definitions/ede-php-autoload-steps.el index b5374be..542e2d8 100644 --- a/features/step-definitions/ede-php-autoload-steps.el +++ b/features/step-definitions/ede-php-autoload-steps.el @@ -45,8 +45,10 @@ (Then "^completions for query \"\\(.+\\)\" should be:" (lambda (query suggestion-table) - (should (equal (ede-php-autoload-complete-type-name (ede-current-project) query) - (car suggestion-table))))) + (let ((suggestions (cl-loop for suggestion in (cdr suggestion-table) + collect (car suggestion)))) + (should (equal (ede-php-autoload-complete-type-name (ede-current-project) query) + suggestions))))) (Then "^completions for query \"\\(.+\\)\" should be nil" (lambda (query) From a2928d874b4f08bf7f6cec7a7ddb79fe5beb2b26 Mon Sep 17 00:00:00 2001 From: Thomas Fini Hansen Date: Mon, 10 Apr 2017 20:33:31 +0200 Subject: [PATCH 4/8] Ignore non-existent directories --- ede-php-autoload/class-loader/core.el | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ede-php-autoload/class-loader/core.el b/ede-php-autoload/class-loader/core.el index 3dd28f9..0f97dab 100644 --- a/ede-php-autoload/class-loader/core.el +++ b/ede-php-autoload/class-loader/core.el @@ -95,12 +95,13 @@ Basically, it returns PROJECT-ROOT/{NS-DIRECTORIES}/RELATIVE-PATH/{PREFIX}*" dir (expand-file-name dir project-root)) full-dir (expand-file-name relative-path absolute-dir) - files (append files (directory-files - full-dir - nil - (concat "^" (if (string= prefix "") - "[^.]" - (regexp-quote prefix))))))) + files (when (file-exists-p full-dir) + (append files (directory-files + full-dir + nil + (concat "^" (if (string= prefix "") + "[^.]" + (regexp-quote prefix)))))))) files)) (defun ede-php-autoload--ensure-list (list-or-element) From e98eabedd5f6b753c47d7a2571953cef3c47b791 Mon Sep 17 00:00:00 2001 From: Thomas Fini Hansen Date: Wed, 12 Apr 2017 17:48:00 +0200 Subject: [PATCH 5/8] Add test for fallback namespace --- features/ede-php-autoload-completion.feature | 2 ++ features/support/env.el | 6 ++++-- .../src/Fallback/Psr0/Psr0Fallback/MyClass.php | 4 ++++ .../src/Fallback/Psr4/Psr4Fallback/MyClass.php | 4 ++++ 4 files changed, 14 insertions(+), 2 deletions(-) create mode 100644 test/projects/without-composer/src/Fallback/Psr0/Psr0Fallback/MyClass.php create mode 100644 test/projects/without-composer/src/Fallback/Psr4/Psr4Fallback/MyClass.php diff --git a/features/ede-php-autoload-completion.feature b/features/ede-php-autoload-completion.feature index 33f9569..2ece21c 100644 --- a/features/ede-php-autoload-completion.feature +++ b/features/ede-php-autoload-completion.feature @@ -7,6 +7,7 @@ Feature: Class name completion | Psr0Ns | | Psr0Split\Ns1 | | Psr0Split\Ns2 | + | Psr0Fallback | Scenario: Complete PSR-0 namespace with slashes Given I visit "src/main.php" in project "without-composer" @@ -37,6 +38,7 @@ Feature: Class name completion | Psr4Ns | | Psr4Split\Ns1 | | Psr4Split\Ns2 | + | Psr4Fallback | And completions for query "Psr4Ns\T" should be: | name | | TheClass | diff --git a/features/support/env.el b/features/support/env.el index 5dcacb0..8c6a423 100644 --- a/features/support/env.el +++ b/features/support/env.el @@ -61,12 +61,14 @@ COMPOSER-FILE-NAME is either default or new." "without-composer/project") :class-autoloads '(:psr-0 (("Psr0Ns" . "src/Psr0Ns") ("Psr0Split\\Ns1" . "src/Psr0Split/Ns1") - ("Psr0Split\\Ns2" . "src/Psr0Split/Ns2")) + ("Psr0Split\\Ns2" . "src/Psr0Split/Ns2") + ("" . "src/Fallback/Psr0")) :psr-4 (("Psr4Ns" . "src/Psr4Ns") ("MultiDirNs" . ("src/MultiDirNs1" "src/MultiDirNs2")) ("Psr4Split\\Ns1" . "src/Psr4Split/Ns1") ("Psr4Split\\Ns2" . "src/Psr4Split/Ns2") - ("NonExisting" . "src/NonExisting")) + ("NonExisting" . "src/NonExisting") + ("" . "src/Fallback/Psr4")) :class-map ((ClassMapNs\\MyClass . "src/ClassMapNs/MyClass.php"))) :include-path '(".") :system-include-path '("/usr/share/php"))) diff --git a/test/projects/without-composer/src/Fallback/Psr0/Psr0Fallback/MyClass.php b/test/projects/without-composer/src/Fallback/Psr0/Psr0Fallback/MyClass.php new file mode 100644 index 0000000..c7c9e64 --- /dev/null +++ b/test/projects/without-composer/src/Fallback/Psr0/Psr0Fallback/MyClass.php @@ -0,0 +1,4 @@ + Date: Mon, 17 Apr 2017 21:19:51 +0200 Subject: [PATCH 6/8] Rename type test step --- features/ede-php-autoload-completion.feature | 22 +++++++++---------- .../ede-php-autoload-steps.el | 4 ++-- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/features/ede-php-autoload-completion.feature b/features/ede-php-autoload-completion.feature index 2ece21c..a783bd2 100644 --- a/features/ede-php-autoload-completion.feature +++ b/features/ede-php-autoload-completion.feature @@ -2,7 +2,7 @@ Feature: Class name completion Scenario: Complete PSR-0 namespaces Given I visit "src/main.php" in project "without-composer" - Then completions for query "Psr0" should be: + Then type completions for query "Psr0" should be: | name | | Psr0Ns | | Psr0Split\Ns1 | @@ -11,56 +11,56 @@ Feature: Class name completion Scenario: Complete PSR-0 namespace with slashes Given I visit "src/main.php" in project "without-composer" - Then completions for query "Psr0Ns\T" should be: + Then type completions for query "Psr0Ns\T" should be: | name | | TheClass | | TheSubdir | - And completions for query "Psr0Ns\TheSubdir\" should be: + And type completions for query "Psr0Ns\TheSubdir\" should be: | name | | TheClass1 | | TheClass2 | Scenario: Complete PSR-0 namespace with underscores Given I visit "src/main.php" in project "without-composer" - Then completions for query "Psr0Ns_T" should be: + Then type completions for query "Psr0Ns_T" should be: | name | | Psr0Ns_TheClass | | Psr0Ns_TheSubdir | - And completions for query "Psr0Ns_TheSubdir_" should be: + And type 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" - Then completions for query "Psr4" should be: + Then type completions for query "Psr4" should be: | name | | Psr4Ns | | Psr4Split\Ns1 | | Psr4Split\Ns2 | | Psr4Fallback | - And completions for query "Psr4Ns\T" should be: + And type completions for query "Psr4Ns\T" should be: | name | | TheClass | | TheSubdir | - And completions for query "Psr4Ns\TheSubdir\" should be: + And type completions for query "Psr4Ns\TheSubdir\" should be: | name | | TheClass1 | | TheClass2 | Scenario: Complete PSR-4 multi-dir namespaces Given I visit "src/main.php" in project "without-composer" - Then completions for query "MultiDirNs\T" should be: + Then type completions for query "MultiDirNs\T" should be: | name | | TheClass1 | | TheClass2 | Scenario: Complete classmap namespaces Given I visit "src/main.php" in project "without-composer" - Then completions for query "ClassMapNs\" should be: + Then type 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 completions for query "NonExisting\" should be nil + Then type completions for query "NonExisting\" should be nil diff --git a/features/step-definitions/ede-php-autoload-steps.el b/features/step-definitions/ede-php-autoload-steps.el index 542e2d8..af1c0d5 100644 --- a/features/step-definitions/ede-php-autoload-steps.el +++ b/features/step-definitions/ede-php-autoload-steps.el @@ -43,13 +43,13 @@ file-name) class-name)))) -(Then "^completions for query \"\\(.+\\)\" should be:" +(Then "^type 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-type-name (ede-current-project) query) suggestions))))) -(Then "^completions for query \"\\(.+\\)\" should be nil" +(Then "^type completions for query \"\\(.+\\)\" should be nil" (lambda (query) (should (null (ede-php-autoload-complete-type-name (ede-current-project) query))))) From 40f508b08ea9ce4ae2ae7d26ce28d6dd5d9a9f87 Mon Sep 17 00:00:00 2001 From: Thomas Fini Hansen Date: Mon, 17 Apr 2017 21:20:19 +0200 Subject: [PATCH 7/8] Add backslash to namespace and directory completions --- ede-php-autoload/class-loader/core.el | 18 ++++++---- ede-php-autoload/class-loader/psr0.el | 5 ++- ede-php-autoload/class-loader/psr4.el | 4 +-- features/ede-php-autoload-completion.feature | 38 ++++++++++---------- 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/ede-php-autoload/class-loader/core.el b/ede-php-autoload/class-loader/core.el index 0f97dab..369402a 100644 --- a/ede-php-autoload/class-loader/core.el +++ b/ede-php-autoload/class-loader/core.el @@ -96,12 +96,18 @@ Basically, it returns PROJECT-ROOT/{NS-DIRECTORIES}/RELATIVE-PATH/{PREFIX}*" (expand-file-name dir project-root)) full-dir (expand-file-name relative-path absolute-dir) files (when (file-exists-p full-dir) - (append files (directory-files - full-dir - nil - (concat "^" (if (string= prefix "") - "[^.]" - (regexp-quote prefix)))))))) + (append files (mapcar + #'(lambda (file-name) + (if (file-directory-p + (expand-file-name file-name full-dir)) + (concat file-name "\\") + file-name)) + (directory-files + full-dir + nil + (concat "^" (if (string= prefix "") + "[^.]" + (regexp-quote prefix))))))))) files)) (defun ede-php-autoload--ensure-list (list-or-element) diff --git a/ede-php-autoload/class-loader/psr0.el b/ede-php-autoload/class-loader/psr0.el index f0fda45..290aa26 100644 --- a/ede-php-autoload/class-loader/psr0.el +++ b/ede-php-autoload/class-loader/psr0.el @@ -83,7 +83,6 @@ It basically tries to infer whether \"_\" or \"\\\" should be used. PREFIX is the type prefix to complete." (let ((suggestion (file-name-base file-name))) (cond - ((string-match-p suggestion ".") nil) ((string-match-p "\\\\" prefix) suggestion) (t (mapconcat 'identity @@ -110,8 +109,8 @@ PREFIX is the beginning of the type to complete." (cond ((string-prefix-p prefix namespace t) ;; If `prefix' is the beginning of `namespace', let's use - ;; `namespace' as suggestion - (push namespace suggestions)) + ;; `namespace' as suggestion. + (push (concat namespace "\\") suggestions)) ((string-prefix-p namespace prefix) ;; If `prefix' starts with `namespace', let's use directory and ;; file structure to create suggestions diff --git a/ede-php-autoload/class-loader/psr4.el b/ede-php-autoload/class-loader/psr4.el index 40072c7..9d833af 100644 --- a/ede-php-autoload/class-loader/psr4.el +++ b/ede-php-autoload/class-loader/psr4.el @@ -133,8 +133,8 @@ PREFIX is the beginning of the type to complete." (cond ((string-prefix-p prefix namespace t) ;; If `prefix' is the beginning of `namespace', let's use - ;; `namespace' as suggestion - (push namespace suggestions)) + ;; `namespace' as suggestion. + (push (concat namespace "\\") suggestions)) ((string-prefix-p namespace prefix) ;; If `prefix' starts with `namespace', let's use directory and ;; file structure to create suggestions diff --git a/features/ede-php-autoload-completion.feature b/features/ede-php-autoload-completion.feature index a783bd2..e1cd5c1 100644 --- a/features/ede-php-autoload-completion.feature +++ b/features/ede-php-autoload-completion.feature @@ -3,18 +3,18 @@ Feature: Class name completion Scenario: Complete PSR-0 namespaces Given I visit "src/main.php" in project "without-composer" Then type completions for query "Psr0" should be: - | name | - | Psr0Ns | - | Psr0Split\Ns1 | - | Psr0Split\Ns2 | - | Psr0Fallback | + | name | + | Psr0Ns\ | + | Psr0Split\Ns1\ | + | Psr0Split\Ns2\ | + | Psr0Fallback\ | Scenario: Complete PSR-0 namespace with slashes Given I visit "src/main.php" in project "without-composer" Then type completions for query "Psr0Ns\T" should be: - | name | - | TheClass | - | TheSubdir | + | name | + | TheClass | + | TheSubdir\ | And type completions for query "Psr0Ns\TheSubdir\" should be: | name | | TheClass1 | @@ -23,9 +23,9 @@ Feature: Class name completion Scenario: Complete PSR-0 namespace with underscores Given I visit "src/main.php" in project "without-composer" Then type completions for query "Psr0Ns_T" should be: - | name | - | Psr0Ns_TheClass | - | Psr0Ns_TheSubdir | + | name | + | Psr0Ns_TheClass | + | Psr0Ns_TheSubdir\ | And type completions for query "Psr0Ns_TheSubdir_" should be: | name | | Psr0Ns_TheSubdir_TheClass1 | @@ -34,15 +34,15 @@ Feature: Class name completion Scenario: Complete PSR-4 namespaces Given I visit "src/main.php" in project "without-composer" Then type completions for query "Psr4" should be: - | name | - | Psr4Ns | - | Psr4Split\Ns1 | - | Psr4Split\Ns2 | - | Psr4Fallback | + | name | + | Psr4Ns\ | + | Psr4Split\Ns1\ | + | Psr4Split\Ns2\ | + | Psr4Fallback\ | And type completions for query "Psr4Ns\T" should be: - | name | - | TheClass | - | TheSubdir | + | name | + | TheClass | + | TheSubdir\ | And type completions for query "Psr4Ns\TheSubdir\" should be: | name | | TheClass1 | From 67b4dc837457c90602f955e3246d15ef42d9ac4b Mon Sep 17 00:00:00 2001 From: Thomas Fini Hansen Date: Mon, 17 Apr 2017 23:01:03 +0200 Subject: [PATCH 8/8] Implement ede-php-autoload-complete for full completions --- ede-php-autoload.el | 9 ++++ ede-php-autoload/class-loader/core.el | 27 +++++++++-- features/ede-php-autoload-completion.feature | 48 ++++++++++++++++++- .../ede-php-autoload-steps.el | 11 +++++ 4 files changed, 90 insertions(+), 5 deletions(-) diff --git a/ede-php-autoload.el b/ede-php-autoload.el index 28a8ce5..79f731c 100644 --- a/ede-php-autoload.el +++ b/ede-php-autoload.el @@ -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. diff --git a/ede-php-autoload/class-loader/core.el b/ede-php-autoload/class-loader/core.el index 369402a..4d7db30 100644 --- a/ede-php-autoload/class-loader/core.el +++ b/ede-php-autoload/class-loader/core.el @@ -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." '()) diff --git a/features/ede-php-autoload-completion.feature b/features/ede-php-autoload-completion.feature index e1cd5c1..33264e2 100644 --- a/features/ede-php-autoload-completion.feature +++ b/features/ede-php-autoload-completion.feature @@ -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" @@ -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" @@ -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" @@ -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 diff --git a/features/step-definitions/ede-php-autoload-steps.el b/features/step-definitions/ede-php-autoload-steps.el index af1c0d5..9102679 100644 --- a/features/step-definitions/ede-php-autoload-steps.el +++ b/features/step-definitions/ede-php-autoload-steps.el @@ -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)))))