1 ;;; semantic/ia.el --- Interactive Analysis functions
3 ;;; Copyright (C) 2000-2015 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <zappo@gnu.org>
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 ;; Interactive access to `semantic-analyze'.
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.
34 ;; fast-jump. For a virtual method, offer some of the possible
35 ;; implementations in various sub-classes.
37 (require 'semantic/analyze)
38 (require 'semantic/format)
40 (require 'semantic/senator)
41 (require 'semantic/analyze/refs)
43 (require 'semantic/analyze)
44 (require 'semantic/find))
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-format-tag-prototype
57 "Function used to convert a tag to a string during completion."
59 :type semantic-format-tag-custom-list)
63 ;; This overload function handles inserting a tag
64 ;; into a buffer for these local completion routines.
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.
71 (define-overloadable-function semantic-ia-insert-tag (tag)
72 "Insert TAG into the current buffer based on completion.")
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)
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.")
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))
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'."
99 (when (semantic-active-p)
100 (or pos (setq pos (point)))
101 ;; Calculating completions is a two step process.
103 ;; The first analyzer the current context, which finds tags for
104 ;; all the stuff that may be references by the code around POS.
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.
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
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* ((completion-ignore-case (string= (downcase pre) pre))
127 (tc (try-completion (or pre "") syms)))
128 (if (and (stringp tc) (not (string= tc (or pre ""))))
129 (let ((tok (semantic-find-first-tag-by-name
131 ;; Delete what came before...
132 (when (and (car (oref a bounds)) (cdr (oref a bounds)))
133 (delete-region (car (oref a bounds))
134 (cdr (oref a bounds)))
135 (goto-char (car (oref a bounds))))
136 ;; We have some new text. Stick it in.
138 (semantic-ia-insert-tag tok)
140 ;; We don't have new text. Show all completions.
141 (when (cdr (oref a bounds))
142 (goto-char (cdr (oref a bounds))))
143 (with-output-to-temp-buffer "*Completions*"
144 (display-completion-list
145 (mapcar semantic-ia-completion-format-tag-function syms)))))))))
147 (defcustom semantic-ia-completion-menu-format-tag-function
148 'semantic-format-tag-uml-concise-prototype
149 "*Function used to convert a tag to a string during completion."
151 :type semantic-format-tag-custom-list)
153 ;; Disabled - see http://debbugs.gnu.org/14522
155 ;; (defun semantic-ia-complete-symbol-menu (point)
156 ;; "Complete the current symbol via a menu based at POINT.
157 ;; Completion options are calculated with `semantic-analyze-possible-completions'."
160 ;; (let* ((a (semantic-analyze-current-context point))
161 ;; (syms (semantic-analyze-possible-completions a))
163 ;; ;; Complete this symbol.
166 ;; (message "No smart completions found. Trying Senator.")
167 ;; (when (semantic-analyze-context-p a)
168 ;; ;; This is a quick way of getting a nice completion list
169 ;; ;; in the menu if the regular context mechanism fails.
170 ;; (senator-completion-menu-popup)))
176 ;; (funcall semantic-ia-completion-menu-format-tag-function tag)
180 ;; (imenu--mouse-menu
181 ;; ;; XEmacs needs that the menu has at least 2 items. So,
182 ;; ;; include a nil item that will be ignored by imenu.
184 ;; (senator-completion-menu-point-as-event)
187 ;; (if (not (semantic-tag-p ans))
188 ;; (setq ans (aref (cdr ans) 0)))
189 ;; (delete-region (car (oref a bounds)) (cdr (oref a bounds)))
190 ;; (semantic-ia-insert-tag ans))
195 ;; This functions shows how to get the list of completions,
196 ;; to place in a tooltip. It doesn't actually do any completion.
199 (defun semantic-ia-complete-tip (point)
200 "Pop up a tooltip for completion at POINT."
202 (let* ((a (semantic-analyze-current-context point))
203 (syms (semantic-analyze-possible-completions a))
204 (x (mod (- (current-column) (window-hscroll))
209 (narrow-to-region (window-start) (point))
210 (goto-char (point-min))
211 (1+ (vertical-motion (buffer-size))))))
212 (str (mapconcat #'semantic-tag-name
216 (cond ((fboundp 'x-show-tip)
228 ;; Like idle-summary-mode, this shows how to get something to
229 ;; show a summary on.
232 (defun semantic-ia-show-summary (point)
233 "Display a summary for the symbol under POINT."
235 (let* ((ctxt (semantic-analyze-current-context point))
237 ;; The CTXT is an EIEIO object. The below
238 ;; method will attempt to pick the most interesting
239 ;; tag associated with the current context.
240 (semantic-analyze-interesting-tag ctxt)))
243 (message "%s" (semantic-format-tag-summarize pf nil t))
244 (message "No summary info available"))))
248 ;; Show all variants for the symbol under point.
251 (defun semantic-ia-show-variants (point)
252 "Display a list of all variants for the symbol under POINT."
254 (let* ((ctxt (semantic-analyze-current-context point))
257 ;; We really want to look at the function if we are on an
258 ;; argument. Are there some additional rules we care about for
259 ;; changing the CTXT we look at?
260 (when (semantic-analyze-context-functionarg-p ctxt)
261 (goto-char (cdr (oref ctxt bounds)))
262 (setq ctxt (semantic-analyze-current-context (point))))
264 ;; Get the "completion list", but remove ALL filters to get the master list
265 ;; of all the possible things.
266 (setq comp (semantic-analyze-possible-completions ctxt 'no-unique 'no-tc))
268 ;; Special case for a single type. List the constructors?
269 (when (and (= (length comp) 1) (semantic-tag-of-class-p (car comp) 'type))
270 (setq comp (semantic-find-tags-by-name (semantic-tag-name (car comp))
271 (semantic-tag-type-members (car comp)))))
273 ;; Display the results.
274 (cond ((= (length comp) 0)
275 (message "No Variants found."))
277 (message "%s" (semantic-format-tag-summarize (car comp) nil t)))
279 (with-output-to-temp-buffer "*Symbol Variants*"
280 (semantic-analyze-princ-sequence comp "" (current-buffer)))
281 (shrink-window-if-larger-than-buffer
282 (get-buffer-window "*Symbol Variants*")))
287 ;; Jump to a destination based on the local context.
289 ;; This shows how to use the analyzer context, and the
290 ;; analyzer references objects to choose a good destination.
292 (defun semantic-ia--fast-jump-helper (dest)
293 "Jump to DEST, a Semantic tag.
294 This helper manages the mark, buffer switching, and pulsing."
295 ;; We have a tag, but in C++, we usually get a prototype instead
296 ;; because of header files. Let's try to find the actual
297 ;; implementation instead.
298 (when (semantic-tag-prototype-p dest)
299 (let* ((refs (semantic-analyze-tag-references dest))
300 (impl (semantic-analyze-refs-impl refs t))
302 (when impl (setq dest (car impl)))))
304 ;; Make sure we have a place to go...
305 (if (not (and (or (semantic-tag-with-position-p dest)
306 (semantic-tag-get-attribute dest :line))
307 (semantic-tag-file-name dest)))
308 (error "Tag %s has no buffer information"
309 (semantic-format-tag-name dest)))
311 ;; Once we have the tag, we can jump to it. Here
312 ;; are the key bits to the jump:
314 ;; 1) Push the mark, so you can pop global mark back, or
315 ;; use semantic-mru-bookmark mode to do so.
317 (when (fboundp 'push-tag-mark)
319 ;; 2) Visits the tag.
320 (semantic-go-to-tag dest)
321 ;; 3) go-to-tag doesn't switch the buffer in the current window,
322 ;; so it is like find-file-noselect. Bring it forward.
323 (switch-to-buffer (current-buffer))
325 (pulse-momentary-highlight-one-line (point))
328 (declare-function semantic-decoration-include-visit "semantic/decorate/include")
331 (defun semantic-ia-fast-jump (point)
332 "Jump to the tag referred to by the code at POINT.
333 Uses `semantic-analyze-current-context' output to identify an accurate
334 origin of the code at point."
336 (let* ((ctxt (semantic-analyze-current-context point))
337 (pf (and ctxt (reverse (oref ctxt prefix))))
338 ;; In the analyzer context, the PREFIX is the list of items
339 ;; that makes up the code context at point. Thus the c++ code
340 ;; this.that().theothe
341 ;; would make a list:
342 ;; ( ("this" variable ..) ("that" function ...) "theothe")
343 ;; Where the first two elements are the semantic tags of the prefix.
345 ;; PF is the reverse of this list. If the first item is a string,
346 ;; then it is an incomplete symbol, thus we pick the second.
347 ;; The second cannot be a string, as that would have been an error.
352 ((semantic-tag-p first)
353 ;; We have a match. Just go there.
354 (semantic-ia--fast-jump-helper first))
356 ((semantic-tag-p second)
357 ;; Because FIRST failed, we should visit our second tag.
358 ;; HOWEVER, the tag we actually want that was only an unfound
359 ;; string may be related to some take in the datatype that belongs
360 ;; to SECOND. Thus, instead of visiting second directly, we
361 ;; can offer to find the type of SECOND, and go there.
362 (let ((secondclass (car (reverse (oref ctxt prefixtypes)))))
364 ((and (semantic-tag-with-position-p secondclass)
365 (y-or-n-p (format "Could not find `%s'. Jump to %s? "
366 first (semantic-tag-name secondclass))))
367 (semantic-ia--fast-jump-helper secondclass)
369 ;; If we missed out on the class of the second item, then
370 ;; just visit SECOND.
371 ((and (semantic-tag-p second)
372 (y-or-n-p (format "Could not find `%s'. Jump to %s? "
373 first (semantic-tag-name second))))
374 (semantic-ia--fast-jump-helper second)
377 ((semantic-tag-of-class-p (semantic-current-tag) 'include)
378 ;; Just borrow this cool fcn.
379 (require 'semantic/decorate/include)
381 ;; Push the mark, so you can pop global mark back, or
382 ;; use semantic-mru-bookmark mode to do so.
384 (when (fboundp 'push-tag-mark)
387 (semantic-decoration-include-visit)
391 (error "Could not find suitable jump point for %s"
396 (defun semantic-ia-fast-mouse-jump (evt)
397 "Jump to the tag referred to by the point clicked on.
398 See `semantic-ia-fast-jump' for details on how it works.
399 This command is meant to be bound to a mouse event."
401 (semantic-ia-fast-jump
403 (posn-set-point (event-end evt))
408 ;; These routines show how to get additional information about a tag
409 ;; for purposes of describing or showing documentation about them.
411 (defun semantic-ia-show-doc (point)
412 "Display the code-level documentation for the symbol at POINT."
414 (let* ((ctxt (semantic-analyze-current-context point))
415 (pf (reverse (oref ctxt prefix)))
417 ;; If PF, the prefix is non-nil, then the last element is either
418 ;; a string (incomplete type), or a semantic TAG. If it is a TAG
419 ;; then we should be able to find DOC for it.
422 (message "Incomplete symbol name."))
423 ((semantic-tag-p (car pf))
424 ;; The `semantic-documentation-for-tag' fcn is language
425 ;; specific. If it doesn't return what you expect, you may
426 ;; need to implement something for your language.
428 ;; The default tries to find a comment in front of the tag
429 ;; and then strings off comment prefixes.
430 (let ((doc (semantic-documentation-for-tag (car pf))))
431 (if (or (null doc) (string= doc ""))
432 (message "Doc unavailable for: %s"
433 (semantic-format-tag-prototype (car pf)))
434 (with-output-to-temp-buffer "*TAG DOCUMENTATION*"
436 (princ (semantic-format-tag-prototype (car pf)))
439 (princ "Snarfed Documentation: ")
444 (princ " Documentation unavailable."))
447 (message "Unknown tag.")))
451 (defun semantic-ia-describe-class (typename)
452 "Display all known parts for the datatype TYPENAME.
453 If the type in question is a class, all methods and other accessible
454 parts of the parent classes are displayed."
455 ;; @todo - use a fancy completing reader.
456 (interactive "sType Name: ")
458 ;; When looking for a tag of any name there are a couple ways to do
459 ;; it. The simple `semanticdb-find-tag-by-...' are simple, and
460 ;; you need to pass it the exact name you want.
462 ;; The analyzer function `semantic-analyze-tag-name' will take
463 ;; more complex names, such as the cpp symbol foo::bar::baz,
464 ;; and break it up, and dive through the namespaces.
465 (let ((class (semantic-analyze-find-tag typename)))
467 (when (not (semantic-tag-p class))
468 (error "Cannot find class %s" class))
469 (with-output-to-temp-buffer "*TAG DOCUMENTATION*"
470 ;; There are many semantic-format-tag-* fcns.
471 ;; The summarize routine is a fairly generic one.
472 (princ (semantic-format-tag-summarize class))
474 (princ " Type Members:\n")
475 ;; The type tag contains all the parts of the type.
476 ;; In complex languages with inheritance, not all the
477 ;; parts are in the tag. This analyzer fcn will traverse
478 ;; the inheritance tree, and find all the pieces that
480 (let ((parts (semantic-analyze-scoped-type-parts class)))
483 (princ (semantic-format-tag-summarize (car parts)))
485 (setq parts (cdr parts)))
489 (provide 'semantic/ia)
492 ;; generated-autoload-file: "loaddefs.el"
493 ;; generated-autoload-load-name: "semantic/ia"
496 ;;; semantic/ia.el ends here