forked from skeeto/javadoc-lookup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
java-import.el
76 lines (61 loc) · 2.09 KB
/
java-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
;;; java-import.el --- quickly add import statements in Java
;; This is free and unencumbered software released into the public domain.
;;; Commentary:
;; Provides the functions `add-java-import' and `sort-java-imports'.
;;; Code:
(require 'javadoc-lookup)
(defvar jdl/import-regexp "^import "
"Regular expression for finding import statements.")
(defvar jdl/package-regexp "^package "
"Regular expression for finding package statements.")
(defun jdl/in-package ()
"Return t if this source has a package statement."
(save-excursion
(goto-char (point-min))
(and (search-forward-regexp jdl/package-regexp nil t) t)))
(defun jdl/has-import ()
"Return t if this source has at least one import statement."
(save-excursion
(goto-char (point-min))
(and (search-forward-regexp jdl/import-regexp nil t) t)))
(defun jdl/goto-first-import ()
"Move cursor to the first import statement."
(goto-char (point-min))
(search-forward-regexp jdl/import-regexp)
(move-beginning-of-line nil)
(point))
(defun jdl/goto-last-import ()
"Move cursor to the first import statement."
(goto-char (point-max))
(search-backward-regexp jdl/import-regexp)
(move-end-of-line nil)
(forward-char)
(point))
;;;###autoload
(defun sort-java-imports ()
"Sort the imports in the import section in proper order."
(interactive)
(when (jdl/has-import)
(save-excursion
(sort-lines nil (jdl/goto-first-import) (jdl/goto-last-import)))))
;;;###autoload
(defun add-java-import ()
"Insert an import statement at import section at the top of the file."
(interactive)
(let ((class (jdl/completing-read)))
(save-excursion
(if (jdl/has-import)
(progn
(jdl/goto-first-import)
(insert "import " class ";\n")
(sort-java-imports))
(progn
(goto-char (point-min))
(when (jdl/in-package)
(search-forward-regexp jdl/package-regexp)
(move-end-of-line nil)
(forward-char)
(insert "\n"))
(insert "import " class ";\n"))))))
(provide 'java-import)
;;; java-import.el ends here