Skip to content

Commit

Permalink
[Fix #216] Better auto-ns for cljc files
Browse files Browse the repository at this point in the history
There is no :refer :all in cljs:

- Require the ns under test :as sut
- Require clojure.test and cljs.test :as t for symmetry
  • Loading branch information
expez committed Aug 27, 2015
1 parent 888a487 commit d4a0762
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

### Changes

- [#216](https://github.com/clojure-emacs/clj-refactor.el/issues/216) Teach our automatic ns generator about cljc files.
- Teach `cljr-extract-constant` about the `^:const` hint to the compiler.
- Use yasnippet for placeholder parameters in `cljr-create-fn-from-example`
- Highlight the function be promoted with overlays in `cljr-promote-function`.
Expand Down
24 changes: 18 additions & 6 deletions clj-refactor.el
Original file line number Diff line number Diff line change
Expand Up @@ -776,19 +776,31 @@ Signal an error if it is not supported."
(when src-ns
(mapconcat 'identity (append (butlast ns-chunks) (list src-ns)) "."))))

(defun cljr--cljc-file? (&optional buf)
"Is BUF, or the current buffer, visiting a cljc file?"
(s-ends-with? ".cljc" (buffer-file-name (or buf (current-buffer)))))

(defun cljr--add-test-use-declarations ()
(save-excursion
(let* ((ns (clojure-find-ns))
(source-ns (cljr--find-source-ns-of-test-ns ns (buffer-file-name))))
(cljr--insert-in-ns ":require")
(when source-ns
(insert "[" source-ns " :refer :all]"))
(if (cljr--cljc-file?)
;; no :refer :all in cljs
(insert "[" source-ns " :as sut]")
(insert "[" source-ns " :refer :all]")))
(cljr--insert-in-ns ":require")
(insert "[" (cond
((cljr--project-depends-on-p "midje") "midje.sweet")
((cljr--project-depends-on-p "expectations") "expectations")
(t "clojure.test"))
" :refer :all]"))))
(insert (cond
((cljr--project-depends-on-p "midje")
"[midje.sweet :refer :all]")
((cljr--project-depends-on-p "expectations")
"[expectations :refer :all]")
((cljr--cljc-file?)
"#?(:clj [clojure.test :as t]
:cljs [cljs.test :as t :include-macros true])")
(t "[clojure.test :refer :all]"))))
(indent-region (point-min) (point-max))))

(defun cljr--in-tests-p ()
"Check whether the current file is a test file.
Expand Down
10 changes: 10 additions & 0 deletions features/auto-ns.feature
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,13 @@ Feature: Add namespace to blank .clj files
(:require [cljr.core :refer :all]
[midje.sweet :refer :all]))
"""

Scenario: cljc file
When I open file "tmp/test/cljr/core_test.cljc"
Then I should see:
"""
(ns cljr.core-test
(:require [cljr.core :as sut]
#?(:clj [clojure.test :as t]
:cljs [cljs.test :as t :include-macros true])))
"""
27 changes: 14 additions & 13 deletions features/support/env.el
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,32 @@
(cljr-add-keybindings-with-prefix "C-!")
(add-hook 'clojure-mode-hook (lambda () (clj-refactor-mode))))

(Before
(save-all-buffers-dont-ask)
(kill-matching-buffers-dont-ask "clj")
(setq cljr-use-multiple-cursors t))
(defun kill-matching-buffers-dont-ask (regexp &optional internal-too)
(dolist (buffer (buffer-list))
(let ((name (buffer-name buffer)))
(when (and name (not (string-equal name ""))
(or internal-too (/= (aref name 0) ?\s))
(string-match regexp name))
(kill-buffer buffer)))))

(defun save-all-buffers-dont-ask ()
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(let ((filename (buffer-file-name)))
(when (and filename
(or (file-exists-p filename)
(s-ends-with? ".clj" filename)))
(s-ends-with? ".clj" filename)
(s-ends-with? ".cljc" filename)))
(save-buffer))))))

(defun kill-matching-buffers-dont-ask (regexp &optional internal-too)
(dolist (buffer (buffer-list))
(let ((name (buffer-name buffer)))
(when (and name (not (string-equal name ""))
(or internal-too (/= (aref name 0) ?\s))
(string-match regexp name))
(kill-buffer buffer)))))
(Before
(save-all-buffers-dont-ask)
(kill-matching-buffers-dont-ask "cljc?")
(setq cljr-use-multiple-cursors t))

(After
(save-all-buffers-dont-ask)
(kill-matching-buffers-dont-ask "clj")
(kill-matching-buffers-dont-ask "cljc?")
(delete-directory (expand-file-name "tmp" clj-refactor-root-path) t))

(Teardown
Expand Down

0 comments on commit d4a0762

Please sign in to comment.