Commit | Line | Data |
---|---|---|
691a065e | 1 | ;;; semantic/db-javascript.el --- Semantic database extensions for javascript |
f273dfc6 | 2 | |
5df4f04c | 3 | ;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 |
cb758101 | 4 | ;; Free Software Foundation, Inc. |
f273dfc6 CY |
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) | |
b90caf50 CY |
38 | (require 'eieio-opt)) |
39 | ||
f273dfc6 CY |
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. | |
db9e401b | 80 | See bottom of this file for instructions on managing this list.") |
f273dfc6 CY |
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 | |
db9e401b | 90 | eieio-singleton ;this db is for js globals, so singleton is appropriate |
f273dfc6 CY |
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. | |
db9e401b | 110 | It is not necessary to do system or recursive searching because of |
f273dfc6 CY |
111 | the 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. | |
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. | |
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. | |
045b9da7 | 148 | Equivalent modes are specified by the `semantic-equivalent-major-modes' |
f273dfc6 | 149 | local variable." |
0816d744 | 150 | (with-current-buffer buffer |
f273dfc6 CY |
151 | (eq (or mode-local-active-mode major-mode) 'javascript-mode))) |
152 | ||
153 | ;;; Usage | |
154 | ;; | |
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) | |
db9e401b | 160 | "Return a list of semanticdb tables associated with PATH. |
f273dfc6 CY |
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) | |
169 | ) | |
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))))) | |
180 | ||
181 | ;;; Search Overrides | |
182 | ;; | |
183 | ;; NOTE WHEN IMPLEMENTING: Be sure to add doc-string updates explaining | |
184 | ;; how your new search routines are implemented. | |
185 | ;; | |
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)) | |
195 | ||
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) | |
204 | )) | |
205 | ||
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) | |
214 | ||
215 | )) | |
216 | ||
217 | (defmethod semanticdb-find-tags-for-completion-method | |
218 | ((table semanticdb-table-javascript) prefix &optional tags) | |
db9e401b | 219 | "In TABLE, find all occurrences of tags matching PREFIX. |
f273dfc6 CY |
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 ".*")) | |
225 | )) | |
226 | ||
227 | (defmethod semanticdb-find-tags-by-class-method | |
228 | ((table semanticdb-table-javascript) class &optional tags) | |
db9e401b | 229 | "In TABLE, find all occurrences of tags of CLASS. |
f273dfc6 CY |
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 | |
234 | ;; | |
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. | |
238 | ;; | |
239 | ;; If it is optional, you can just delete this method. | |
240 | nil)) | |
241 | ||
242 | ;;; Deep Searches | |
243 | ;; | |
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. | |
247 | ;; | |
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. | |
db9e401b | 251 | Optional argument TAGS is a list of tags to search. |
f273dfc6 CY |
252 | Like `semanticdb-find-tags-by-name-method' for javascript." |
253 | (semanticdb-find-tags-by-name-method table name tags)) | |
254 | ||
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)) | |
261 | ||
262 | (defmethod semanticdb-deep-find-tags-for-completion-method | |
263 | ((table semanticdb-table-javascript) prefix &optional tags) | |
db9e401b | 264 | "In TABLE, find all occurrences of tags matching PREFIX. |
f273dfc6 CY |
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)) | |
268 | ||
269 | ;;; Advanced Searches | |
270 | ;; | |
271 | (defmethod semanticdb-find-tags-external-children-of-type-method | |
272 | ((table semanticdb-table-javascript) type &optional tags) | |
db9e401b | 273 | "Find all nonterminals which are child elements of TYPE. |
f273dfc6 CY |
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 | |
278 | ;; | |
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. | |
282 | ;; | |
283 | ;; If it is optional, you can just delete this method. | |
284 | )) | |
285 | ||
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)))))) | |
296 | ||
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){} | |
307 | ||
691a065e | 308 | (provide 'semantic/db-javascript) |
f273dfc6 | 309 | |
3999968a | 310 | ;; arch-tag: 69cf8ad1-7ea9-41af-851c-41f24c873374 |
691a065e | 311 | ;;; semantic/db-javascript.el ends here |