js2-mode-expansions.el (2189B)
1 ;;; js2-mode-expansions.el --- Additional expansions for js2-mode -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2011-2023 Free Software Foundation, Inc 4 5 ;; Author: Magnar Sveen <magnars@gmail.com> 6 ;; Keywords: marking region 7 8 ;; This program is free software; you can redistribute it and/or modify 9 ;; it under the terms of the GNU General Public License as published by 10 ;; the Free Software Foundation, either version 3 of the License, or 11 ;; (at your option) any later version. 12 13 ;; This program is distributed in the hope that it will be useful, 14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 ;; GNU General Public License for more details. 17 18 ;; You should have received a copy of the GNU General Public License 19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>. 20 21 ;;; Commentary: 22 23 ;; Extra expansions specifically for js2-mode, since it has 24 ;; a semantic parser. 25 ;; 26 ;; Feel free to contribute any other expansions for JavaScript at 27 ;; 28 ;; https://github.com/magnars/expand-region.el 29 30 ;;; Code: 31 32 (require 'expand-region-core) 33 (declare-function js2-node-parent-stmt "js2-mode") 34 (declare-function js2-node-at-point "js2-mode") 35 (declare-function js2-node-abs-pos "js2-mode") 36 (declare-function js2-node-len "js2-mode") 37 38 (defun js2-mark-parent-statement () 39 (interactive) 40 (let* ((parent-statement (if (not (er/looking-back-exact ";")) 41 (js2-node-parent-stmt (js2-node-at-point)) 42 (forward-char -1) 43 (js2-node-at-point))) 44 (beg (js2-node-abs-pos parent-statement)) 45 (end (+ beg (js2-node-len parent-statement)))) 46 (goto-char beg) 47 (set-mark end))) 48 49 (defun er/add-js2-mode-expansions () 50 "Adds expansions for buffers in js2-mode" 51 (set (make-local-variable 'er/try-expand-list) (append 52 er/try-expand-list 53 '(js2-mark-parent-statement)))) 54 55 (er/enable-mode-expansions 'js2-mode #'er/add-js2-mode-expansions) 56 57 (provide 'js2-mode-expansions) 58 59 ;; js2-mode-expansions.el ends here