forked from js-emacs/js2-refactor.el
-
Notifications
You must be signed in to change notification settings - Fork 0
/
js2r-iife.el
64 lines (60 loc) · 2.17 KB
/
js2r-iife.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
(defvar js2r--iife-regexp "^(function (")
(defun js2r-wrap-buffer-in-iife ()
"Wrap the entire buffer in an immediately invoked function expression"
(interactive)
(save-excursion
(when (ignore-errors (search-backward-regexp js2r--iife-regexp))
(error "Buffer already contains an immediately invoked function expression."))
(goto-char (point-min))
(insert "(function () {\n")
(when js2r-use-strict (insert "\"use strict\";\n"))
(insert "\n")
(goto-char (point-max))
(insert "\n")
(delete-blank-lines)
(insert "\n}());")
(indent-region (point-min) (point-max))))
(defun js2r--selected-name-positions ()
"Returns the (beginning . end) of the name at cursor, or active region."
(let ((current-node (js2-node-at-point))
beg end)
(unless (js2-name-node-p current-node)
(setq current-node (js2-node-at-point (- (point) 1))))
(if (not (and current-node (js2-name-node-p current-node)))
(error "Point is not on an identifier."))
(if (use-region-p)
(cons (region-beginning) (region-end))
(progn
(setq end (+ (js2-node-abs-pos current-node)
(js2-node-len current-node)))
(skip-syntax-backward ".w_")
(cons (point) end)))))
(defun js2r-inject-global-in-iife ()
"Create shortcut for marked global by injecting it in the wrapping IIFE"
(interactive)
(js2r--guard)
(save-excursion
(let* ((name-pos (js2r--selected-name-positions))
(name-beg (car name-pos))
(name-end (cdr name-pos))
(name (buffer-substring-no-properties name-beg name-end))
(short (buster--global-shortcut name))
beg end)
(unless (search-backward-regexp js2r--iife-regexp)
(error "No immediately invoked function expression found."))
(deactivate-mark)
(forward-char 11)
(insert short)
(unless (looking-at ")")
(insert ", "))
(search-forward "{")
(setq beg (point))
(backward-char)
(forward-list)
(forward-char)
(setq end (point))
(insert name)
(unless (looking-at ")")
(insert ", "))
(replace-string name short t beg end))))
(provide 'js2r-iife)