ia-sb.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. ;;; semantic/ia-sb.el --- Speedbar analysis display interactor
  2. ;;; Copyright (C) 2002-2004, 2006, 2008-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. ;; Speedbar node for displaying derived context information.
  19. ;;
  20. (require 'semantic/analyze)
  21. (require 'speedbar)
  22. ;;; Code:
  23. (defvar semantic-ia-sb-key-map nil
  24. "Keymap used when in semantic analysis display mode.")
  25. (if semantic-ia-sb-key-map
  26. nil
  27. (setq semantic-ia-sb-key-map (speedbar-make-specialized-keymap))
  28. ;; Basic features.
  29. (define-key semantic-ia-sb-key-map "\C-m" 'speedbar-edit-line)
  30. (define-key semantic-ia-sb-key-map "I" 'semantic-ia-sb-show-tag-info)
  31. )
  32. (defvar semantic-ia-sb-easymenu-definition
  33. '( "---"
  34. ; [ "Expand" speedbar-expand-line nil ]
  35. ; [ "Contract" speedbar-contract-line nil ]
  36. [ "Tag Information" semantic-ia-sb-show-tag-info t ]
  37. [ "Jump to Tag" speedbar-edit-line t ]
  38. [ "Complete" speedbar-edit-line t ]
  39. )
  40. "Extra menu items Analysis mode.")
  41. ;; Make sure our special speedbar major mode is loaded
  42. (speedbar-add-expansion-list '("Analyze"
  43. semantic-ia-sb-easymenu-definition
  44. semantic-ia-sb-key-map
  45. semantic-ia-speedbar))
  46. (speedbar-add-mode-functions-list
  47. (list "Analyze"
  48. ;;'(speedbar-item-info . eieio-speedbar-item-info)
  49. '(speedbar-line-directory . semantic-ia-sb-line-path)))
  50. ;;;###autoload
  51. (defun semantic-speedbar-analysis ()
  52. "Start Speedbar in semantic analysis mode.
  53. The analyzer displays information about the current context, plus a smart
  54. list of possible completions."
  55. (interactive)
  56. ;; Make sure that speedbar is active
  57. (speedbar-frame-mode 1)
  58. ;; Now, throw us into Analyze mode on speedbar.
  59. (speedbar-change-initial-expansion-list "Analyze")
  60. )
  61. (defun semantic-ia-speedbar (directory zero)
  62. "Create buttons in speedbar which define the current analysis at POINT.
  63. DIRECTORY is the current directory, which is ignored, and ZERO is 0."
  64. (let ((analysis nil)
  65. (scope nil)
  66. (buffer nil)
  67. (completions nil)
  68. (cf (selected-frame))
  69. (cnt nil)
  70. (mode-local-active-mode nil)
  71. )
  72. ;; Try and get some sort of analysis
  73. (condition-case nil
  74. (progn
  75. (speedbar-select-attached-frame)
  76. (setq buffer (current-buffer))
  77. (setq mode-local-active-mode major-mode)
  78. (save-excursion
  79. ;; Get the current scope
  80. (setq scope (semantic-calculate-scope (point)))
  81. ;; Get the analysis
  82. (setq analysis (semantic-analyze-current-context (point)))
  83. (setq cnt (semantic-find-tag-by-overlay))
  84. (when analysis
  85. (setq completions (semantic-analyze-possible-completions analysis))
  86. )
  87. ))
  88. (error nil))
  89. (select-frame cf)
  90. (with-current-buffer speedbar-buffer
  91. ;; If we have something, do something spiff with it.
  92. (erase-buffer)
  93. (speedbar-insert-separator "Buffer/Function")
  94. ;; Note to self: Turn this into an expandable file name.
  95. (speedbar-make-tag-line 'bracket ? nil nil
  96. (buffer-name buffer)
  97. nil nil 'speedbar-file-face 0)
  98. (when cnt
  99. (semantic-ia-sb-string-list cnt
  100. 'speedbar-tag-face
  101. 'semantic-sb-token-jump))
  102. (when analysis
  103. ;; If this analyzer happens to point at a complete symbol, then
  104. ;; see if we can dig up some documentation for it.
  105. (semantic-ia-sb-show-doc analysis))
  106. (when analysis
  107. ;; Let different classes draw more buttons.
  108. (semantic-ia-sb-more-buttons analysis)
  109. (when completions
  110. (speedbar-insert-separator "Completions")
  111. (semantic-ia-sb-completion-list completions
  112. 'speedbar-tag-face
  113. 'semantic-ia-sb-complete))
  114. )
  115. ;; Show local variables
  116. (when scope
  117. (semantic-ia-sb-show-scope scope))
  118. )))
  119. (defmethod semantic-ia-sb-show-doc ((context semantic-analyze-context))
  120. "Show documentation about CONTEXT iff CONTEXT points at a complete symbol."
  121. (let ((sym (car (reverse (oref context prefix))))
  122. (doc nil))
  123. (when (semantic-tag-p sym)
  124. (setq doc (semantic-documentation-for-tag sym))
  125. (when doc
  126. (speedbar-insert-separator "Documentation")
  127. (insert doc)
  128. (insert "\n")
  129. ))
  130. ))
  131. (defun semantic-ia-sb-show-scope (scope)
  132. "Show SCOPE information."
  133. (let ((localvars (when scope
  134. (oref scope localvar)))
  135. )
  136. (when localvars
  137. (speedbar-insert-separator "Local Variables")
  138. (semantic-ia-sb-string-list localvars
  139. 'speedbar-tag-face
  140. ;; This is from semantic-sb
  141. 'semantic-sb-token-jump))))
  142. (defmethod semantic-ia-sb-more-buttons ((context semantic-analyze-context))
  143. "Show a set of speedbar buttons specific to CONTEXT."
  144. (let ((prefix (oref context prefix)))
  145. (when prefix
  146. (speedbar-insert-separator "Prefix")
  147. (semantic-ia-sb-string-list prefix
  148. 'speedbar-tag-face
  149. 'semantic-sb-token-jump))
  150. ))
  151. (defmethod semantic-ia-sb-more-buttons ((context semantic-analyze-context-assignment))
  152. "Show a set of speedbar buttons specific to CONTEXT."
  153. (call-next-method)
  154. (let ((assignee (oref context assignee)))
  155. (when assignee
  156. (speedbar-insert-separator "Assignee")
  157. (semantic-ia-sb-string-list assignee
  158. 'speedbar-tag-face
  159. 'semantic-sb-token-jump))))
  160. (defmethod semantic-ia-sb-more-buttons ((context semantic-analyze-context-functionarg))
  161. "Show a set of speedbar buttons specific to CONTEXT."
  162. (call-next-method)
  163. (let ((func (oref context function)))
  164. (when func
  165. (speedbar-insert-separator "Function")
  166. (semantic-ia-sb-string-list func
  167. 'speedbar-tag-face
  168. 'semantic-sb-token-jump)
  169. ;; An index for the argument the prefix is in:
  170. (let ((arg (oref context argument))
  171. (args (semantic-tag-function-arguments (car func)))
  172. (idx 0)
  173. )
  174. (speedbar-insert-separator
  175. (format "Argument #%d" (oref context index)))
  176. (if args
  177. (semantic-ia-sb-string-list args
  178. 'speedbar-tag-face
  179. 'semantic-sb-token-jump
  180. (oref context index)
  181. 'speedbar-selected-face)
  182. ;; Else, no args list, so use what the context had.
  183. (semantic-ia-sb-string-list arg
  184. 'speedbar-tag-face
  185. 'semantic-sb-token-jump))
  186. ))))
  187. (defun semantic-ia-sb-string-list (list face function &optional idx idxface)
  188. "Create some speedbar buttons from LIST.
  189. Each button will use FACE, and be activated with FUNCTION.
  190. Optional IDX is an index into LIST to apply IDXFACE instead."
  191. (let ((count 1))
  192. (while list
  193. (let* ((usefn nil)
  194. (string (cond ((stringp (car list))
  195. (car list))
  196. ((semantic-tag-p (car list))
  197. (setq usefn (semantic-tag-with-position-p (car list)))
  198. (semantic-format-tag-uml-concise-prototype (car list)))
  199. (t "<No Tag>")))
  200. (localface (if (or (not idx) (/= idx count))
  201. face
  202. idxface))
  203. )
  204. (if (semantic-tag-p (car list))
  205. (speedbar-make-tag-line 'angle ?i
  206. 'semantic-ia-sb-tag-info (car list)
  207. string (if usefn function) (car list) localface
  208. 0)
  209. (speedbar-make-tag-line 'statictag ??
  210. nil nil
  211. string (if usefn function) (car list) localface
  212. 0))
  213. (setq list (cdr list)
  214. count (1+ count)))
  215. )))
  216. (defun semantic-ia-sb-completion-list (list face function)
  217. "Create some speedbar buttons from LIST.
  218. Each button will use FACE, and be activated with FUNCTION."
  219. (while list
  220. (let* ((documentable nil)
  221. (string (cond ((stringp (car list))
  222. (car list))
  223. ((semantic-tag-p (car list))
  224. (setq documentable t)
  225. (semantic-format-tag-uml-concise-prototype (car list)))
  226. (t "foo"))))
  227. (if documentable
  228. (speedbar-make-tag-line 'angle ?i
  229. 'semantic-ia-sb-tag-info
  230. (car list)
  231. string function (car list) face
  232. 0)
  233. (speedbar-make-tag-line 'statictag ? nil nil
  234. string function (car list) face
  235. 0))
  236. (setq list (cdr list)))))
  237. (defun semantic-ia-sb-show-tag-info ()
  238. "Display information about the tag on the current line.
  239. Same as clicking on the <i> button.
  240. See `semantic-ia-sb-tag-info' for more."
  241. (interactive)
  242. (let ((tok nil))
  243. (save-excursion
  244. (end-of-line)
  245. (forward-char -1)
  246. (setq tok (get-text-property (point) 'speedbar-token)))
  247. (semantic-ia-sb-tag-info nil tok 0)))
  248. (defun semantic-ia-sb-tag-info (text tag indent)
  249. "Display as much information as we can about tag.
  250. Show the information in a shrunk split-buffer and expand
  251. out as many details as possible.
  252. TEXT, TAG, and INDENT are speedbar function arguments."
  253. (when (semantic-tag-p tag)
  254. (unwind-protect
  255. (let ((ob nil))
  256. (speedbar-select-attached-frame)
  257. (setq ob (current-buffer))
  258. (with-output-to-temp-buffer "*Tag Information*"
  259. ;; Output something about this tag:
  260. (with-current-buffer "*Tag Information*"
  261. (goto-char (point-max))
  262. (insert
  263. (semantic-format-tag-prototype tag nil t)
  264. "\n")
  265. (let ((typetok
  266. (condition-case nil
  267. (with-current-buffer ob
  268. ;; @todo - We need a context to derive a scope from.
  269. (semantic-analyze-tag-type tag nil))
  270. (error nil))))
  271. (if typetok
  272. (insert (semantic-format-tag-prototype
  273. typetok nil t))
  274. ;; No type found by the analyzer
  275. ;; The below used to try and select the buffer from the last
  276. ;; analysis, but since we are already in the correct buffer, I
  277. ;; don't think that is needed.
  278. (let ((type (semantic-tag-type tag)))
  279. (cond ((semantic-tag-p type)
  280. (setq type (semantic-tag-name type)))
  281. ((listp type)
  282. (setq type (car type))))
  283. (if (semantic-lex-keyword-p type)
  284. (setq typetok
  285. (semantic-lex-keyword-get type 'summary))))
  286. (if typetok
  287. (insert typetok))
  288. ))
  289. ))
  290. ;; Make it small
  291. (shrink-window-if-larger-than-buffer
  292. (get-buffer-window "*Tag Information*")))
  293. (select-frame speedbar-frame))))
  294. (defun semantic-ia-sb-line-path (&optional depth)
  295. "Return the file name associated with DEPTH."
  296. (save-match-data
  297. (let* ((tok (speedbar-line-token))
  298. (buff (if (semantic-tag-buffer tok)
  299. (semantic-tag-buffer tok)
  300. (current-buffer))))
  301. (buffer-file-name buff))))
  302. (defun semantic-ia-sb-complete (text tag indent)
  303. "At point in the attached buffer, complete the symbol clicked on.
  304. TEXT TAG and INDENT are the details."
  305. ;; Find the specified bounds from the current analysis.
  306. (speedbar-select-attached-frame)
  307. (unwind-protect
  308. (let* ((a (semantic-analyze-current-context (point)))
  309. (bounds (oref a bounds))
  310. (movepoint nil)
  311. )
  312. (save-excursion
  313. (if (and (<= (point) (cdr bounds)) (>= (point) (car bounds)))
  314. (setq movepoint t))
  315. (goto-char (car bounds))
  316. (delete-region (car bounds) (cdr bounds))
  317. (insert (semantic-tag-name tag))
  318. (if movepoint (setq movepoint (point)))
  319. ;; I'd like to use this to add fancy () or what not at the end
  320. ;; but we need the parent file which requires an upgrade to the
  321. ;; analysis tool.
  322. ;;(semantic-insert-foreign-tag tag ??))
  323. )
  324. (if movepoint
  325. (let ((cf (selected-frame)))
  326. (speedbar-select-attached-frame)
  327. (goto-char movepoint)
  328. (select-frame cf))))
  329. (select-frame speedbar-frame)))
  330. (provide 'semantic/ia-sb)
  331. ;; Local variables:
  332. ;; generated-autoload-file: "loaddefs.el"
  333. ;; generated-autoload-load-name: "semantic/ia-sb"
  334. ;; End:
  335. ;;; semantic/ia-sb.el ends here