mirror of
https://github.com/joaotavora/yasnippet.git
synced 2025-10-14 05:23:04 +00:00
transformation supported for snippets
This commit is contained in:
parent
fcbd79ca21
commit
06c08dded4
13
snippets/objc-mode/prop
Normal file
13
snippets/objc-mode/prop
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#name : foo { ... } ; setFoo { ... }
|
||||||
|
# --
|
||||||
|
- (${1:id})${2:foo}
|
||||||
|
{
|
||||||
|
return $2;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)set${2:$(capitalize text)}:($1)aValue
|
||||||
|
{
|
||||||
|
[$2 autorelease];
|
||||||
|
$2 = [aValue retain];
|
||||||
|
}
|
||||||
|
$0
|
24
yasnippet.el
24
yasnippet.el
@ -143,10 +143,11 @@ mode will be listed under the menu \"yasnippet\".")
|
|||||||
(next nil)
|
(next nil)
|
||||||
(prev nil)
|
(prev nil)
|
||||||
snippet)
|
snippet)
|
||||||
(defstruct (yas/field (:constructor yas/make-field (overlay number value)))
|
(defstruct (yas/field (:constructor yas/make-field (overlay number value transform)))
|
||||||
"A field in a snippet."
|
"A field in a snippet."
|
||||||
overlay
|
overlay
|
||||||
number
|
number
|
||||||
|
transform
|
||||||
value)
|
value)
|
||||||
|
|
||||||
(defun yas/snippet-add-field (snippet field)
|
(defun yas/snippet-add-field (snippet field)
|
||||||
@ -205,6 +206,15 @@ have, compare through the start point of the overlay."
|
|||||||
(format "%s" (eval (read string)))
|
(format "%s" (eval (read string)))
|
||||||
(error (format "(error in elisp evaluation: %s)"
|
(error (format "(error in elisp evaluation: %s)"
|
||||||
(error-message-string err)))))
|
(error-message-string err)))))
|
||||||
|
(defun yas/calculate-field-value (field value)
|
||||||
|
"Calculate the value of the field. If there's a transform
|
||||||
|
for this field, apply it. Otherwise, the value is returned
|
||||||
|
unmodified."
|
||||||
|
(let ((text value)
|
||||||
|
(transform (yas/field-transform field)))
|
||||||
|
(if transform
|
||||||
|
(yas/eval-string transform)
|
||||||
|
text)))
|
||||||
(defsubst yas/replace-all (from to)
|
(defsubst yas/replace-all (from to)
|
||||||
"Replace all occurance from FROM to TO."
|
"Replace all occurance from FROM to TO."
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
@ -264,7 +274,7 @@ the template of a snippet in the current snippet-table."
|
|||||||
(overlay-start field-overlay))))
|
(overlay-start field-overlay))))
|
||||||
(unless (eq field-overlay primary-overlay)
|
(unless (eq field-overlay primary-overlay)
|
||||||
(goto-char (overlay-start field-overlay))
|
(goto-char (overlay-start field-overlay))
|
||||||
(insert text)
|
(insert (yas/calculate-field-value field text))
|
||||||
(if (= (overlay-start field-overlay)
|
(if (= (overlay-start field-overlay)
|
||||||
(overlay-end field-overlay))
|
(overlay-end field-overlay))
|
||||||
(move-overlay field-overlay
|
(move-overlay field-overlay
|
||||||
@ -362,7 +372,11 @@ will be deleted before inserting template."
|
|||||||
(while (re-search-forward yas/field-regexp nil t)
|
(while (re-search-forward yas/field-regexp nil t)
|
||||||
(let ((number (or (match-string-no-properties 1)
|
(let ((number (or (match-string-no-properties 1)
|
||||||
(match-string-no-properties 2)))
|
(match-string-no-properties 2)))
|
||||||
|
(transform nil)
|
||||||
(value (match-string-no-properties 3)))
|
(value (match-string-no-properties 3)))
|
||||||
|
(when (eq (elt value 0) ?\$)
|
||||||
|
(setq transform (substring value 1))
|
||||||
|
(setq value nil))
|
||||||
(if (and number
|
(if (and number
|
||||||
(string= "0" number))
|
(string= "0" number))
|
||||||
(progn
|
(progn
|
||||||
@ -374,7 +388,8 @@ will be deleted before inserting template."
|
|||||||
(yas/make-field
|
(yas/make-field
|
||||||
(make-overlay (match-beginning 0) (match-end 0))
|
(make-overlay (match-beginning 0) (match-end 0))
|
||||||
(and number (string-to-number number))
|
(and number (string-to-number number))
|
||||||
value)))))
|
value
|
||||||
|
transform)))))
|
||||||
|
|
||||||
;; Step 6: Sort and link each field group
|
;; Step 6: Sort and link each field group
|
||||||
(setf (yas/snippet-groups snippet)
|
(setf (yas/snippet-groups snippet)
|
||||||
@ -411,7 +426,7 @@ will be deleted before inserting template."
|
|||||||
(end (overlay-end overlay))
|
(end (overlay-end overlay))
|
||||||
(length (- end start)))
|
(length (- end start)))
|
||||||
(goto-char start)
|
(goto-char start)
|
||||||
(insert value)
|
(insert (yas/calculate-field-value field value))
|
||||||
(delete-char length)))))
|
(delete-char length)))))
|
||||||
|
|
||||||
;; Step 9: restore all escape characters
|
;; Step 9: restore all escape characters
|
||||||
@ -760,5 +775,4 @@ the menu if `yas/use-menu' is `t'."
|
|||||||
(dolist (field (yas/group-fields group))
|
(dolist (field (yas/group-fields group))
|
||||||
(delete-overlay (yas/field-overlay field)))))
|
(delete-overlay (yas/field-overlay field)))))
|
||||||
|
|
||||||
|
|
||||||
(provide 'yasnippet)
|
(provide 'yasnippet)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user