1 ;;; js2-imenu-extras.el --- Imenu support for additional constructs
3 ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
5 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
6 ;; Keywords: languages, javascript, imenu
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; This package adds Imenu support for additional framework constructs and
26 ;; structural patterns to `js2-mode'.
30 ;; (add-hook 'js2-mode-hook 'js2-imenu-extras-mode)
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)
58 :call-re "\\_<enyo\\.kind\\s-*("
59 :recorder js2-imenu-record-enyo-kind))
60 "List of JavaScript class definition or extension styles.
62 :framework is a valid value in `js2-imenu-enabled-frameworks'.
64 :call-re is a regular expression that has no capturing groups.
66 :recorder is a function name that will be called when the regular
67 expression matches some text in the buffer. When it's called, point will be
68 at the end of the match. The function must keep the point position.")
70 (defconst js2-imenu-available-frameworks
71 (mapcar (lambda (style) (plist-get style :framework)) js2-imenu-extension-styles)
72 "List of available JavaScript framework symbols.")
74 (defcustom js2-imenu-enabled-frameworks js2-imenu-available-frameworks
75 "Frameworks to be recognized by `js2-mode'."
76 :type (cons 'set (mapcar (lambda (x) (list 'const x))
77 js2-imenu-available-frameworks))
80 (defcustom js2-imenu-show-other-functions t
81 "Non-nil to show functions not recognized by other mechanisms,
82 in a shared namespace."
86 (defcustom js2-imenu-other-functions-ns "?"
87 "Namespace name to use for other functions."
91 (defcustom js2-imenu-show-module-pattern t
92 "Non-nil to recognize the module pattern:
94 var foobs = (function(a) {
95 return {fib: function() {}, fub: function() {}};
98 We record the returned hash as belonging to the named module, and
99 prefix any functions defined inside the IIFE with the module name."
103 (defcustom js2-imenu-split-string-identifiers t
104 "When non-nil, split string identifiers on dots.
105 Currently used for jQuery widgets, Dojo and Enyo declarations."
110 (defun js2-imenu-extras-setup ()
111 (when js2-imenu-enabled-frameworks
112 (add-hook 'js2-post-parse-callbacks 'js2-imenu-record-declarations t t))
113 (when (or js2-imenu-show-other-functions js2-imenu-show-module-pattern)
114 (add-hook 'js2-post-parse-callbacks 'js2-imenu-walk-ast t t)))
116 (defun js2-imenu-extras-remove ()
117 (remove-hook 'js2-post-parse-callbacks 'js2-imenu-record-declarations t)
118 (remove-hook 'js2-post-parse-callbacks 'js2-imenu-walk-ast t))
120 (defun js2-imenu-record-declarations ()
121 (let* ((styles (loop for style in js2-imenu-extension-styles
122 when (memq (plist-get style :framework)
123 js2-imenu-enabled-frameworks)
125 (re (mapconcat (lambda (style)
126 (concat "\\(" (plist-get style :call-re) "\\)"))
128 (goto-char (point-min))
129 (while (js2-re-search-forward re nil t)
130 (loop for i from 0 to (1- (length styles))
131 when (match-beginning (1+ i))
132 return (funcall (plist-get (nth i styles) :recorder))))))
134 (defun js2-imenu-record-jquery-extend ()
135 (let ((pred (lambda (subject)
137 (js2-prop-get-node-p subject)
138 (string= (js2-name-node-name (js2-prop-get-node-right subject))
140 (js2-imenu-record-extend-first-arg (1- (point)) pred
141 'js2-compute-nested-prop-get)))
143 (defun js2-imenu-record-string-declare ()
144 (js2-imenu-record-extend-first-arg
145 (1- (point)) 'js2-string-node-p
147 (if js2-imenu-split-string-identifiers
148 (split-string (js2-string-node-value node) "\\." t)
149 (list (js2-string-node-value node))))))
151 (defun js2-imenu-record-extend-first-arg (point pred qname-fn)
152 (let* ((node (js2-node-at-point point))
153 (args (js2-call-node-args node))
154 (subject (first args)))
155 (when (funcall pred subject)
156 (loop for arg in (cdr args)
157 when (js2-object-node-p arg)
158 do (js2-record-object-literal
159 arg (funcall qname-fn subject) (js2-node-abs-pos arg))))))
161 (defun js2-imenu-record-backbone-extend ()
162 (let* ((node (js2-node-at-point (1- (point))))
163 (args (js2-call-node-args node))
164 (methods (first args))
165 (parent (js2-node-parent node)))
166 (when (js2-object-node-p methods)
167 (let ((subject (cond ((js2-var-init-node-p parent)
168 (js2-var-init-node-target parent))
169 ((js2-assign-node-p parent)
170 (js2-assign-node-left parent)))))
172 (js2-record-object-literal methods
173 (js2-compute-nested-prop-get subject)
174 (js2-node-abs-pos methods)))))))
176 (defun js2-imenu-record-enyo-kind ()
177 (let* ((node (js2-node-at-point (1- (point))))
178 (args (js2-call-node-args node))
179 (options (first args)))
180 (when (js2-object-node-p options)
182 (loop for elem in (js2-object-node-elems options)
184 (let ((key (js2-object-prop-node-left elem))
185 (value (js2-object-prop-node-right elem)))
187 (cond ((js2-name-node-p key)
188 (js2-name-node-name key))
189 ((js2-string-node-p key)
190 (js2-string-node-value key)))
192 (js2-string-node-p value))
193 (js2-string-node-value value))))))
195 (js2-record-object-literal options
196 (if js2-imenu-split-string-identifiers
197 (split-string name-value "\\.")
199 (js2-node-abs-pos options)))))))
201 (defun js2-imenu-walk-ast ()
207 ((and js2-imenu-show-other-functions
208 (js2-object-prop-node-p node))
209 (js2-imenu-record-orphan-function node))
210 ((and js2-imenu-show-module-pattern
211 (js2-assign-node-p node))
212 (js2-imenu-record-module-pattern node)))
215 (defun js2-imenu-get-parent-keyname-list (node)
216 "get the list of keys of parent of node
217 for example, for javascript code, {rules:{ password {required: function(){}}}}
218 the return will be '(rules password)."
221 (while (setq n (js2-imenu-parent-prop-node n))
222 (add-to-list 'rlt (js2-prop-node-name (js2-object-prop-node-left n))))
226 (defun js2-imenu-parent-prop-node (node)
227 "for javascript code: parent-key-name:{ required:function(){} }
228 we need know the parent-key-name.
229 step1, 'required:function(){}' is the js2-object-prop-node
230 step2, '{ required:function(){} }' is the js2-object-node
231 step3, 'parent-key-name:{ required:function(){} }' is js2-object-prop-node
235 (setq p2 (js2-node-parent node))
237 (when (and p2 (js2-object-node-p p2))
238 (setq p3 (js2-node-parent p2))
239 (if (and p3 (js2-object-prop-node-p p3)) p3))))
241 (defun js2-imenu-record-orphan-function (node)
242 "Record orphan function when it's the value of NODE.
243 NODE must be `js2-object-prop-node'."
244 (when (js2-function-node-p (js2-object-prop-node-right node))
245 (let ((fn-node (js2-object-prop-node-right node)))
246 (unless (and js2-imenu-function-map
247 (gethash fn-node js2-imenu-function-map))
248 (let ((key-node (js2-object-prop-node-left node))
249 (parent-prop-node (js2-imenu-parent-prop-node node))
251 (setq mylist (append (js2-imenu-get-parent-keyname-list node)
252 (list (js2-prop-node-name key-node))))
253 (add-to-list 'mylist js2-imenu-other-functions-ns)
254 (js2-record-imenu-entry fn-node mylist
255 (js2-node-abs-pos key-node)))))))
257 (defun js2-imenu-record-module-pattern (node)
258 "Recognize and record module pattern use instance.
259 NODE must be `js2-assign-node'."
260 (let ((init (js2-assign-node-right node)))
261 (when (js2-call-node-p init)
262 (let ((target (js2-assign-node-left node))
263 (callt (js2-call-node-target init)))
264 ;; Just basic call form: (function() {...})();
265 ;; TODO: Handle variations without duplicating `js2-wrapper-function-p'?
266 (when (and (js2-paren-node-p callt)
267 (js2-function-node-p (js2-paren-node-expr callt)))
268 (let* ((fn (js2-paren-node-expr callt))
269 (blk (js2-function-node-body fn))
270 (ret (car (last (js2-block-node-kids blk)))))
271 (when (and (js2-return-node-p ret)
272 (js2-object-node-p (js2-return-node-retval ret)))
273 ;; TODO: Map function names when revealing module pattern is used.
274 (let ((retval (js2-return-node-retval ret))
275 (target-qname (js2-compute-nested-prop-get target)))
276 (js2-record-object-literal retval target-qname
277 (js2-node-abs-pos retval))
278 (js2-record-imenu-entry fn target-qname
279 (js2-node-abs-pos target))))))))))
282 (define-minor-mode js2-imenu-extras-mode
283 "Toggle Imenu support for frameworks and structural patterns."
285 (if js2-imenu-extras-mode
286 (js2-imenu-extras-setup)
287 (js2-imenu-extras-remove)))
289 (provide 'js2-imenu-extras)