0
|
1 ;;!emacs
|
|
2 ;;
|
|
3 ;; FILE: br-java-ft.el
|
|
4 ;; SUMMARY: Java OO-Browser class and member functions.
|
|
5 ;; USAGE: GNU Emacs Lisp Library
|
100
|
6 ;; KEYWORDS: c, oop, tools
|
0
|
7 ;;
|
|
8 ;; AUTHOR: Bob Weiner
|
100
|
9 ;; ORG: InfoDock Associates
|
0
|
10 ;;
|
|
11 ;; ORIG-DATE: 01-Aug-95
|
100
|
12 ;; LAST-MOD: 13-Nov-96 at 00:08:46 by Bob Weiner
|
0
|
13 ;;
|
100
|
14 ;; Copyright (C) 1995, 1996 Free Software Foundation, Inc.
|
0
|
15 ;; See the file BR-COPY for license information.
|
|
16 ;;
|
|
17 ;; This file is part of the OO-Browser.
|
|
18 ;;
|
|
19 ;; DESCRIPTION:
|
|
20 ;; DESCRIP-END.
|
|
21
|
|
22 ;;; ************************************************************************
|
|
23 ;;; Other required Elisp libraries
|
|
24 ;;; ************************************************************************
|
|
25
|
|
26 (mapcar 'require '(br-c-ft br-java))
|
|
27
|
|
28 ;;; ************************************************************************
|
|
29 ;;; Public variables
|
|
30 ;;; ************************************************************************
|
|
31
|
|
32 (defconst java-return-type-identifier
|
100
|
33 (concat "\\([\[a-zA-Z][][" java-return-type-chars "]*"
|
|
34 "[][" java-return-type-chars "]+\\|[\[a-zA-Z]\\)"
|
0
|
35 "[ \t\n\^M]*"))
|
|
36
|
|
37 (defconst java-type-identifier
|
100
|
38 (concat "\\([\[a-zA-Z][][" java-identifier-chars "]*[ \t\n\^M]+\\)"))
|
0
|
39
|
|
40 (defconst java-type-tag-separator "@"
|
|
41 "String that separates a tag's type from its normalized definition form.
|
|
42 This should be a single character which is unchanged when quoted for use as a
|
|
43 literal in a regular expression.")
|
|
44
|
|
45 (defconst java-tag-fields-regexp
|
|
46 ;; The \\\\? below is necessary because we sometimes use this expression to
|
|
47 ;; test against a string that has been regexp-quoted and some of the
|
|
48 ;; characters in br-feature-type-regexp will then be preceded by \\.
|
100
|
49 (format "\\`\\([^%s \n]+\\)%s\\\\?\\(%s \\)\\([^%s\n]+\\)%s?"
|
0
|
50 java-type-tag-separator java-type-tag-separator
|
|
51 br-feature-type-regexp java-type-tag-separator
|
|
52 java-type-tag-separator)
|
|
53 "Regexp matching the fields of a java feature tag line.
|
|
54 Group 1 is the class of the feature. Group 2 is the prefix preceding the
|
|
55 feature when displayed within a listing buffer. Group 3 is the feature name.
|
|
56 The feature definition signature begins at the end of the regexp match,
|
|
57 i.e. (match-end 0), and goes to the end of the string or line.")
|
|
58
|
|
59 ;;; ************************************************************************
|
|
60 ;;; Public functions
|
|
61 ;;; ************************************************************************
|
|
62
|
|
63 (defun java-member-p ()
|
|
64 "Prints whether entity at point is a java member definition or declaration."
|
|
65 (interactive)
|
|
66 (let ((name))
|
|
67 (save-excursion
|
|
68 (message
|
|
69 (concat
|
|
70 "Is " (if (java-feature-def-p)
|
|
71 (progn (setq name
|
|
72 (buffer-substring (match-beginning
|
|
73 java-feature-name-grpn)
|
|
74 (match-end
|
|
75 java-feature-name-grpn)))
|
|
76 "")
|
|
77 "not ")
|
|
78 "a def. "
|
|
79 "Is " (if (and (java-skip-to-statement) (java-feature-decl))
|
|
80 (progn (setq name
|
|
81 (buffer-substring (match-beginning
|
|
82 java-feature-name-grpn)
|
|
83 (match-end
|
|
84 java-feature-name-grpn)))
|
|
85 "")
|
|
86 "not ")
|
|
87 "a member decl. "
|
|
88 (if name (concat " Name = " name)))))))
|
|
89
|
|
90 (defun java-feature-implementors (name)
|
|
91 "Return unsorted list of java feature tags which implement feature NAME.
|
|
92 This includes classes which declare abstract functions with NAME."
|
|
93 (java-feature-matches (concat "^" (regexp-quote name) "$")))
|
|
94
|
|
95 (defun java-feature-locate-p (feature-tag &optional regexp-flag)
|
|
96 "Leaves point at the start of FEATURE-TAG's definition in the current buffer.
|
|
97 Assumes caller has moved point to the beginning of the buffer or to the point
|
|
98 of desired search start.
|
|
99 Optional REGEXP-FLAG means FEATURE-TAG is a regular expression."
|
|
100 ;; Match to function definitions, not declarations, except for abstract
|
|
101 ;; methods which are declared, not defined, and so end with a ';'.
|
|
102 ;;
|
100
|
103 ;; First move to the proper class implementation if this is not a
|
|
104 ;; [default-class], so that if two classes in the same file have the same
|
|
105 ;; feature signature, we still end up at the right one.
|
|
106 (let ((found t) (start))
|
|
107 (if (string-match java-tag-fields-regexp feature-tag)
|
|
108 (let ((class (substring feature-tag (match-beginning 1) (match-end 1))))
|
|
109 (setq feature-tag (substring feature-tag (match-end 0))
|
|
110 found (re-search-forward
|
|
111 (java-class-definition-regexp class regexp-flag) nil t))))
|
|
112
|
|
113 ;; If class was searched for and not found, return nil.
|
|
114 (if (not found)
|
|
115 nil
|
|
116 ;; Otherwise, look for feature expression.
|
|
117 (or regexp-flag (setq feature-tag
|
|
118 (java-feature-signature-to-regexp feature-tag)))
|
|
119 (while (and (re-search-forward feature-tag nil t)
|
|
120 (setq start (match-beginning 0))
|
|
121 (not (setq found (not
|
|
122 (if (c-within-comment-p)
|
|
123 (progn (search-forward "*/" nil t)
|
|
124 t)))))))
|
|
125 (if found
|
|
126 (progn (goto-char start)
|
|
127 (skip-chars-forward " \t\n\^M")
|
|
128 (java-to-comments-begin)
|
|
129 (recenter 0)
|
|
130 (goto-char start)
|
|
131 t)))))
|
|
132
|
|
133 (defun java-feature-map-class-tags (function class)
|
|
134 "Apply FUNCTION to all feature tags from CLASS and return a list of the results.
|
|
135 Feature tags come from the file named by br-feature-tags-file."
|
|
136 (let ((obuf (current-buffer))
|
|
137 (class-tag (concat "\n" class java-type-tag-separator))
|
|
138 (results)
|
|
139 start end)
|
|
140 (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
|
|
141 (goto-char 1)
|
|
142 (if (not (search-forward class-tag nil t))
|
|
143 nil
|
|
144 (setq start (match-beginning 0)
|
|
145 end (if (search-forward "\^L\n" nil t)
|
|
146 (match-beginning 0)
|
|
147 (point-max)))
|
|
148 (goto-char start)
|
|
149 ;; Feature defs can occur only within a single file.
|
|
150 (while (search-forward class-tag end t)
|
|
151 (setq results (cons (funcall function) results))
|
|
152 ;; Might have deleted current tag and would miss next tag unless point
|
|
153 ;; is moved backwards.
|
|
154 (backward-char)))
|
|
155 (set-buffer obuf)
|
|
156 results))
|
0
|
157
|
|
158 (defun java-feature-name-to-regexp (name)
|
|
159 "Converts routine NAME into a regular expression matching the routine's name tag."
|
100
|
160 (java-feature-signature-to-regexp name))
|
0
|
161
|
|
162 (defun java-feature-signature-to-name (signature &optional with-class for-display)
|
|
163 "Extracts the feature name from SIGNATURE.
|
|
164 The feature's class name is dropped from signature unless optional WITH-CLASS
|
|
165 is non-nil. If optional FOR-DISPLAY is non-nil, a feature type character is
|
|
166 prepended to the name for display in a browser listing."
|
|
167 (let ((name))
|
|
168 (cond
|
|
169 ;; member
|
|
170 ((string-match java-tag-fields-regexp signature)
|
|
171 (setq name (substring signature (match-beginning (if for-display 2 3))
|
|
172 (match-end 3)))
|
|
173 (if with-class
|
|
174 (setq name (concat
|
|
175 (substring signature (match-beginning 1) (match-end 1))
|
100
|
176 java-type-tag-separator name)))
|
0
|
177 ;; Remove any trailing whitespace.
|
|
178 (br-delete-space name))
|
|
179 ;;
|
|
180 ;; unknown
|
|
181 (t ;; Remove any trailing whitespace and add display prefix.
|
|
182 (setq name (br-delete-space signature))
|
|
183 (if for-display (java-feature-add-prefix name "" signature) name)))))
|
|
184
|
|
185 (defun java-feature-signature-to-regexp (signature)
|
|
186 "Given a java SIGNATURE, return regexp used to match to its definition."
|
|
187 (setq signature (regexp-quote signature))
|
|
188 (let ((prefix-info
|
|
189 (if (string-match java-tag-fields-regexp signature)
|
|
190 (prog1 (substring signature (match-beginning 0) (match-end 0))
|
|
191 (setq signature (substring signature (match-end 0)))))))
|
|
192 (let ((pat) (i 0) (c) (len (length signature)))
|
|
193 (while (< i len)
|
|
194 (setq c (aref signature i)
|
|
195 pat (cond ((= c ? )
|
|
196 ;; Allow for possible single line comment
|
|
197 ;; following any whitespace, e.g. following
|
|
198 ;; each routine argument.
|
|
199 (concat pat "[ \t\n\^M]*\\(//.*\\)?"))
|
|
200 (t
|
|
201 (concat pat (char-to-string c))))
|
|
202 i (1+ i)))
|
|
203 (setq pat (concat prefix-info pat)))))
|
|
204
|
100
|
205 (defun java-feature-tag-regexp (class feature-name)
|
|
206 "Return a regexp that matches to the feature tag entry for CLASS' FEATURE-NAME."
|
|
207 (concat "^" (regexp-quote class) java-type-tag-separator
|
|
208 br-feature-type-regexp " "
|
|
209 (regexp-quote feature-name) java-type-tag-separator))
|
|
210
|
0
|
211 (defun java-feature-tree-command-p (class-or-signature)
|
|
212 "Display definition of CLASS-OR-SIGNATURE if a signature and return t, else return nil."
|
100
|
213 ;; A class name won't contain a space.
|
|
214 (if (string-match " " class-or-signature)
|
0
|
215 (progn
|
|
216 (if (br-in-browser) (br-to-view-window))
|
|
217 (br-feature-found-p (br-feature-file class-or-signature)
|
|
218 class-or-signature))))
|
|
219
|
|
220 (defun java-list-features (class &optional indent)
|
100
|
221 "Return sorted list of Java feature tags lexically defined in CLASS.
|
|
222 Optional INDENT is unused but is required for multi-language OO-Browser conformance."
|
|
223 ;; Use nreverse here so that stable sort ends up leaving same named
|
|
224 ;; features in the order they were defined in the source file.
|
|
225 (java-sort-features
|
|
226 (nreverse (java-feature-map-class-tags 'br-feature-current class))))
|
0
|
227
|
100
|
228 (defun java-sort-features (feature-list)
|
|
229 (sort feature-list 'java-feature-lessp))
|
0
|
230
|
|
231 (defun java-to-definition (&optional other-win)
|
|
232 "If point is within a declaration, try to move to its definition.
|
|
233 With OTHER-WIN non-nil, show it in another window."
|
|
234 (interactive)
|
|
235 (let ((opoint (point)))
|
|
236 (cond
|
|
237 ((br-check-for-class (java-class-decl-p) other-win))
|
|
238 ((java-feature other-win))
|
|
239 ((and (goto-char opoint)
|
|
240 (br-check-for-class (java-find-class-name) other-win)))
|
|
241 (t (beep)
|
|
242 (message
|
|
243 "(OO-Browser): Select a java declaration to move to its definition.")
|
|
244 nil))))
|
|
245
|
|
246 ;;; ************************************************************************
|
|
247 ;;; Private functions
|
|
248 ;;; ************************************************************************
|
|
249
|
|
250 (defun java-class-decl-p ()
|
|
251 "Return nil unless point is within a class declaration, referenced by another
|
|
252 class. Commented declarations also return nil. When value is non-nil, it is
|
|
253 the class name from the declaration. Leave point at start of statement for
|
|
254 visual clarity."
|
|
255 (java-skip-to-statement)
|
|
256 (save-excursion
|
|
257 (let ((class))
|
|
258 (and (looking-at java-class-decl)
|
|
259 (setq class (buffer-substring
|
|
260 (match-beginning java-class-name-grpn)
|
|
261 (match-end java-class-name-grpn)))
|
|
262 (not (c-within-comment-p))
|
|
263 (progn (beginning-of-line)
|
|
264 (not (looking-at "[ \t]*//")))
|
|
265 class))))
|
|
266
|
|
267 (defun java-feature (&optional other-win)
|
|
268 "Move point to definition of member given by declaration at point.
|
|
269 Return nil if point is not within a member declaration."
|
|
270 (interactive)
|
|
271 (let ((feature-def) (ftr) (class) (ftr-pat))
|
|
272 (cond ((java-feature-def-p)
|
|
273 (recenter 0)
|
|
274 t)
|
100
|
275 ;; Now look for feature definition in ancestor classes.
|
0
|
276 ((progn (setq feature-def (java-feature-def-pat)
|
|
277 ftr (car (cdr (cdr feature-def)))
|
|
278 class (car (cdr feature-def))
|
|
279 ftr-pat (car feature-def))
|
|
280 (java-locate-feature ftr class ftr-pat other-win)))
|
|
281 ((java-feature-decl)
|
|
282 (beep)
|
|
283 (message "(OO-Browser): '%s' feature definition not found." ftr)
|
|
284 t))))
|
|
285
|
|
286 (defun java-feature-add-prefix (feature-name class signature)
|
|
287 "Add a browser listing display prefix to FEATURE-NAME from CLASS based on feature's SIGNATURE."
|
|
288 (concat (cond ((string-match java-native-method-regexp signature)
|
|
289 "/ ")
|
|
290 ((string-match java-abstract-method-regexp signature)
|
|
291 "> ")
|
100
|
292 ;; constructors and destructors
|
0
|
293 ((or (string-equal feature-name class)
|
|
294 (string-equal feature-name "finalize"))
|
|
295 "+ ")
|
100
|
296 ;; attributes
|
|
297 ((string-match "[=;,]\\'" signature)
|
|
298 "= ")
|
|
299 ;; regular methods
|
0
|
300 (t "- "))
|
|
301 feature-name))
|
|
302
|
|
303 (defun java-feature-decl ()
|
|
304 (if (looking-at java-class-decl)
|
|
305 nil
|
|
306 (looking-at java-feature-decl)))
|
|
307
|
|
308 (defun java-feature-def-p ()
|
100
|
309 "Return nil unless point is within a member declaration.
|
|
310 Commented member declarations also return nil.
|
0
|
311 Leaves point at start of statement for visual clarity."
|
|
312 (java-skip-to-statement)
|
100
|
313 (and (not (c-within-comment-p))
|
|
314 (save-excursion (beginning-of-line)
|
|
315 (not (looking-at "[ \t]*//")))
|
|
316 (not (looking-at java-class-decl))
|
|
317 (looking-at (concat java-at-feature-regexp "[=;,{]"))
|
|
318 (or (= ?\{ (char-after (1- (match-end 0))))
|
|
319 (if (not (match-end java-feature-parens-grpn))
|
|
320 ;; This is an attribute.
|
|
321 t
|
|
322 ;; If this is a native or abstract method, alert user that
|
|
323 ;; its definition is elsewhere.
|
|
324 (save-restriction
|
|
325 (narrow-to-region (match-beginning 0) (match-end 0))
|
|
326 (cond ((looking-at "\\s *\\<abstract\\>[^;{}]+;")
|
|
327 (message "(OO-Browser): Abstract method, definition deferred to descendants.")
|
|
328 t)
|
|
329 ((looking-at "\\s *\\<native\\>[^;{}]+;")
|
|
330 (message "(OO-Browser): Native method, defined in an external language.")
|
|
331 t)))))))
|
|
332
|
0
|
333
|
|
334 (defun java-feature-def-pat ()
|
|
335 "Return (list <feature-def-pat> <feature-class> <feature-name>) associated with declaration at point."
|
|
336 (and (java-skip-to-statement)
|
|
337 (java-feature-decl)
|
|
338 ;; Don't regexp-quote member-name yet
|
|
339 (let* ((member-name (buffer-substring
|
|
340 (match-beginning java-feature-name-grpn)
|
|
341 (match-end java-feature-name-grpn)))
|
|
342 (member-modifiers (if (match-end java-feature-mod-grpn)
|
|
343 (br-quote-match java-feature-mod-grpn)))
|
|
344 (class)
|
|
345 (member-type
|
|
346 (concat (and (match-end java-feature-type-grpn)
|
|
347 ;; Handle possible regexp bug
|
|
348 (not
|
|
349 (equal
|
|
350 (match-beginning java-feature-type-grpn)
|
|
351 (match-beginning java-feature-name-grpn)))
|
|
352 (concat (br-quote-match
|
|
353 java-feature-type-grpn)))))
|
|
354 (func-args (if (match-end java-feature-parens-grpn)
|
|
355 (cons (match-beginning java-feature-parens-grpn)
|
100
|
356 (match-end java-feature-parens-grpn)))))
|
0
|
357
|
|
358 (and member-type (string-match "[ \t]+$" member-type)
|
|
359 (setq member-type (substring member-type 0
|
|
360 (match-beginning 0))))
|
|
361 (and (stringp member-type)
|
|
362 (not (equal member-type ""))
|
|
363 (setq member-type (concat member-type "[ \t\n]*")))
|
|
364
|
|
365 (let ((pre-member-regexp
|
|
366 (concat
|
|
367 java-type-modifier-keyword
|
|
368 (if member-modifiers
|
|
369 (let ((def-mods "") (mod))
|
|
370 (while (string-match "\\([a-z]+\\)[ \t\n]+"
|
|
371 member-modifiers)
|
|
372 (setq mod (substring member-modifiers
|
|
373 (match-beginning 1)
|
|
374 (match-end 1))
|
|
375 member-modifiers (substring member-modifiers
|
|
376 (match-end 0)))
|
|
377 (if (equal (string-match
|
|
378 java-type-def-modifier mod) 0)
|
|
379 (setq def-mods (concat def-mods "\\(" mod
|
|
380 "[ \t\n]+\\)?"))))
|
|
381 def-mods))
|
|
382 ))
|
|
383 (post-member-regexp
|
|
384 (concat
|
|
385 ;; Point at beginning of line may imply a non-member func.
|
|
386 (progn
|
|
387 ;; Class name is not part of declaration
|
|
388 ;; so look for declaration within a
|
|
389 ;; class definition and locate the class
|
|
390 ;; name. If not within a class, assume
|
|
391 ;; declaration is global.
|
|
392 (setq class (java-get-class-name-from-source))
|
|
393 (br-regexp-quote member-name))
|
|
394 "[ \t\n]*"
|
|
395 (if func-args
|
|
396 (concat "\\(" (java-func-args-regexp func-args)
|
|
397 "\\|" (java-func-args-string func-args)
|
|
398 "\\)"))
|
100
|
399 "[ \t\n]*")))
|
0
|
400 (list
|
|
401 (` (lambda (class)
|
|
402 (concat "^" (br-regexp-quote class)
|
|
403 (, (concat
|
|
404 java-type-tag-separator
|
|
405 br-feature-type-regexp " "
|
|
406 (br-regexp-quote member-name)
|
|
407 java-type-tag-separator
|
|
408 pre-member-regexp))
|
|
409 (br-regexp-quote class)
|
|
410 (, post-member-regexp))))
|
|
411 class member-name)))))
|
|
412
|
100
|
413 (defun java-feature-display (class-list ftr-pat &optional other-win)
|
|
414 "Display feature declaration derived from CLASS-LIST, matching FTR-PAT.
|
|
415 Use feature tags table to locate a match. Caller must use 'set-buffer'
|
0
|
416 to restore prior buffer when a match is not found."
|
|
417 (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
|
|
418 (let ((classes class-list)
|
|
419 (found-ftr)
|
|
420 (ftr-regexp)
|
|
421 (class)
|
|
422 (ftr-path))
|
|
423 (if (or (null class-list) (equal class-list '(nil)))
|
|
424 nil
|
|
425 (while (and (not found-ftr) classes)
|
|
426 (setq class (car classes)
|
|
427 ftr-regexp (funcall ftr-pat class)
|
|
428 ftr-path (br-feature-def-file ftr-regexp)
|
|
429 found-ftr (if ftr-path
|
100
|
430 (br-edit-feature-from-tag (br-feature-current)
|
|
431 ftr-path other-win))
|
0
|
432 classes (if found-ftr nil (cdr classes))))
|
|
433 (if found-ftr
|
|
434 (or class t)
|
100
|
435 (java-feature-display
|
0
|
436 (apply 'append (mapcar (function (lambda (cl) (br-get-parents cl)))
|
|
437 class-list))
|
|
438 ftr-pat)))))
|
|
439
|
100
|
440 (defun java-feature-lessp (routine1 routine2)
|
|
441 (string-lessp (java-feature-signature-to-name routine1)
|
|
442 (java-feature-signature-to-name routine2)))
|
|
443
|
|
444 (defun java-feature-map-tags (function regexp)
|
|
445 "Apply FUNCTION to all current feature tags that match REGEXP and return a list of the results.
|
|
446 Feature tags come from the file named by br-feature-tags-file."
|
|
447 ;; Ensure match to feature names only; also handle "^" and "$" meta-chars
|
|
448 (let ((identifier-chars (concat "[" java-identifier-chars "]*"))
|
|
449 (results))
|
|
450 (setq regexp
|
|
451 (concat (format "^[^%s \n]+%s%s "
|
|
452 java-type-tag-separator java-type-tag-separator
|
|
453 br-feature-type-regexp)
|
|
454 (if (equal (substring regexp 0 1) "^")
|
|
455 (progn (setq regexp (substring regexp 1)) nil)
|
|
456 identifier-chars)
|
|
457 (if (equal (substring regexp -1) "$")
|
|
458 (substring regexp 0 -1)
|
|
459 (concat regexp identifier-chars))
|
|
460 java-type-tag-separator))
|
|
461 (save-excursion
|
|
462 (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
|
|
463 (goto-char 1)
|
|
464 (while (re-search-forward regexp nil t)
|
|
465 (setq results (cons (funcall function) results))))
|
|
466 results))
|
|
467
|
|
468 (defun java-feature-matches (regexp)
|
|
469 "Return an unsorted list of feature tags whose names match in part or whole to REGEXP.
|
|
470 ^ and $ characters may be used to match to the beginning and end of a feature name,
|
|
471 respectively."
|
|
472 (java-feature-map-tags 'br-feature-current regexp))
|
|
473
|
|
474 (defun java-feature-normalize (feature class name)
|
|
475 (setq class (br-delete-space class)
|
|
476 name (java-feature-add-prefix name class feature)
|
|
477 feature (concat class java-type-tag-separator
|
|
478 name java-type-tag-separator
|
|
479 (br-delete-space feature)))
|
|
480 (let* ((len (length feature))
|
|
481 (normal-feature (make-string len ?\ ))
|
|
482 (n 0) (i 0)
|
|
483 (space-list '(?\ ?\t ?\n ?\^M))
|
|
484 (space-regexp "[ \t\n\^M]+")
|
|
485 chr)
|
|
486 (while (< i len)
|
|
487 (setq chr (aref feature i))
|
|
488 (cond
|
|
489 ;; Convert sequences of space characters to a single space.
|
|
490 ((memq chr space-list)
|
|
491 (aset normal-feature n ?\ )
|
|
492 (if (string-match space-regexp feature i)
|
|
493 (setq i (match-end 0)
|
|
494 n (1+ n))
|
|
495 (setq i (1+ i)
|
|
496 n (1+ n))))
|
|
497 ;;
|
|
498 ;; Remove // style comments
|
|
499 ((and (= chr ?/)
|
|
500 (< (1+ i) len)
|
|
501 (= (aref feature (1+ i)) ?/))
|
|
502 (setq i (+ i 2))
|
|
503 (while (and (< i len) (/= (aref feature i) ?\n))
|
|
504 (setq i (1+ i))))
|
|
505 ;;
|
|
506 (t ;; Normal character
|
|
507 (aset normal-feature n chr)
|
|
508 (setq i (1+ i)
|
|
509 n (1+ n)))))
|
|
510 (substring normal-feature 0 n)))
|
|
511
|
|
512 (defun java-feature-tag-class (signature)
|
|
513 "Extract the class name from SIGNATURE."
|
|
514 (cond ((string-match java-type-tag-separator signature)
|
|
515 (substring signature 0 (match-beginning 0)))
|
|
516 ((string-match "\\([^ \t]+\\)\." signature)
|
|
517 (substring signature (match-beginning 1) (match-end 1)))
|
|
518 (t "")))
|
|
519
|
0
|
520 (defun java-files-with-source (class)
|
|
521 "Use CLASS to compute set of files that match to a java source file regexp.
|
|
522 Return as a list."
|
|
523 (let ((file (if class (br-class-path class) buffer-file-name)))
|
|
524 (and file
|
|
525 (let* ((src-file-regexp (concat "^" (br-filename-head file)
|
|
526 java-code-file-regexp))
|
|
527 (dir (file-name-directory file))
|
|
528 (files (directory-files dir nil src-file-regexp)))
|
|
529 (mapcar (function (lambda (f) (concat dir f)))
|
|
530 files)))))
|
|
531
|
|
532 (defun java-find-ancestors-feature (class-list ftr-pat &optional other-win)
|
|
533 "Scan ancestors of CLASS-LIST and show routine definition matching FTR-PAT."
|
|
534 ;; If no class, search for non-member function.
|
|
535 (or class-list (setq class-list '(nil)))
|
|
536 (let ((obuf (current-buffer)))
|
|
537 (prog1
|
|
538 (if (and br-feature-tags-file
|
|
539 (file-exists-p br-feature-tags-file)
|
|
540 (file-readable-p br-feature-tags-file))
|
100
|
541 (java-feature-display class-list ftr-pat other-win)
|
0
|
542 ;; Only works if features are in same directory as class def.
|
|
543 (java-scan-ancestors-feature class-list ftr-pat other-win))
|
|
544 (set-buffer obuf))))
|
|
545
|
|
546 (defun java-find-class-name ()
|
|
547 "Return current word as a potential class name."
|
|
548 (save-excursion
|
|
549 (let* ((start)
|
|
550 (ignore "\]\[ \t\n;,.\(\){}-")
|
|
551 (pat (concat "^" ignore)))
|
|
552 (forward-char 1)
|
|
553 (skip-chars-backward ignore)
|
|
554 (skip-chars-backward pat)
|
|
555 (setq start (point))
|
100
|
556 (skip-chars-forward pat)
|
0
|
557 (buffer-substring start (point)))))
|
|
558
|
|
559 (defun java-func-args-regexp (func-args)
|
|
560 (let* ((space "\\\\\\s-*")
|
|
561 (obuf (current-buffer))
|
|
562 (tmp-buf-nm "*br-java-tmp*")
|
|
563 (tmp-buf (progn (if (get-buffer tmp-buf-nm)
|
|
564 (kill-buffer tmp-buf-nm))
|
|
565 (get-buffer-create tmp-buf-nm))))
|
|
566 (or tmp-buf (error "OO-Browser: (java-func-args-regexp) - Can't create tmp-buf."))
|
|
567 ;; Fill tmp-buffer with all func-args, including parens.
|
|
568 (copy-to-buffer tmp-buf (car func-args) (cdr func-args))
|
|
569
|
|
570 (set-buffer tmp-buf)
|
|
571 (let ((quoted-args (br-regexp-quote (buffer-substring
|
|
572 (point-min) (point-max)))))
|
|
573 (erase-buffer)
|
|
574 (insert quoted-args))
|
|
575
|
|
576 (goto-char (point-min))
|
|
577 (if (looking-at "(\\s-*)")
|
|
578 (replace-match "(\\\\s-*)" t)
|
|
579
|
|
580 ;; Replace all "\( +" with "\(" temporarily
|
|
581 (br-buffer-replace "\\(^\\|[^\\]\\)\([ \t\n]+" "\\1\(")
|
|
582
|
|
583 ;; Replace all "+ \)" with "\)" temporarily
|
|
584 (br-buffer-replace "[ \t\n]+\)" "\)")
|
|
585
|
|
586 ;; Replace all "...\)" with "...@" temporarily
|
|
587 (br-buffer-replace "\\\\\\.\\\\\\.\\\\\\.\)" "@@@")
|
|
588
|
|
589 ;; Optionalize right hand side of argument assignments.
|
|
590 (br-buffer-replace "\\([^=,\( \t\n]+\\)\\([ \t\n]*=[^,\)]+\\)"
|
|
591 (concat "\\1\\\\( "
|
|
592 (br-regexp-quote java-arg-identifier)
|
|
593 "\\\\)? \\\\(\\2\\\\)?"))
|
|
594
|
|
595 ;; Replace all "\)" with "optional <java-identifier> \)"
|
|
596 (br-buffer-replace
|
|
597 "\\([\(,][^=\)]+\\)\)"
|
|
598 (concat "\\1\\\\( " (br-regexp-quote java-arg-identifier)
|
|
599 "\\\\)?\)"))
|
|
600
|
|
601 ;; Replace all "," with "optional <java-identifier>,"
|
|
602 (br-buffer-replace
|
|
603 "\\([\(,][^=,]+\\),"
|
|
604 (concat "\\1\\\\( " (br-regexp-quote java-arg-identifier) "\\\\)?,"))
|
|
605
|
|
606 ;; Replace all " *, *" with "<spc>,<spc>"
|
|
607 (br-buffer-replace "[ \t\n]*,[ \t\n]*" (concat space "," space))
|
|
608
|
|
609 ;; Replace all " +" with "<spc>"
|
|
610 (br-buffer-replace "[ \t\n]+" space)
|
|
611
|
|
612 ;; Replace all "\(" with "\(<spc>"
|
|
613 (br-buffer-replace "\\(^\\|[^\\]\\)\(" (concat "\\1\(" space))
|
|
614
|
|
615 ;; Replace all "\)" with "<spc>\)"
|
|
616 (br-buffer-replace "\\([^\\]\\)\)" (concat "\\1" space "\)"))
|
|
617
|
|
618 ;; Replace all & and quoted \\* with "<spc>[*&]+<spc>"
|
|
619 (br-buffer-replace "\\(&\\|\\\\\\*\\)+" (concat space "\\1" space))
|
|
620
|
|
621 ;; Replace all "<spc>" with "[ \t\n]*"
|
|
622 (br-buffer-replace "\\\\s-\\*" "[ \t\n]*")
|
|
623
|
|
624 ;; Replace all "@@@" with any # of args
|
|
625 (br-buffer-replace "@@@" "[^\)]*\)")
|
|
626 )
|
|
627
|
|
628 ;; Return final buffer as a string.
|
|
629 (prog1 (buffer-substring (point-min) (point-max))
|
|
630 (kill-buffer tmp-buf-nm)
|
|
631 (set-buffer obuf))))
|
|
632
|
|
633 (defun java-func-args-string (func-args)
|
|
634 (let* ((space "\\\\\\s-*")
|
|
635 (obuf (current-buffer))
|
|
636 (tmp-buf-nm "*br-java-tmp*")
|
|
637 (tmp-buf (progn (if (get-buffer tmp-buf-nm)
|
|
638 (kill-buffer tmp-buf-nm))
|
|
639 (get-buffer-create tmp-buf-nm))))
|
|
640 (or tmp-buf (error "OO-Browser: (java-func-args-string) - Can't create tmp-buf."))
|
|
641 ;; Fill tmp-buffer with all func-args, including parens.
|
|
642 (copy-to-buffer tmp-buf (car func-args) (cdr func-args))
|
|
643
|
|
644 (set-buffer tmp-buf)
|
|
645 (let ((quoted-args (br-regexp-quote (buffer-substring
|
|
646 (point-min) (point-max)))))
|
|
647 (erase-buffer)
|
|
648 (insert quoted-args))
|
|
649
|
|
650 (goto-char (point-min))
|
|
651 (if (looking-at "(\\s-*)")
|
|
652 (replace-match "(\\\\s-*)" t)
|
|
653
|
|
654 ;; Replace all "\( +" with "\(" temporarily
|
|
655 (br-buffer-replace "\\(^\\|[^\\]\\)\([ \t\n]+" "\\1\(")
|
|
656
|
|
657 ;; Replace all "+ \)" with "\)" temporarily
|
|
658 (br-buffer-replace "[ \t\n]+\)" "\)")
|
|
659
|
|
660 ;; Replace all "...\)" with "@@@" temporarily
|
|
661 (br-buffer-replace "\\\\\\.\\\\\\.\\\\\\.\)" "@@@")
|
|
662
|
|
663 ;; Optionalize right hand side of argument assignments.
|
|
664 (br-buffer-replace "\\([^=,\( \t\n]+\\)\\([ \t\n]+=[^,\)]+\\)"
|
|
665 (concat "\\1\\\\(\\2\\\\)?"))
|
|
666
|
|
667 ;; If an arg consists of 2 or more words, replace last with <identifier>
|
|
668 (br-buffer-replace
|
|
669 "\\([\(,][^=,\)]*[^ \t\n=,\)]+[ \t\n]+\\)[^ \t\n=,\)]+\\([ \t\n]*[,\)]\\)"
|
|
670 (concat "\\1" (br-regexp-quote java-arg-identifier) "\\2"))
|
|
671
|
|
672 ;; If an arg consists of only 1 word, add a second
|
|
673 (br-buffer-replace
|
|
674 "\\([\(,][ \t\n]*\\)\\([^ \t\n=,\)]+\\)\\([ \t\n]*[,\)]\\)"
|
|
675 (concat "\\1\\2 " (br-regexp-quote java-arg-identifier) "\\3"))
|
|
676
|
|
677 ;; Replace all " *, *" with "<spc>,<spc>"
|
|
678 (br-buffer-replace "[ \t\n]*,[ \t\n]*" (concat space "," space))
|
|
679
|
|
680 ;; Replace all " +" with "<spc>"
|
|
681 (br-buffer-replace "[ \t\n]+" space)
|
|
682
|
|
683 ;; Replace all "\(" with "\(<spc>"
|
|
684 (br-buffer-replace "\\(^\\|[^\\]\\)\(" (concat "\\1\(" space))
|
|
685
|
|
686 ;; Replace all "\)" with "<spc>\)"
|
|
687 (br-buffer-replace "\\([^\\]\\)\)" (concat "\\1" space "\)"))
|
|
688
|
|
689 ;; Replace all "<spc>" with "[ \t\n]*"
|
|
690 (br-buffer-replace "\\\\s-\\*" "[ \t\n]*")
|
|
691
|
|
692 ;; Replace all "@@@" with any # of args
|
|
693 (br-buffer-replace "@@@" "[^\)]*\)")
|
|
694 )
|
|
695
|
|
696 ;; Return final buffer as a string.
|
|
697 (prog1 (buffer-substring (point-min) (point-max))
|
|
698 (kill-buffer tmp-buf-nm)
|
|
699 (set-buffer obuf))))
|
|
700
|
|
701 (defun java-get-class-name-from-source ()
|
|
702 "Return class name from closest class definition preceding point or nil."
|
|
703 (let ((opoint (point))
|
|
704 (class))
|
|
705 (save-excursion
|
|
706 (if (re-search-backward java-class-def-regexp nil t)
|
|
707 (progn (goto-char (match-beginning java-class-def-derived-grpn))
|
|
708 (setq class (java-normalize-class-match))
|
|
709 ;; Ensure that declaration occurs within class definition.
|
|
710 (forward-list)
|
|
711 (and (> (point) opoint)
|
|
712 class))))))
|
|
713
|
100
|
714 (defun java-get-feature-tags (feature-file &optional feature-list)
|
|
715 "Scan java FEATURE-FILE and hold feature tags in 'br-feature-tags-file'.
|
|
716 Assume FEATURE-FILE has already been read into a buffer and that
|
|
717 'br-feature-tags-init' has been called. Optional FEATURE-LIST can be
|
0
|
718 provided so that a non-standard scan function can be used before calling
|
|
719 this function."
|
|
720 (interactive)
|
|
721 (let ((obuf (current-buffer)))
|
|
722 (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
|
|
723 (goto-char 1)
|
100
|
724 ;; Delete any prior feature tags associated with feature-file
|
|
725 (if (search-forward feature-file nil 'end)
|
0
|
726 (progn (forward-line -1)
|
|
727 (let ((start (point)))
|
|
728 (search-forward "\^L" nil 'end 2)
|
|
729 (backward-char 1)
|
|
730 (delete-region start (point))
|
|
731 )))
|
100
|
732 (if feature-list
|
|
733 (progn (insert "\^L\n" feature-file "\n")
|
0
|
734 (mapcar (function (lambda (tag) (insert tag "\n")))
|
100
|
735 feature-list)
|
0
|
736 ))
|
|
737 (set-buffer obuf)))
|
|
738
|
|
739 (defun java-locate-feature (ftr class ftr-pat &optional other-win)
|
|
740 ;; 'class' may = nil, implying non-member function
|
|
741 (or class (setq class "[function]"))
|
|
742 (let ((def-class))
|
|
743 (if (and ftr-pat
|
|
744 (setq def-class
|
|
745 (java-find-ancestors-feature (list class)
|
|
746 ftr-pat other-win)))
|
|
747 (progn (if (and class (not (equal class def-class)))
|
|
748 (message
|
|
749 "Member `%s` of class '%s' inherited from class '%s'."
|
|
750 ftr class def-class))
|
|
751 t))))
|
|
752
|
|
753 (defun java-scan-ancestors-feature (class-list ftr-pat &optional other-win)
|
100
|
754 "Display feature definition derived from CLASS-LIST, matching FTR-PAT.
|
0
|
755 Scan files with same base name as class file."
|
|
756 (let ((classes class-list)
|
|
757 (found-ftr)
|
|
758 (code-def-files)
|
|
759 (file)
|
|
760 (ftr-regexp)
|
|
761 (class))
|
|
762 (if (null class-list)
|
|
763 nil
|
|
764 (while (and (not found-ftr) classes)
|
|
765 (setq class (car classes)
|
|
766 code-def-files (java-files-with-source class)
|
|
767 ftr-regexp (funcall ftr-pat class))
|
|
768 (while (and (setq file (car code-def-files))
|
|
769 (not (setq found-ftr
|
|
770 (br-feature-found-p file ftr-regexp
|
|
771 nil other-win t))))
|
|
772 (setq code-def-files (cdr code-def-files)))
|
|
773 (setq classes (if found-ftr nil (cdr classes))))
|
|
774 (if found-ftr
|
|
775 (or class t)
|
|
776 (java-scan-ancestors-feature
|
|
777 (apply 'append (mapcar (function (lambda (cl) (br-get-parents cl)))
|
|
778 class-list))
|
|
779 ftr-pat)))))
|
|
780
|
|
781 (defun java-scan-features (class start end)
|
100
|
782 "Return reverse ordered list of java feature declarations within CLASS def.
|
|
783 START and END give buffer region to search.
|
|
784
|
|
785 Multiple declarations with only one type, e.g. float a, b;
|
|
786 are missed, because that would require too much effort right now.
|
|
787 Use the clearer style with a type keyword for each feature defined."
|
0
|
788 (setq class (br-delete-space class))
|
|
789 (save-excursion
|
|
790 (save-restriction
|
|
791 (narrow-to-region start end)
|
|
792 (goto-char start)
|
100
|
793 (let ((features) ftr name)
|
0
|
794 ;;
|
|
795 ;; Get member definitions and abstract method declarations.
|
100
|
796 (while (re-search-forward java-feature-decl nil t)
|
0
|
797 (setq start (match-beginning 0)
|
|
798 name (buffer-substring
|
|
799 (match-beginning java-feature-name-grpn)
|
|
800 (match-end java-feature-name-grpn))
|
100
|
801 ftr (buffer-substring (match-beginning 0) (match-end 0)))
|
0
|
802 ;; This is necessary to remove a possible double expression match
|
|
803 ;; where there is a blank line within the match.
|
100
|
804 (if (string-match "[\n\^M]\\([ \t]*[\n\^M]\\)+" ftr)
|
|
805 (progn (setq ftr (substring ftr (match-end 0)))
|
0
|
806 (goto-char (+ start (match-end 0))))
|
|
807 (if (c-within-comment-p)
|
|
808 (search-forward "*/" nil t)
|
100
|
809 ;; Move point to precede the feature match termination character.
|
0
|
810 (backward-char)
|
100
|
811 (cond ((= (following-char) ?\{)
|
|
812 (condition-case ()
|
|
813 ;; Move to end of feature but ignore any error if braces
|
|
814 ;; are unbalanced. Let the compiler tell the user about
|
|
815 ;; this.
|
|
816 (forward-sexp)
|
|
817 (error nil)))
|
|
818 ((= (following-char) ?=)
|
|
819 (skip-chars-forward "^;")))
|
|
820 (setq ftr (java-feature-normalize ftr class name)
|
|
821 features (cons ftr features)))))
|
|
822 features))))
|
0
|
823
|
|
824 (defun java-skip-to-statement ()
|
|
825 (if (re-search-backward "\\(^\\|[;{}]\\)[ \t]*" nil t)
|
|
826 (progn (goto-char (match-end 0))
|
|
827 (skip-chars-forward " \t")
|
|
828 t)))
|
|
829
|
|
830 ;;; ************************************************************************
|
|
831 ;;; Private variables
|
|
832 ;;; ************************************************************************
|
|
833
|
100
|
834 (defconst java-code-file-regexp "\\.java?$"
|
|
835 "Regular expression matching a unique part of a Java source file name and no others.")
|
0
|
836
|
|
837 (defconst java-type-def-modifier
|
|
838 "\\(const\\|final\\|static\\|abstract\\|public\\|protected\\|private\\)")
|
|
839
|
|
840 (defconst java-type-modifier-keyword
|
|
841 (concat "\\(\\(public\\|protected\\|private\\|const\\|abstract\\|"
|
100
|
842 "synchronized\\|final\\|static\\|transient\\|"
|
|
843 "native\\|volatile\\)[ \t\n\^M]+\\)"))
|
0
|
844
|
|
845 (defconst java-type-identifier-group
|
|
846 (concat "\\(\\(" java-return-type-identifier "\\)[ \t\n\^M]+\\)"))
|
|
847
|
|
848 (defconst java-function-identifier (concat
|
|
849 "[_a-zA-Z][^][ \t:;.,{}()=]*")
|
|
850 "Regular expression matching a Java function name.")
|
|
851
|
|
852 (defconst java-arg-identifier
|
|
853 (concat "[_a-zA-Z][" java-identifier-chars "]*")
|
|
854 "Regular expression matching a Java function argument identifier.")
|
|
855
|
|
856 (defconst java-feature-decl-or-def
|
|
857 (concat "^[ \t]*\\(" java-type-modifier-keyword "*"
|
|
858 java-type-identifier-group "\\)?"
|
|
859 "\\(" java-type-identifier "[ \t\n\^M]*\\)?"
|
|
860 "\\(" java-function-identifier "\\|" java-identifier "\\)"
|
100
|
861 ;; It is hard to tell arguments from parenthesized initializing
|
0
|
862 ;; expressions.
|
100
|
863 "[ \t\n\^M]*\\(([^\);{}]*)\\)?\\([][ \t]*\\)"
|
|
864 ;; Optional exceptions that a method can throw.
|
|
865 "\\([ \t\n\^M]*\\<throws\\>[ \t\n\^M]*\\("
|
0
|
866 java-identifier "[, \t\n\^M]*\\)+\\)?"
|
|
867 )
|
|
868 "Regexp matching a java member declaration or definition.
|
|
869 Member modifier keywords are grouped expression 'java-feature-mode-grpn'.
|
|
870 Member type is grouped expression 'java-feature-type-grpn'. Member name is
|
|
871 group 'java-feature-name-grpn'. Function parentheses, if any, are group
|
|
872 'java-feature-parens-grpn'. Comma separated list of exceptions that can be
|
|
873 thrown by a function are group 'java-feature-exceptions-grpn'.")
|
|
874
|
|
875 (defconst java-feature-mod-grpn 2)
|
|
876 (defconst java-feature-type-grpn 5)
|
|
877 (defconst java-feature-name-grpn 9)
|
|
878 (defconst java-feature-parens-grpn 11)
|
|
879 (defconst java-feature-exceptions-grpn 14)
|
100
|
880 (defconst java-feature-terminator-grpn 15)
|
0
|
881
|
|
882 (defconst java-at-feature-regexp
|
|
883 (concat java-feature-decl-or-def "[ \t\n]*")
|
|
884 "See documentation of 'java-feature-decl-or-def' for grouping expressions.")
|
|
885
|
|
886 (defconst java-feature-decl
|
100
|
887 (concat java-at-feature-regexp "\\([=;{]\\)")
|
|
888 "See documentation of 'java-feature-decl-or-def' for grouping expressions.
|
|
889 'java-feature-terminator-grpn' holds the equal-sign, semi-color or opening brace
|
|
890 that triggers the end of the match.")
|
0
|
891
|
|
892 (defconst java-routine-def
|
100
|
893 (concat java-at-feature-regexp "\\([;{]\\)")
|
|
894 "See documentation of 'java-feature-decl-or-def' for grouping expressions.
|
|
895 'java-feature-terminator-grpn' holds the opening brace that terminates the
|
|
896 feature declaration or the semi-colon that terminates native and abstract
|
|
897 method declarations.")
|
0
|
898
|
|
899 (defconst java-class-decl
|
|
900 (concat java-class-modifier-keyword
|
|
901 java-class-keyword java-identifier "[ \t]*[;,]")
|
|
902 "Regexp matching a java class declaration.
|
|
903 Class name is grouping 'java-class-name-grpn'.")
|
|
904
|
|
905 (defconst java-class-name-grpn 4)
|
|
906
|
100
|
907 (defconst java-abstract-method-regexp "\\<abstract\\>[^;{}]+;"
|
0
|
908 "Regexp matching a Java abstract method signature.")
|
|
909
|
100
|
910 (defconst java-native-method-regexp "\\<native\\>[^;{}]+;"
|
0
|
911 "Regexp matching a Java native method signature, one implemented in another language.")
|
|
912
|
|
913 (provide 'br-java-ft)
|