]> code.delx.au - gnu-emacs-elpa/blob - js2-imenu-extras.el
Merge branch 'imenu'
[gnu-emacs-elpa] / js2-imenu-extras.el
1 ;;; js2-imenu-extras.el --- Imenu support for additional constructs
2
3 ;; Author: Dmitry Gutov <dgutov@yandex.ru>
4 ;; Keywords: languages, javascript, imenu
5
6 ;; This file is part of GNU Emacs.
7
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.
12
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.
17
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/>.
20
21 ;;; Commentary:
22
23 ;; This package adds Imenu support for additional framework constructs and
24 ;; general patterns to `js2-mode'.
25
26 ;; Usage:
27
28 ;; (eval-after-load 'js2-mode
29 ;; (require 'js2-imenu-extras)
30 ;; (js2-imenu-extras-setup))
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 "List of JavaScript class definition or extension styles.
57
58 :framework is a valid value in `js2-imenu-enabled-frameworks.
59
60 :call-re is a regular expression that has no capturing groups.
61
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.")
65
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.")
69
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))
74 :group 'js2-imenu)
75
76 (defcustom js2-imenu-show-other-functions t
77 "Non-nil to show functions not recognized by other mechanisms,
78 in a shared namespace."
79 :type 'boolean
80 :group 'js2-imenu)
81
82 (defcustom js2-imenu-other-functions-ns "?"
83 "Namespace name to use for other functions."
84 :type 'string
85 :group 'js2-imenu)
86
87 (defcustom js2-imenu-show-module-pattern t
88 "Non-nil to recognize the module pattern:
89
90 var foobs = (function(a) {
91 return {fib: function() {}, fub: function() {}};
92 })(b);
93
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."
96 :type 'boolean
97 :group 'js2-imenu)
98
99 ;;;###autoload
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)))
105
106 (declare (special root))
107
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)
112 collect style))
113 (re (mapconcat (lambda (style)
114 (concat "\\(" (plist-get style :call-re) "\\)"))
115 styles "\\|"))
116 ;; Dynamic scoping. Ew.
117 (js2-mode-ast root))
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))))))
123
124 (defun js2-imenu-record-jquery-extend ()
125 (let ((pred (lambda (subject)
126 (and
127 (js2-prop-get-node-p subject)
128 (string= (js2-name-node-name (js2-prop-get-node-right subject))
129 "prototype")))))
130 (js2-imenu-record-extend-first-arg (1- (point)) pred
131 'js2-compute-nested-prop-get)))
132
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))))
137
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))))))
147
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)))))
158 (when subject
159 (js2-record-object-literal methods
160 (js2-compute-nested-prop-get subject)
161 (js2-node-abs-pos methods)))))))
162
163 (defun js2-imenu-walk-ast ()
164 (js2-visit-ast
165 root
166 (lambda (node end-p)
167 (unless end-p
168 (cond
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)))
175 t))))
176
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)))))))
189
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))))))))))
213
214 (provide 'js2-imenu-extras)