cedet/semantic/db-debug.el: Don't require semantic/db-mode, since
[bpt/emacs.git] / lisp / cedet / semantic / db-javascript.el
CommitLineData
691a065e 1;;; semantic/db-javascript.el --- Semantic database extensions for javascript
f273dfc6
CY
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
691a065e
CY
32(require 'semantic/db)
33(require 'semantic/db-find)
34
f273dfc6
CY
35(eval-when-compile
36 ;; For generic function searching.
37 (require 'eieio)
38 (require 'eieio-opt)
39 )
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.
80See bottom of this file for instruction on managing this list.")
81
82;;; Classes:
83(defclass semanticdb-table-javascript (semanticdb-search-results-table)
84 ((major-mode :initform javascript-mode)
85 )
86 "A table for returning search results from javascript.")
87
88(defclass semanticdb-project-database-javascript
89 (semanticdb-project-database
90 eieio-singleton ;this db is for js globals, so singleton is apropriate
91 )
92 ((new-table-class :initform semanticdb-table-javascript
93 :type class
94 :documentation
95 "New tables created for this database are of this class.")
96 )
97 "Database representing javascript.")
98
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.")
104
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.
110It is not necessary to to system or recursive searching because of
111the omniscience database.")
112
113;;; Filename based methods
114;;
115(defmethod semanticdb-get-database-tables ((obj semanticdb-project-database-javascript))
116 "For a javascript database, there are no explicit tables.
117Create 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.
121
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)
129 ))
130 (call-next-method)
131 )
132
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))
137 )
138
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)
145
146(defmethod semanticdb-equivalent-mode ((table semanticdb-table-javascript) &optional buffer)
147 "Return non-nil if TABLE's mode is equivalent to BUFFER.
148Equivalent modes are specified by by `semantic-equivalent-major-modes'
149local variable."
150 (save-excursion
151 (set-buffer buffer)
152 (eq (or mode-local-active-mode major-mode) 'javascript-mode)))
153
154;;; Usage
155;;
156;; Unlike other tables, an omniscent database does not need to
157;; be associated with a path. Use this routine to always add ourselves
158;; to a search list.
159(define-mode-local-override semanticdb-find-translate-path javascript-mode
160 (path brutish)
161 "Return a list of semanticdb tables asociated with PATH.
162If brutish, do the default action.
163If not brutish, do the default action, and append the system
164database (if available.)"
165 (let ((default
166 ;; When we recurse, disable searching of system databases
167 ;; so that our Javascript database only shows up once when
168 ;; we append it in this iteration.
169 (let ((semanticdb-search-system-databases nil)
170 )
171 (semanticdb-find-translate-path-default path brutish))))
172 ;; Don't add anything if BRUTISH is on (it will be added in that fcn)
173 ;; or if we aren't supposed to search the system.
174 (if (or brutish (not semanticdb-search-system-databases))
175 default
176 (let ((tables (apply #'append
177 (mapcar
178 (lambda (db) (semanticdb-get-database-tables db))
179 semanticdb-project-system-databases))))
180 (append default tables)))))
181
182;;; Search Overrides
183;;
184;; NOTE WHEN IMPLEMENTING: Be sure to add doc-string updates explaining
185;; how your new search routines are implemented.
186;;
187(defun semanticdb-javascript-regexp-search (regexp)
188 "Search for REGEXP in our fixed list of javascript tags."
189 (let* ((tags semanticdb-javascript-tags)
190 (result nil))
191 (while tags
192 (if (string-match regexp (caar tags))
193 (setq result (cons (car tags) result)))
194 (setq tags (cdr tags)))
195 result))
196
197(defmethod semanticdb-find-tags-by-name-method
198 ((table semanticdb-table-javascript) name &optional tags)
199 "Find all tags named NAME in TABLE.
200Return a list of tags."
201 (if tags
202 ;; If TAGS are passed in, then we don't need to do work here.
203 (call-next-method)
204 (assoc-string name semanticdb-javascript-tags)
205 ))
206
207(defmethod semanticdb-find-tags-by-name-regexp-method
208 ((table semanticdb-table-javascript) regex &optional tags)
209 "Find all tags with name matching REGEX in TABLE.
210Optional argument TAGS is a list of tags to search.
211Return a list of tags."
212 (if tags (call-next-method)
213 ;; YOUR IMPLEMENTATION HERE
214 (semanticdb-javascript-regexp-search regex)
215
216 ))
217
218(defmethod semanticdb-find-tags-for-completion-method
219 ((table semanticdb-table-javascript) prefix &optional tags)
220 "In TABLE, find all occurances of tags matching PREFIX.
221Optional argument TAGS is a list of tags to search.
222Returns a table of all matching tags."
223 (if tags (call-next-method)
224 ;; YOUR IMPLEMENTATION HERE
225 (semanticdb-javascript-regexp-search (concat "^" prefix ".*"))
226 ))
227
228(defmethod semanticdb-find-tags-by-class-method
229 ((table semanticdb-table-javascript) class &optional tags)
230 "In TABLE, find all occurances of tags of CLASS.
231Optional argument TAGS is a list of tags to search.
232Returns a table of all matching tags."
233 (if tags (call-next-method)
234 ;; YOUR IMPLEMENTATION HERE
235 ;;
236 ;; Note: This search method could be considered optional in an
237 ;; omniscient database. It may be unwise to return all tags
238 ;; that exist for a language that are a variable or function.
239 ;;
240 ;; If it is optional, you can just delete this method.
241 nil))
242
243;;; Deep Searches
244;;
245;; If your language does not have a `deep' concept, these can be left
246;; alone, otherwise replace with implementations similar to those
247;; above.
248;;
249(defmethod semanticdb-deep-find-tags-by-name-method
250 ((table semanticdb-table-javascript) name &optional tags)
251 "Find all tags name NAME in TABLE.
252Optional argument TAGS is a list of tags t
253Like `semanticdb-find-tags-by-name-method' for javascript."
254 (semanticdb-find-tags-by-name-method table name tags))
255
256(defmethod semanticdb-deep-find-tags-by-name-regexp-method
257 ((table semanticdb-table-javascript) regex &optional tags)
258 "Find all tags with name matching REGEX in TABLE.
259Optional argument TAGS is a list of tags to search.
260Like `semanticdb-find-tags-by-name-method' for javascript."
261 (semanticdb-find-tags-by-name-regexp-method table regex tags))
262
263(defmethod semanticdb-deep-find-tags-for-completion-method
264 ((table semanticdb-table-javascript) prefix &optional tags)
265 "In TABLE, find all occurances of tags matching PREFIX.
266Optional argument TAGS is a list of tags to search.
267Like `semanticdb-find-tags-for-completion-method' for javascript."
268 (semanticdb-find-tags-for-completion-method table prefix tags))
269
270;;; Advanced Searches
271;;
272(defmethod semanticdb-find-tags-external-children-of-type-method
273 ((table semanticdb-table-javascript) type &optional tags)
274 "Find all nonterminals which are child elements of TYPE
275Optional argument TAGS is a list of tags to search.
276Return a list of tags."
277 (if tags (call-next-method)
278 ;; YOUR IMPLEMENTATION HERE
279 ;;
280 ;; OPTIONAL: This could be considered an optional function. It is
281 ;; used for `semantic-adopt-external-members' and may not
282 ;; be possible to do in your language.
283 ;;
284 ;; If it is optional, you can just delete this method.
285 ))
286
287;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
288(defun semanticdb-javascript-strip-tags (tags)
289 "Strip TAGS from overlays and reparse symbols."
290 (cond ((and (consp tags) (eq 'reparse-symbol (car tags)))
291 nil)
292 ((overlayp tags) nil)
293 ((atom tags) tags)
294 (t (cons (semanticdb-javascript-strip-tags
295 (car tags)) (semanticdb-javascript-strip-tags
296 (cdr tags))))))
297
298;this list was made from a javascript file, and the above function
299;; function eval(x){}
300;; function parseInt(string,radix){}
301;; function parseFloat(string){}
302;; function isNaN(number){}
303;; function isFinite(number){}
304;; function decodeURI(encodedURI){}
305;; function decodeURIComponent (encodedURIComponent){}
306;; function encodeURI (uri){}
307;; function encodeURIComponent (uriComponent){}
308
309
691a065e 310(provide 'semantic/db-javascript)
f273dfc6 311
691a065e 312;;; semantic/db-javascript.el ends here