forked from tomtt/emacs-rails
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrails-refactoring.el
372 lines (313 loc) · 16.8 KB
/
rails-refactoring.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
;;; rails-refactoring.el -- common refactoring operations on rails projects
;; Copyright (C) 2009 by Remco van 't Veer
;; Author: Remco van 't Veer
;; Keywords: ruby rails languages oop refactoring
;;; License
;; 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
(require 'cl)
(require 'rails-core)
;; Customizations
(defcustom rails-refactoring-source-extensions '("builder" "erb" "haml" "liquid" "mab" "rake" "rb" "rjs" "rxml" "yml" "rtex" "prawn" "rabl" "json_builder" "jbuilder" "slim")
"List of file extensions for refactoring search and replace operations."
:group 'rails
:type '(repeat string))
;; Helper functions
(defmacro rails-refactoring:disclaim (name)
`(when (interactive-p)
(when (not (y-or-n-p (concat "Warning! " ,name " can not be undone! Are you sure you want to continue? ")))
(error "cancelled"))
(save-some-buffers)))
(defun rails-refactoring:decamelize (name)
"Translate Ruby class name to corresponding file name."
(replace-regexp-in-string "::" "/" (decamelize name)))
(assert (string= "foo_bar/quux" (rails-refactoring:decamelize "FooBar::Quux")))
(defun rails-refactoring:camelize (name)
"Translate file name into corresponding Ruby class name."
(replace-regexp-in-string "/" "::"
(replace-regexp-in-string "_\\([a-z]\\)" (lambda (match)
(upcase (substring match 1)))
(capitalize name))))
(assert (string= "FooBar::Quux" (rails-refactoring:camelize "foo_bar/quux")))
(defun rails-refactoring:source-file-p (name)
"Test if file has extension from `rails-refactoring-source-extensions'."
(find-if (lambda (ext) (string-match (concat "\\." ext "$") name))
rails-refactoring-source-extensions))
(defun rails-refactoring:source-files ()
"Return a list of all the source files in the current rails
project. This includes all the files in the 'app', 'config',
'lib' and 'test' directories."
(apply #'append
(mapcar (lambda (dirname)
(delete-if (lambda (file) (string-match "_flymake.rb" file))
(delete-if-not 'rails-refactoring:source-file-p
(mapcar (lambda (f) (concat dirname f))
(directory-files-recursive (rails-core:file dirname))))))
'("app/" "config/" "lib/" "test/" ))))
(defun rails-refactoring:class-files ()
"Return list of all Ruby class files."
(delete-if-not (lambda (file) (string-match "\\.rb$" file)) (rails-refactoring:source-files)))
(defun rails-refactoring:class-from-file (file)
"Return corresponding class/module name for given FILE."
(let ((path (find-if (lambda (path) (string-match (concat "^" (regexp-quote path)) file))
'("app/models/" "app/controllers/" "app/helpers/" "lib/"
"test/models/helpers/" "test/models/" "test/controllers/" ))))
(when path
(rails-refactoring:camelize
(replace-regexp-in-string path "" (replace-regexp-in-string "\\.rb$" "" file))))))
(assert (string= "FooBar" (rails-refactoring:class-from-file "app/models/foo_bar.rb")))
(assert (string= "Foo::BarController" (rails-refactoring:class-from-file "app/controllers/foo/bar_controller.rb")))
(assert (string= "Foo::Bar::Quux" (rails-refactoring:class-from-file "lib/foo/bar/quux.rb")))
(assert (string= "FooTest" (rails-refactoring:class-from-file "test/models/foo_test.rb")))
(assert (string= "FooHelperTest" (rails-refactoring:class-from-file "test/models/helpers/foo_helper_test.rb")))
(defun rails-refactoring:legal-class-name-p (name)
"Return t when NAME is a valid Ruby class name."
(let ((case-fold-search nil))
(not (null (string-match "^\\([A-Z][A-Za-z0-9]*\\)\\(::[A-Z][A-Za-z0-9]*\\)*$" name)))))
(assert (rails-refactoring:legal-class-name-p "FooBar"))
(assert (rails-refactoring:legal-class-name-p "Foo::Bar"))
(assert (not (rails-refactoring:legal-class-name-p "Foo Bar")))
(assert (not (rails-refactoring:legal-class-name-p "foo")))
(defun rails-refactoring:read-string (prompt &optional
pred error
initial-input
history
default-value
inherit-input-method)
"Prompt for string in minibuffer like `read-string'. The
second argument PRED determines is the input is valid. If the
input is invalid and the third argument ERROR is given that
message is displayed."
(let (result)
(while (not result)
(let ((answer (read-string prompt initial-input history
default-value inherit-input-method)))
(if (or (null pred) (funcall pred answer))
(setq result answer)
(progn
(message (or error "invalid input") answer)
(sleep-for 1)))))
result))
(defun rails-refactoring:read-class-name (prompt &optional initial-input history default-value inherit-input-method)
"Prompt for a Ruby class name in minibuffer like `read-string'.
Only a legal class name is accepted."
(rails-refactoring:read-string prompt
'rails-refactoring:legal-class-name-p
"`%s' is not a valid Ruby class name"
initial-input history default-value
inherit-input-method))
;; Refactoring methods
(defun rails-refactoring:query-replace (from to &optional dirs)
"Replace some occurrences of FROM to TO in all the project
source files. If DIRS argument is given the files are limited to
these directories.
The function returns nil when the user cancelled or an alist of
the form (FILE . SITES) where SITES are the replacement sites as
returned by `perform-replace' per FILE."
(interactive "sFrom: \nsTo: ")
(let ((result nil)
(keep-going t)
(files (if dirs
(delete-if-not (lambda (file)
(find-if (lambda (dir)
(string-match (concat "^" (regexp-quote dir)) file))
dirs))
(rails-refactoring:source-files))
(rails-refactoring:source-files)))
(case-fold-search (and case-fold-search (string= from (downcase from))))
(original-buffer (current-buffer)))
(while (and keep-going files)
(let* ((file (car files))
(flymake-start-syntax-check-on-find-file nil)
(existing-buffer (get-file-buffer (rails-core:file file))))
(set-buffer (or existing-buffer (find-file-noselect (rails-core:file file))))
(message ".. %s .." file)
(goto-char (point-min))
(if (re-search-forward from nil t)
(progn
(switch-to-buffer (current-buffer))
(goto-char (point-min))
(let ((sites (perform-replace from to t t nil)))
(if sites
(push (cons file sites) result)
(setq keep-going nil))))
(unless existing-buffer (kill-buffer nil)))
(set-buffer original-buffer))
(setq files (cdr files)))
(and keep-going result)))
(defun rails-refactoring:rename-class (from-file to-file)
"Rename class given their file names; FROM-FILE to TO-FILE.
The file is renamed and the class or module definition is
modified."
(interactive (list (completing-read "From: " (rails-refactoring:class-files) nil t)
(read-string "To: ")))
(rails-refactoring:disclaim "Rename class")
(let ((from (rails-refactoring:class-from-file from-file))
(to (rails-refactoring:class-from-file to-file)))
(message "rename file from %s to %s" from-file to-file)
(rename-file (rails-core:file from-file) (rails-core:file to-file))
(let ((buffer (get-file-buffer (rails-core:file from-file))))
(when buffer (kill-buffer buffer)))
(message "change definition from %s to %s" from to)
(let ((buffer (get-file-buffer (rails-core:file to-file))))
(when buffer (kill-buffer buffer)))
(find-file (rails-core:file to-file))
(goto-char (point-min))
(while (re-search-forward (concat "^\\(class\\|module\\)[ \t]+" from) nil t)
(replace-match (concat "\\1 " to) nil nil))
(save-buffer))
(when (interactive-p)
(ignore-errors (rails-refactoring:query-replace (concat "\\b" (regexp-quote from)) to))
(save-some-buffers)))
(defun rails-refactoring:rename-layout (from to)
"Rename all named layouts from FROM to TO."
(interactive (list (completing-read "From: " (rails-refactoring:layouts) nil t)
(read-string "To: ")))
(rails-refactoring:disclaim "Rename layout")
(mapc (lambda (from-file)
(let ((to-file (concat to (substring from-file (length from)))))
(message "renaming layout from %s to %s" from-file to-file)
(rename-file (rails-core:file (format "app/views/layouts/%s" from-file))
(rails-core:file (format "app/views/layouts/%s" to-file)))))
(delete-if-not (lambda (file) (string-match (concat "^" (regexp-quote from) "\\.") file))
(directory-files-recursive (rails-core:file "app/views/layouts"))))
(when (interactive-p)
(let ((case-fold-search nil))
(ignore-errors (rails-refactoring:query-replace from to)))
(save-some-buffers)))
(defun rails-refactoring:rename-controller (from to)
"Rename controller from FROM to TO. All appropriate files and
directories are renamed and `rails-refactoring:query-replace' is
started to do the rest."
(interactive (list (completing-read "Rename controller: "
(mapcar (lambda (name) (remove-postfix name "Controller"))
(rails-core:controllers))
nil t
(ignore-errors (rails-core:current-controller)))
(rails-refactoring:read-class-name "To: ")))
(rails-refactoring:disclaim "Rename controller")
(mapc (lambda (func)
(when (file-exists-p (rails-core:file (funcall func from)))
(rails-refactoring:rename-class (funcall func from)
(funcall func to))))
'(rails-core:controller-file rails-core:controllers-test-file
rails-core:helper-file rails-core:helper-test-file))
(when (file-exists-p (rails-core:file (rails-core:views-dir from)))
(let ((from-dir (rails-core:views-dir from))
(to-dir (rails-core:views-dir to)))
(message "rename view directory from %s to %s" from-dir to-dir)
(rename-file (rails-core:file from-dir) (rails-core:file to-dir))))
(rails-refactoring:rename-layout (rails-refactoring:decamelize from)
(rails-refactoring:decamelize to))
(when (interactive-p)
(let ((case-fold-search nil))
(rails-refactoring:query-replace (concat "\\b" (regexp-quote from))
to
'("app/controllers/"
"app/helpers/"
"app/views/"
"test/controllers/"))
(rails-refactoring:query-replace (concat "\\b\\(:?\\)" (regexp-quote (rails-refactoring:decamelize from)) "\\b")
(concat "\\1" (rails-refactoring:decamelize to))
'("app/controllers/"
"app/helpers/"
"app/views/"
"test/controllers/"
"config/routes.rb")))
(save-some-buffers)))
(defun rails-refactoring:rename-model (from to)
"Rename model from FROM to TO. All appropriate files are
renamed and `rails-refactoring:query-replace' is started to do
the rest."
(interactive (list (completing-read "Rename model: "
(rails-core:models)
nil t
(ignore-errors (rails-core:current-model)))
(rails-refactoring:read-class-name "To: ")))
(rails-refactoring:disclaim "Rename model")
(mapc (lambda (func)
(when (file-exists-p (rails-core:file (funcall func from)))
(rails-refactoring:rename-class (funcall func from)
(funcall func to))))
'(rails-core:model-file rails-core:models-test-file))
(mapc (lambda (func)
(when (file-exists-p (rails-core:file (funcall func from)))
(rename-file (rails-core:file (funcall func from))
(rails-core:file (funcall func to)))))
'(rails-core:fixture-file))
(when (interactive-p)
(let ((case-fold-search nil))
(mapc (lambda (args)
(let ((from (car args))
(to (cadr args)))
(rails-refactoring:query-replace from to '("app/" "test/"))))
(mapcar (lambda (func)
(list (concat "\\b\\(:?\\)" (regexp-quote (funcall func from)))
(concat "\\1" (funcall func to))))
(list 'identity
'pluralize-string
'rails-refactoring:decamelize
(lambda (val) (rails-refactoring:decamelize (pluralize-string val))))))))
(let ((migration-name (concat "Rename" (pluralize-string from) "To" (pluralize-string to))))
(rails-refactoring:enqueue-migration-edit migration-name
'rails-refactoring:rename-table-migration-edit from to)
(rails-script:generate-migration migration-name)))
(defun rails-refactoring:rename-table-migration-edit (from to)
"Add rename table code to migration in current buffer."
(let ((from-table (rails-refactoring:decamelize (pluralize-string from)))
(to-table (rails-refactoring:decamelize (pluralize-string to))))
(goto-char (point-min))
(re-search-forward "\\bdef self.up")
(end-of-line)
(insert "\n")
(indent-according-to-mode)
(insert (format "rename_table :%s, :%s" from-table to-table))
(re-search-forward "\\bdef self.down")
(insert "\n")
(indent-according-to-mode)
(insert (format "rename_table :%s, :%s" to-table from-table))
(save-buffer)))
;; Setup hooks
(defvar rails-refactoring:after-rails-script-jobs nil
"Queue of jobs to be ran via
`rails-script:after-hook-internal'. Jobs are ran by
`rails-refactoring:run-after-rails-script-jobs' and dequeued when
they return non nil.")
(defun rails-refactoring:run-after-rails-script-jobs ()
"Run pending `rails-script:after-hook-internal' refactoring
jobs"
(setq rails-refactoring:after-rails-script-jobs
(delete-if (lambda (spec)
(funcall (car spec) (cadr spec) (cddr spec)))
rails-refactoring:after-rails-script-jobs)))
(add-hook 'rails-script:after-hook-internal 'rails-refactoring:run-after-rails-script-jobs)
(defmacro rails-refactoring:enqueue-migration-edit (migration function &rest arguments)
"Enqueue migration edit to be run when
`rails-script:generation-migration' is finished and migration
file is available."
(let ((migration-file (gensym)))
`(push (cons (lambda (migration args)
(let ((,migration-file (rails-core:migration-file migration)))
(when ,migration-file
(with-current-buffer (find-file-noselect (rails-core:file ,migration-file))
(apply ,function args))
t)))
(list ,migration ,@arguments))
rails-refactoring:after-rails-script-jobs)))
;; Tie up in UI
(require 'rails-ui)
(define-keys rails-minor-mode-map
((rails-key "\C-c R q") 'rails-refactoring:query-replace)
((rails-key "\C-c R m") 'rails-refactoring:rename-model)
((rails-key "\C-c R c") 'rails-refactoring:rename-controller)
((rails-key "\C-c R l") 'rails-refactoring:rename-layout))
(provide 'rails-refactoring)