Skip to content

Commit 02c7a97

Browse files
[Fix #305] Handle keywords in cljr-slash
Don't call lookup-alias for non namespaced keywords at all when slash is typed. However trigger lookup alias with the leading :: stripped off the prefix if the keyword is namespaced. - fix bug in `cljr--keyword?` so it recognises aliased, `::` keywords
1 parent 236d875 commit 02c7a97

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

clj-refactor.el

+19-11
Original file line numberDiff line numberDiff line change
@@ -2183,9 +2183,10 @@ FEATURE is either :clj or :cljs."
21832183
(defun cljr--magic-requires-lookup-alias ()
21842184
"Return (alias (ns.candidate1 ns.candidate1)) if we recognize
21852185
the alias in the project."
2186-
(let ((short (buffer-substring-no-properties
2187-
(cljr--point-after 'paredit-backward)
2188-
(1- (point)))))
2186+
(let ((short (->> (buffer-substring-no-properties
2187+
(cljr--point-after 'paredit-backward)
2188+
(1- (point)))
2189+
(s-chop-prefix "::"))))
21892190
(unless (or (cljr--resolve-alias short)
21902191
(cljr--is-global-alias short))
21912192
(-if-let* ((aliases (ignore-errors (cljr--get-aliases-from-middleware)))
@@ -2198,6 +2199,12 @@ the alias in the project."
21982199
(-when-let (long (cljr--aget cljr-magic-require-namespaces short))
21992200
(list short (list long))))))))
22002201

2202+
(defun cljr--in-keyword-sans-alias-p ()
2203+
"Checks if thing at point is keyword without an alias."
2204+
(let ((sym (cider-symbol-at-point)))
2205+
(and (cljr--keyword? sym)
2206+
(not (s-matches-p "::.+" (cljr--symbol-prefix sym))))))
2207+
22012208
;;;###autoload
22022209
(defun cljr-slash ()
22032210
"Inserts / as normal, but also checks for common namespace shorthands to require.
@@ -2210,6 +2217,7 @@ form."
22102217
(-when-let (aliases (and cljr-magic-requires
22112218
(not (cider-in-comment-p))
22122219
(not (cider-in-string-p))
2220+
(not (cljr--in-keyword-sans-alias-p))
22132221
(clojure-find-ns)
22142222
(cljr--magic-requires-lookup-alias)))
22152223
(let ((short (first aliases)))
@@ -3491,7 +3499,7 @@ argument of a `reduce', the defn will take two arguments.
34913499
34923500
See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-create-fn-from-example"
34933501
(interactive)
3494-
(while (cljr--is-keyword? (car (cljr--extract-sexp-as-list)))
3502+
(while (cljr--keyword? (car (cljr--extract-sexp-as-list)))
34953503
(paredit-backward-up))
34963504
(let* ((sexp-forms* (cljr--extract-sexp-as-list))
34973505
(fn-name (car sexp-forms*))
@@ -3572,7 +3580,7 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-create-fn-from-e
35723580
(defun cljr--create-fn-from-update (args path)
35733581
(let ((keyfn (cadr args)))
35743582
(cljr--insert-example-fn (cider-symbol-at-point)
3575-
(if (cljr--is-keyword? keyfn)
3583+
(if (cljr--keyword? keyfn)
35763584
(list (s-chop-prefix ":" keyfn))
35773585
(list 0))
35783586
path)))
@@ -3582,7 +3590,7 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-create-fn-from-e
35823590
(paredit-backward-down)
35833591
(cider-symbol-at-point))))
35843592
(cljr--insert-example-fn (cider-symbol-at-point)
3585-
(if (cljr--is-keyword? last-path-entry)
3593+
(if (cljr--keyword? last-path-entry)
35863594
(list (s-chop-prefix ":" last-path-entry))
35873595
(list 0))
35883596
path)))
@@ -3603,7 +3611,7 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-create-fn-from-e
36033611
(making-comparator? (and (string= fn-name (cadr args))
36043612
(= 3 (length args))))
36053613
(param-name (if making-comparator?
3606-
(when (cljr--is-keyword? (car args))
3614+
(when (cljr--keyword? (car args))
36073615
(s-chop-prefix ":" (car args)))
36083616
(-when-let (coll-name (cljr--guess-param-name (-last-item args)))
36093617
(singularize-string coll-name)))))
@@ -3642,8 +3650,8 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-create-fn-from-e
36423650
(buffer-substring (point-min) (point-max)))
36433651
s))
36443652

3645-
(defun cljr--is-keyword? (s)
3646-
(s-matches? "^:[^0-9:[{(\"][^[{(\"]*$"
3653+
(defun cljr--keyword? (s)
3654+
(s-matches? "^::?[^0-9:[{(\"][^[{(\"]*$"
36473655
(s-replace "\n" " " s)))
36483656

36493657
(defun cljr--symbol? (s)
@@ -3745,7 +3753,7 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-create-fn-from-e
37453753
(paredit-forward 3)
37463754
(paredit-backward-down)
37473755
(cider-symbol-at-point))))
3748-
(when (cljr--is-keyword? last-path-entry)
3756+
(when (cljr--keyword? last-path-entry)
37493757
(s-chop-prefix ":" last-path-entry))))
37503758

37513759
(defun cljr--find-param-name-from-get (form)
@@ -3754,7 +3762,7 @@ See: https://github.com/clojure-emacs/clj-refactor.el/wiki/cljr-create-fn-from-e
37543762
(paredit-forward 2)
37553763
(cljr--skip-past-whitespace-and-comments)
37563764
(cljr--extract-sexp))))
3757-
(when (cljr--is-keyword? key)
3765+
(when (cljr--keyword? key)
37583766
(s-chop-prefix ":" key))))
37593767

37603768
(defun cljr--insert-example-fn (name args path)

features/magic-requires.feature

+23
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,29 @@ Feature: Magic requires
4646
(util/get-last-sexp)
4747
"""
4848

49+
Scenario: Require is inserted automagically for namespaced keyword
50+
When I insert:
51+
"""
52+
(ns cljr.core)
53+
54+
(::util)
55+
"""
56+
And the cache of namespace aliases is populated
57+
And I place the cursor after "util"
58+
And I start an action chain
59+
And I type "/"
60+
And I type "refactor-nrepl.util"
61+
And I press "RET"
62+
And I type "some-keyword"
63+
And I execute the action chain
64+
Then I should see:
65+
"""
66+
(ns cljr.core
67+
(:require [refactor-nrepl.util :as util]))
68+
69+
(::util/some-keyword)
70+
"""
71+
4972

5073
Scenario: If alias exists nothing happens
5174
When I insert:

0 commit comments

Comments
 (0)