Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

349 lines
14 KiB

  1. ;;; js2-imenu-extras.el --- Imenu support for additional constructs
  2. ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
  3. ;; Author: Dmitry Gutov <dgutov@yandex.ru>
  4. ;; Keywords: languages, javascript, imenu
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This package adds Imenu support for additional framework constructs and
  18. ;; structural patterns to `js2-mode'.
  19. ;; Usage:
  20. ;; (add-hook 'js2-mode-hook 'js2-imenu-extras-mode)
  21. ;; To customize how it works:
  22. ;; M-x customize-group RET js2-imenu RET
  23. (eval-when-compile
  24. (require 'cl))
  25. (require 'js2-mode)
  26. (defvar js2-imenu-extension-styles
  27. `((:framework jquery
  28. :call-re "\\_<\\(?:jQuery\\|\\$\\|_\\)\\.extend\\s-*("
  29. :recorder js2-imenu-record-jquery-extend)
  30. (:framework jquery-ui
  31. :call-re "^\\s-*\\(?:jQuery\\|\\$\\)\\.widget\\s-*("
  32. :recorder js2-imenu-record-string-declare)
  33. (:framework dojo
  34. :call-re "^\\s-*dojo.declare\\s-*("
  35. :recorder js2-imenu-record-string-declare)
  36. (:framework backbone
  37. :call-re ,(concat "\\_<" js2-mode-identifier-re "\\.extend\\s-*(")
  38. :recorder js2-imenu-record-backbone-extend)
  39. (:framework enyo
  40. :call-re "\\_<enyo\\.kind\\s-*("
  41. :recorder js2-imenu-record-enyo-kind)
  42. (:framework react
  43. :call-re "\\_<React\\.createClass\\s-*("
  44. :recorder js2-imenu-record-react-class)
  45. (:framework sencha
  46. :call-re "^\\s-*Ext\\.define\\s-*("
  47. :recorder js2-imenu-record-sencha-class))
  48. "List of JavaScript class definition or extension styles.
  49. :framework is a valid value in `js2-imenu-enabled-frameworks'.
  50. :call-re is a regular expression that has no capturing groups.
  51. :recorder is a function name that will be called when the regular
  52. expression matches some text in the buffer. When it's called, point will be
  53. at the end of the match. The function must keep the point position.")
  54. (defconst js2-imenu-available-frameworks
  55. (mapcar (lambda (style) (plist-get style :framework)) js2-imenu-extension-styles)
  56. "List of available JavaScript framework symbols.")
  57. (defcustom js2-imenu-enabled-frameworks js2-imenu-available-frameworks
  58. "Frameworks to be recognized by `js2-mode'."
  59. :type (cons 'set (mapcar (lambda (x) (list 'const x))
  60. js2-imenu-available-frameworks))
  61. :group 'js2-imenu)
  62. (defcustom js2-imenu-show-other-functions t
  63. "Non-nil to show functions not recognized by other mechanisms,
  64. in a shared namespace."
  65. :type 'boolean
  66. :group 'js2-imenu)
  67. (defcustom js2-imenu-other-functions-ns "?"
  68. "Namespace name to use for other functions."
  69. :type 'string
  70. :group 'js2-imenu)
  71. (defcustom js2-imenu-show-module-pattern t
  72. "Non-nil to recognize the module pattern:
  73. var foobs = (function(a) {
  74. return {fib: function() {}, fub: function() {}};
  75. })(b);
  76. We record the returned hash as belonging to the named module, and
  77. prefix any functions defined inside the IIFE with the module name."
  78. :type 'boolean
  79. :group 'js2-imenu)
  80. (defcustom js2-imenu-split-string-identifiers t
  81. "When non-nil, split string identifiers on dots.
  82. Currently used for jQuery widgets, Dojo and Enyo declarations."
  83. :type 'boolean
  84. :group 'js2-imenu)
  85. ;;;###autoload
  86. (defun js2-imenu-extras-setup ()
  87. (when js2-imenu-enabled-frameworks
  88. (add-hook 'js2-build-imenu-callbacks 'js2-imenu-record-declarations t t))
  89. (when (or js2-imenu-show-other-functions js2-imenu-show-module-pattern)
  90. (add-hook 'js2-build-imenu-callbacks 'js2-imenu-walk-ast t t)))
  91. (defun js2-imenu-extras-remove ()
  92. (remove-hook 'js2-build-imenu-callbacks 'js2-imenu-record-declarations t)
  93. (remove-hook 'js2-build-imenu-callbacks 'js2-imenu-walk-ast t))
  94. (defun js2-imenu-record-declarations ()
  95. (let* ((styles (loop for style in js2-imenu-extension-styles
  96. when (memq (plist-get style :framework)
  97. js2-imenu-enabled-frameworks)
  98. collect style))
  99. (re (mapconcat (lambda (style)
  100. (concat "\\(" (plist-get style :call-re) "\\)"))
  101. styles "\\|")))
  102. (goto-char (point-min))
  103. (while (js2-re-search-forward re nil t)
  104. (loop for i from 0 to (1- (length styles))
  105. when (match-beginning (1+ i))
  106. return (funcall (plist-get (nth i styles) :recorder))))))
  107. (defun js2-imenu-record-jquery-extend ()
  108. (let ((pred (lambda (subject)
  109. (and
  110. (js2-prop-get-node-p subject)
  111. (string= (js2-name-node-name (js2-prop-get-node-right subject))
  112. "prototype")))))
  113. (js2-imenu-record-extend-first-arg (1- (point)) pred
  114. 'js2-compute-nested-prop-get)))
  115. (defun js2-imenu-record-string-declare ()
  116. (js2-imenu-record-extend-first-arg
  117. (1- (point)) 'js2-string-node-p
  118. (lambda (node)
  119. (if js2-imenu-split-string-identifiers
  120. (split-string (js2-string-node-value node) "\\." t)
  121. (list (js2-string-node-value node))))))
  122. (defun js2-imenu-record-extend-first-arg (point pred qname-fn)
  123. (let* ((node (js2-node-at-point point))
  124. (args (js2-call-node-args node))
  125. (subject (first args)))
  126. (when (funcall pred subject)
  127. (loop for arg in (cdr args)
  128. when (js2-object-node-p arg)
  129. do (js2-record-object-literal
  130. arg (funcall qname-fn subject) (js2-node-abs-pos arg))))))
  131. (defun js2-imenu-record-backbone-or-react ()
  132. (let* ((node (js2-node-at-point (1- (point))))
  133. (args (js2-call-node-args node))
  134. (methods (first args))
  135. (parent (js2-node-parent node)))
  136. (when (js2-object-node-p methods)
  137. (let ((subject (cond ((js2-var-init-node-p parent)
  138. (js2-var-init-node-target parent))
  139. ((js2-assign-node-p parent)
  140. (js2-assign-node-left parent)))))
  141. (when subject
  142. (js2-record-object-literal methods
  143. (js2-compute-nested-prop-get subject)
  144. (js2-node-abs-pos methods)))))))
  145. (defalias 'js2-imenu-record-backbone-extend 'js2-imenu-record-backbone-or-react)
  146. (defalias 'js2-imenu-record-react-class 'js2-imenu-record-backbone-or-react)
  147. (defun js2-imenu-record-enyo-kind ()
  148. (let* ((node (js2-node-at-point (1- (point))))
  149. (args (js2-call-node-args node))
  150. (options (first args)))
  151. (when (js2-object-node-p options)
  152. (let ((name-value
  153. (loop for elem in (js2-object-node-elems options)
  154. thereis
  155. (let ((key (js2-object-prop-node-left elem))
  156. (value (js2-object-prop-node-right elem)))
  157. (when (and (equal
  158. (cond ((js2-name-node-p key)
  159. (js2-name-node-name key))
  160. ((js2-string-node-p key)
  161. (js2-string-node-value key)))
  162. "name")
  163. (js2-string-node-p value))
  164. (js2-string-node-value value))))))
  165. (when name-value
  166. (js2-record-object-literal options
  167. (if js2-imenu-split-string-identifiers
  168. (split-string name-value "\\.")
  169. (list name-value))
  170. (js2-node-abs-pos options)))))))
  171. (defun js2-imenu-record-sencha-class ()
  172. (let* ((node (js2-node-at-point (1- (point))))
  173. (args (js2-call-node-args node))
  174. (name (first args))
  175. (methods (second args)))
  176. (when (and (js2-string-node-p name) (js2-object-node-p methods))
  177. (let ((name-value (js2-string-node-value name)))
  178. (js2-record-object-literal methods
  179. (if js2-imenu-split-string-identifiers
  180. (split-string name-value "\\." t)
  181. (list name-value))
  182. (js2-node-abs-pos methods))))))
  183. (defun js2-imenu-walk-ast ()
  184. (js2-visit-ast
  185. js2-mode-ast
  186. (lambda (node end-p)
  187. (unless end-p
  188. (cond
  189. ((and js2-imenu-show-other-functions
  190. (js2-object-prop-node-p node))
  191. (js2-imenu-record-orphan-prop-node-function node))
  192. ((js2-assign-node-p node)
  193. (cond
  194. ((and js2-imenu-show-other-functions
  195. (js2-function-node-p
  196. (js2-assign-node-right node)))
  197. (js2-imenu-record-orphan-assign-node-function
  198. (js2-assign-node-left node)
  199. (js2-assign-node-right node)))
  200. ((and js2-imenu-show-module-pattern
  201. (js2-call-node-p
  202. (js2-assign-node-right node)))
  203. (js2-imenu-record-module-pattern
  204. (js2-assign-node-left node)
  205. (js2-assign-node-right node)))))
  206. ((js2-var-init-node-p node)
  207. (cond
  208. ((and js2-imenu-show-other-functions
  209. (js2-function-node-p
  210. (js2-var-init-node-initializer node)))
  211. (js2-imenu-record-orphan-assign-node-function
  212. (js2-var-init-node-target node)
  213. (js2-var-init-node-initializer node)))
  214. ((and js2-imenu-show-module-pattern
  215. (js2-call-node-p
  216. (js2-var-init-node-initializer node)))
  217. (js2-imenu-record-module-pattern
  218. (js2-var-init-node-target node)
  219. (js2-var-init-node-initializer node))))))
  220. t))))
  221. (defun js2-imenu-parent-key-names (node)
  222. "Get the list of parent key names of NODE.
  223. For example, for code
  224. {rules: {password: {required: function() {}}}}
  225. when NODE is the inner `js2-object-prop-mode',
  226. it returns `(\"rules\" \"password\")'."
  227. (let (rlt (n node))
  228. (while (setq n (js2-imenu-parent-prop-node n))
  229. (push (js2-prop-node-name (js2-object-prop-node-left n)) rlt))
  230. rlt))
  231. (defun js2-imenu-parent-prop-node (node)
  232. "When the parent of NODE is `js2-object-node',
  233. and the grandparent is `js2-object-prop-node',
  234. return the grandparent."
  235. ;; Suppose the code is:
  236. ;; {parent-key: {required: function() {}}}
  237. ;; NODE is `required: function() {}'.
  238. (let (p2 p3)
  239. ;; Parent is `{required: function() {}}'.
  240. (setq p2 (js2-node-parent node))
  241. ;; GP is `parent-key: {required: function() {}}'.
  242. (when (and p2 (js2-object-node-p p2))
  243. (setq p3 (js2-node-parent p2))
  244. (if (and p3 (js2-object-prop-node-p p3)) p3))))
  245. (defun js2-imenu-record-orphan-prop-node-function (node)
  246. "Record orphan function when it's the value of NODE.
  247. NODE must be `js2-object-prop-node'."
  248. (when (js2-function-node-p (js2-object-prop-node-right node))
  249. (let ((fn-node (js2-object-prop-node-right node)))
  250. (unless (and js2-imenu-function-map
  251. (gethash fn-node js2-imenu-function-map))
  252. (let ((key-node (js2-object-prop-node-left node))
  253. (parent-prop-node (js2-imenu-parent-prop-node node))
  254. chain)
  255. (setq chain (nconc (js2-imenu-parent-key-names node)
  256. (list (js2-prop-node-name key-node))))
  257. (push js2-imenu-other-functions-ns chain)
  258. (js2-record-imenu-entry fn-node chain
  259. (js2-node-abs-pos key-node)))))))
  260. (defun js2-imenu-record-orphan-assign-node-function (target-node fn-node)
  261. "Record orphan function FN-NODE assigned to node TARGET."
  262. (when (or (not js2-imenu-function-map)
  263. (eq 'skip
  264. (gethash fn-node js2-imenu-function-map 'skip)))
  265. (let ((chain (js2-compute-nested-prop-get target-node)))
  266. (when chain
  267. (push js2-imenu-other-functions-ns chain)
  268. (js2-record-imenu-entry fn-node chain (js2-node-abs-pos fn-node))))))
  269. (defun js2-imenu-record-module-pattern (target init)
  270. "Recognize and record module pattern use instance.
  271. INIT must be `js2-call-node'."
  272. (let ((callt (js2-call-node-target init)))
  273. ;; Just basic call form: (function() {...})();
  274. ;; TODO: Handle variations without duplicating `js2-wrapper-function-p'?
  275. (when (and (js2-paren-node-p callt)
  276. (js2-function-node-p (js2-paren-node-expr callt)))
  277. (let* ((fn (js2-paren-node-expr callt))
  278. (blk (js2-function-node-body fn))
  279. (ret (car (last (js2-block-node-kids blk)))))
  280. (when (and (js2-return-node-p ret)
  281. (js2-object-node-p (js2-return-node-retval ret)))
  282. ;; TODO: Map function names when revealing module pattern is used.
  283. (let ((retval (js2-return-node-retval ret))
  284. (target-qname (js2-compute-nested-prop-get target)))
  285. (js2-record-object-literal retval target-qname
  286. (js2-node-abs-pos retval))
  287. (js2-record-imenu-entry fn target-qname
  288. (js2-node-abs-pos target))))))))
  289. ;;;###autoload
  290. (define-minor-mode js2-imenu-extras-mode
  291. "Toggle Imenu support for frameworks and structural patterns."
  292. :lighter ""
  293. (if js2-imenu-extras-mode
  294. (js2-imenu-extras-setup)
  295. (js2-imenu-extras-remove)))
  296. (provide 'js2-imenu-extras)