1 ;;; js2-imenu-extras.el --- Imenu support for additional constructs
3 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
4 ;; Keywords: languages, javascript, imenu
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs 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.
13 ;; GNU Emacs 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.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;; This package adds Imenu support for additional framework constructs and
24 ;; general patterns to `js2-mode'.
28 ;; (eval-after-load 'js2-mode
29 ;; (require 'js2-imenu-extras)
30 ;; (js2-imenu-extras-setup))
32 ;; To customize how it works:
33 ;; M-x customize-group RET js2-imenu RET
40 (defconst js2-imenu-extension-styles
42 :call-re "\\_<\\(?:jQuery\\|\\$\\|_\\)\\.extend\\s-*("
43 :recorder js2-imenu-record-jquery-extend)
46 :call-re "^\\s-*\\(?:jQuery\\|\\$\\)\\.widget\\s-*("
47 :recorder js2-imenu-record-string-declare)
50 :call-re "^\\s-*dojo.declare\\s-*("
51 :recorder js2-imenu-record-string-declare)
54 :call-re ,(concat "\\_<" js2-mode-identifier-re "\\.extend\\s-*(")
55 :recorder js2-imenu-record-backbone-extend))
56 "List of JavaScript class definition or extension styles.
58 :framework is a valid value in `js2-imenu-enabled-frameworks.
60 :call-re is a regular expression that has no capturing groups.
62 :recorder is a function name that will be called when the regular
63 expression matches some text in the buffer. When it's called, point will be
64 at the end of the match. The function must keep the point position.")
66 (defconst js2-imenu-available-frameworks
67 (mapcar (lambda (style) (plist-get style :framework)) js2-imenu-extension-styles)
68 "List of available JavaScript framework symbols.")
70 (defcustom js2-imenu-enabled-frameworks js2-imenu-available-frameworks
71 "Frameworks to be recognized by `js2-mode'."
72 :type (cons 'set (mapcar (lambda (x) (list 'const x))
73 js2-imenu-available-frameworks))
76 (defcustom js2-imenu-show-other-functions t
77 "Non-nil to show functions not recognized by other mechanisms,
78 in a shared namespace."
82 (defcustom js2-imenu-other-functions-ns "?"
83 "Namespace name to use for other functions."
87 (defcustom js2-imenu-show-module-pattern t
88 "Non-nil to recognize the module pattern:
90 var foobs = (function(a) {
91 return {fib: function() {}, fub: function() {}};
94 We record the returned hash as belonging to the named module, and
95 prefix any functions defined inside the IIFE with the module name."
100 (defun js2-imenu-extras-setup ()
101 (when js2-imenu-enabled-frameworks
102 (add-to-list 'js2-post-parse-callbacks 'js2-imenu-record-declarations t))
103 (when (or js2-imenu-show-other-functions js2-imenu-show-module-pattern)
104 (add-to-list 'js2-post-parse-callbacks 'js2-imenu-walk-ast t)))
106 (declare (special root))
108 (defun js2-imenu-record-declarations ()
109 (let* ((styles (loop for style in js2-imenu-extension-styles
110 when (memq (plist-get style :framework)
111 js2-imenu-enabled-frameworks)
113 (re (mapconcat (lambda (style)
114 (concat "\\(" (plist-get style :call-re) "\\)"))
116 ;; Dynamic scoping. Ew.
118 (goto-char (point-min))
119 (while (js-re-search-forward re nil t)
120 (loop for i from 0 to (1- (length styles))
121 when (match-beginning (1+ i))
122 return (funcall (plist-get (nth i styles) :recorder))))))
124 (defun js2-imenu-record-jquery-extend ()
125 (let ((pred (lambda (subject)
127 (js2-prop-get-node-p subject)
128 (string= (js2-name-node-name (js2-prop-get-node-right subject))
130 (js2-imenu-record-extend-first-arg (1- (point)) pred
131 'js2-compute-nested-prop-get)))
133 (defun js2-imenu-record-string-declare ()
134 (js2-imenu-record-extend-first-arg
135 (1- (point)) 'js2-string-node-p
136 (lambda (node) (split-string (js2-string-node-value node) "\\." t))))
138 (defun js2-imenu-record-extend-first-arg (point pred qname-fn)
139 (let* ((node (js2-node-at-point point))
140 (args (js2-call-node-args node))
141 (subject (first args)))
142 (when (funcall pred subject)
143 (loop for arg in (cdr args)
144 when (js2-object-node-p arg)
145 do (js2-record-object-literal
146 arg (funcall qname-fn subject) (js2-node-abs-pos arg))))))
148 (defun js2-imenu-record-backbone-extend ()
149 (let* ((node (js2-node-at-point (1- (point))))
150 (args (js2-call-node-args node))
151 (methods (first args))
152 (parent (js2-node-parent node)))
153 (when (js2-object-node-p methods)
154 (let ((subject (cond ((js2-var-init-node-p parent)
155 (js2-var-init-node-target parent))
156 ((js2-assign-node-p parent)
157 (js2-assign-node-left parent)))))
159 (js2-record-object-literal methods
160 (js2-compute-nested-prop-get subject)
161 (js2-node-abs-pos methods)))))))
163 (defun js2-imenu-walk-ast ()
169 ((and js2-imenu-show-other-functions
170 (js2-object-prop-node-p node))
171 (js2-imenu-record-orphan-function node))
172 ((and js2-imenu-show-module-pattern
173 (js2-assign-node-p node))
174 (js2-imenu-record-module-pattern node)))
177 (defun js2-imenu-record-orphan-function (node)
178 "Record orphan function when it's the value of NODE.
179 NODE must be `js2-object-prop-node'."
180 (when (js2-function-node-p (js2-object-prop-node-right node))
181 (let ((fn-node (js2-object-prop-node-right node)))
182 (unless (and js2-imenu-function-map
183 (gethash fn-node js2-imenu-function-map))
184 (let ((key-node (js2-object-prop-node-left node)))
185 (js2-record-imenu-entry fn-node
186 (list js2-imenu-other-functions-ns
187 (js2-prop-node-name key-node))
188 (js2-node-abs-pos key-node)))))))
190 (defun js2-imenu-record-module-pattern (node)
191 "Recognize and record module pattern use instance.
192 NODE must be `js2-assign-node'."
193 (let ((init (js2-assign-node-right node)))
194 (when (js2-call-node-p init)
195 (let ((target (js2-assign-node-left node))
196 (callt (js2-call-node-target init)))
197 ;; Just basic call form: (function() {...})();
198 ;; TODO: Handle variations without duplicating `js2-wrapper-function-p'?
199 (when (and (js2-paren-node-p callt)
200 (js2-function-node-p (js2-paren-node-expr callt)))
201 (let* ((fn (js2-paren-node-expr callt))
202 (blk (js2-function-node-body fn))
203 (ret (car (last (js2-block-node-kids blk)))))
204 (when (and (js2-return-node-p ret)
205 (js2-object-node-p (js2-return-node-retval ret)))
206 ;; TODO: Map function names when revealing module pattern is used.
207 (let ((retval (js2-return-node-retval ret))
208 (target-qname (js2-compute-nested-prop-get target)))
209 (js2-record-object-literal retval target-qname
210 (js2-node-abs-pos retval))
211 (js2-record-imenu-entry fn target-qname
212 (js2-node-abs-pos target))))))))))
214 (provide 'js2-imenu-extras)