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