From 8cdf6aee8e375ae39e1af6c33bafc814a44fae3c Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Thu, 7 Oct 2021 18:43:27 +0200 Subject: [PATCH 1/2] Add test for java-imports-scan-file In preparation for expanding the functionality, quite a bit of setup needed but it seems to work. --- test/java-imports-test.el | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/java-imports-test.el b/test/java-imports-test.el index 6da661b..33fe583 100644 --- a/test/java-imports-test.el +++ b/test/java-imports-test.el @@ -128,5 +128,38 @@ (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")))) + (pcache-destroy-repository java-imports-cache-name)))) + ;; End: ;;; java-imports-test.el ends here From 7f9489fca591593a3dee09225cb085c036a0e2ee Mon Sep 17 00:00:00 2001 From: Joel Klinghed Date: Thu, 7 Oct 2021 18:57:11 +0200 Subject: [PATCH 2/2] Add public top level types to cache in java-imports-scan-file Add new function java-imports-list-top-level and use it in java-imports-scan-file to add public top level types to list of classes to add to cache. Useful when adding new types or working with a new project, save having to specify the package on first import. Only works if java-imports-scan-file runs after type is declared in file so for new files it might be worth adding (java-imports-scan-file) to define-auto-insert for example. No kotlin-mode support yet, haven't figured out a good way to do a "negative" match as "public" modifier is the default in kotlin. Also the primary constructor syntax is tricky to include and the regex is already rather horrible. --- java-imports.el | 22 ++++++++- test/java-imports-test.el | 100 +++++++++++++++++++++++++++++++++++++- 2 files changed, 120 insertions(+), 2 deletions(-) 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") @@ -158,7 +252,11 @@ (should (equal (pcache-get cache 'ArrayList) - "java.util")))) + "java.util")) + (should + (equal + (pcache-get cache 'Foo) + "mypackage")))) (pcache-destroy-repository java-imports-cache-name)))) ;; End: