Skip to content

Commit 640271f

Browse files
[Fix #371] Teach cljr-slash about in-ns
`cljr-slash` should not do its autoinsert require magic in case the namepace is defined in the buffer by a `(in-ns ...)` form.
1 parent 86e8641 commit 640271f

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
- [#341](https://github.com/clojure-emacs/clj-refactor.el/issues/341) Avoid creating circular dependencies with move form when source namespace refers to the target namespace with a require.
3434
- [refactor-nrepl#183](https://github.com/clojure-emacs/refactor-nrepl/issues/183) Applied conversion between Unix-like path generated by Emacs under Cygwin and Windows-like path required by nREPL. Cider functions `cider-to-nrepl-filename-function` and `cider-from-nrepl-filename-function` are used.
3535
- Fix `cljr--normalize-symbol-name` when the symbol name starts with `#'`.
36+
- [#371](https://github.com/clojure-emacs/clj-refactor.el/issues/371) Stop `cljr-slash` messing up `(in-ns ...)` forms
3637

3738
## 2.2.0
3839

clj-refactor.el

+21-1
Original file line numberDiff line numberDiff line change
@@ -894,11 +894,25 @@ Please, install (or update) refactor-nrepl %s and restart the REPL."
894894
;; ------ ns statements -----------
895895

896896
(defun cljr--goto-ns ()
897+
"Go to the first namespace defining form in the buffer."
897898
(goto-char (point-min))
898899
(if (re-search-forward clojure-namespace-name-regex nil t)
899900
(cljr--goto-toplevel)
900901
(error "No namespace declaration found")))
901902

903+
(defun cljr--goto-closest-ns ()
904+
"Go to the closest namespace defining form.
905+
Go to the namespace form closest to point and above it. If there is
906+
no namespace form above point, return the first one in the buffer."
907+
(save-restriction
908+
(widen)
909+
;; The closest ns form above point.
910+
(when (or (re-search-backward clojure-namespace-name-regex nil t)
911+
;; Or any form at all.
912+
(and (goto-char (point-min))
913+
(re-search-forward clojure-namespace-name-regex nil t)))
914+
(cljr--goto-toplevel))))
915+
902916
(defun cljr--goto-cljs-branch ()
903917
"Move POINT to the cljs branch of the reader conditional following point."
904918
(if (re-search-forward ":cljs" (cljr--point-after 'paredit-forward) :no-error)
@@ -1858,6 +1872,11 @@ the alias in the project."
18581872
(paredit-forward-down)
18591873
(looking-at-p ":keys"))))
18601874

1875+
(defun cljr--in-ns-above-point-p ()
1876+
(save-excursion
1877+
(cljr--goto-closest-ns)
1878+
(looking-at-p "(\\s-*in-ns")))
1879+
18611880
;;;###autoload
18621881
(defun cljr-slash ()
18631882
"Inserts / as normal, but also checks for common namespace shorthands to require.
@@ -1867,7 +1886,8 @@ command will add the corresponding require statement to the ns
18671886
form."
18681887
(interactive)
18691888
(insert "/")
1870-
(unless (cljr--in-map-destructuring?)
1889+
(unless (or (cljr--in-map-destructuring?)
1890+
(cljr--in-ns-above-point-p))
18711891
(when-let (aliases (and cljr-magic-requires
18721892
(not (cider-in-comment-p))
18731893
(not (cider-in-string-p))

features/magic-requires.feature

+16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ Feature: Magic requires
2323
(set/union)
2424
"""
2525

26+
Scenario: Require is not inserted automagically when in-ns is used
27+
When I insert:
28+
"""
29+
(in-ns 'cljr.core)
30+
31+
(set)
32+
"""
33+
And I place the cursor after "set"
34+
And I type "/union"
35+
Then I should see:
36+
"""
37+
(in-ns 'cljr.core)
38+
39+
(set/union)
40+
"""
41+
2642
Scenario: Require is inserted automagically after getting suggestions from middleware
2743
When I insert:
2844
"""

0 commit comments

Comments
 (0)