]> code.delx.au - gnu-emacs-elpa/blob - js2-imenu-extras.el
New option: js2-imenu-split-string-identifiers
[gnu-emacs-elpa] / js2-imenu-extras.el
1 ;;; js2-imenu-extras.el --- Imenu support for additional constructs
2
3 ;; Copyright (C) 2012-2013 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-record-orphan-function (node)
216 "Record orphan function when it's the value of NODE.
217 NODE must be `js2-object-prop-node'."
218 (when (js2-function-node-p (js2-object-prop-node-right node))
219 (let ((fn-node (js2-object-prop-node-right node)))
220 (unless (and js2-imenu-function-map
221 (gethash fn-node js2-imenu-function-map))
222 (let ((key-node (js2-object-prop-node-left node)))
223 (js2-record-imenu-entry fn-node
224 (list js2-imenu-other-functions-ns
225 (js2-prop-node-name key-node))
226 (js2-node-abs-pos key-node)))))))
227
228 (defun js2-imenu-record-module-pattern (node)
229 "Recognize and record module pattern use instance.
230 NODE must be `js2-assign-node'."
231 (let ((init (js2-assign-node-right node)))
232 (when (js2-call-node-p init)
233 (let ((target (js2-assign-node-left node))
234 (callt (js2-call-node-target init)))
235 ;; Just basic call form: (function() {...})();
236 ;; TODO: Handle variations without duplicating `js2-wrapper-function-p'?
237 (when (and (js2-paren-node-p callt)
238 (js2-function-node-p (js2-paren-node-expr callt)))
239 (let* ((fn (js2-paren-node-expr callt))
240 (blk (js2-function-node-body fn))
241 (ret (car (last (js2-block-node-kids blk)))))
242 (when (and (js2-return-node-p ret)
243 (js2-object-node-p (js2-return-node-retval ret)))
244 ;; TODO: Map function names when revealing module pattern is used.
245 (let ((retval (js2-return-node-retval ret))
246 (target-qname (js2-compute-nested-prop-get target)))
247 (js2-record-object-literal retval target-qname
248 (js2-node-abs-pos retval))
249 (js2-record-imenu-entry fn target-qname
250 (js2-node-abs-pos target))))))))))
251
252 ;;;###autoload
253 (define-minor-mode js2-imenu-extras-mode
254 "Toggle Imenu support for frameworks and structural patterns."
255 :lighter ""
256 (if js2-imenu-extras-mode
257 (js2-imenu-extras-setup)
258 (js2-imenu-extras-remove)))
259
260 (provide 'js2-imenu-extras)