Skip to content

Instantly share code, notes, and snippets.

@stew
Last active August 29, 2015 13:57
Show Gist options
  • Select an option

  • Save stew/9351294 to your computer and use it in GitHub Desktop.

Select an option

Save stew/9351294 to your computer and use it in GitHub Desktop.
This tries to find where the Spec/test of the current buffer lives,
and either creates it, or visits it
#+begin_src emacs-lisp
(defun split-path-of-file (f)
"return dirname.filename" (let ((sp (reverse (split-string f "/"))))
(cons (mapconcat 'identity (reverse (cdr sp)) "/") (car sp))))
(defun scala-test-file-name (f)
(let* ((sp (reverse (split-string f "\\.")))
(h (car sp))
(fn (cadr sp)))
(mapconcat 'identity (reverse (cons h (cons (concat fn "Spec")(cddr sp)))) ".")))
(defun scala-find-src (sf d)
(if (null d)
sf
(let ((c (car d)))
(if (equal c "main")
(append (reverse (cdr d)) (list "test") sf)
(scala-find-src (cons c sf) (cdr d))))))
(defun scala-test-file-from-buffer ()
(let* ((d (reverse (split-string (buffer-file-name) "/")))
(test (scala-find-src nil d)))
(scala-test-file-name (mapconcat 'identity test "/"))))
(defun scala-visit-spec ()
(interactive)
(let* ((tf (scala-test-file-from-buffer))
(pf (split-path-of-file tf))
(dn (car pf))
(fn (concat dn "/" (cdr pf))))
(if (file-exists-p dn)
(find-file fn)
(if (y-or-n-p (concat dn " doesn't exist, create it?"))
(progn
(mkdir dn t)
(find-file fn))))))
(add-hook 'scala-mode-hook
'(lambda ()
(local-set-key (kbd "C-c t") `scala-visit-spec)))
#+end_src
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment