Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.3 KiB

  1. ;;; js2r-wrapping.el --- Helper functions for wrapping/unwrapping -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017 Nicolas Petton
  3. ;; Author: Nicolas Petton
  4. ;; This program is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;; This program is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. ;; GNU General Public License for more details.
  12. ;; You should have received a copy of the GNU General Public License
  13. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ;;; Commentary:
  15. ;;
  16. ;;; Code:
  17. (require 'yasnippet)
  18. (defvar js2r--space-str " \t\n")
  19. (defun js2r--skip-region-whitespace ()
  20. (let ((p-first (< (point) (mark))))
  21. (unless p-first
  22. (exchange-point-and-mark))
  23. (skip-chars-forward js2r--space-str)
  24. (exchange-point-and-mark)
  25. (skip-chars-backward js2r--space-str)
  26. (when p-first
  27. (exchange-point-and-mark))))
  28. (defun js2r-unwrap ()
  29. (interactive)
  30. (js2r--guard)
  31. (let (beg end)
  32. (if (use-region-p)
  33. (progn
  34. (js2r--skip-region-whitespace)
  35. (setq beg (min (point) (mark)))
  36. (setq end (max (point) (mark))))
  37. (let ((stmt (js2-node-parent-stmt (js2-node-at-point))))
  38. (setq beg (js2-node-abs-pos stmt))
  39. (setq end (js2-node-abs-end stmt))))
  40. (let* ((ancestor (js2-node-parent-stmt
  41. (js2r--first-common-ancestor-in-region beg end)))
  42. (abeg (js2-node-abs-pos ancestor))
  43. (aend (js2-node-abs-end ancestor)))
  44. (save-excursion
  45. (goto-char end)
  46. (delete-char (- aend end))
  47. (goto-char abeg)
  48. (delete-char (- beg abeg)))
  49. (indent-region (point-min) (point-max)))))
  50. (defun js2r-wrap-in-for-loop (beg end)
  51. (interactive "r")
  52. (js2r--skip-region-whitespace)
  53. (setq beg (min (point) (mark)))
  54. (setq end (max (point) (mark)))
  55. (let ((yas-wrap-around-region t))
  56. (yas-expand-snippet "for (var i = 0, l = ${1:length}; i < l; i++) {\n$0\n}"
  57. beg end)))
  58. (provide 'js2r-wrapping)
  59. ;;; js2r-wrapping.el ends here