blob: 9e437807396099db019b0ebc8f0d1a51bd78c1e9 (
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
|
;;; js2r-wrapping.el --- Helper functions for wrapping/unwrapping -*- lexical-binding: t; -*-
;; Copyright (C) 2017 Nicolas Petton
;; Author: Nicolas Petton
;; 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/>.
;;; Commentary:
;;
;;; Code:
(require 'yasnippet)
(require 'js2r-helpers)
(defvar js2r--space-str " \t\n")
(defun js2r--skip-region-whitespace ()
(let ((p-first (< (point) (mark))))
(unless p-first
(exchange-point-and-mark))
(skip-chars-forward js2r--space-str)
(exchange-point-and-mark)
(skip-chars-backward js2r--space-str)
(when p-first
(exchange-point-and-mark))))
(defun js2r-unwrap ()
(interactive)
(js2r--guard)
(js2r--wait-for-parse
(let (beg end)
(if (use-region-p)
(progn
(js2r--skip-region-whitespace)
(setq beg (min (point) (mark)))
(setq end (max (point) (mark))))
(let ((stmt (js2-node-parent-stmt (js2-node-at-point))))
(setq beg (js2-node-abs-pos stmt))
(setq end (js2-node-abs-end stmt))))
(let* ((ancestor (js2-node-parent-stmt
(js2r--first-common-ancestor-in-region beg end)))
(abeg (js2-node-abs-pos ancestor))
(aend (js2-node-abs-end ancestor)))
(save-excursion
(goto-char end)
(delete-char (- aend end))
(goto-char abeg)
(delete-char (- beg abeg)))
(indent-region (point-min) (point-max))))))
(defun js2r-wrap-in-for-loop (beg end)
(interactive "r")
(js2r--skip-region-whitespace)
(setq beg (min (point) (mark)))
(setq end (max (point) (mark)))
(let ((yas-wrap-around-region t))
(yas-expand-snippet "for (var i = 0, l = ${1:length}; i < l; i++) {\n$0\n}"
beg end)))
(provide 'js2r-wrapping)
;;; js2r-wrapping.el ends here
|