diff --git a/java-imports.el b/java-imports.el index 5ad5eae..088b033 100644 --- a/java-imports.el +++ b/java-imports.el @@ -183,7 +183,8 @@ overwrites any existing cache entries for the file." (interactive) (when (member major-mode '(java-mode kotlin-mode)) (let* ((cache (pcache-repository java-imports-cache-name))) - (dolist (import (java-imports-list-imports)) + (dolist (import (append (java-imports-list-imports) + (java-imports-list-top-levels))) (let ((pkg-class-list (java-imports-get-package-and-class import))) (when pkg-class-list (let* ((pkg (car pkg-class-list)) @@ -204,6 +205,25 @@ Java-mode or Kotlin-mode buffer" (cl-remove-if-not (lambda (str) (s-matches? "import[ \t]+.+?[ \t]*;?" str)) (s-lines (buffer-string))))) +;;;###autoload +(defun java-imports-list-top-levels () + "Return a list of all public top-level type declarations in +the current Java-mode buffer" + (interactive) + (let* ((package-match + (s-match "^package[ \t]+\\\(.+?\\\);?$" (buffer-string))) + ;; public is optional to always match the top-level entry and + ;; not the first public one (which might be a inner entry). + (identifier-match + (s-match "\\\(\\\ {}") + (should + (equal + (java-imports-list-top-levels) + '("Foo")))) + + (with-temp-buffer + (insert "package mypackage;\n") + (insert "\n") + (insert "class Foo {\n") + (insert "public static class Bar {}\n") + (insert "}\n") + (should + (equal + (java-imports-list-top-levels) + '()))) + + (with-temp-buffer + (should + (equal + (java-imports-list-top-levels) + '()))) + + (with-temp-buffer + (insert "package mypackage;\n") + (insert "\n") + (insert "public\nenum\nFoo\n{}") + (should + (equal + (java-imports-list-top-levels) + '("mypackage.Foo")))) + + (with-temp-buffer + (insert "package mypackage;\n") + (insert "\n") + (insert "public @interface Annotation {}") + (should + (equal + (java-imports-list-top-levels) + '("mypackage.Annotation")))) + + (with-temp-buffer + (insert "package mypackage;\n") + (insert "\n") + (insert "public interface Foo {}") + (should + (equal + (java-imports-list-top-levels) + '("mypackage.Foo"))))) + (ert-deftest t-pkg-and-class-from-import () (should (equal (java-imports-get-package-and-class "java.util.Map") @@ -128,5 +222,42 @@ (equal (java-imports-get-package-and-class "org.foo.bar.baz.ThingOne") '("org.foo.bar.baz" "ThingOne")))) +(ert-deftest t-scan-file () + (let ((java-imports-cache-name "java-imports-test/tmp") + (inhibit-message t) + (c-initialization-hook nil) + (c-mode-common-hook nil) + (java-mode-hook nil)) + (unwind-protect + (with-temp-buffer + (insert "package mypackage;\n") + (insert "\n") + (insert "import org.Thing;\n") + (insert "\n") + (insert "import java.util.List;\n") + (insert "import java.util.ArrayList;\n") + (insert "\n") + (insert "public class Foo {}") + (java-mode) + (java-imports-scan-file) + (let ((cache (pcache-repository java-imports-cache-name))) + (should + (equal + (pcache-get cache 'Thing) + "org")) + (should + (equal + (pcache-get cache 'List) + "java.util")) + (should + (equal + (pcache-get cache 'ArrayList) + "java.util")) + (should + (equal + (pcache-get cache 'Foo) + "mypackage")))) + (pcache-destroy-repository java-imports-cache-name)))) + ;; End: ;;; java-imports-test.el ends here