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.

346 lines
14 KiB

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