db-javascript.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. ;;; semantic/db-javascript.el --- Semantic database extensions for javascript
  2. ;; Copyright (C) 2002-2012 Free Software Foundation, Inc.
  3. ;; Author: Joakim Verona
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; Semanticdb database for Javascript.
  18. ;;
  19. ;; This is an omniscient database with a hard-coded list of symbols for
  20. ;; Javascript. See the doc at the end of this file for adding or modifying
  21. ;; the list of tags.
  22. ;;
  23. (require 'semantic/db)
  24. (require 'semantic/db-find)
  25. (eval-when-compile
  26. ;; For generic function searching.
  27. (require 'eieio)
  28. (require 'eieio-opt))
  29. ;;; Code:
  30. (defvar semanticdb-javascript-tags
  31. '(("eval" function
  32. (:arguments
  33. (("x" variable nil nil nil)))
  34. nil nil)
  35. ("parseInt" function
  36. (:arguments
  37. (("string" variable nil nil nil)
  38. ("radix" variable nil nil nil)))
  39. nil nil)
  40. ("parseFloat" function
  41. (:arguments
  42. (("string" variable nil nil nil)))
  43. nil nil)
  44. ("isNaN" function
  45. (:arguments
  46. (("number" variable nil nil nil)))
  47. nil nil)
  48. ("isFinite" function
  49. (:arguments
  50. (("number" variable nil nil nil)))
  51. nil nil)
  52. ("decodeURI" function
  53. (:arguments
  54. (("encodedURI" variable nil nil nil)))
  55. nil nil)
  56. ("decodeURIComponent" function
  57. (:arguments
  58. (("encodedURIComponent" variable nil nil nil)))
  59. nil nil)
  60. ("encodeURI" function
  61. (:arguments
  62. (("uri" variable nil nil nil)))
  63. nil nil)
  64. ("encodeURIComponent" function
  65. (:arguments
  66. (("uriComponent" variable nil nil nil)))
  67. nil nil))
  68. "Hard-coded list of javascript tags for semanticdb.
  69. See bottom of this file for instructions on managing this list.")
  70. ;;; Classes:
  71. (defclass semanticdb-table-javascript (semanticdb-search-results-table)
  72. ((major-mode :initform javascript-mode)
  73. )
  74. "A table for returning search results from javascript.")
  75. (defclass semanticdb-project-database-javascript
  76. (semanticdb-project-database
  77. eieio-singleton ;this db is for js globals, so singleton is appropriate
  78. )
  79. ((new-table-class :initform semanticdb-table-javascript
  80. :type class
  81. :documentation
  82. "New tables created for this database are of this class.")
  83. )
  84. "Database representing javascript.")
  85. ;; Create the database, and add it to searchable databases for javascript mode.
  86. (defvar-mode-local javascript-mode semanticdb-project-system-databases
  87. (list
  88. (semanticdb-project-database-javascript "Javascript"))
  89. "Search javascript for symbols.")
  90. ;; NOTE: Be sure to modify this to the best advantage of your
  91. ;; language.
  92. (defvar-mode-local javascript-mode semanticdb-find-default-throttle
  93. '(project omniscience)
  94. "Search project files, then search this omniscience database.
  95. It is not necessary to do system or recursive searching because of
  96. the omniscience database.")
  97. ;;; Filename based methods
  98. ;;
  99. (defmethod semanticdb-get-database-tables ((obj semanticdb-project-database-javascript))
  100. "For a javascript database, there are no explicit tables.
  101. Create one of our special tables that can act as an intermediary."
  102. ;; NOTE: This method overrides an accessor for the `tables' slot in
  103. ;; a database. You can either construct your own (like tmp here
  104. ;; or you can manage any number of tables.
  105. ;; We need to return something since there is always the "master table"
  106. ;; The table can then answer file name type questions.
  107. (when (not (slot-boundp obj 'tables))
  108. (let ((newtable (semanticdb-table-javascript "tmp")))
  109. (oset obj tables (list newtable))
  110. (oset newtable parent-db obj)
  111. (oset newtable tags nil)
  112. ))
  113. (call-next-method)
  114. )
  115. (defmethod semanticdb-file-table ((obj semanticdb-project-database-javascript) filename)
  116. "From OBJ, return FILENAME's associated table object."
  117. ;; NOTE: See not for `semanticdb-get-database-tables'.
  118. (car (semanticdb-get-database-tables obj))
  119. )
  120. (defmethod semanticdb-get-tags ((table semanticdb-table-javascript ))
  121. "Return the list of tags belonging to TABLE."
  122. ;; NOTE: Omniscient databases probably don't want to keep large tables
  123. ;; lolly-gagging about. Keep internal Emacs tables empty and
  124. ;; refer to alternate databases when you need something.
  125. semanticdb-javascript-tags)
  126. (defmethod semanticdb-equivalent-mode ((table semanticdb-table-javascript) &optional buffer)
  127. "Return non-nil if TABLE's mode is equivalent to BUFFER.
  128. Equivalent modes are specified by the `semantic-equivalent-major-modes'
  129. local variable."
  130. (with-current-buffer buffer
  131. (eq (or mode-local-active-mode major-mode) 'javascript-mode)))
  132. ;;; Usage
  133. ;;
  134. ;; Unlike other tables, an omniscient database does not need to
  135. ;; be associated with a path. Use this routine to always add ourselves
  136. ;; to a search list.
  137. (define-mode-local-override semanticdb-find-translate-path javascript-mode
  138. (path brutish)
  139. "Return a list of semanticdb tables associated with PATH.
  140. If brutish, do the default action.
  141. If not brutish, do the default action, and append the system
  142. database (if available.)"
  143. (let ((default
  144. ;; When we recurse, disable searching of system databases
  145. ;; so that our Javascript database only shows up once when
  146. ;; we append it in this iteration.
  147. (let ((semanticdb-search-system-databases nil)
  148. )
  149. (semanticdb-find-translate-path-default path brutish))))
  150. ;; Don't add anything if BRUTISH is on (it will be added in that fcn)
  151. ;; or if we aren't supposed to search the system.
  152. (if (or brutish (not semanticdb-search-system-databases))
  153. default
  154. (let ((tables (apply #'append
  155. (mapcar
  156. (lambda (db) (semanticdb-get-database-tables db))
  157. semanticdb-project-system-databases))))
  158. (append default tables)))))
  159. ;;; Search Overrides
  160. ;;
  161. ;; NOTE WHEN IMPLEMENTING: Be sure to add doc-string updates explaining
  162. ;; how your new search routines are implemented.
  163. ;;
  164. (defun semanticdb-javascript-regexp-search (regexp)
  165. "Search for REGEXP in our fixed list of javascript tags."
  166. (let* ((tags semanticdb-javascript-tags)
  167. (result nil))
  168. (while tags
  169. (if (string-match regexp (caar tags))
  170. (setq result (cons (car tags) result)))
  171. (setq tags (cdr tags)))
  172. result))
  173. (defmethod semanticdb-find-tags-by-name-method
  174. ((table semanticdb-table-javascript) name &optional tags)
  175. "Find all tags named NAME in TABLE.
  176. Return a list of tags."
  177. (if tags
  178. ;; If TAGS are passed in, then we don't need to do work here.
  179. (call-next-method)
  180. (assoc-string name semanticdb-javascript-tags)
  181. ))
  182. (defmethod semanticdb-find-tags-by-name-regexp-method
  183. ((table semanticdb-table-javascript) regex &optional tags)
  184. "Find all tags with name matching REGEX in TABLE.
  185. Optional argument TAGS is a list of tags to search.
  186. Return a list of tags."
  187. (if tags (call-next-method)
  188. ;; YOUR IMPLEMENTATION HERE
  189. (semanticdb-javascript-regexp-search regex)
  190. ))
  191. (defmethod semanticdb-find-tags-for-completion-method
  192. ((table semanticdb-table-javascript) prefix &optional tags)
  193. "In TABLE, find all occurrences of tags matching PREFIX.
  194. Optional argument TAGS is a list of tags to search.
  195. Returns a table of all matching tags."
  196. (if tags (call-next-method)
  197. ;; YOUR IMPLEMENTATION HERE
  198. (semanticdb-javascript-regexp-search (concat "^" prefix ".*"))
  199. ))
  200. (defmethod semanticdb-find-tags-by-class-method
  201. ((table semanticdb-table-javascript) class &optional tags)
  202. "In TABLE, find all occurrences of tags of CLASS.
  203. Optional argument TAGS is a list of tags to search.
  204. Returns a table of all matching tags."
  205. (if tags (call-next-method)
  206. ;; YOUR IMPLEMENTATION HERE
  207. ;;
  208. ;; Note: This search method could be considered optional in an
  209. ;; omniscient database. It may be unwise to return all tags
  210. ;; that exist for a language that are a variable or function.
  211. ;;
  212. ;; If it is optional, you can just delete this method.
  213. nil))
  214. ;;; Deep Searches
  215. ;;
  216. ;; If your language does not have a `deep' concept, these can be left
  217. ;; alone, otherwise replace with implementations similar to those
  218. ;; above.
  219. ;;
  220. (defmethod semanticdb-deep-find-tags-by-name-method
  221. ((table semanticdb-table-javascript) name &optional tags)
  222. "Find all tags name NAME in TABLE.
  223. Optional argument TAGS is a list of tags to search.
  224. Like `semanticdb-find-tags-by-name-method' for javascript."
  225. (semanticdb-find-tags-by-name-method table name tags))
  226. (defmethod semanticdb-deep-find-tags-by-name-regexp-method
  227. ((table semanticdb-table-javascript) regex &optional tags)
  228. "Find all tags with name matching REGEX in TABLE.
  229. Optional argument TAGS is a list of tags to search.
  230. Like `semanticdb-find-tags-by-name-method' for javascript."
  231. (semanticdb-find-tags-by-name-regexp-method table regex tags))
  232. (defmethod semanticdb-deep-find-tags-for-completion-method
  233. ((table semanticdb-table-javascript) prefix &optional tags)
  234. "In TABLE, find all occurrences of tags matching PREFIX.
  235. Optional argument TAGS is a list of tags to search.
  236. Like `semanticdb-find-tags-for-completion-method' for javascript."
  237. (semanticdb-find-tags-for-completion-method table prefix tags))
  238. ;;; Advanced Searches
  239. ;;
  240. (defmethod semanticdb-find-tags-external-children-of-type-method
  241. ((table semanticdb-table-javascript) type &optional tags)
  242. "Find all nonterminals which are child elements of TYPE.
  243. Optional argument TAGS is a list of tags to search.
  244. Return a list of tags."
  245. (if tags (call-next-method)
  246. ;; YOUR IMPLEMENTATION HERE
  247. ;;
  248. ;; OPTIONAL: This could be considered an optional function. It is
  249. ;; used for `semantic-adopt-external-members' and may not
  250. ;; be possible to do in your language.
  251. ;;
  252. ;; If it is optional, you can just delete this method.
  253. ))
  254. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  255. (defun semanticdb-javascript-strip-tags (tags)
  256. "Strip TAGS from overlays and reparse symbols."
  257. (cond ((and (consp tags) (eq 'reparse-symbol (car tags)))
  258. nil)
  259. ((overlayp tags) nil)
  260. ((atom tags) tags)
  261. (t (cons (semanticdb-javascript-strip-tags
  262. (car tags)) (semanticdb-javascript-strip-tags
  263. (cdr tags))))))
  264. ;this list was made from a javascript file, and the above function
  265. ;; function eval(x){}
  266. ;; function parseInt(string,radix){}
  267. ;; function parseFloat(string){}
  268. ;; function isNaN(number){}
  269. ;; function isFinite(number){}
  270. ;; function decodeURI(encodedURI){}
  271. ;; function decodeURIComponent (encodedURIComponent){}
  272. ;; function encodeURI (uri){}
  273. ;; function encodeURIComponent (uriComponent){}
  274. (provide 'semantic/db-javascript)
  275. ;;; semantic/db-javascript.el ends here