]> code.delx.au - gnu-emacs-elpa/blob - packages/js2-mode/js2-imenu-extras.el
Merge commit '3db1ea76a02993663d40e90c58da989212b9e81a' into gnorb-1.0.1
[gnu-emacs-elpa] / packages / js2-mode / js2-imenu-extras.el
1 ;;; js2-imenu-extras.el --- Imenu support for additional constructs
2
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
4
5 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
6 ;; Keywords: languages, javascript, imenu
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;; This package adds Imenu support for additional framework constructs and
26 ;; structural patterns to `js2-mode'.
27
28 ;; Usage:
29
30 ;; (add-hook 'js2-mode-hook 'js2-imenu-extras-mode)
31
32 ;; To customize how it works:
33 ;; M-x customize-group RET js2-imenu RET
34
35 (eval-when-compile
36 (require 'cl))
37
38 (require 'js2-mode)
39
40 (defconst js2-imenu-extension-styles
41 `((:framework jquery
42 :call-re "\\_<\\(?:jQuery\\|\\$\\|_\\)\\.extend\\s-*("
43 :recorder js2-imenu-record-jquery-extend)
44
45 (:framework jquery-ui
46 :call-re "^\\s-*\\(?:jQuery\\|\\$\\)\\.widget\\s-*("
47 :recorder js2-imenu-record-string-declare)
48
49 (:framework dojo
50 :call-re "^\\s-*dojo.declare\\s-*("
51 :recorder js2-imenu-record-string-declare)
52
53 (:framework backbone
54 :call-re ,(concat "\\_<" js2-mode-identifier-re "\\.extend\\s-*(")
55 :recorder js2-imenu-record-backbone-extend)
56
57 (:framework enyo
58 :call-re "\\_<enyo\\.kind\\s-*("
59 :recorder js2-imenu-record-enyo-kind))
60 "List of JavaScript class definition or extension styles.
61
62 :framework is a valid value in `js2-imenu-enabled-frameworks'.
63
64 :call-re is a regular expression that has no capturing groups.
65
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.")
69
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.")
73
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))
78 :group 'js2-imenu)
79
80 (defcustom js2-imenu-show-other-functions t
81 "Non-nil to show functions not recognized by other mechanisms,
82 in a shared namespace."
83 :type 'boolean
84 :group 'js2-imenu)
85
86 (defcustom js2-imenu-other-functions-ns "?"
87 "Namespace name to use for other functions."
88 :type 'string
89 :group 'js2-imenu)
90
91 (defcustom js2-imenu-show-module-pattern t
92 "Non-nil to recognize the module pattern:
93
94 var foobs = (function(a) {
95 return {fib: function() {}, fub: function() {}};
96 })(b);
97
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."
100 :type 'boolean
101 :group 'js2-imenu)
102
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."
106 :type 'boolean
107 :group 'js2-imenu)
108
109 ;;;###autoload
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)))
115
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))
119
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)
124 collect style))
125 (re (mapconcat (lambda (style)
126 (concat "\\(" (plist-get style :call-re) "\\)"))
127 styles "\\|")))
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))))))
133
134 (defun js2-imenu-record-jquery-extend ()
135 (let ((pred (lambda (subject)
136 (and
137 (js2-prop-get-node-p subject)
138 (string= (js2-name-node-name (js2-prop-get-node-right subject))
139 "prototype")))))
140 (js2-imenu-record-extend-first-arg (1- (point)) pred
141 'js2-compute-nested-prop-get)))
142
143 (defun js2-imenu-record-string-declare ()
144 (js2-imenu-record-extend-first-arg
145 (1- (point)) 'js2-string-node-p
146 (lambda (node)
147 (if js2-imenu-split-string-identifiers
148 (split-string (js2-string-node-value node) "\\." t)
149 (list (js2-string-node-value node))))))
150
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))))))
160
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)))))
171 (when subject
172 (js2-record-object-literal methods
173 (js2-compute-nested-prop-get subject)
174 (js2-node-abs-pos methods)))))))
175
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)
181 (let ((name-value
182 (loop for elem in (js2-object-node-elems options)
183 thereis
184 (let ((key (js2-object-prop-node-left elem))
185 (value (js2-object-prop-node-right elem)))
186 (when (and (equal
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)))
191 "name")
192 (js2-string-node-p value))
193 (js2-string-node-value value))))))
194 (when name-value
195 (js2-record-object-literal options
196 (if js2-imenu-split-string-identifiers
197 (split-string name-value "\\.")
198 (list name-value))
199 (js2-node-abs-pos options)))))))
200
201 (defun js2-imenu-walk-ast ()
202 (js2-visit-ast
203 js2-mode-ast
204 (lambda (node end-p)
205 (unless end-p
206 (cond
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)))
213 t))))
214
215 (defun js2-imenu-parent-key-names (node)
216 "Get the list of parent key names of NODE.
217
218 For example, for code
219
220 {rules: {password: {required: function() {}}}}
221
222 when NODE is the inner `js2-object-prop-mode',
223 it returns `(\"rules\" \"password\")'."
224 (let (rlt (n node))
225 (while (setq n (js2-imenu-parent-prop-node n))
226 (push (js2-prop-node-name (js2-object-prop-node-left n)) rlt))
227 rlt))
228
229 (defun js2-imenu-parent-prop-node (node)
230 "When the parent of NODE is `js2-object-node',
231 and the grandparent is `js2-object-prop-node',
232 return the grandparent."
233 ;; Suppose the code is:
234 ;; {parent-key: {required: function() {}}}
235 ;; NODE is `required: function() {}'.
236 (let (p2 p3)
237 ;; Parent is `{required: function() {}}'.
238 (setq p2 (js2-node-parent node))
239 ;; GP is `parent-key: {required: function() {}}'.
240 (when (and p2 (js2-object-node-p p2))
241 (setq p3 (js2-node-parent p2))
242 (if (and p3 (js2-object-prop-node-p p3)) p3))))
243
244 (defun js2-imenu-record-orphan-function (node)
245 "Record orphan function when it's the value of NODE.
246 NODE must be `js2-object-prop-node'."
247 (when (js2-function-node-p (js2-object-prop-node-right node))
248 (let ((fn-node (js2-object-prop-node-right node)))
249 (unless (and js2-imenu-function-map
250 (gethash fn-node js2-imenu-function-map))
251 (let ((key-node (js2-object-prop-node-left node))
252 chain)
253 (setq chain (nconc (js2-imenu-parent-key-names node)
254 (list (js2-prop-node-name key-node))))
255 (push js2-imenu-other-functions-ns chain)
256 (js2-record-imenu-entry fn-node chain
257 (js2-node-abs-pos key-node)))))))
258
259 (defun js2-imenu-record-module-pattern (node)
260 "Recognize and record module pattern use instance.
261 NODE must be `js2-assign-node'."
262 (let ((init (js2-assign-node-right node)))
263 (when (js2-call-node-p init)
264 (let ((target (js2-assign-node-left node))
265 (callt (js2-call-node-target init)))
266 ;; Just basic call form: (function() {...})();
267 ;; TODO: Handle variations without duplicating `js2-wrapper-function-p'?
268 (when (and (js2-paren-node-p callt)
269 (js2-function-node-p (js2-paren-node-expr callt)))
270 (let* ((fn (js2-paren-node-expr callt))
271 (blk (js2-function-node-body fn))
272 (ret (car (last (js2-block-node-kids blk)))))
273 (when (and (js2-return-node-p ret)
274 (js2-object-node-p (js2-return-node-retval ret)))
275 ;; TODO: Map function names when revealing module pattern is used.
276 (let ((retval (js2-return-node-retval ret))
277 (target-qname (js2-compute-nested-prop-get target)))
278 (js2-record-object-literal retval target-qname
279 (js2-node-abs-pos retval))
280 (js2-record-imenu-entry fn target-qname
281 (js2-node-abs-pos target))))))))))
282
283 ;;;###autoload
284 (define-minor-mode js2-imenu-extras-mode
285 "Toggle Imenu support for frameworks and structural patterns."
286 :lighter ""
287 (if js2-imenu-extras-mode
288 (js2-imenu-extras-setup)
289 (js2-imenu-extras-remove)))
290
291 (provide 'js2-imenu-extras)