Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion java-imports.el
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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 "\\\(\\\<public[ \t\n]+\\\)?.*?\\\<\\\(?:class\\\|enum\\\|@?interface\\\)[ \t\n]+\\\([^. \t\n<]+\\\).*?[ \t\n]+{" (buffer-string))))
(when (and identifier-match
(cl-second identifier-match))
(let ((identifier (cl-third identifier-match)))
(cons (if package-match
(concat (cl-second package-match) "." identifier)
identifier)
nil)))))

;;;###autoload
(defun java-imports-add-import-with-package (class-name package)
"Add an import for the class for the name and package. Uses no caching."
Expand Down
131 changes: 131 additions & 0 deletions test/java-imports-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,100 @@
(java-imports-list-imports)
'("org.Thing" "java.util.List" "java.util.ArrayList")))))

(ert-deftest t-list-top-levels ()
(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 {}")
(should
(equal
(java-imports-list-top-levels)
'("mypackage.Foo"))))

(with-temp-buffer
(insert "public class Foo {}")
(should
(equal
(java-imports-list-top-levels)
'("Foo"))))

(with-temp-buffer
(insert "public abstract class Foo {}")
(should
(equal
(java-imports-list-top-levels)
'("Foo"))))

(with-temp-buffer
(insert "public class Foo extends Bar {}")
(should
(equal
(java-imports-list-top-levels)
'("Foo"))))

(with-temp-buffer
(insert "public class Foo implements Bar {}")
(should
(equal
(java-imports-list-top-levels)
'("Foo"))))

(with-temp-buffer
(insert "public class Foo<T> {}")
(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")
Expand All @@ -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