PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/cedet/srecode/srecode-getset.el

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