/
malabar-import.el
executable file
·415 lines (389 loc) · 16.2 KB
/
malabar-import.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
;;; malabar-import.el --- Import stuff for malabar-mode
;;
;; Copyright (c) 2009, 2010 Espen Wiborg <espenhw@grumblesmurf.org>
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of the
;; License, or (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
;; 02110-1301 USA.
;;
(require 'cc-vars)
(require 'malabar-util)
(require 'malabar-reflection)
(defcustom malabar-import-excluded-classes-regexp-list
'("^java\\.lang\\.[^.]+$" ;; Always imported
"^sun\\." ;; Implementation internals
"^com\\.sun\\..*\\.internal\\." ;; ...
"\\$" ;; If you want to import
;; an inner class, do it
;; yourself
)
"Any class that matches a regexp on this list will never be
automatically imported."
:group 'malabar-mode
:type '(repeat regexp))
(defcustom malabar-import-precedence-order
'("java.util"
"java.io"
"java.net"
"java.lang.reflect"
"java.sql"
"java.text"
"javax.swing")
"Sort order by package for classes to import. A class from a
package not in this list will sort after a class from any package
in the list."
:group 'malabar-mode
:type '(repeat string))
(defcustom malabar-import-post-insert-function nil
"Function run after inserting imports."
:group 'malabar-mode
:type '(radio (const nil)
(function-item malabar-import-sort-imports)
(function-item malabar-import-group-imports)
function))
(defvar malabar-import-java-lang-classes
(mapcar (lambda (c)
(let ((n (symbol-name c)))
(cons n (concat "java.lang." n))))
'(;;Interfaces
Appendable
CharSequence
Cloneable
Comparable
Iterable
Readable
Runnable
;;Classes
Boolean
Byte
Character
Class
ClassLoader
Compiler
Double
Enum
Float
InheritableThreadLocal
Integer
Long
Math
Number
Object
Package
Process
ProcessBuilder
Runtime
RuntimePermission
SecurityManager
Short
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
System
Thread
ThreadGroup
ThreadLocal
Throwable
Void
;;Exceptions
ArithmeticException
ArrayIndexOutOfBoundsException
ArrayStoreException
ClassCastException
ClassNotFoundException
CloneNotSupportedException
EnumConstantNotPresentException
Exception
IllegalAccessException
IllegalArgumentException
IllegalMonitorStateException
IllegalStateException
IllegalThreadStateException
IndexOutOfBoundsException
InstantiationException
InterruptedException
NegativeArraySizeException
NoSuchFieldException
NoSuchMethodException
NullPointerException
NumberFormatException
RuntimeException
SecurityException
StringIndexOutOfBoundsException
TypeNotPresentException
UnsupportedOperationException
;;Errors
AbstractMethodError
AssertionError
ClassCircularityError
ClassFormatError
Error
ExceptionInInitializerError
IllegalAccessError
IncompatibleClassChangeError
InstantiationError
InternalError
LinkageError
NoClassDefFoundError
NoSuchFieldError
NoSuchMethodError
OutOfMemoryError
StackOverflowError
ThreadDeath
UnknownError
UnsatisfiedLinkError
UnsupportedClassVersionError
VerifyError
VirtualMachineError
Annotation Types
Deprecated
Override
SuppressWarnings)))
(defun malabar-type-token-candidates ()
(remove nil (mapcar (lambda (token)
(when (eq (car token) 'IDENTIFIER)
(buffer-substring-no-properties (cadr token) (cddr token))))
(semantic-lex-buffer 1000))))
(defun malabar-type-token-p (token)
(let ((case-fold-search nil))
(and (> (length token) 1)
(some (lambda (re)
(string-match-p (concat "^" re "$") token))
java-font-lock-extra-types))))
(defun malabar-find-imported-class (classname &optional buffer)
;; Shortcut - this will be wrong in some cases, but if you get
;; bitten by this you deserve all the pain in the world.
(if (or (equal "Object" classname)
(equal "java.lang.Object" classname))
"java.lang.Object"
(let ((tags (semantic-find-tags-by-class 'include (or buffer (current-buffer)))))
(let ((import-tag (find classname tags
:key #'semantic-tag-name
:test (lambda (classname tag)
(and (stringp classname)
(string-ends-with tag (concat "." classname)))))))
(or (when (> (count ?. classname) 1)
classname)
(when import-tag
(semantic-tag-name import-tag))
(if (malabar-find-project-file buffer)
(or (malabar-find-imported-class-from-wildcard-imports classname buffer)
(find (concat "java.lang." classname)
(malabar-qualify-class-name classname buffer)
:test #'equal))
;; Punt
(or (cdr (assoc classname malabar-import-java-lang-classes))
(concat (malabar-get-package-name buffer) "." classname))))))))
(defun malabar-find-imported-class-from-wildcard-imports (class &optional buffer)
(let ((tags
(remove-if-not (lambda (package)
(string-ends-with package "*"))
(semantic-find-tags-by-class 'include (or buffer (current-buffer)))
:key #'semantic-tag-name))
(classes (malabar-qualify-class-name class buffer)))
(some (lambda (tag)
(find (concat (malabar-get-package-of (semantic-tag-name tag)) "." class)
classes
:test #'equal))
tags)))
(defun malabar-import-candidates ()
(let ((type-tokens (remove-if-not #'malabar-type-token-p (malabar-type-token-candidates))))
(remove-duplicates
(remove-if (lambda (token)
(or (malabar-class-defined-in-buffer-p token)
(malabar-find-imported-class token)))
type-tokens)
:test #'equal)))
(defun malabar-import-current-package-p (qualified-class)
(when-let (package (malabar-get-package-name))
(string-match-p (concat "^" (regexp-quote package) "\\.[^.]+$") qualified-class)))
(defun malabar-import-exclude (qualified-class)
(or (some (lambda (re)
(string-match-p re qualified-class))
malabar-import-excluded-classes-regexp-list)
(malabar-import-current-package-p qualified-class)))
(defun malabar-import-sort-by-precedence (class-a class-b)
(let ((a-package (malabar-get-package-of class-a))
(b-package (malabar-get-package-of class-b)))
(let ((a-package-successors (member a-package malabar-import-precedence-order))
(b-package-successors (member b-package malabar-import-precedence-order)))
(or (member b-package a-package-successors)
(and a-package-successors
(null b-package-successors))))))
(defun malabar-import-find-import (unqualified)
(when-let (possible-classes
(remove-duplicates (sort (remove-if #'malabar-import-exclude
(malabar-qualify-class-name unqualified))
#'malabar-import-sort-by-precedence)
:test #'equal))
(if (= 1 (length possible-classes))
(car possible-classes)
(malabar-choose (format "%d classes named '%s', pick one: "
(length possible-classes)
unqualified)
possible-classes
(car possible-classes)))))
(defun malabar-import-all ()
"Attempts to add import statements for all unqualified type
names in the current buffer."
(interactive)
(when-let (imports (remove nil
(mapcar #'malabar-import-find-import
(malabar-import-candidates))))
(malabar-import-insert-imports imports)))
(defun malabar--type-at-point ()
(save-excursion
(skip-chars-forward "A-Za-z0-9._")
(mapconcat 'identity (semantic-ctxt-current-thing) ".")))
(defun malabar-import-and-unqualify (qualified)
"Imports QUALIFIED if necessary, and unqualifies all
occurrences of qualified in buffer, unless another class of the
same name is already imported."
(interactive (list (read-from-minibuffer "Class: " (malabar--type-at-point))))
(let* ((unqualified (malabar-get-classname qualified))
(existing-import (malabar-find-imported-class unqualified)))
(cond ((or (equal existing-import qualified)
(malabar-import-exclude qualified))
;; Already imported, just unqualify
(message "Class %s does not need to be imported" qualified)
(malabar--conditional-replace (regexp-quote qualified) unqualified
(point-min) (point-max)
(lambda ()
(not (semantic-current-tag-of-class 'include)))))
((null existing-import)
(malabar--conditional-replace (regexp-quote qualified) unqualified
(point-min) (point-max)
(lambda ()
(not (semantic-current-tag-of-class 'include))))
(malabar-import-insert-imports (list qualified)))
(t
(message "Import of %s would clash with %s"
qualified existing-import)))))
(defun malabar--import-handle-import-candidates (candidates)
(dolist (candidate candidates)
(unless (or (malabar--primitive-type-p candidate)
(malabar--type-variable-name-p candidate))
(malabar-import-and-unqualify candidate))))
;; See gh-107
(defun malabar-import-one-class (unqualified)
"Qualifies and adds an import statement for a single type name.
If UNQUALIFIED is NIL, prompts in the minibuffer.
If UNQUALIFIED starts with an @, strip it off."
(interactive (list (read-from-minibuffer "Class: " (thing-at-point 'symbol))))
(let ((unqualified (if (string-match "^@.*" unqualified) (substring unqualified 1) unqualified)))
(if (or (malabar-class-defined-in-buffer-p unqualified)
(malabar-find-imported-class unqualified))
(message "Class %s does not need to be imported" unqualified)
(let ((class-to-import (malabar-import-find-import unqualified)))
(unless (null class-to-import)
(malabar-import-insert-imports (list class-to-import)))))))
(defun malabar-import-insert-imports (qualified-classes)
(when qualified-classes
(let* ((tags (malabar-semantic-fetch-tags))
(last-import-tag (car (last (semantic-brute-find-tag-by-class 'include tags))))
(package-tag (car (semantic-brute-find-tag-by-class 'package tags)))
(class-tag (car (semantic-brute-find-tag-by-class 'type tags)))
insertion-point)
(cond (last-import-tag
(setq insertion-point (1+ (semantic-tag-end last-import-tag))))
(package-tag
(save-excursion
(goto-char (semantic-tag-end package-tag))
(forward-line)
(insert "\n")
(setq insertion-point (point))))
(class-tag
(setq insertion-point
(let ((class-doc (semantic-documentation-for-tag class-tag 'lex)))
(if class-doc
(semantic-lex-token-start class-doc)
(semantic-tag-start class-tag)))))
(t
(setq insertion-point (point-min))))
(save-excursion
(goto-char insertion-point)
(unless (and (bolp) (eolp))
(insert "\n"))
(goto-char insertion-point)
(dolist (qualified-class qualified-classes)
(when (> (length qualified-class) 0)
(insert "import " qualified-class ";\n")
(message "Imported %s" qualified-class)))
(when malabar-import-post-insert-function
(funcall malabar-import-post-insert-function))))))
(defun malabar-import--imports-region ()
(semantic-parse-tree-set-needs-rebuild)
(let* ((tags (malabar-semantic-fetch-tags))
(import-tags (semantic-brute-find-tag-by-class 'include tags))
(first-import (car import-tags))
(last-import (car (last import-tags))))
(list (semantic-tag-start first-import)
(semantic-tag-end last-import)
import-tags)))
(defun malabar-import-sort-imports ()
"Sort imports alphabetically, removing blank lines."
(interactive)
;; This screws any inline comments on imports. Watch me care.
(destructuring-bind (start end import-tags)
(malabar-import--imports-region)
(save-excursion
(goto-char start)
(delete-region start end)
(insert (mapconcat 'semantic-format-tag-prototype
(sort import-tags #'(lambda (a b)
(string< (semantic-tag-name a)
(semantic-tag-name b))))
"\n")))))
(defcustom malabar-import-group-token-count 2
"The number of tokens to consider when grouping imports.
Mostly this controls the positioning of blank lines. If the
symbol ALL, consider the entire package."
:group 'malabar-mode
:type '(choice integer
(const all)))
(defun malabar-import-group-imports ()
"Sort imports, and then group them by
`malabar-import-group-token-count' tokens."
(interactive)
;; This screws any inline comments on imports. Watch me care.
(malabar-import-sort-imports)
(let* ((region (malabar-import--imports-region))
(start (car region))
(end (cadr region)))
(goto-char start)
(let ((last-tag (semantic-current-tag-of-class 'include))
(tag (progn (forward-line 1)
(semantic-current-tag-of-class 'include))))
(flet ((package (include-tag)
(let ((package-tokens
(nreverse
(split-string
(semantic-tag-name include-tag)
"\\."))))
(nreverse (if (eq malabar-import-group-token-count 'all)
(cdr package-tokens)
(last package-tokens
malabar-import-group-token-count))))))
(while tag
(unless (equal (package last-tag) (package tag))
(insert "\n"))
(forward-line 1)
(setq last-tag tag)
(setq tag (semantic-current-tag-of-class 'include)))))))
(provide 'malabar-import)
;; Local Variables:
;; byte-compile-warnings:(not cl-functions)
;; End: