1 ;;; semantic/ia.el --- Interactive Analysis functions
3 ;;; Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 ;;; 2008, 2009 Free Software Foundation, Inc.
6 ;; Author: Eric M. Ludlam <zappo@gnu.org>
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;; Interactive access to `semantic-analyze'.
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.
35 ;; fast-jump. For a virtual method, offer some of the possible
36 ;; implementations in various sub-classes.
39 (require 'semantic
/analyze
)
40 (require 'semantic
/format
)
43 (require 'semantic
/analyze
)
44 (require 'semantic
/analyze
/refs
))
46 (declare-function imenu--mouse-menu
"imenu")
52 ;; This set of routines provides some simplisting completion
55 (defcustom semantic-ia-completion-format-tag-function
56 'semantic-prototype-nonterminal
57 "*Function used to convert a tag to a string during completion."
59 :type semantic-format-tag-custom-list
)
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
67 (make-variable-buffer-local 'semantic-ia-cache
)
71 ;; This overload function handles inserting a tag
72 ;; into a buffer for these local completion routines.
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.
79 (define-overloadable-function semantic-ia-insert-tag
(tag)
80 "Insert TAG into the current buffer based on completion.")
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
)
90 (declare-function semantic-analyze-possible-completions
91 "semantic/analyze/complete")
93 (defun semantic-ia-get-completions (context point
)
94 "Fetch the completion of CONTEXT at POINT.
96 ;; Cache the current set of symbols so that we can get at
97 ;; them quickly the second time someone presses the
100 (if (and semantic-ia-cache
101 (= point
(car semantic-ia-cache
)))
102 (cdr semantic-ia-cache
)
103 (semantic-analyze-possible-completions context
))))
105 (setq semantic-ia-cache
(cons point symbols
))
109 (defun semantic-ia-complete-symbol (point)
110 "Complete the current symbol at POINT.
111 Completion options are calculated with `semantic-analyze-possible-completions'."
113 ;; Calculating completions is a two step process.
115 ;; The first analyzer the current context, which finds tags
116 ;; for all the stuff that may be references by the code around
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
))))
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.
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
138 ;; This is a way of making this fcn more useful since the
139 ;; smart completion engine sometimes failes.
140 (senator-complete-symbol)
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
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.
154 (semantic-ia-insert-tag tok
)
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
))
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."
168 :type semantic-format-tag-custom-list
)
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'."
176 (let* ((a (semantic-analyze-current-context point
))
177 (syms (semantic-ia-get-completions a point
))
179 ;; Complete this symbol.
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)))
192 (funcall semantic-ia-completion-menu-format-tag-function tag
)
197 ;; XEmacs needs that the menu has at least 2 items. So,
198 ;; include a nil item that will be ignored by imenu.
200 (senator-completion-menu-point-as-event)
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
))
211 ;; This functions shows how to get the list of completions,
212 ;; to place in a tooltip. It doesn't actually do any completion.
215 (defun semantic-ia-complete-tip (point)
216 "Pop up a tooltip for completion at POINT."
218 (let* ((a (semantic-analyze-current-context point
))
219 (syms (semantic-ia-get-completions a point
))
220 (x (mod (- (current-column) (window-hscroll))
225 (narrow-to-region (window-start) (point))
226 (goto-char (point-min))
227 (1+ (vertical-motion (buffer-size))))))
228 (str (mapconcat #'semantic-tag-name
232 (cond ((fboundp 'x-show-tip
)
244 ;; Like idle-summary-mode, this shows how to get something to
245 ;; show a summary on.
248 (defun semantic-ia-show-summary (point)
249 "Display a summary for the symbol under POINT."
251 (let* ((ctxt (semantic-analyze-current-context point
))
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
)))
259 (message "%s" (semantic-format-tag-summarize pf nil t
)))))
263 ;; Jump to a destination based on the local context.
265 ;; This shows how to use the analyzer context, and the
266 ;; analyer references objects to choose a good destination.
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
))
278 (when impl
(setq dest
(car impl
)))))
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
)))
287 ;; Once we have the tag, we can jump to it. Here
288 ;; are the key bits to the jump:
290 ;; 1) Push the mark, so you can pop global mark back, or
291 ;; use semantic-mru-bookmark mode to do so.
293 (when (fboundp '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))
301 (pulse-momentary-highlight-one-line (point))
304 (declare-function semantic-decoration-include-visit
"semantic/decorate/include")
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."
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.
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.
328 ((semantic-tag-p first
)
329 ;; We have a match. Just go there.
330 (semantic-ia--fast-jump-helper first
))
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
)))))
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
)
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
)
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)
360 (error "Could not find suitable jump point for %s"
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."
370 (semantic-ia-fast-jump
372 (posn-set-point (event-end evt
))
377 ;; These routines show how to get additional information about a tag
378 ;; for purposes of describing or showing documentation about them.
380 (defun semantic-ia-show-doc (point)
381 "Display the code-level documentation for the symbol at POINT."
383 (let* ((ctxt (semantic-analyze-current-context point
))
384 (pf (reverse (oref ctxt prefix
)))
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.
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.
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*"
402 (princ (semantic-format-tag-prototype (car pf
)))
405 (princ "Snarfed Documentation: ")
410 (princ " Documentation unavailable."))
413 (message "Unknown tag.")))
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: ")
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.
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
)))
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
))
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
446 (let ((parts (semantic-analyze-scoped-type-parts class
)))
449 (princ (semantic-format-tag-summarize (car parts
)))
451 (setq parts
(cdr parts
)))
455 (provide 'semantic
/ia
)
458 ;; generated-autoload-file: "loaddefs.el"
459 ;; generated-autoload-feature: semantic/loaddefs
460 ;; generated-autoload-load-name: "semantic/ia"
463 ;;; semantic/ia.el ends here