Skip to content

Commit 2002d13

Browse files
committed
[Fix #309] am uses alias for fully qualified ns
1 parent 330f267 commit 2002d13

File tree

4 files changed

+38
-17
lines changed

4 files changed

+38
-17
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
### Bugs fixed
1414

15+
- [#309](https://github.com/clojure-emacs/clj-refactor.el/issues/309) `*data-readers*` `am` creates alias for fully-qualified symbols.
1516
- [#320](https://github.com/clojure-emacs/clj-refactor.el/issues/320) `*data-readers*` ignored when searching for macros.
1617

1718
## 2.2.0

clj-refactor.el

+23-17
Original file line numberDiff line numberDiff line change
@@ -985,18 +985,18 @@ If CLJS? is T we insert in the cljs part of the ns declaration."
985985
(defun cljr--cljs-file-p (&optional buf)
986986
"Is BUF, or the current buffer, visiting a cljc file?"
987987
(s-equals-p (file-name-extension (buffer-file-name (or buf (current-buffer))))
988-
"cljs"))
988+
"cljs"))
989989

990990
(defun cljr--cljc-file-p (&optional buf)
991991
"Is BUF, or the current buffer, visiting a cljc file?"
992992
(s-equals-p (file-name-extension (buffer-file-name (or buf (current-buffer))))
993-
"cljc"))
993+
"cljc"))
994994

995995
(defun cljr--clj-file-p (&optional buf)
996996
"Is BUF, or the current buffer, visiting a clj file?"
997997
(or (eq major-mode 'clojure-mode)
998998
(s-equals-p (file-name-extension (buffer-file-name (or buf (current-buffer))))
999-
"clj")))
999+
"clj")))
10001000

10011001
(defun cljr--add-test-declarations ()
10021002
(save-excursion
@@ -2138,8 +2138,8 @@ FEATURE is either :clj or :cljs."
21382138
(if (cljr--point-in-reader-conditional-p)
21392139
(cljr--point-in-reader-conditional-branch-p :clj)
21402140
(s-equals-p (cljr--prompt-user-for "Language context at point? "
2141-
(list "clj" "cljs"))
2142-
"clj")))))
2141+
(list "clj" "cljs"))
2142+
"clj")))))
21432143

21442144
(defun cljr--aget (map key)
21452145
(cdr (assoc key map)))
@@ -2867,7 +2867,7 @@ Also adds the alias prefix to all occurrences of public symbols in the namespace
28672867
(cljr--ensure-op-supported "find-used-publics")
28682868
(let ((filename (buffer-file-name)))
28692869
(let* ((alias (or alias
2870-
(cljr--prompt-user-for (format "alias for [%s]: " ns))))
2870+
(cljr--prompt-user-for (format "alias for [%s]: " ns))))
28712871
(request
28722872
(cljr--create-msg "find-used-publics"
28732873
"used-ns" ns
@@ -2970,15 +2970,21 @@ Inspect SYMBOL, the thing at point, to find out whether we have
29702970
to create an alias or refer."
29712971
(save-excursion
29722972
(cljr--insert-in-ns ":require")
2973-
(let ((missing (format "%s" missing-symbol))
2974-
(alias? (cljr--qualified-symbol-p symbol)))
2975-
(if alias?
2976-
(cljr--insert-libspec-verbosely (format "[%s :as %s]" missing
2977-
(cljr--symbol-prefix symbol)))
2978-
(if (and (s-contains-p "." missing)
2979-
(s-uppercase? (s-left 1 (cljr--symbol-suffix missing))))
2980-
(cljr--insert-libspec-verbosely (cljr--symbol-prefix symbol))
2981-
(cljr--insert-libspec-verbosely (format "[%s :refer [%s]]"
2973+
(let* ((missing (format "%s" missing-symbol))
2974+
(alias? (cljr--qualified-symbol-p symbol)))
2975+
(cond
2976+
;; defrecord / deftype where the package must be required
2977+
((and (s-contains-p "." missing)
2978+
(s-uppercase? (s-left 1 (cljr--symbol-suffix symbol))))
2979+
(cljr--insert-libspec-verbosely (cljr--symbol-prefix missing)))
2980+
;; Fully qualified symbol
2981+
((and (cljr--qualified-symbol-p symbol)
2982+
(string= (cljr--symbol-prefix symbol) missing))
2983+
(cljr--insert-libspec-verbosely missing))
2984+
(alias?
2985+
(cljr--insert-libspec-verbosely (format "[%s :as %s]" missing
2986+
(cljr--symbol-prefix symbol))))
2987+
(t (cljr--insert-libspec-verbosely (format "[%s :refer [%s]]"
29822988
missing symbol)))))))
29832989

29842990
(defun cljr--add-missing-libspec (symbol candidates)
@@ -2993,8 +2999,8 @@ to create an alias or refer."
29932999
;; In the line below we're assuming that all clojure code
29943000
;; will prefer - over _ when naming namespaces :(
29953001
(progn (cljr--insert-missing-require
2996-
(s-replace "_" "-" (format "%s" missing-symbol))
2997-
missing-symbol)
3002+
symbol
3003+
(s-replace "_" "-" (format "%s" missing-symbol)))
29983004
(cljr--insert-missing-import missing-symbol)))
29993005
((eq type :class) (cljr--insert-missing-import missing-symbol))
30003006
(t (error (format "Uknown type %s" type))))))

features/add-missing-libspec.feature

+8
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,11 @@ Feature: resolve-missing; nrepl middleware response mocked
4343
(:require modular.ring)
4444
(:import modular.ring.WebrequestHandler)
4545
"""
46+
47+
Scenario: Require fully qualified without aliasing #309
48+
Given I call the add-missing-libspec callback directly with mock data to require schema.test
49+
Then I should see:
50+
"""
51+
(ns example.core
52+
(:require schema.test))
53+
"""

features/step-definitions/clj-refactor-steps.el

+6
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@
112112
(list (cljr--make-seeded-hash-table
113113
:name 'modular.ring.WebrequestHandler :type :type)))))
114114

115+
(Given "^I call the add-missing-libspec callback directly with mock data to require schema.test"
116+
(lambda ()
117+
(cljr--add-missing-libspec "schema.test/validate-schemas"
118+
(list (cljr--make-seeded-hash-table
119+
:name 'schema.test :type :ns)))))
120+
115121
(Then "^the file should be named \"\\([^\"]+\\)\"$"
116122
(lambda (file-name-postfix)
117123
(assert (s-ends-with? file-name-postfix (buffer-file-name)) nil "Expected %S to end with %S" (buffer-file-name) file-name-postfix)))

0 commit comments

Comments
 (0)