Skip to content

Commit

Permalink
nrepl.el support for Midje-mode.
Browse files Browse the repository at this point in the history
  • Loading branch information
bonega committed Sep 23, 2012
1 parent 6794f98 commit df09c3b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 44 deletions.
2 changes: 1 addition & 1 deletion midje-mode-pkg.el
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(define-package "midje-mode" "0.1.1"
"Minor mode for running Midje tests in emacs, see: https://github.com/dnaumov/midje-mode"
'((slime "1.0") (clojure-mode "1.0")))
'((nrepl "0.1.4") (clojure-mode "1.0")))
67 changes: 24 additions & 43 deletions midje-mode.el
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
;;; midje-mode.el --- Minor mode for Midje tests

(require 'clojure-mode)
(require 'slime)
(require 'nrepl)
(require 'newcomment)
(require 'midje-mode-praise)

Expand All @@ -12,16 +12,13 @@
(defvar midje-fact-regexp "^(facts?\\([[:space:]]\\|$\\)")
(defvar midje-syntax-table nil)

;; Callbacks
(defun midje-insert-above-fact (result)
(defun midje-goto-above-fact ()
(if (bolp) (forward-char)) ; at first character of defun, beginning-of-defun moves back.
(beginning-of-defun)
(midje-provide-result-info result))
(beginning-of-defun))

(defun midje-insert-below-code-under-test (result)
(defun midje-goto-below-code-under-test ()
(end-of-defun)
(next-line)
(midje-provide-result-info result))
(next-line))

;; Util

Expand Down Expand Up @@ -73,7 +70,7 @@
(defun midje-eval-unfinished ()
(midje-to-unfinished)
(end-of-defun)
(slime-eval-last-expression))
(nrepl-eval-last-expression))

(defun midje-add-identifier-to-unfinished-list (identifier)
(save-excursion
Expand Down Expand Up @@ -108,12 +105,6 @@
(search-backward "[]")
(forward-char))

(defun midje-provide-result-info (result)
(destructuring-bind (output value) result
(if (string= output "")
(midje-display-reward)
(midje-insert-failure-message output))))

(defun midje-insert-failure-message (str &optional justify)
(let ((start-point (point))
(end-point (progn (insert str) (point))))
Expand Down Expand Up @@ -190,6 +181,17 @@ all such comments."
(beginning-of-line)
(kill-line)))))

(defun nrepl-check-fact-handler (buffer)
(nrepl-make-response-handler buffer
(lambda (buffer str)
(with-current-buffer buffer
(if (string-equal str "true") (midje-display-reward))))
(lambda (buffer str)
(with-current-buffer buffer
(midje-insert-failure-message (format "%s" str))))
'()
'()))

(defun midje-check-fact-near-point ()
"Used when `point' is on or just after a Midje fact.
Check that fact and also save it for use of
Expand All @@ -200,8 +202,9 @@ Check that fact and also save it for use of
(mark-defun)
(buffer-substring-no-properties (mark) (point)))))
(setq last-checked-midje-fact string)
(slime-eval-async `(swank:eval-and-grab-output ,string)
'midje-insert-above-fact)))
(midje-goto-above-fact)
(nrepl-send-string string nrepl-buffer-ns
(nrepl-check-fact-handler (current-buffer)))))

(defun midje-recheck-last-fact-checked ()
"Used when `point` is on or just after a def* form.
Expand All @@ -210,28 +213,9 @@ the last fact checked (by `midje-check-fact-near-point')."

(interactive)
(midje-clear-comments)
(setq midje-running-fact t)
(slime-compile-defun)
; Callback is slime-compilation-finished, then midje-after-compilation-check-fact
)

;; This is a HACK. I want to add midje-after-compilation-check-fact to
;; the slime-compilation-finished-hook, but I can't seem to override the
;; :options declaration in the original slime.el defcustom.
(unless (fboundp 'original-slime-compilation-finished)
(setf (symbol-function 'original-slime-compilation-finished)
(symbol-function 'slime-compilation-finished)))

(defun slime-compilation-finished (result)
(original-slime-compilation-finished result)
(with-struct (slime-compilation-result. notes duration successp) result
(if successp (midje-after-compilation-check-fact))))

(defun midje-after-compilation-check-fact ()
(if midje-running-fact
(slime-eval-async `(swank:eval-and-grab-output ,last-checked-midje-fact)
'midje-insert-below-code-under-test))
(setq midje-running-fact nil))
(midje-goto-below-code-under-test)
(nrepl-send-string last-checked-midje-fact nrepl-buffer-ns
(nrepl-check-fact-handler (current-buffer))))

(defun midje-check-fact ()
"If on or near a Midje fact, check it with
Expand Down Expand Up @@ -285,13 +269,10 @@ nearby Clojure form and recheck the last fact checked

;;;###autoload
(define-minor-mode midje-mode
"A minor mode for running Midje tests when in `slime-mode'.
"A minor mode for running Midje tests when in `nrepl-mode'.
\\{midje-mode-map}"
nil " Midje" midje-mode-map
;; This doesn't seem to work.
;; (custom-add-option 'slime-compilation-finished-hook
;; 'midje-post-compilation-action)
(hs-minor-mode 1))

;;;###autoload
Expand Down

0 comments on commit df09c3b

Please sign in to comment.