summaryrefslogtreecommitdiff
path: root/elpa/js2-refactor-20210306.2003/js2r-vars.el
blob: fc27eae06fb94206ccc2187b724e35e3f5178e16 (plain)
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
;;; js2r-vars.el --- Variable declaration manipulation functions for js2-refactor    -*- lexical-binding: t; -*-

;; Copyright (C) 2012-2014 Magnar Sveen
;; Copyright (C) 2015-2016 Magnar Sveen and Nicolas Petton

;; Author: Magnar Sveen <magnars@gmail.com>,
;;         Nicolas Petton <nicolas@petton.fr>
;; Keywords: conveniences

;; 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/>.

;;; Code:

(require 'multiple-cursors-core)
(require 'dash)

(require 'js2r-helpers)

;; Helpers

(defun js2r--name-node-at-point (&optional pos)
  (setq pos (or pos (point)))
  (let ((current-node (js2-node-at-point pos)))
    (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.")
      current-node)))

(defun js2r--local-name-node-at-point (&optional pos)
  (setq pos (or pos (point)))
  (let ((current-node (js2r--name-node-at-point pos)))
    (unless (js2r--local-name-node-p current-node)
      (error "Point is not on a local identifier"))
    current-node))

(defun js2r--local-name-node-p (node)
  (let ((parent (js2-node-parent node)))
    (and parent (js2-name-node-p node)
         ;; is not foo in { foo: 1 }
         (not (and (js2-object-prop-node-p parent)
                   (eq node (js2-object-prop-node-left parent))
                   ;; could be { foo } though, in which case the node
                   ;; is parent's both left and right.
                   (not (eq node (js2-object-prop-node-right parent)))))
         ;; is not foo in x.foo
         (not (and (js2-prop-get-node-p parent)
                   (eq node (js2-prop-get-node-right parent))))
         ;; is not foo in import { foo as bar } from ...
         ;; could be bar, though.
         (not (and (js2-export-binding-node-p parent)
                   (eq node (js2-export-binding-node-extern-name parent))
                   (not (eq node (js2-export-binding-node-local-name parent))))))))

(defun js2r--name-node-defining-scope (name-node)
  (unless (js2r--local-name-node-p name-node)
    (error "Node is not on a local identifier"))
  (js2-get-defining-scope
   (js2-node-get-enclosing-scope name-node)
   (js2-name-node-name name-node)))

(defun js2r--local-usages-of-name-node (name-node)
  (unless (js2r--local-name-node-p name-node)
    (error "Node is not on a local identifier"))
  (let* ((name (js2-name-node-name name-node))
         (scope (js2r--name-node-defining-scope name-node))
         (result nil))
    (js2-visit-ast
     scope
     (lambda (node end-p)
       (when (and (not end-p)
                  (js2r--local-name-node-p node)
                  (string= name (js2-name-node-name node))
                  (eq scope (js2r--name-node-defining-scope node)))
         (add-to-list 'result node))
       t))
    result))

(defun js2r--local-var-positions (name-node)
  (-map 'js2-node-abs-pos (js2r--local-usages-of-name-node name-node)))

(defun js2r--var-defining-node (var-node)
  (unless (js2r--local-name-node-p var-node)
    (error "Node is not on a local identifier"))
  (let* ((name (js2-name-node-name var-node))
         (scope (js2r--name-node-defining-scope var-node)))
    (js2-symbol-ast-node
     (js2-scope-get-symbol scope name))))


;; Add to jslint globals annotation

(defun current-line-contents ()
  "Find the contents of the current line, minus indentation."
  (buffer-substring (save-excursion (back-to-indentation) (point))
                    (save-excursion (end-of-line) (point))))

(require 'thingatpt)

(defun js2r-add-to-globals-annotation ()
  (interactive)
  (let ((var (thing-at-point 'symbol)))
    (save-excursion
      (beginning-of-buffer)
      (when (not (string-match "^/\\* *global " (current-line-contents)))
        (newline)
        (forward-line -1)
        (insert "/* global */")
        (newline)
        (forward-line -1))
      (while (not (string-match "*/" (current-line-contents)))
        (forward-line))
      (end-of-line)
      (delete-char -2)
      (unless (looking-back "global ")
        (while (looking-back " ")
          (delete-char -1))
        (insert ", "))
      (insert (concat var " */")))))


;; Rename variable

;;;###autoload
(defun js2r-rename-var ()
  "Renames the variable on point and all occurrences in its lexical scope."
  (interactive)
  (js2r--guard)
  (js2r--wait-for-parse
   (let* ((current-node (js2r--local-name-node-at-point))
	  (len (js2-node-len current-node))
	  (current-start (js2-node-abs-pos current-node))
	  (current-end (+ current-start len))
	  (delta (- (point) current-start)))
     (save-excursion
       (mapc (lambda (beg)
	       (when (not (= beg current-start))
		 (goto-char (+ beg delta))
		 (set-mark (+ beg len))
		 (mc/create-fake-cursor-at-point)))
	     (js2r--local-var-positions current-node)))
     (push-mark current-end)
     (activate-mark))
   (mc/maybe-multiple-cursors-mode)))

(add-to-list 'mc--default-cmds-to-run-once 'js2r-rename-var)

;; Change local variable to use this. instead

(defun js2r-var-to-this ()
  "Changes the variable on point to use this.var instead."
  (interactive)
  (js2r--guard)
  (js2r--wait-for-parse
   (save-excursion
     (let ((node (js2-node-at-point)))
       (when (js2-var-decl-node-p node)
	 (let ((kids (js2-var-decl-node-kids node)))
	   (when (cdr kids)
	     (error "Currently does not support converting multivar statements."))
	   (goto-char (js2-node-abs-pos (car kids))))))
     (--each (js2r--local-var-positions (js2r--local-name-node-at-point))
       (goto-char it)
       (cond ((looking-back "var ") (delete-char -4))
	     ((looking-back "let ") (delete-char -4))
	     ((looking-back "const ") (delete-char -6)))
       (insert "this.")))))

;; Inline var

(defun js2r-inline-var ()
  (interactive)
  (js2r--guard)
  (js2r--wait-for-parse
   (save-excursion
     (let* ((current-node (js2r--local-name-node-at-point))
	    (definer (js2r--var-defining-node current-node))
	    (definer-start (js2-node-abs-pos definer))
	    (var-init (js2-node-parent definer))
	    (initializer (js2-var-init-node-initializer
			  var-init)))
       (unless initializer
	 (error "Var is not initialized when defined."))
       (let* ((var-len (js2-node-len current-node))
	      (init-beg (js2-node-abs-pos initializer))
	      (init-end (+ init-beg (js2-node-len initializer)))
	      (var-init-beg (copy-marker (js2-node-abs-pos var-init)))
	      (var-init-end (copy-marker (+ var-init-beg (js2-node-len var-init))))
	      (contents (buffer-substring init-beg init-end)))
	 (mapc (lambda (beg)
		 (when (not (= beg definer-start))
		   (goto-char beg)
		   (delete-char var-len)
		   (insert contents)))
	       (js2r--local-var-positions current-node))
	 (js2r--delete-var-init var-init-beg var-init-end))))))


(defun js2r--was-single-var ()
  (save-excursion
    (goto-char (point-at-bol))
    (or (looking-at "^[[:space:]]*\\(var\\|const\\|let\\)[[:space:]]?;?$")
	(looking-at "^[[:space:]]*,[[:space:]]*$"))))

(defun js2r--was-starting-var ()
  (or (looking-back "var ")
      (looking-back "const ")
      (looking-back "let ")))

(defun js2r--was-ending-var ()
  (looking-at ";"))

(defun js2r--delete-var-init (beg end)
  (goto-char beg)
  (delete-char (- end beg))
  (cond
   ((js2r--was-single-var)
    (delete-region (point-at-bol) (point-at-eol))
    (delete-blank-lines))

   ((js2r--was-starting-var)
    (delete-char 1)
    (if (looking-at " ")
        (delete-char 1)
      (join-line -1)))

   ((js2r--was-ending-var)
    (if (looking-back ", ")
        (delete-char -1)
      (join-line)
      (delete-char 1))
    (delete-char -1))

   (t (delete-char 2))))

;; two cases
;;   - it's the only var -> remove the line
;;   - there are several vars -> remove the node then clean up commas


;; Extract variable

(defun js2r--start-of-parent-stmt ()
  (js2-node-abs-pos (js2r--closest-stmt-node)))

(defun js2r--object-literal-key-behind (pos)
  (save-excursion
    (goto-char pos)
    (when (looking-back "\\sw: ?")
      (backward-char 2)
      (js2-name-node-name (js2r--name-node-at-point)))))

(defun js2r--line-above-is-blank ()
  (save-excursion
    (forward-line -1)
    (string= "" (current-line-contents))))

;;;###autoload
(defun js2r-extract-var ()
  (interactive)
  (js2r--guard)
  (js2r--wait-for-parse
   (let ((keyword (if js2r-prefer-let-over-var "let" "var")))
        (if (use-region-p)
            (js2r--extract-var-between (region-beginning) (region-end) keyword)
          (let ((node (js2r--closest-extractable-node)))
            (js2r--extract-var-between (js2-node-abs-pos node)
                                       (js2-node-abs-end node) keyword))))))

;;;###autoload
(defun js2r-extract-let ()
  (interactive)
  (js2r--guard)
  (js2r--wait-for-parse
   (if (use-region-p)
       (js2r--extract-var-between (region-beginning) (region-end) "let")
     (let ((node (js2r--closest-extractable-node)))
       (js2r--extract-var-between (js2-node-abs-pos node)
                                  (js2-node-abs-end node) "let")))))

;;;###autoload
(defun js2r-extract-const ()
  (interactive)
  (js2r--guard)
  (js2r--wait-for-parse
   (if (use-region-p)
       (js2r--extract-var-between (region-beginning) (region-end) "const")
     (let ((node (js2r--closest-extractable-node)))
       (js2r--extract-var-between (js2-node-abs-pos node)
                                  (js2-node-abs-end node) "const")))))

(add-to-list 'mc--default-cmds-to-run-once 'js2r-extract-var)
(add-to-list 'mc--default-cmds-to-run-once 'js2r-extract-let)
(add-to-list 'mc--default-cmds-to-run-once 'js2r-extract-const)

(defun js2r--extract-var-between (beg end keyword)
  (interactive "r")
  (unless (js2r--single-complete-expression-between-p beg end)
    (error "Can only extract single, complete expressions to var"))

  (let ((deactivate-mark nil)
        (expression (buffer-substring beg end))
        (orig-var-end (make-marker))
        (new-var-end (make-marker))
        (name (or (js2r--object-literal-key-behind beg) "name")))

    (delete-region beg end)
    (insert name)
    (set-marker orig-var-end (point))

    (goto-char (js2r--start-of-parent-stmt))
    (js2r--insert-var name keyword)
    (set-marker new-var-end (point))
    (insert " = " expression ";")
    (when (or (js2r--line-above-is-blank)
              (string-match-p "^function " expression))
      (newline))
    (newline)
    (indent-region new-var-end orig-var-end)
    (save-excursion
      (goto-char new-var-end)
      (set-mark (- (point) (length name)))
      (mc/create-fake-cursor-at-point))
    (goto-char orig-var-end)
    (set-mark (- (point) (length name)))
    (set-marker orig-var-end nil)
    (set-marker new-var-end nil))
  (mc/maybe-multiple-cursors-mode))

(defun js2r--insert-var (name keyword)
  "Insert a var definition for NAME."
  (insert (format "%s %s" keyword name)))

(defun js2r-split-var-declaration ()
  "Split a variable declaration into separate variable
declarations for each declared variable."
  (interactive)
  (js2r--guard)
  (js2r--wait-for-parse
   (save-excursion
     (let* ((declaration (or (js2r--closest #'js2-var-decl-node-p) (error "No var declaration at point.")))
	    (kids (js2-var-decl-node-kids declaration))
	    (stmt (js2-node-parent-stmt declaration))
        (tt (js2-var-decl-node-decl-type declaration))
        (keyword (cond
                  ((= tt js2-VAR) "var")
                  ((= tt js2-LET) "let")
                  ((= tt js2-CONST) "const"))))
       (goto-char (js2-node-abs-end stmt))
       (mapc (lambda (kid)
           (js2r--insert-var (js2-node-string kid) keyword)
	       (insert ";")
	       (newline)
	       (if (save-excursion
		     (goto-char (js2-node-abs-end kid))
		     (looking-at ", *\n *\n"))
		   (newline)))
	     kids)
       (delete-char -1) ;; delete final newline
       (let ((end (point)))
	 (js2r--goto-and-delete-node stmt)
	 (indent-region (point) end))))))

(provide 'js2r-vars)
;;; js2-vars.el ends here