1 ;;; srecode/getset.el --- Package for inserting new get/set methods.
3 ;; Copyright (C) 2007-2013 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;; SRecoder application for inserting new get/set methods into a class.
27 (require 'semantic
/analyze
)
28 (require 'semantic
/find
)
29 (require 'srecode
/insert
)
30 (require 'srecode
/dictionary
)
33 (defvar srecode-insert-getset-fully-automatic-flag nil
34 "Non-nil means accept choices srecode comes up with without asking.")
37 (defun srecode-insert-getset (&optional class-in field-in
)
38 "Insert get/set methods for the current class.
39 CLASS-IN is the semantic tag of the class to update.
40 FIELD-IN is the semantic tag, or string name, of the field to add.
41 If you do not specify CLASS-IN or FIELD-IN then a class and field
45 (srecode-load-tables-for-mode major-mode
)
46 (srecode-load-tables-for-mode major-mode
'getset
)
48 (if (not (srecode-table))
49 (error "No template table found for mode %s" major-mode
))
51 (if (not (srecode-template-get-table (srecode-table)
55 (error "No templates for inserting get/set"))
57 ;; Step 1: Try to derive the tag for the class we will use
59 (let* ((class (or class-in
(srecode-auto-choose-class (point))))
60 (tagstart (when class
(semantic-tag-start class
)))
61 (inclass (eq (semantic-current-tag-of-class 'type
) class
))
66 (error "Move point to a class and try again"))
68 ;; Step 2: Select a name for the field we will use.
70 (setq field field-in
))
72 (when (and inclass
(not field
))
73 (setq field
(srecode-auto-choose-field (point))))
76 (setq field
(srecode-query-for-field class
)))
78 ;; Step 3: Insert a new field if needed
82 (srecode-position-new-field class inclass
)
84 (let* ((dict (srecode-create-dictionary))
85 (temp (srecode-template-get-table (srecode-table)
91 (error "Getset templates for %s not loaded!" major-mode
))
92 (srecode-resolve-arguments temp dict
)
93 (srecode-dictionary-set-value dict
"NAME" field
)
94 (when srecode-insert-getset-fully-automatic-flag
95 (srecode-dictionary-set-value dict
"TYPE" "int"))
96 (srecode-insert-fcn temp dict
)
101 ;; Refresh our class tag.
102 (setq class
(srecode-auto-choose-class (point)))
105 (let ((tmptag (semantic-deep-find-tags-by-name-regexp
106 field
(current-buffer))))
107 (setq tmptag
(semantic-find-tags-by-class 'variable tmptag
))
110 (setq field
(car tmptag
))
111 (error "Could not find new field %s" field
)))
114 ;; Step 3.5: Insert an initializer if needed.
118 ;; Set up for the rest.
121 (if (not (semantic-tag-p field
))
122 (error "Must specify field for get/set. (parts may not be impl'd yet.)"))
124 ;; Set 4: Position for insertion of methods
125 (srecode-position-new-methods class field
)
127 ;; Step 5: Insert the get/set methods
128 (if (not (eq (semantic-current-tag) class
))
129 ;; We are positioned on top of something else.
133 (let* ((dict (srecode-create-dictionary))
134 (srecode-semantic-selected-tag field
)
135 (temp (srecode-template-get-table (srecode-table)
141 (error "Getset templates for %s not loaded!" major-mode
))
142 (srecode-resolve-arguments temp dict
)
143 (srecode-dictionary-set-value dict
"GROUPNAME"
144 (concat (semantic-tag-name field
)
146 (srecode-dictionary-set-value dict
"NICENAME"
147 (srecode-strip-fieldname
148 (semantic-tag-name field
)))
149 (srecode-insert-fcn temp dict
)
152 (defun srecode-strip-fieldname (name)
153 "Strip the fieldname NAME of polish notation things."
154 (cond ((string-match "[a-z]\\([A-Z]\\w+\\)" name
)
155 (substring name
(match-beginning 1)))
156 ;; Add more rules here.
160 (defun srecode-position-new-methods (class field
)
161 "Position the cursor in CLASS where new getset methods should go.
162 FIELD is the field for the get sets.
163 INCLASS specifies if the cursor is already in CLASS or not."
164 (semantic-go-to-tag field
)
166 (let ((prev (semantic-find-tag-by-overlay-prev))
167 (next (semantic-find-tag-by-overlay-next))
172 ((and prev
(semantic-tag-of-class-p prev
'variable
))
175 (srecode-strip-fieldname (semantic-tag-name prev
))))
177 ((and next
(semantic-tag-of-class-p next
'variable
))
180 (srecode-strip-fieldname (semantic-tag-name prev
)))))
183 (setq aftertag
(semantic-find-first-tag-by-name
184 setname
(semantic-tag-type-members class
)))
187 (setq aftertag
(car-safe
188 (semantic--find-tags-by-macro
189 (semantic-tag-get-attribute (car tags
) :destructor-flag
)
190 (semantic-tag-type-members class
))))
191 ;; Make sure the tag is public
192 (when (not (eq (semantic-tag-protection aftertag class
) 'public
))
197 (setq aftertag
(car-safe
198 (semantic--find-tags-by-macro
199 (semantic-tag-get-attribute (car tags
) :constructor-flag
)
200 (semantic-tag-type-members class
))))
201 ;; Make sure the tag is public
202 (when (not (eq (semantic-tag-protection aftertag class
) 'public
))
207 (setq aftertag
(semantic-find-first-tag-by-name
208 "public" (semantic-tag-type-members class
))))
211 (setq aftertag
(car (semantic-tag-type-members class
))))
214 (let ((te (semantic-tag-end aftertag
)))
216 (message "Unknown location for tag-end in %s:" (semantic-tag-name aftertag
)))
218 ;; If there is a comment immediately after aftertag, skip over it.
219 (when (looking-at (concat "\\s-*\n?\\s-*" semantic-lex-comment-regex
))
221 (rnext (semantic-find-tag-by-overlay-next (point))))
223 ;; Make sure the comment we skipped didn't say anything about
227 (regexp-quote (semantic-tag-name rnext
)) pos t
))
228 ;; It did mention rnext, so go back to our starting position.
234 ;; At the very beginning of the class.
235 (goto-char (semantic-tag-end class
))
245 (defun srecode-position-new-field (class inclass
)
246 "Select a position for a new field for CLASS.
247 If INCLASS is non-nil, then the cursor is already in the class
248 and should not be moved during point selection."
250 ;; If we aren't in the class, get the cursor there, pronto!
253 (error "You must position the cursor where to insert the new field")
255 (let ((kids (semantic-find-tags-by-class
256 'variable
(semantic-tag-type-members class
))))
258 (semantic-go-to-tag (car kids
) class
))
260 (semantic-go-to-tag class
)))
263 (switch-to-buffer (current-buffer))
265 ;; Once the cursor is in our class, ask the user to position
266 ;; the cursor to keep going.
269 (if (or srecode-insert-getset-fully-automatic-flag
270 (y-or-n-p "Insert new field here? "))
272 (error "You must position the cursor where to insert the new field first"))
277 (defun srecode-auto-choose-field (point)
278 "Choose a field for the get/set methods.
279 Base selection on the field related to POINT."
284 (let ((field (semantic-current-tag-of-class 'variable
)))
286 ;; If we get a field, make sure the user gets a chance to choose.
288 (if srecode-insert-getset-fully-automatic-flag
291 (format "Use field %s? " (semantic-tag-name field
))))
296 (defun srecode-query-for-field (class)
297 "Query for a field in CLASS."
298 (let* ((kids (semantic-find-tags-by-class
299 'variable
(semantic-tag-type-members class
)))
300 (sel (completing-read "Use Field: " kids
))
301 (fields (semantic-find-tags-by-name sel kids
)))
307 (defun srecode-auto-choose-class (point)
308 "Choose a class based on location of POINT."
313 (let ((tag (semantic-current-tag-of-class 'type
)))
316 (not (string= (semantic-tag-type tag
) "class")))
317 ;; The current tag is not a class. Are we in a fcn
319 (setq tag
(semantic-current-tag-of-class 'function
))
322 (semantic-tag-function-parent tag
))
323 (let ((p (semantic-tag-function-parent tag
)))
324 ;; @TODO : Copied below out of semantic-analyze
325 ;; Turn into a routine.
327 (let* ((searchname (cond ((stringp p
) p
)
329 (semantic-tag-name p
))
330 ((and (listp p
) (stringp (car p
)))
332 (ptag (semantic-analyze-find-tag searchname
334 (when ptag
(setq tag ptag
))
338 (not (semantic-tag-of-class-p tag
'type
))
339 (not (string= (semantic-tag-type tag
) "class")))
340 ;; We are not in a class that needs a get/set method.
341 ;; Analyze the current context, and derive a class name.
342 (let* ((ctxt (semantic-analyze-current-context))
346 (setq pfix
(reverse (oref ctxt prefix
)))
347 (while (and (not ans
) pfix
)
348 ;; Start at the end and back up to the first class.
349 (when (and (semantic-tag-p (car pfix
))
350 (semantic-tag-of-class-p (car pfix
) 'type
)
351 (string= (semantic-tag-type (car pfix
))
353 (setq ans
(car pfix
)))
354 (setq pfix
(cdr pfix
))))
359 (provide 'srecode
/getset
)
362 ;; generated-autoload-file: "loaddefs.el"
363 ;; generated-autoload-load-name: "srecode/getset"
366 ;;; srecode/getset.el ends here