-
Notifications
You must be signed in to change notification settings - Fork 5
/
grails.el
409 lines (341 loc) · 13.7 KB
/
grails.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
;;; grails.el --- Minor mode for Grails projects
;;
;; Copyright (c) 2016 Alessandro Miliucci
;;
;; Authors: Alessandro Miliucci <[email protected]>
;; Version: 0.4.1
;; URL: https://github.com/lifeisfoo/emacs-grails
;; Package-Requires: ((emacs "24"))
;; 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 3 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, see <http://www.gnu.org/licenses/>.
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Description:
;; Grails.el is a minor mode that allows an easy
;; navigation of Gails projects. It allows jump to a model, to a view,
;; to a controller or to a service.
;; Features:
;; - Jump to the related Domain (from the current buffer)
;; - Jump to the related Controller (from the current buffer)
;; - Jump to the related Service (from the current buffer)
;; - Jump to the related view(s) (from the current buffer)
;; - Open the Bootstrap file
;; - Open the UrlMappings file
;; - Find file prompt for domain classes
;; - Find file prompt for controller classes
;; - Find file prompt for service classes
;; - Find file prompt for views
;; For the complete documentation, see the project page at
;; https://github.com/lifeisfoo/emacs-grails
;; Installation:
;; Copy this file to to some location in your Emacs load path. Then add
;; "(require 'grails)" to your Emacs initialization (.emacs,
;; init.el, or something).
;; Example config:
;; (require 'grails)
;; To auto enable grails minor mode, create a .dir-locals.el file
;; in the root of the grails project with this configuration:
;; ((nil . ((grails . 1))))
;; In this way, the grails minor mode will be always active inside your project tree.
;; The first time that this code is executed, Emacs will show a security
;; prompt: answer "!" to mark code secure and save your decision (a configuration
;; line is automatically added to your .emacs file).
;; Otherwise, if you want to have grails mode auto enabled only
;; when using certain major modes, place this inside your `.dir-locals.el`:
;; ((groovy-mode (grails . 1))
;; (html-mode (grails . 1))
;; (java-mode (grails . 1)))
;; In this way, the grails mode will be auto enabled when any of
;; these modes are loaded (only in this directory tree - the project tree)
;; (you can attach it to other modes if you want).
;;; Code:
(defcustom grails-base-package ""
"Grails source code base package."
:type 'string
:group 'grails)
(eval-and-compile
(defvar grails-dir-name-by-type
'((controller "controllers")
(domain "domain")
(service "services")
(view "views"))))
(defvar grails-postfix-by-type
'((controller "Controller.groovy")
(domain ".groovy")
(service "Service.groovy")
(view ".gsp")))
(defvar grails-properties-by-version
'((2 "application.properties" "^app.grails.version=")
(3 "gradle.properties" "^grailsVersion=")
(4 "gradle.properties" "^grailsVersion=")
(5 "gradle.properties" "^grailsVersion=")
))
(defvar grails-source-code-base-directory
(s-replace "." "\/" grails-base-package)
)
(defvar grails-urlmappings-by-version
`((2 "conf/UrlMappings.groovy")
(3 "controllers/UrlMappings.groovy")
(4 ,(concat "controllers/" grails-source-code-base-directory "/UrlMappings.groovy"))
(5 ,(concat "controllers/" grails-source-code-base-directory "/UrlMappings.groovy"))
))
;; TODO: refactor using only one list
(defvar grails-bootstrap-by-version
`((2 "conf/BootStrap.groovy")
(3 "init/BootStrap.groovy")
(4 ,(concat "init/" grails-source-code-base-directory "/BootStrap.groovy"))
(5 ,(concat "init/" grails-source-code-base-directory "/BootStrap.groovy"))
))
;;
;;
;; Utils functions
;;
;;
(defun util-string-from-file (file-path)
"Return a string with file-path contents."
(with-temp-buffer
(insert-file-contents file-path)
(buffer-string)))
;;
;;
;; Internal functions (non interactive)
;;
;;
(defun grails-grep-version (string)
"Try all regex on the string to detect major version.
E.g. string=grailsVersion=3.0.1 will return 3
"
(let (version)
(dolist (elt grails-properties-by-version version)
(if (string-match (car (cddr elt)) string)
(setq version (car elt))))))
(defun grails-version-detect (file-path)
"Try to detect the Grails version of the project from file-path.
Grails 2 projects have an application.properties file.
Grails 3 projects have a gradle.properties file.
"
(let (version)
(dolist (elt grails-properties-by-version version)
(let ((prop-file
(concat (grails-project-root file-path) (car (cdr elt)))))
(if (file-readable-p prop-file)
(setq version
(grails-grep-version
(util-string-from-file prop-file))))))))
(defun grails-dir-by-type-and-name (type class-name base-path)
"Return the file path (string) for the type and the class-name.
E.g. type='domain, class-name=User and base-path=/prj/grails-app/
will output /prj/grails-app/domain/User.groovy
"
(concat
base-path
(car (cdr (assoc type grails-dir-name-by-type)))
"/"
class-name
(car (cdr (assoc type grails-postfix-by-type)))))
(defun grails-extract-name (file-path grails-type)
"Transform MyClassController.groovy to MyClass,
or my/package/MyClassController.groovy to my/package/MyClass.
"
(cond ((eq grails-type 'controller)
(let ((end (string-match "Controller\.groovy" file-path)))
(substring file-path 0 end)))
((eq grails-type 'domain)
(let ((end (string-match "\.groovy" file-path)))
(substring file-path 0 end)))
((eq grails-type 'view)
(error "Jumping from views isn't supported"))
((eq grails-type 'service)
(let ((end (string-match "Service\.groovy" file-path)))
(substring file-path 0 end)))
(t (error "Grails type not recognized"))))
;;TODO - remove duplicated code
(defun grails-type-by-dir (file)
"Detect current file type using its path"
(string-match "\\(^.*/grails-app/\\)\\([a-zA-Z]+\\)/\\(.*\\)" file)
(let ((dir-type (match-string 2 file)))
(car (rassoc (cons dir-type '()) grails-dir-name-by-type))))
(defun grails-clean-name (file)
"Detect current file type and extract it's clean class-name.
E.g. ~/prj/grails-app/controllers/UserController.groovy
will results in User
Or ~/grails-app/domain/pkg/User.groovy
will results in pkg/User
"
(string-match "\\(^.*/grails-app/\\)\\([a-zA-Z]+\\)/\\(.*\\)" file)
(let ((base-path (match-string 1 file))
(dir-type (match-string 2 file))
(file-path (match-string 3 file)))
(let ((grails-type (car (rassoc (cons dir-type '()) grails-dir-name-by-type))))
(if grails-type
(grails-extract-name file-path grails-type)
(error "Current Grails filetype not recognized")))))
(defun grails-clean-name-no-pkg (file)
"Same as grails-clean-name but without package prefix"
(let ((clean-name (grails-clean-name file)))
(string-match "\\([a-zA-Z0-9]+\\)$" clean-name)
(match-string 1 clean-name)))
(defun grails-app-base (path)
"Get the current grails app base path /my/abs/path/grails-app/.
If exists return the app base path, else return nil.
path must be a file or must end with / - see file-name-directory doc
"
(let ((project-root (grails-project-root path)))
(if project-root
(concat project-root "grails-app/")
(error "Grails app not found"))))
(defun grails-project-root (path)
"Find project root for dir.
path must be a file or must end with / - see file-name-directory doc.
"
(locate-dominating-file (file-name-directory path) "grails-app"))
(defun grails-find-file-auto (grails-type current-file)
"Generate the corresponding file path for the current-file and grails-type.
grails-type is a symbol (e.g. 'domain, 'controller, 'service)
current-file is a file path
E.g. (grails-find-file-auto
'domain'
'~/prj/grails-app/controllers/UserController.groovy')
Will output: '~/prj/grails-app/domain/User.groovy'
"
(let ((base-path (grails-app-base current-file))
(class-name (grails-clean-name current-file)))
(grails-dir-by-type-and-name grails-type class-name base-path)))
(defun grails-string-is-action (line)
"Detect if line contains a controller action name"
(if (string-match "^.*def[[:blank:]]+\\([a-zA-Z0-9]+\\)[[:blank:]]*(.*).*{" line)
(match-string 1 line)
nil))
(defun grails-current-line-number ()
"Return the current buffer line (cursor)"
(1+ (count-lines 1 (point))))
(defun grails-find-current-controller-action ()
"Loop from the current line backwards, looking for a controller action definition."
(let ((continue 'true)
(action-name nil))
;; (setq continue 'true)
;; (setq action-name nil)
(save-excursion
(while continue
(if (> (grails-current-line-number) 1)
(let ((cur-line
(buffer-substring-no-properties (line-beginning-position) (line-end-position))))
(if (grails-string-is-action cur-line)
(progn (setq continue nil) ;;break
(setq action-name (grails-string-is-action cur-line))))
(forward-line -1))
(setq continue nil)))
(if action-name
action-name
(error "Action name not found")))))
(defun grails-view-from-string (view-name)
"Open a view for the current controller and the view-name."
(if (eq 'controller (grails-type-by-dir (buffer-file-name)))
(switch-to-buffer
(find-file-noselect
(concat
(grails-app-base (buffer-file-name))
"views/"
(downcase (grails-clean-name-no-pkg (buffer-file-name)))
"/"
view-name
".gsp")))
(error "This is not a controller class")))
;;
;;
;; INTERACTIVE FUNCTIONS
;;
;;
(defun grails-version ()
"Show Grails version for the project in the minibuffer"
(interactive)
(let ((version (grails-version-detect (buffer-file-name))))
(if version
(message (concat "Grails " (number-to-string version)))
(error "Grails version not found"))))
;; TODO: refactor using a macro
(defun grails-urlmappings-file ()
"Open the UrlMappings file"
(interactive)
(let ((version (grails-version-detect (buffer-file-name))))
(if version
(switch-to-buffer
(find-file-noselect
(concat (grails-app-base (buffer-file-name))
(car (cdr (assoc version grails-urlmappings-by-version))))))
(error "Grails version not found"))))
;; TODO: refactor using a macro
(defun grails-bootstrap-file ()
"Open the Bootstrap file"
(interactive)
(let ((version (grails-version-detect (buffer-file-name))))
(if version
(switch-to-buffer
(find-file-noselect
(concat (grails-app-base (buffer-file-name))
(car (cdr (assoc version grails-bootstrap-by-version))))))
(error "Grails version not found"))))
(defun grails-view-from-cursor ()
"Open a view from the current cursor."
(interactive)
(grails-view-from-string (thing-at-point 'word)))
(defun grails-view-from-context ()
"Open a view for the current controller action."
(interactive)
(grails-view-from-string (grails-find-current-controller-action)))
(defmacro grails-fun-gen-from-file (grails-type)
(let ((funsymbol (intern (concat "grails-" (symbol-name grails-type) "-from-file"))))
`(defun ,funsymbol () (interactive) (switch-to-buffer
(find-file-noselect
(grails-find-file-auto
',grails-type (buffer-file-name)))))))
(defmacro grails-fun-gen-from-name (grails-type)
(let ((funsymbol (intern (concat "grails-" (symbol-name grails-type) "-from-name"))))
`(defun ,funsymbol () (interactive)
(let ((x
(read-file-name
"Enter file name:"
(concat
(grails-app-base (buffer-file-name))
,(concat (car (cdr (assoc grails-type grails-dir-name-by-type))) "/")))))
(switch-to-buffer
(find-file-noselect x))))))
(defvar grails-key-map
(let ((keymap (make-sparse-keymap)))
(define-key keymap (kbd "C-c - d") (grails-fun-gen-from-file domain))
(define-key keymap (kbd "C-c - c") (grails-fun-gen-from-file controller))
(define-key keymap (kbd "C-c - s") (grails-fun-gen-from-file service))
(define-key keymap (kbd "C-c - v v") 'grails-view-from-context)
(define-key keymap (kbd "C-c - v w") 'grails-view-from-cursor)
(define-key keymap (kbd "C-c - n d") (grails-fun-gen-from-name domain))
(define-key keymap (kbd "C-c - n c") (grails-fun-gen-from-name controller))
(define-key keymap (kbd "C-c - n s") (grails-fun-gen-from-name service))
(define-key keymap (kbd "C-c - n v") (grails-fun-gen-from-name view))
;; p for project properties - TODO: show more information
(define-key keymap (kbd "C-c - p") 'grails-version)
(define-key keymap (kbd "C-c - u") 'grails-urlmappings-file)
(define-key keymap (kbd "C-c - b") 'grails-bootstrap-file)
keymap)
"Keymap for `grails` mode.")
;;;###autoload
(define-minor-mode grails
"Grails minor mode.
With no argument, this command toggles the mode.
Non-null prefix argument turns on the mode.
Null prefix argument turns off the mode.
When Grails minor mode is enabled you have some
shortcut to fast navigate a Grails project."
:init-value nil
:lighter " Grails"
:keymap grails-key-map
:group 'grails)
(provide 'grails)
;;; grails.el ends here