Skip to content

Instantly share code, notes, and snippets.

@mzp
Created March 1, 2009 06:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mzp/72244 to your computer and use it in GitHub Desktop.
Save mzp/72244 to your computer and use it in GitHub Desktop.
Toggle between test and implementation files for scala project by Maven2
;;; toggle-scala.el --- Toggle between test and implementation files.
;; Author: MIZUNO Hiroki<mzp.ppp_at_gmail.com>
;; Keywords: tools
;;; Usage:
;; Insert into your .emacs file the following line, (and eval it for
;; immediate usage:
;;
;; (require 'toggle-scala)
;; (add-hook 'scala-mode-hook
;; (lambda ()
;; (define-key scala-mode-map "\M-t" 'toggle-scala))))
;;
;;
;; Optionally byte compile this file, and change the default
;; keybinding.
;;; History:
;; 0.1 : Initial version.
;;; Code:
(defun toggle-scala-pom (try s)
(cond
((= 0 try) nil)
((file-exists-p (expand-file-name "pom.xml" s))
s)
(t
(toggle-scala-pom (- try 1)
(expand-file-name ".." s)))))
(defun toggle-scala-src-p (s)
(string-match "^src/main/" s))
(defun toggle-scala-src->test (s)
(let ((base
(replace-regexp-in-string "^src/main/" "src/test/" s)))
(list
(replace-regexp-in-string "¥¥.scala$" "Test.scala" base)
(replace-regexp-in-string "¥¥.scala$" "Spec.scala" base))))
(defun toggle-scala-test->src (s)
(let ((base
(replace-regexp-in-string "^src/test/" "src/main/" s)))
(list
(replace-regexp-in-string "Test¥¥|Spec¥¥.scala$" ".scala" base))))
(defun toggle-scala-find (p xs)
(if (consp xs)
(if (funcall p (car xs))
(car xs)
(toggle-scala-find p (cdr xs)))
nil))
(defun toggle-scala-test-p (s)
(string-match "^src/test/" s))
(defun toggle-scala-other-file (path)
(let* ((base (toggle-scala-pom 10 path))
(file (file-relative-name path base)))
(toggle-scala-find (function file-exists-p)
(mapcar (lambda (x)
(expand-file-name x base))
(if (toggle-scala-src-p file)
(toggle-scala-src->test file)
(toggle-scala-test->src file))))))
(defun toggle-scala ()
(interactive)
(when (buffer-file-name)
(let ((file (toggle-scala-other-file (buffer-file-name))))
(when file
(find-file file)))))
(provide 'toggle-scala)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment