ia.el 15 KB

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