getset.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. ;;; srecode/getset.el --- Package for inserting new get/set methods.
  2. ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <eric@siege-engine.com>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;; SRecoder application for inserting new get/set methods into a class.
  18. (require 'semantic)
  19. (require 'semantic/analyze)
  20. (require 'semantic/find)
  21. (require 'srecode/insert)
  22. (require 'srecode/dictionary)
  23. ;;; Code:
  24. (defvar srecode-insert-getset-fully-automatic-flag nil
  25. "Non-nil means accept choices srecode comes up with without asking.")
  26. ;;;###autoload
  27. (defun srecode-insert-getset (&optional class-in field-in)
  28. "Insert get/set methods for the current class.
  29. CLASS-IN is the semantic tag of the class to update.
  30. FIELD-IN is the semantic tag, or string name, of the field to add.
  31. If you do not specify CLASS-IN or FIELD-IN then a class and field
  32. will be derived."
  33. (interactive)
  34. (srecode-load-tables-for-mode major-mode)
  35. (srecode-load-tables-for-mode major-mode 'getset)
  36. (if (not (srecode-table))
  37. (error "No template table found for mode %s" major-mode))
  38. (if (not (srecode-template-get-table (srecode-table)
  39. "getset-in-class"
  40. "declaration"
  41. 'getset))
  42. (error "No templates for inserting get/set"))
  43. ;; Step 1: Try to derive the tag for the class we will use
  44. (semantic-fetch-tags)
  45. (let* ((class (or class-in (srecode-auto-choose-class (point))))
  46. (tagstart (when class (semantic-tag-start class)))
  47. (inclass (eq (semantic-current-tag-of-class 'type) class))
  48. (field nil)
  49. )
  50. (when (not class)
  51. (error "Move point to a class and try again"))
  52. ;; Step 2: Select a name for the field we will use.
  53. (when field-in
  54. (setq field field-in))
  55. (when (and inclass (not field))
  56. (setq field (srecode-auto-choose-field (point))))
  57. (when (not field)
  58. (setq field (srecode-query-for-field class)))
  59. ;; Step 3: Insert a new field if needed
  60. (when (stringp field)
  61. (goto-char (point))
  62. (srecode-position-new-field class inclass)
  63. (let* ((dict (srecode-create-dictionary))
  64. (temp (srecode-template-get-table (srecode-table)
  65. "getset-field"
  66. "declaration"
  67. 'getset))
  68. )
  69. (when (not temp)
  70. (error "Getset templates for %s not loaded!" major-mode))
  71. (srecode-resolve-arguments temp dict)
  72. (srecode-dictionary-set-value dict "NAME" field)
  73. (when srecode-insert-getset-fully-automatic-flag
  74. (srecode-dictionary-set-value dict "TYPE" "int"))
  75. (srecode-insert-fcn temp dict)
  76. (semantic-fetch-tags)
  77. (save-excursion
  78. (goto-char tagstart)
  79. ;; Refresh our class tag.
  80. (setq class (srecode-auto-choose-class (point)))
  81. )
  82. (let ((tmptag (semantic-deep-find-tags-by-name-regexp
  83. field (current-buffer))))
  84. (setq tmptag (semantic-find-tags-by-class 'variable tmptag))
  85. (if tmptag
  86. (setq field (car tmptag))
  87. (error "Could not find new field %s" field)))
  88. )
  89. ;; Step 3.5: Insert an initializer if needed.
  90. ;; ...
  91. ;; Set up for the rest.
  92. )
  93. (if (not (semantic-tag-p field))
  94. (error "Must specify field for get/set. (parts may not be impl'd yet.)"))
  95. ;; Set 4: Position for insertion of methods
  96. (srecode-position-new-methods class field)
  97. ;; Step 5: Insert the get/set methods
  98. (if (not (eq (semantic-current-tag) class))
  99. ;; We are positioned on top of something else.
  100. ;; insert a /n
  101. (insert "\n"))
  102. (let* ((dict (srecode-create-dictionary))
  103. (srecode-semantic-selected-tag field)
  104. (temp (srecode-template-get-table (srecode-table)
  105. "getset-in-class"
  106. "declaration"
  107. 'getset))
  108. )
  109. (if (not temp)
  110. (error "Getset templates for %s not loaded!" major-mode))
  111. (srecode-resolve-arguments temp dict)
  112. (srecode-dictionary-set-value dict "GROUPNAME"
  113. (concat (semantic-tag-name field)
  114. " Accessors"))
  115. (srecode-dictionary-set-value dict "NICENAME"
  116. (srecode-strip-fieldname
  117. (semantic-tag-name field)))
  118. (srecode-insert-fcn temp dict)
  119. )))
  120. (defun srecode-strip-fieldname (name)
  121. "Strip the fieldname NAME of polish notation things."
  122. (cond ((string-match "[a-z]\\([A-Z]\\w+\\)" name)
  123. (substring name (match-beginning 1)))
  124. ;; Add more rules here.
  125. (t
  126. name)))
  127. (defun srecode-position-new-methods (class field)
  128. "Position the cursor in CLASS where new getset methods should go.
  129. FIELD is the field for the get sets.
  130. INCLASS specifies if the cursor is already in CLASS or not."
  131. (semantic-go-to-tag field)
  132. (let ((prev (semantic-find-tag-by-overlay-prev))
  133. (next (semantic-find-tag-by-overlay-next))
  134. (setname nil)
  135. (aftertag nil)
  136. )
  137. (cond
  138. ((and prev (semantic-tag-of-class-p prev 'variable))
  139. (setq setname
  140. (concat "set"
  141. (srecode-strip-fieldname (semantic-tag-name prev))))
  142. )
  143. ((and next (semantic-tag-of-class-p next 'variable))
  144. (setq setname
  145. (concat "set"
  146. (srecode-strip-fieldname (semantic-tag-name prev)))))
  147. (t nil))
  148. (setq aftertag (semantic-find-first-tag-by-name
  149. setname (semantic-tag-type-members class)))
  150. (when (not aftertag)
  151. (setq aftertag (car-safe
  152. (semantic--find-tags-by-macro
  153. (semantic-tag-get-attribute (car tags) :destructor-flag)
  154. (semantic-tag-type-members class))))
  155. ;; Make sure the tag is public
  156. (when (not (eq (semantic-tag-protection aftertag class) 'public))
  157. (setq aftertag nil))
  158. )
  159. (if (not aftertag)
  160. (setq aftertag (car-safe
  161. (semantic--find-tags-by-macro
  162. (semantic-tag-get-attribute (car tags) :constructor-flag)
  163. (semantic-tag-type-members class))))
  164. ;; Make sure the tag is public
  165. (when (not (eq (semantic-tag-protection aftertag class) 'public))
  166. (setq aftertag nil))
  167. )
  168. (when (not aftertag)
  169. (setq aftertag (semantic-find-first-tag-by-name
  170. "public" (semantic-tag-type-members class))))
  171. (when (not aftertag)
  172. (setq aftertag (car (semantic-tag-type-members class))))
  173. (if aftertag
  174. (let ((te (semantic-tag-end aftertag)))
  175. (when (not te)
  176. (message "Unknown location for tag-end in %s:" (semantic-tag-name aftertag)))
  177. (goto-char te)
  178. ;; If there is a comment immediately after aftertag, skip over it.
  179. (when (looking-at (concat "\\s-*\n?\\s-*" semantic-lex-comment-regex))
  180. (let ((pos (point))
  181. (rnext (semantic-find-tag-by-overlay-next (point))))
  182. (forward-comment 1)
  183. ;; Make sure the comment we skipped didn't say anything about
  184. ;; the rnext tag.
  185. (when (and rnext
  186. (re-search-backward
  187. (regexp-quote (semantic-tag-name rnext)) pos t))
  188. ;; It did mention rnext, so go back to our starting position.
  189. (goto-char pos)
  190. )
  191. ))
  192. )
  193. ;; At the very beginning of the class.
  194. (goto-char (semantic-tag-end class))
  195. (forward-sexp -1)
  196. (forward-char 1)
  197. )
  198. (end-of-line)
  199. (forward-char 1)
  200. ))
  201. (defun srecode-position-new-field (class inclass)
  202. "Select a position for a new field for CLASS.
  203. If INCLASS is non-nil, then the cursor is already in the class
  204. and should not be moved during point selection."
  205. ;; If we aren't in the class, get the cursor there, pronto!
  206. (when (not inclass)
  207. (error "You must position the cursor where to insert the new field")
  208. (let ((kids (semantic-find-tags-by-class
  209. 'variable (semantic-tag-type-members class))))
  210. (cond (kids
  211. (semantic-go-to-tag (car kids) class))
  212. (t
  213. (semantic-go-to-tag class)))
  214. )
  215. (switch-to-buffer (current-buffer))
  216. ;; Once the cursor is in our class, ask the user to position
  217. ;; the cursor to keep going.
  218. )
  219. (if (or srecode-insert-getset-fully-automatic-flag
  220. (y-or-n-p "Insert new field here? "))
  221. nil
  222. (error "You must position the cursor where to insert the new field first"))
  223. )
  224. (defun srecode-auto-choose-field (point)
  225. "Choose a field for the get/set methods.
  226. Base selection on the field related to POINT."
  227. (save-excursion
  228. (when point
  229. (goto-char point))
  230. (let ((field (semantic-current-tag-of-class 'variable)))
  231. ;; If we get a field, make sure the user gets a chance to choose.
  232. (when field
  233. (if srecode-insert-getset-fully-automatic-flag
  234. nil
  235. (when (not (y-or-n-p
  236. (format "Use field %s? " (semantic-tag-name field))))
  237. (setq field nil))
  238. ))
  239. field)))
  240. (defun srecode-query-for-field (class)
  241. "Query for a field in CLASS."
  242. (let* ((kids (semantic-find-tags-by-class
  243. 'variable (semantic-tag-type-members class)))
  244. (sel (completing-read "Use Field: " kids))
  245. )
  246. (or (semantic-find-tags-by-name sel kids)
  247. sel)
  248. ))
  249. (defun srecode-auto-choose-class (point)
  250. "Choose a class based on location of POINT."
  251. (save-excursion
  252. (when point
  253. (goto-char point))
  254. (let ((tag (semantic-current-tag-of-class 'type)))
  255. (when (or (not tag)
  256. (not (string= (semantic-tag-type tag) "class")))
  257. ;; The current tag is not a class. Are we in a fcn
  258. ;; that is a method?
  259. (setq tag (semantic-current-tag-of-class 'function))
  260. (when (and tag
  261. (semantic-tag-function-parent tag))
  262. (let ((p (semantic-tag-function-parent tag)))
  263. ;; @TODO : Copied below out of semantic-analyze
  264. ;; Turn into a routine.
  265. (let* ((searchname (cond ((stringp p) p)
  266. ((semantic-tag-p p)
  267. (semantic-tag-name p))
  268. ((and (listp p) (stringp (car p)))
  269. (car p))))
  270. (ptag (semantic-analyze-find-tag searchname
  271. 'type nil)))
  272. (when ptag (setq tag ptag ))
  273. ))))
  274. (when (or (not tag)
  275. (not (semantic-tag-of-class-p tag 'type))
  276. (not (string= (semantic-tag-type tag) "class")))
  277. ;; We are not in a class that needs a get/set method.
  278. ;; Analyze the current context, and derive a class name.
  279. (let* ((ctxt (semantic-analyze-current-context))
  280. (pfix nil)
  281. (ans nil))
  282. (when ctxt
  283. (setq pfix (reverse (oref ctxt prefix)))
  284. (while (and (not ans) pfix)
  285. ;; Start at the end and back up to the first class.
  286. (when (and (semantic-tag-p (car pfix))
  287. (semantic-tag-of-class-p (car pfix) 'type)
  288. (string= (semantic-tag-type (car pfix))
  289. "class"))
  290. (setq ans (car pfix)))
  291. (setq pfix (cdr pfix))))
  292. (setq tag ans)))
  293. tag)))
  294. (provide 'srecode/getset)
  295. ;; Local variables:
  296. ;; generated-autoload-file: "loaddefs.el"
  297. ;; generated-autoload-load-name: "srecode/getset"
  298. ;; End:
  299. ;;; srecode/getset.el ends here