Skip to content

Commit b9a0105

Browse files
committed
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.
1 parent 69a582e commit b9a0105

File tree

2 files changed

+120
-2
lines changed

2 files changed

+120
-2
lines changed

java-imports.el

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ overwrites any existing cache entries for the file."
183183
(interactive)
184184
(when (member major-mode '(java-mode kotlin-mode))
185185
(let* ((cache (pcache-repository java-imports-cache-name)))
186-
(dolist (import (java-imports-list-imports))
186+
(dolist (import (append (java-imports-list-imports)
187+
(java-imports-list-top-levels)))
187188
(let ((pkg-class-list (java-imports-get-package-and-class import)))
188189
(when pkg-class-list
189190
(let* ((pkg (car pkg-class-list))
@@ -204,6 +205,25 @@ Java-mode or Kotlin-mode buffer"
204205
(cl-remove-if-not (lambda (str) (s-matches? "import[ \t]+.+?[ \t]*;?" str))
205206
(s-lines (buffer-string)))))
206207

208+
;;;###autoload
209+
(defun java-imports-list-top-levels ()
210+
"Return a list of all public top-level type declarations in
211+
the current Java-mode buffer"
212+
(interactive)
213+
(let* ((package-match
214+
(s-match "^package[ \t]+\\\(.+?\\\);?$" (buffer-string)))
215+
;; public is optional to always match the top-level entry and
216+
;; not the first public one (which might be a inner entry).
217+
(identifier-match
218+
(s-match "\\\(\\\<public[ \t\n]+\\\)?.*?\\\<\\\(?:class\\\|enum\\\|@?interface\\\)[ \t\n]+\\\([^. \t\n<]+\\\).*?[ \t\n]+{" (buffer-string))))
219+
(when (and identifier-match
220+
(cl-second identifier-match))
221+
(let ((identifier (cl-third identifier-match)))
222+
(cons (if package-match
223+
(concat (cl-second package-match) "." identifier)
224+
identifier)
225+
nil)))))
226+
207227
;;;###autoload
208228
(defun java-imports-add-import-with-package (class-name package)
209229
"Add an import for the class for the name and package. Uses no caching."

test/java-imports-test.el

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,100 @@
120120
(java-imports-list-imports)
121121
'("org.Thing" "java.util.List" "java.util.ArrayList")))))
122122

123+
(ert-deftest t-list-top-levels ()
124+
(with-temp-buffer
125+
(insert "package mypackage;\n")
126+
(insert "\n")
127+
(insert "import org.Thing;\n")
128+
(insert "\n")
129+
(insert "import java.util.List;\n")
130+
(insert "import java.util.ArrayList;\n")
131+
(insert "\n")
132+
(insert "public class Foo {}")
133+
(should
134+
(equal
135+
(java-imports-list-top-levels)
136+
'("mypackage.Foo"))))
137+
138+
(with-temp-buffer
139+
(insert "public class Foo {}")
140+
(should
141+
(equal
142+
(java-imports-list-top-levels)
143+
'("Foo"))))
144+
145+
(with-temp-buffer
146+
(insert "public abstract class Foo {}")
147+
(should
148+
(equal
149+
(java-imports-list-top-levels)
150+
'("Foo"))))
151+
152+
(with-temp-buffer
153+
(insert "public class Foo extends Bar {}")
154+
(should
155+
(equal
156+
(java-imports-list-top-levels)
157+
'("Foo"))))
158+
159+
(with-temp-buffer
160+
(insert "public class Foo implements Bar {}")
161+
(should
162+
(equal
163+
(java-imports-list-top-levels)
164+
'("Foo"))))
165+
166+
(with-temp-buffer
167+
(insert "public class Foo<T> {}")
168+
(should
169+
(equal
170+
(java-imports-list-top-levels)
171+
'("Foo"))))
172+
173+
(with-temp-buffer
174+
(insert "package mypackage;\n")
175+
(insert "\n")
176+
(insert "class Foo {\n")
177+
(insert "public static class Bar {}\n")
178+
(insert "}\n")
179+
(should
180+
(equal
181+
(java-imports-list-top-levels)
182+
'())))
183+
184+
(with-temp-buffer
185+
(should
186+
(equal
187+
(java-imports-list-top-levels)
188+
'())))
189+
190+
(with-temp-buffer
191+
(insert "package mypackage;\n")
192+
(insert "\n")
193+
(insert "public\nenum\nFoo\n{}")
194+
(should
195+
(equal
196+
(java-imports-list-top-levels)
197+
'("mypackage.Foo"))))
198+
199+
(with-temp-buffer
200+
(insert "package mypackage;\n")
201+
(insert "\n")
202+
(insert "public @interface Annotation {}")
203+
(should
204+
(equal
205+
(java-imports-list-top-levels)
206+
'("mypackage.Annotation"))))
207+
208+
(with-temp-buffer
209+
(insert "package mypackage;\n")
210+
(insert "\n")
211+
(insert "public interface Foo {}")
212+
(should
213+
(equal
214+
(java-imports-list-top-levels)
215+
'("mypackage.Foo")))))
216+
123217
(ert-deftest t-pkg-and-class-from-import ()
124218
(should
125219
(equal (java-imports-get-package-and-class "java.util.Map")
@@ -158,7 +252,11 @@
158252
(should
159253
(equal
160254
(pcache-get cache 'ArrayList)
161-
"java.util"))))
255+
"java.util"))
256+
(should
257+
(equal
258+
(pcache-get cache 'Foo)
259+
"mypackage"))))
162260
(pcache-destroy-repository java-imports-cache-name))))
163261

164262
;; End:

0 commit comments

Comments
 (0)