Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 4530a6e3b5
Fetching contributors…

Cannot retrieve contributors at this time

file 96 lines (72 sloc) 3.25 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
;;; testswitch.el

;; Copyright (C) 2012 Toshiyuki Takahashi

;; Author: Toshiyuki Takahashi (@tototoshi)

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;;


;;; Code:

(require 'string-utils)

(defun testswitch-current-file-or-directory ()
  (if (eq major-mode 'dired-mode)
      (dired-current-directory)
    (buffer-file-name)))

(defun testswitch-main-p (path)
  (string-contains path "/main/"))

(defun testswitch-test-p (path)
  (string-contains path "/test/"))

(defun testswitch-get-directory-from-path (path)
  (replace-regexp-in-string
   "/\\\([^/]*\\\)\\.\\\(java\\\|scala\\\)" "" path))

(defun testswitch-main-to-test ()
  (let ((test-dir (testswitch-get-directory-from-path
                   (replace-regexp-in-string
                    "/main/" "/test/" (testswitch-current-file-or-directory)))))
    (testswitch-find-directory test-dir)))

(defun testswitch-test-to-main ()
  (let ((main-dir (testswitch-get-directory-from-path
                   (replace-regexp-in-string
                    "/test/" "/main/" (testswitch-current-file-or-directory)))))
    (testswitch-find-directory main-dir)))

(defun testswitch-find-directory (directory)
  (testswitch-make-directory-if-not-exists directory)
  (find-file directory))

(defun testswitch-make-directory-if-not-exists (directory)
  (unless (file-exists-p directory)
    (make-directory directory t)))

(defun testswitch-directory ()
  (interactive)
  (let ((buf (testswitch-current-file-or-directory)))
    (cond ((testswitch-test-p buf) (testswitch-test-to-main))
          ((testswitch-main-p buf) (testswitch-main-to-test))
          (t nil))))

(defun testswitch-basename (path)
  (car (last (split-string path "/"))))

(defun testswitch-find-associated-file (filename)
  (cond ((string-contains filename "Test")
         (replace-regexp-in-string "Test" "" filename))
        ((string-contains filename "Spec")
         (replace-regexp-in-string "Spec" "" filename))
        ((string-contains filename ".java")
         (replace-regexp-in-string "\\.java" "Test.java" filename))
        ((string-contains filename ".scala")
         (replace-regexp-in-string "\\.scala" "Spec.scala" filename))))

(defun testswitch ()
  (interactive)
  (cond ((buffer-file-name)
         (let ((filename (testswitch-basename (buffer-file-name))))
           (testswitch-directory)
           (let ((associated-file (find (testswitch-find-associated-file filename) (directory-files ".") :test #'string=)))
             (when associated-file
               (find-file associated-file)))))
        (t (testswitch-directory))))


(provide 'testswitch)
;;; testswitch.el ends here
Something went wrong with that request. Please try again.