HOME

snippet-init

Snippets

Yasnippet

First, turn the thing on:

(require-package 'yasnippet)
(require 'yasnippet)
(yas/global-mode 1)
(setq yas/trigger-key (kbd "C-c /"))

Set up my local snippets:

(setq toby/snippets-dir
      (expand-file-name "snippets" dotfiles-dir))
(add-to-list 'yas/root-directory toby/snippets-dir)

Experiments with using Yasnippet for Refactoring

These may never come to anything, but there is promise.

;; http://stackoverflow.com/questions/10211730/insert-yasnippet-by-name
(defun yas/insert-by-name (name)
  (flet ((dummy-prompt (prompt choices &optional display-fn)
                       (declare (ignore prompt))
                       (or (find name choices :key display-fn :test #'string=)
                           (throw 'notfound nil))))
    (let ((yas-prompt-functions '(dummy-prompt)))
      (catch 'notfound
        (yas-insert-snippet t))))
  )

;; (yas/insert-by-name "def method ...")

(defun ruby/extract-method (region-begin region-end)
  "Extract the current region into a new method"
  (interactive "r")
;;  (message "Region starts: %d, end at: %d" region-begin region-end)
  (kill-region region-begin region-end)
  ;; Need to insert a call to the method and move the point to outside
  ;; the current block somehow
  (yas-expand-snippet "def ${1:method}(${ *args})\n`(current-kill 0)`\nend"))

Author: Toby Tripp

Created: 2015-02-04 Wed 10:40

Emacs 24.3.1 (Org mode 8.2.10)

Validate