diff --git a/CHANGELOG.md b/CHANGELOG.md index c51ee0f..3b70ceb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +# Next + +Features: +- Automatically remove autoloads leading to non-existing dir + +Fixes: + # 1.1.0 Features: diff --git a/ede-php-autoload.el b/ede-php-autoload.el index d37e2f0..28a8ce5 100644 --- a/ede-php-autoload.el +++ b/ede-php-autoload.el @@ -146,7 +146,52 @@ to the associated directories." (add-to-list 'loaders (ede-php-autoload-class-loader-call-factory key (cadr load-config))) (setq load-config (cddr load-config)))) (ede-php-autoload-aggregate-class-loader "Aggregate loader" - :class-loaders loaders))) + :class-loaders loaders))) + +(defun ede-php-autoload-remove-non-existing-dirs (conf root-dir) + "Remove from CONF the non-existing directories. + +CONF is the same kind of argument than `ede-php-autoload-create-class-loader'. + +ROOT-DIR is the root directory of the project." + (let ((cleaned-conf '()) + (conf-length (length conf)) + (index 0) + key + namespaces cleaned-namespaces + namespace + paths cleaned-paths) + + ;; key: `:psr-0', ... + (while (< index conf-length) + (setq key (nth index conf) + namespaces (nth (1+ index) conf) + cleaned-namespaces '()) + + ;; pair: (ns . paths) + (dolist (pair namespaces) + (setq namespace (car pair) + paths (if (listp (cdr pair)) (cdr pair) (list (cdr pair))) + cleaned-paths '()) + + (dolist (path paths) + (when (file-exists-p (expand-file-name path root-dir)) + (add-to-list 'cleaned-paths path t))) + + (when (> (length cleaned-paths) 0) + (add-to-list 'cleaned-namespaces + (cons + namespace + (if (= (length cleaned-paths) 1) + (car cleaned-paths) + cleaned-paths)) + t))) + + (when (> (length cleaned-namespaces) 0) + (setq cleaned-conf (append cleaned-conf (list key cleaned-namespaces)))) + + (setq index (+ index 2))) + cleaned-conf)) (defclass ede-php-autoload-target (ede-target) @@ -205,12 +250,15 @@ to the associated directories." This can be used when some composer dependencies changed, to take the new autoloads into account." - (oset this class-loader - (ede-php-autoload-create-class-loader - (ede-php-autoload--append-composer-autoload-data - (file-name-directory (ede-project-root-directory this)) - (oref this explicit-class-autoloads)) - ))) + (let* ((raw-autoloads + (ede-php-autoload--append-composer-autoload-data + (file-name-directory (ede-project-root-directory this)) + (oref this explicit-class-autoloads))) + (root-dir (ede-project-root-directory this)) + (cleaned-autoloads (ede-php-autoload-remove-non-existing-dirs raw-autoloads root-dir))) + + (oset this class-loader + (ede-php-autoload-create-class-loader cleaned-autoloads)))) (defmethod ede-find-subproject-for-directory ((proj ede-php-autoload-project) dir) "Return PROJ, for handling all subdirs below DIR." diff --git a/features/ede-php-autoload-completion.feature b/features/ede-php-autoload-completion.feature index ff880f8..974ec11 100644 --- a/features/ede-php-autoload-completion.feature +++ b/features/ede-php-autoload-completion.feature @@ -31,3 +31,7 @@ Feature: Class name completion Given I visit "src/main.php" in project "without-composer" Then completions for query "ClassMapNs\" should be: | 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 diff --git a/features/ede-php-autoload.feature b/features/ede-php-autoload.feature index b9ef1ed..ec8d4ab 100644 --- a/features/ede-php-autoload.feature +++ b/features/ede-php-autoload.feature @@ -48,6 +48,6 @@ Feature: Hand-made EDE project creation THen guessing the class name for "src/ClassMapNs/MyClass.php" should return "ClassMapNs\MyClass" - Scenario: Load a class the doesn't exist + Scenario: Load a class that doesn't exist Given I visit "src/main.php" in project "without-composer" Then the class "Psr4Ns\DoesNotExist" should not be detected diff --git a/features/step-definitions/ede-php-autoload-steps.el b/features/step-definitions/ede-php-autoload-steps.el index b135185..b5374be 100644 --- a/features/step-definitions/ede-php-autoload-steps.el +++ b/features/step-definitions/ede-php-autoload-steps.el @@ -47,3 +47,7 @@ (lambda (query suggestion-table) (should (equal (ede-php-autoload-complete-type-name (ede-current-project) query) (car suggestion-table))))) + +(Then "^completions for query \"\\(.+\\)\" should be nil" + (lambda (query) + (should (null (ede-php-autoload-complete-type-name (ede-current-project) query))))) diff --git a/features/support/env.el b/features/support/env.el index 99bef2b..5dcacb0 100644 --- a/features/support/env.el +++ b/features/support/env.el @@ -65,7 +65,8 @@ COMPOSER-FILE-NAME is either default or new." :psr-4 (("Psr4Ns" . "src/Psr4Ns") ("MultiDirNs" . ("src/MultiDirNs1" "src/MultiDirNs2")) ("Psr4Split\\Ns1" . "src/Psr4Split/Ns1") - ("Psr4Split\\Ns2" . "src/Psr4Split/Ns2")) + ("Psr4Split\\Ns2" . "src/Psr4Split/Ns2") + ("NonExisting" . "src/NonExisting")) :class-map ((ClassMapNs\\MyClass . "src/ClassMapNs/MyClass.php"))) :include-path '(".") :system-include-path '("/usr/share/php")))