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