lisp/cedet/semantic/ia.el: Add local vars for autoloading.
[bpt/emacs.git] / lisp / cedet / semantic / ia.el
1 ;;; semantic/ia.el --- Interactive Analysis functions
2
3 ;;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;;; 2008, 2009 Free Software Foundation, Inc.
5
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
7 ;; Keywords: syntax
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25 ;;
26 ;; Interactive access to `semantic-analyze'.
27 ;;
28 ;; These routines are fairly simple, and show how to use the Semantic
29 ;; analyzer to provide things such as completion lists, summaries,
30 ;; locations, or documentation.
31 ;;
32
33 ;;; TODO
34 ;;
35 ;; fast-jump. For a virtual method, offer some of the possible
36 ;; implementations in various sub-classes.
37
38 ;; (require 'senator)
39 (require 'semantic/analyze)
40 (require 'semantic/format)
41 (require 'pulse)
42 (eval-when-compile
43 (require 'semantic/analyze)
44 (require 'semantic/analyze/refs))
45
46 (declare-function imenu--mouse-menu "imenu")
47
48 ;;; Code:
49
50 ;;; COMPLETION
51 ;;
52 ;; This set of routines provides some simplisting completion
53 ;; functions.
54
55 (defcustom semantic-ia-completion-format-tag-function
56 'semantic-prototype-nonterminal
57 "*Function used to convert a tag to a string during completion."
58 :group 'semantic
59 :type semantic-format-tag-custom-list)
60
61 (defvar semantic-ia-cache nil
62 "Cache of the last completion request.
63 Of the form ( POINT . COMPLETIONS ) where POINT is a location in the
64 buffer where the completion was requested. COMPLETONS is the list
65 of semantic tag names that provide logical completions from that
66 location.")
67 (make-variable-buffer-local 'semantic-ia-cache)
68
69 ;;; COMPLETION HELPER
70 ;;
71 ;; This overload function handles inserting a tag
72 ;; into a buffer for these local completion routines.
73 ;;
74 ;; By creating the functions as overloadable, it can be
75 ;; customized. For example, the default will put a paren "("
76 ;; character after function names. For Lisp, it might check
77 ;; to put a "(" in front of a function name.
78
79 (define-overloadable-function semantic-ia-insert-tag (tag)
80 "Insert TAG into the current buffer based on completion.")
81
82 (defun semantic-ia-insert-tag-default (tag)
83 "Insert TAG into the current buffer based on completion."
84 (insert (semantic-tag-name tag))
85 (let ((tt (semantic-tag-class tag)))
86 (cond ((eq tt 'function)
87 (insert "("))
88 (t nil))))
89
90 (declare-function semantic-analyze-possible-completions
91 "semantic/analyze/complete")
92
93 (defun semantic-ia-get-completions (context point)
94 "Fetch the completion of CONTEXT at POINT.
95 Supports caching."
96 ;; Cache the current set of symbols so that we can get at
97 ;; them quickly the second time someone presses the
98 ;; complete button.
99 (let ((symbols
100 (if (and semantic-ia-cache
101 (= point (car semantic-ia-cache)))
102 (cdr semantic-ia-cache)
103 (semantic-analyze-possible-completions context))))
104 ;; Set the cache
105 (setq semantic-ia-cache (cons point symbols))
106 symbols))
107
108 ;;;###autoload
109 (defun semantic-ia-complete-symbol (point)
110 "Complete the current symbol at POINT.
111 Completion options are calculated with `semantic-analyze-possible-completions'."
112 (interactive "d")
113 ;; Calculating completions is a two step process.
114 ;;
115 ;; The first analyzer the current context, which finds tags
116 ;; for all the stuff that may be references by the code around
117 ;; POINT.
118 ;;
119 ;; The second step derives completions from that context.
120 (let* ((a (semantic-analyze-current-context point))
121 (syms (semantic-ia-get-completions a point))
122 (pre (car (reverse (oref a prefix))))
123 )
124 ;; If PRE was actually an already completed symbol, it doesn't
125 ;; come in as a string, but as a tag instead.
126 (if (semantic-tag-p pre)
127 ;; We will try completions on it anyway.
128 (setq pre (semantic-tag-name pre)))
129 ;; Complete this symbol.
130 (if (null syms)
131 (progn
132 ;(message "No smart completions found. Trying senator-complete-symbol.")
133 (if (semantic-analyze-context-p a)
134 ;; This is a clever hack. If we were unable to find any
135 ;; smart completions, lets divert to how senator derives
136 ;; completions.
137 ;;
138 ;; This is a way of making this fcn more useful since the
139 ;; smart completion engine sometimes failes.
140 (senator-complete-symbol)
141 ))
142 ;; Use try completion to seek a common substring.
143 (let ((tc (try-completion (or pre "") syms)))
144 (if (and (stringp tc) (not (string= tc (or pre ""))))
145 (let ((tok (semantic-find-first-tag-by-name
146 tc syms)))
147 ;; Delete what came before...
148 (when (and (car (oref a bounds)) (cdr (oref a bounds)))
149 (delete-region (car (oref a bounds))
150 (cdr (oref a bounds)))
151 (goto-char (car (oref a bounds))))
152 ;; We have some new text. Stick it in.
153 (if tok
154 (semantic-ia-insert-tag tok)
155 (insert tc)))
156 ;; We don't have new text. Show all completions.
157 (when (cdr (oref a bounds))
158 (goto-char (cdr (oref a bounds))))
159 (with-output-to-temp-buffer "*Completions*"
160 (display-completion-list
161 (mapcar semantic-ia-completion-format-tag-function syms))
162 ))))))
163
164 (defcustom semantic-ia-completion-menu-format-tag-function
165 'semantic-uml-concise-prototype-nonterminal
166 "*Function used to convert a tag to a string during completion."
167 :group 'semantic
168 :type semantic-format-tag-custom-list)
169
170 ;;;###autoload
171 (defun semantic-ia-complete-symbol-menu (point)
172 "Complete the current symbol via a menu based at POINT.
173 Completion options are calculated with `semantic-analyze-possible-completions'."
174 (interactive "d")
175 (require 'imenu)
176 (let* ((a (semantic-analyze-current-context point))
177 (syms (semantic-ia-get-completions a point))
178 )
179 ;; Complete this symbol.
180 (if (not syms)
181 (progn
182 (message "No smart completions found. Trying Senator.")
183 (when (semantic-analyze-context-p a)
184 ;; This is a quick way of getting a nice completion list
185 ;; in the menu if the regular context mechanism fails.
186 (senator-completion-menu-popup)))
187
188 (let* ((menu
189 (mapcar
190 (lambda (tag)
191 (cons
192 (funcall semantic-ia-completion-menu-format-tag-function tag)
193 (vector tag)))
194 syms))
195 (ans
196 (imenu--mouse-menu
197 ;; XEmacs needs that the menu has at least 2 items. So,
198 ;; include a nil item that will be ignored by imenu.
199 (cons nil menu)
200 (senator-completion-menu-point-as-event)
201 "Completions")))
202 (when ans
203 (if (not (semantic-tag-p ans))
204 (setq ans (aref (cdr ans) 0)))
205 (delete-region (car (oref a bounds)) (cdr (oref a bounds)))
206 (semantic-ia-insert-tag ans))
207 ))))
208
209 ;;; Completions Tip
210 ;;
211 ;; This functions shows how to get the list of completions,
212 ;; to place in a tooltip. It doesn't actually do any completion.
213
214 ;;;###autoload
215 (defun semantic-ia-complete-tip (point)
216 "Pop up a tooltip for completion at POINT."
217 (interactive "d")
218 (let* ((a (semantic-analyze-current-context point))
219 (syms (semantic-ia-get-completions a point))
220 (x (mod (- (current-column) (window-hscroll))
221 (window-width)))
222 (y (save-excursion
223 (save-restriction
224 (widen)
225 (narrow-to-region (window-start) (point))
226 (goto-char (point-min))
227 (1+ (vertical-motion (buffer-size))))))
228 (str (mapconcat #'semantic-tag-name
229 syms
230 "\n"))
231 )
232 (cond ((fboundp 'x-show-tip)
233 (x-show-tip str
234 (selected-frame)
235 nil
236 nil
237 x y)
238 )
239 (t (message str))
240 )))
241
242 ;;; Summary
243 ;;
244 ;; Like idle-summary-mode, this shows how to get something to
245 ;; show a summary on.
246
247 ;;;###autoload
248 (defun semantic-ia-show-summary (point)
249 "Display a summary for the symbol under POINT."
250 (interactive "P")
251 (let* ((ctxt (semantic-analyze-current-context point))
252 (pf (when ctxt
253 ;; The CTXT is an EIEIO object. The below
254 ;; method will attempt to pick the most interesting
255 ;; tag associated with the current context.
256 (semantic-analyze-interesting-tag ctxt)))
257 )
258 (when pf
259 (message "%s" (semantic-format-tag-summarize pf nil t)))))
260
261 ;;; FAST Jump
262 ;;
263 ;; Jump to a destination based on the local context.
264 ;;
265 ;; This shows how to use the analyzer context, and the
266 ;; analyer references objects to choose a good destination.
267
268 (defun semantic-ia--fast-jump-helper (dest)
269 "Jump to DEST, a Semantic tag.
270 This helper manages the mark, buffer switching, and pulsing."
271 ;; We have a tag, but in C++, we usually get a prototype instead
272 ;; because of header files. Lets try to find the actual
273 ;; implementaion instead.
274 (when (semantic-tag-prototype-p dest)
275 (let* ((refs (semantic-analyze-tag-references dest))
276 (impl (semantic-analyze-refs-impl refs t))
277 )
278 (when impl (setq dest (car impl)))))
279
280 ;; Make sure we have a place to go...
281 (if (not (and (or (semantic-tag-with-position-p dest)
282 (semantic-tag-get-attribute dest :line))
283 (semantic-tag-file-name dest)))
284 (error "Tag %s has no buffer information"
285 (semantic-format-tag-name dest)))
286
287 ;; Once we have the tag, we can jump to it. Here
288 ;; are the key bits to the jump:
289
290 ;; 1) Push the mark, so you can pop global mark back, or
291 ;; use semantic-mru-bookmark mode to do so.
292 (push-mark)
293 (when (fboundp 'push-tag-mark)
294 (push-tag-mark))
295 ;; 2) Visits the tag.
296 (semantic-go-to-tag dest)
297 ;; 3) go-to-tag doesn't switch the buffer in the current window,
298 ;; so it is like find-file-noselect. Bring it forward.
299 (switch-to-buffer (current-buffer))
300 ;; 4) Fancy pulsing.
301 (pulse-momentary-highlight-one-line (point))
302 )
303
304 (declare-function semantic-decoration-include-visit "semantic/decorate/include")
305
306 ;;;###autoload
307 (defun semantic-ia-fast-jump (point)
308 "Jump to the tag referred to by the code at POINT.
309 Uses `semantic-analyze-current-context' output to identify an accurate
310 origin of the code at point."
311 (interactive "d")
312 (let* ((ctxt (semantic-analyze-current-context point))
313 (pf (and ctxt (reverse (oref ctxt prefix))))
314 ;; In the analyzer context, the PREFIX is the list of items
315 ;; that makes up the code context at point. Thus the c++ code
316 ;; this.that().theothe
317 ;; would make a list:
318 ;; ( ("this" variable ..) ("that" function ...) "theothe")
319 ;; Where the first two elements are the semantic tags of the prefix.
320 ;;
321 ;; PF is the reverse of this list. If the first item is a string,
322 ;; then it is an incomplete symbol, thus we pick the second.
323 ;; The second cannot be a string, as that would have been an error.
324 (first (car pf))
325 (second (nth 1 pf))
326 )
327 (cond
328 ((semantic-tag-p first)
329 ;; We have a match. Just go there.
330 (semantic-ia--fast-jump-helper first))
331
332 ((semantic-tag-p second)
333 ;; Because FIRST failed, we should visit our second tag.
334 ;; HOWEVER, the tag we actually want that was only an unfound
335 ;; string may be related to some take in the datatype that belongs
336 ;; to SECOND. Thus, instead of visiting second directly, we
337 ;; can offer to find the type of SECOND, and go there.
338 (let ((secondclass (car (reverse (oref ctxt prefixtypes)))))
339 (cond
340 ((and (semantic-tag-with-position-p secondclass)
341 (y-or-n-p (format "Could not find `%s'. Jump to %s? "
342 first (semantic-tag-name secondclass))))
343 (semantic-ia--fast-jump-helper secondclass)
344 )
345 ;; If we missed out on the class of the second item, then
346 ;; just visit SECOND.
347 ((and (semantic-tag-p second)
348 (y-or-n-p (format "Could not find `%s'. Jump to %s? "
349 first (semantic-tag-name second))))
350 (semantic-ia--fast-jump-helper second)
351 ))))
352
353 ((semantic-tag-of-class-p (semantic-current-tag) 'include)
354 ;; Just borrow this cool fcn.
355 (require 'semantic/decorate/include)
356 (semantic-decoration-include-visit)
357 )
358
359 (t
360 (error "Could not find suitable jump point for %s"
361 first))
362 )))
363
364 ;;;###autoload
365 (defun semantic-ia-fast-mouse-jump (evt)
366 "Jump to the tag referred to by the point clicked on.
367 See `semantic-ia-fast-jump' for details on how it works.
368 This command is meant to be bound to a mouse event."
369 (interactive "e")
370 (semantic-ia-fast-jump
371 (save-excursion
372 (posn-set-point (event-end evt))
373 (point))))
374
375 ;;; DOC/DESCRIBE
376 ;;
377 ;; These routines show how to get additional information about a tag
378 ;; for purposes of describing or showing documentation about them.
379 ;;;###autoload
380 (defun semantic-ia-show-doc (point)
381 "Display the code-level documentation for the symbol at POINT."
382 (interactive "d")
383 (let* ((ctxt (semantic-analyze-current-context point))
384 (pf (reverse (oref ctxt prefix)))
385 )
386 ;; If PF, the prefix is non-nil, then the last element is either
387 ;; a string (incomplete type), or a semantic TAG. If it is a TAG
388 ;; then we should be able to find DOC for it.
389 (cond
390 ((stringp (car pf))
391 (message "Incomplete symbol name."))
392 ((semantic-tag-p (car pf))
393 ;; The `semantic-documentation-for-tag' fcn is language
394 ;; specific. If it doesn't return what you expect, you may
395 ;; need to implement something for your language.
396 ;;
397 ;; The default tries to find a comment in front of the tag
398 ;; and then strings off comment prefixes.
399 (let ((doc (semantic-documentation-for-tag (car pf))))
400 (with-output-to-temp-buffer "*TAG DOCUMENTATION*"
401 (princ "Tag: ")
402 (princ (semantic-format-tag-prototype (car pf)))
403 (princ "\n")
404 (princ "\n")
405 (princ "Snarfed Documentation: ")
406 (princ "\n")
407 (princ "\n")
408 (if doc
409 (princ doc)
410 (princ " Documentation unavailable."))
411 )))
412 (t
413 (message "Unknown tag.")))
414 ))
415
416 ;;;###autoload
417 (defun semantic-ia-describe-class (typename)
418 "Display all known parts for the datatype TYPENAME.
419 If the type in question is a class, all methods and other accessible
420 parts of the parent classes are displayed."
421 ;; @todo - use a fancy completing reader.
422 (interactive "sType Name: ")
423
424 ;; When looking for a tag of any name there are a couple ways to do
425 ;; it. The simple `semanticdb-find-tag-by-...' are simple, and
426 ;; you need to pass it the exact name you want.
427 ;;
428 ;; The analyzer function `semantic-analyze-tag-name' will take
429 ;; more complex names, such as the cpp symbol foo::bar::baz,
430 ;; and break it up, and dive through the namespaces.
431 (let ((class (semantic-analyze-find-tag typename)))
432
433 (when (not (semantic-tag-p class))
434 (error "Cannot find class %s" class))
435 (with-output-to-temp-buffer "*TAG DOCUMENTATION*"
436 ;; There are many semantic-format-tag-* fcns.
437 ;; The summarize routine is a fairly generic one.
438 (princ (semantic-format-tag-summarize class))
439 (princ "\n")
440 (princ " Type Members:\n")
441 ;; The type tag contains all the parts of the type.
442 ;; In complex languages with inheritance, not all the
443 ;; parts are in the tag. This analyzer fcn will traverse
444 ;; the inheritance tree, and find all the pieces that
445 ;; are inherited.
446 (let ((parts (semantic-analyze-scoped-type-parts class)))
447 (while parts
448 (princ " ")
449 (princ (semantic-format-tag-summarize (car parts)))
450 (princ "\n")
451 (setq parts (cdr parts)))
452 )
453 )))
454
455 (provide 'semantic/ia)
456
457 ;; Local variables:
458 ;; generated-autoload-file: "loaddefs.el"
459 ;; generated-autoload-feature: semantic/loaddefs
460 ;; generated-autoload-load-name: "semantic/ia"
461 ;; End:
462
463 ;;; semantic/ia.el ends here