Nuke arch-tags.
[emacs.git] / lisp / cedet / semantic / db-javascript.el
blob587708afd663e217e4f7ce93bc8b0868b1abc432
1 ;;; semantic/db-javascript.el --- Semantic database extensions for javascript
3 ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 ;; Free Software Foundation, Inc.
6 ;; Author: Joakim Verona
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
25 ;; Semanticdb database for Javascript.
27 ;; This is an omniscient database with a hard-coded list of symbols for
28 ;; Javascript. See the doc at the end of this file for adding or modifying
29 ;; the list of tags.
32 (require 'semantic/db)
33 (require 'semantic/db-find)
35 (eval-when-compile
36 ;; For generic function searching.
37 (require 'eieio)
38 (require 'eieio-opt))
40 ;;; Code:
41 (defvar semanticdb-javascript-tags
42 '(("eval" function
43 (:arguments
44 (("x" variable nil nil nil)))
45 nil nil)
46 ("parseInt" function
47 (:arguments
48 (("string" variable nil nil nil)
49 ("radix" variable nil nil nil)))
50 nil nil)
51 ("parseFloat" function
52 (:arguments
53 (("string" variable nil nil nil)))
54 nil nil)
55 ("isNaN" function
56 (:arguments
57 (("number" variable nil nil nil)))
58 nil nil)
59 ("isFinite" function
60 (:arguments
61 (("number" variable nil nil nil)))
62 nil nil)
63 ("decodeURI" function
64 (:arguments
65 (("encodedURI" variable nil nil nil)))
66 nil nil)
67 ("decodeURIComponent" function
68 (:arguments
69 (("encodedURIComponent" variable nil nil nil)))
70 nil nil)
71 ("encodeURI" function
72 (:arguments
73 (("uri" variable nil nil nil)))
74 nil nil)
75 ("encodeURIComponent" function
76 (:arguments
77 (("uriComponent" variable nil nil nil)))
78 nil nil))
79 "Hard-coded list of javascript tags for semanticdb.
80 See bottom of this file for instructions on managing this list.")
82 ;;; Classes:
83 (defclass semanticdb-table-javascript (semanticdb-search-results-table)
84 ((major-mode :initform javascript-mode)
86 "A table for returning search results from javascript.")
88 (defclass semanticdb-project-database-javascript
89 (semanticdb-project-database
90 eieio-singleton ;this db is for js globals, so singleton is appropriate
92 ((new-table-class :initform semanticdb-table-javascript
93 :type class
94 :documentation
95 "New tables created for this database are of this class.")
97 "Database representing javascript.")
99 ;; Create the database, and add it to searchable databases for javascript mode.
100 (defvar-mode-local javascript-mode semanticdb-project-system-databases
101 (list
102 (semanticdb-project-database-javascript "Javascript"))
103 "Search javascript for symbols.")
105 ;; NOTE: Be sure to modify this to the best advantage of your
106 ;; language.
107 (defvar-mode-local javascript-mode semanticdb-find-default-throttle
108 '(project omniscience)
109 "Search project files, then search this omniscience database.
110 It is not necessary to do system or recursive searching because of
111 the omniscience database.")
113 ;;; Filename based methods
115 (defmethod semanticdb-get-database-tables ((obj semanticdb-project-database-javascript))
116 "For a javascript database, there are no explicit tables.
117 Create one of our special tables that can act as an intermediary."
118 ;; NOTE: This method overrides an accessor for the `tables' slot in
119 ;; a database. You can either construct your own (like tmp here
120 ;; or you can manage any number of tables.
122 ;; We need to return something since there is always the "master table"
123 ;; The table can then answer file name type questions.
124 (when (not (slot-boundp obj 'tables))
125 (let ((newtable (semanticdb-table-javascript "tmp")))
126 (oset obj tables (list newtable))
127 (oset newtable parent-db obj)
128 (oset newtable tags nil)
130 (call-next-method)
133 (defmethod semanticdb-file-table ((obj semanticdb-project-database-javascript) filename)
134 "From OBJ, return FILENAME's associated table object."
135 ;; NOTE: See not for `semanticdb-get-database-tables'.
136 (car (semanticdb-get-database-tables obj))
139 (defmethod semanticdb-get-tags ((table semanticdb-table-javascript ))
140 "Return the list of tags belonging to TABLE."
141 ;; NOTE: Omniscient databases probably don't want to keep large tabes
142 ;; lolly-gagging about. Keep internal Emacs tables empty and
143 ;; refer to alternate databases when you need something.
144 semanticdb-javascript-tags)
146 (defmethod semanticdb-equivalent-mode ((table semanticdb-table-javascript) &optional buffer)
147 "Return non-nil if TABLE's mode is equivalent to BUFFER.
148 Equivalent modes are specified by the `semantic-equivalent-major-modes'
149 local variable."
150 (with-current-buffer buffer
151 (eq (or mode-local-active-mode major-mode) 'javascript-mode)))
153 ;;; Usage
155 ;; Unlike other tables, an omniscent database does not need to
156 ;; be associated with a path. Use this routine to always add ourselves
157 ;; to a search list.
158 (define-mode-local-override semanticdb-find-translate-path javascript-mode
159 (path brutish)
160 "Return a list of semanticdb tables associated with PATH.
161 If brutish, do the default action.
162 If not brutish, do the default action, and append the system
163 database (if available.)"
164 (let ((default
165 ;; When we recurse, disable searching of system databases
166 ;; so that our Javascript database only shows up once when
167 ;; we append it in this iteration.
168 (let ((semanticdb-search-system-databases nil)
170 (semanticdb-find-translate-path-default path brutish))))
171 ;; Don't add anything if BRUTISH is on (it will be added in that fcn)
172 ;; or if we aren't supposed to search the system.
173 (if (or brutish (not semanticdb-search-system-databases))
174 default
175 (let ((tables (apply #'append
176 (mapcar
177 (lambda (db) (semanticdb-get-database-tables db))
178 semanticdb-project-system-databases))))
179 (append default tables)))))
181 ;;; Search Overrides
183 ;; NOTE WHEN IMPLEMENTING: Be sure to add doc-string updates explaining
184 ;; how your new search routines are implemented.
186 (defun semanticdb-javascript-regexp-search (regexp)
187 "Search for REGEXP in our fixed list of javascript tags."
188 (let* ((tags semanticdb-javascript-tags)
189 (result nil))
190 (while tags
191 (if (string-match regexp (caar tags))
192 (setq result (cons (car tags) result)))
193 (setq tags (cdr tags)))
194 result))
196 (defmethod semanticdb-find-tags-by-name-method
197 ((table semanticdb-table-javascript) name &optional tags)
198 "Find all tags named NAME in TABLE.
199 Return a list of tags."
200 (if tags
201 ;; If TAGS are passed in, then we don't need to do work here.
202 (call-next-method)
203 (assoc-string name semanticdb-javascript-tags)
206 (defmethod semanticdb-find-tags-by-name-regexp-method
207 ((table semanticdb-table-javascript) regex &optional tags)
208 "Find all tags with name matching REGEX in TABLE.
209 Optional argument TAGS is a list of tags to search.
210 Return a list of tags."
211 (if tags (call-next-method)
212 ;; YOUR IMPLEMENTATION HERE
213 (semanticdb-javascript-regexp-search regex)
217 (defmethod semanticdb-find-tags-for-completion-method
218 ((table semanticdb-table-javascript) prefix &optional tags)
219 "In TABLE, find all occurrences of tags matching PREFIX.
220 Optional argument TAGS is a list of tags to search.
221 Returns a table of all matching tags."
222 (if tags (call-next-method)
223 ;; YOUR IMPLEMENTATION HERE
224 (semanticdb-javascript-regexp-search (concat "^" prefix ".*"))
227 (defmethod semanticdb-find-tags-by-class-method
228 ((table semanticdb-table-javascript) class &optional tags)
229 "In TABLE, find all occurrences of tags of CLASS.
230 Optional argument TAGS is a list of tags to search.
231 Returns a table of all matching tags."
232 (if tags (call-next-method)
233 ;; YOUR IMPLEMENTATION HERE
235 ;; Note: This search method could be considered optional in an
236 ;; omniscient database. It may be unwise to return all tags
237 ;; that exist for a language that are a variable or function.
239 ;; If it is optional, you can just delete this method.
240 nil))
242 ;;; Deep Searches
244 ;; If your language does not have a `deep' concept, these can be left
245 ;; alone, otherwise replace with implementations similar to those
246 ;; above.
248 (defmethod semanticdb-deep-find-tags-by-name-method
249 ((table semanticdb-table-javascript) name &optional tags)
250 "Find all tags name NAME in TABLE.
251 Optional argument TAGS is a list of tags to search.
252 Like `semanticdb-find-tags-by-name-method' for javascript."
253 (semanticdb-find-tags-by-name-method table name tags))
255 (defmethod semanticdb-deep-find-tags-by-name-regexp-method
256 ((table semanticdb-table-javascript) regex &optional tags)
257 "Find all tags with name matching REGEX in TABLE.
258 Optional argument TAGS is a list of tags to search.
259 Like `semanticdb-find-tags-by-name-method' for javascript."
260 (semanticdb-find-tags-by-name-regexp-method table regex tags))
262 (defmethod semanticdb-deep-find-tags-for-completion-method
263 ((table semanticdb-table-javascript) prefix &optional tags)
264 "In TABLE, find all occurrences of tags matching PREFIX.
265 Optional argument TAGS is a list of tags to search.
266 Like `semanticdb-find-tags-for-completion-method' for javascript."
267 (semanticdb-find-tags-for-completion-method table prefix tags))
269 ;;; Advanced Searches
271 (defmethod semanticdb-find-tags-external-children-of-type-method
272 ((table semanticdb-table-javascript) type &optional tags)
273 "Find all nonterminals which are child elements of TYPE.
274 Optional argument TAGS is a list of tags to search.
275 Return a list of tags."
276 (if tags (call-next-method)
277 ;; YOUR IMPLEMENTATION HERE
279 ;; OPTIONAL: This could be considered an optional function. It is
280 ;; used for `semantic-adopt-external-members' and may not
281 ;; be possible to do in your language.
283 ;; If it is optional, you can just delete this method.
286 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
287 (defun semanticdb-javascript-strip-tags (tags)
288 "Strip TAGS from overlays and reparse symbols."
289 (cond ((and (consp tags) (eq 'reparse-symbol (car tags)))
290 nil)
291 ((overlayp tags) nil)
292 ((atom tags) tags)
293 (t (cons (semanticdb-javascript-strip-tags
294 (car tags)) (semanticdb-javascript-strip-tags
295 (cdr tags))))))
297 ;this list was made from a javascript file, and the above function
298 ;; function eval(x){}
299 ;; function parseInt(string,radix){}
300 ;; function parseFloat(string){}
301 ;; function isNaN(number){}
302 ;; function isFinite(number){}
303 ;; function decodeURI(encodedURI){}
304 ;; function decodeURIComponent (encodedURIComponent){}
305 ;; function encodeURI (uri){}
306 ;; function encodeURIComponent (uriComponent){}
308 (provide 'semantic/db-javascript)
310 ;;; semantic/db-javascript.el ends here