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.

89 lines
3.1 KiB

  1. ;;; js2r-iife.el --- IIFE wrapping functions for js2-refactor -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2012-2014 Magnar Sveen
  3. ;; Copyright (C) 2015-2016 Magnar Sveen and Nicolas Petton
  4. ;; Author: Magnar Sveen <magnars@gmail.com>,
  5. ;; Nicolas Petton <nicolas@petton.fr>
  6. ;; Keywords: conveniences
  7. ;; This program is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; This program is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Code:
  18. (defvar js2r--iife-regexp "^(function (")
  19. (defun js2r-wrap-buffer-in-iife ()
  20. "Wrap the entire buffer in an immediately invoked function expression"
  21. (interactive)
  22. (save-excursion
  23. (when (ignore-errors (search-backward-regexp js2r--iife-regexp))
  24. (error "Buffer already contains an immediately invoked function expression."))
  25. (goto-char (point-min))
  26. (insert "(function () {\n")
  27. (when js2r-use-strict (insert "\"use strict\";\n"))
  28. (insert "\n")
  29. (goto-char (point-max))
  30. (insert "\n")
  31. (delete-blank-lines)
  32. (insert "\n}());")
  33. (indent-region (point-min) (point-max))))
  34. (defun js2r--selected-name-positions ()
  35. "Returns the (beginning . end) of the name at cursor, or active region."
  36. (let ((current-node (js2-node-at-point))
  37. beg end)
  38. (unless (js2-name-node-p current-node)
  39. (setq current-node (js2-node-at-point (- (point) 1))))
  40. (if (not (and current-node (js2-name-node-p current-node)))
  41. (error "Point is not on an identifier."))
  42. (if (use-region-p)
  43. (cons (region-beginning) (region-end))
  44. (progn
  45. (setq end (+ (js2-node-abs-pos current-node)
  46. (js2-node-len current-node)))
  47. (skip-syntax-backward ".w_")
  48. (cons (point) end)))))
  49. (defun js2r-inject-global-in-iife ()
  50. "Create shortcut for marked global by injecting it in the wrapping IIFE"
  51. (interactive)
  52. (js2r--guard)
  53. (save-excursion
  54. (let* ((name-pos (js2r--selected-name-positions))
  55. (name-beg (car name-pos))
  56. (name-end (cdr name-pos))
  57. (name (buffer-substring-no-properties name-beg name-end))
  58. (short (buster--global-shortcut name))
  59. beg end)
  60. (unless (search-backward-regexp js2r--iife-regexp)
  61. (error "No immediately invoked function expression found."))
  62. (deactivate-mark)
  63. (forward-char 11)
  64. (insert short)
  65. (unless (looking-at ")")
  66. (insert ", "))
  67. (search-forward "{")
  68. (setq beg (point))
  69. (backward-char)
  70. (forward-list)
  71. (forward-char)
  72. (setq end (point))
  73. (insert name)
  74. (unless (looking-at ")")
  75. (insert ", "))
  76. (replace-string name short t beg end))))
  77. (provide 'js2r-iife)
  78. ;;; js2-iife.el ends here