Skip to content

Commit

Permalink
New option: aya-create-with-newline
Browse files Browse the repository at this point in the history
  • Loading branch information
rubikitch committed Nov 9, 2015
1 parent c8e60d5 commit deb00c7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
13 changes: 13 additions & 0 deletions auto-yasnippet-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,16 @@
" param1, "
(2 . "Type")
" param2)"))))
(ert-deftest aya--maybe-append-newline ()
(should (equal (let ((aya-create-with-newline t))
(aya--maybe-append-newline "snippet"))
"snippet\n"))
(should (equal (let ((aya-create-with-newline nil))
(aya--maybe-append-newline "snippet"))
"snippet"))
(should (equal (let ((aya-create-with-newline t))
(aya--maybe-append-newline "snippet\n"))
"snippet\n"))
(should (equal (let ((aya-create-with-newline nil))
(aya--maybe-append-newline "snippet\n"))
"snippet\n")))
28 changes: 21 additions & 7 deletions auto-yasnippet.el
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@
"Directory to save auto yasnippets."
:type 'directory)

(defcustom aya-create-with-newline nil
"If non-nil `aya-create' creates snippet with trailing newline."
:type 'boolean)

(defvar aya-current ""
"Used as snippet body, when `aya-expand' is called.")

Expand All @@ -122,6 +126,13 @@ But if you set [A-Za-z0-9-_], Foo_bar will expand to $1.")
"Function to call if no snippet markers were on line / in region.")
(make-variable-buffer-local 'aya-default-function)

(defun aya--maybe-append-newline (str)
"Append newline to STR if `aya-create-with-newline' is non-nil."
(if (and aya-create-with-newline
(not (string= "\n" (substring str -1))))
(concat str "\n")
str))

;;;###autoload
(defun aya-create-one-line ()
"A simplistic `aya-create' to create only one mirror.
Expand All @@ -139,11 +150,13 @@ menu.add_item(spamspamspam, \"spamspamspam\")"
(when (and (not (string-match (regexp-quote aya-marker) line))
(string-match re line))
(setq line
(concat
(replace-regexp-in-string re "$1" line)
(if (= (point) end) "" "$1")
(buffer-substring-no-properties (point) end)))
(aya--maybe-append-newline
(concat
(replace-regexp-in-string re "$1" line)
(if (= (point) end) "" "$1")
(buffer-substring-no-properties (point) end))))
(delete-region beg end)
(when aya-create-with-newline (delete-char 1))
(setq aya-current line)
(yas-expand-snippet line)))))

Expand Down Expand Up @@ -193,9 +206,10 @@ with words prefixed by `aya-marker' as fields, and mirrors properly set up."
(lambda (x) (if (consp x) (cdr x) x))
res ""))
(setq aya-current
(mapconcat
(lambda (x) (if (consp x) (format "$%d" (car x)) x))
res ""))
(aya--maybe-append-newline
(mapconcat
(lambda (x) (if (consp x) (format "$%d" (car x)) x))
res "")))
;; try some other useful action if it's defined for current buffer
(and (functionp aya-default-function)
(funcall aya-default-function))))))
Expand Down

0 comments on commit deb00c7

Please sign in to comment.