Skip to content

Commit 2192625

Browse files
committed
Introduce completable-for-cljr-slash?
Fixes #492
1 parent 466822f commit 2192625

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- [#483](https://github.com/clojure-emacs/clj-refactor.el/issues/483): Improve performance of cljr-slash when typing fraction literals.
66
- [#482](https://github.com/clojure-emacs/clj-refactor.el/issues/482): Add missing defgroup form.
77
- [#470](https://github.com/clojure-emacs/clj-refactor.el/issues/470): Choose `deps.edn` over `pom.xml` as project file.
8+
- [#492](https://github.com/clojure-emacs/clj-refactor.el/issues/492): Speed up `cljr-slash` for predictably non-completable inputs.
89
- Introduce `defcustom cljr-insert-newline-after-require` option.
910

1011
## 2.5.1 (2021-02-16)

clj-refactor.el

+55-24
Original file line numberDiff line numberDiff line change
@@ -1983,37 +1983,68 @@ the alias in the project."
19831983
(backward-sexp 1)
19841984
(looking-at-p "[-+0-9]")))
19851985

1986+
(defun completable-for-cljr-slash? (sym)
1987+
(when sym
1988+
(not (null (string-match-p "^\\([a-zA-Z]+[a-zA-Z0-9\\-]*\\)+\\(\\.?[a-zA-Z]+[a-zA-Z0-9\\-]*\\)*$" sym)))))
1989+
1990+
(progn
1991+
(assert (not (completable-for-cljr-slash? nil)))
1992+
(assert (completable-for-cljr-slash? "a"))
1993+
(assert (completable-for-cljr-slash? "a-"))
1994+
(assert (completable-for-cljr-slash? "a2"))
1995+
(assert (completable-for-cljr-slash? "a-2"))
1996+
(assert (completable-for-cljr-slash? "a2-"))
1997+
(assert (completable-for-cljr-slash? "a2.a"))
1998+
(assert (completable-for-cljr-slash? "a2.a-"))
1999+
(assert (completable-for-cljr-slash? "a2.a2"))
2000+
(assert (completable-for-cljr-slash? "a2.a-2"))
2001+
(assert (completable-for-cljr-slash? "a2.a2-"))
2002+
(assert (not (completable-for-cljr-slash? "a2.a.")))
2003+
(assert (not (completable-for-cljr-slash? "a2.2")))
2004+
(assert (not (completable-for-cljr-slash? "a2.2a")))
2005+
(assert (not (completable-for-cljr-slash? "a2.-")))
2006+
(assert (not (completable-for-cljr-slash? "a2.-a")))
2007+
(assert (not (completable-for-cljr-slash? "-")))
2008+
(assert (not (completable-for-cljr-slash? ".")))
2009+
(assert (not (completable-for-cljr-slash? "2")))
2010+
(assert (not (completable-for-cljr-slash? "+")))
2011+
(assert (not (completable-for-cljr-slash? "a/")))
2012+
(assert (not (completable-for-cljr-slash? "a/a")))
2013+
(assert (not (completable-for-cljr-slash? "a/a/"))))
2014+
19862015
;;;###autoload
19872016
(defun cljr-slash ()
19882017
"Inserts / as normal, but also checks for common namespace shorthands to require.
19892018
If `cljr-magic-requires' is non-nil, executing this command after one of the aliases
19902019
listed in `cljr-magic-require-namespaces', or any alias used elsewhere in the project,
19912020
will add the corresponding require statement to the ns form."
19922021
(interactive)
1993-
(insert "/")
1994-
(when-let (aliases (and cljr-magic-requires
1995-
(not (cljr--in-map-destructuring?))
1996-
(not (cljr--in-ns-above-point-p))
1997-
(not (cljr--in-reader-literal-p))
1998-
(not (cider-in-comment-p))
1999-
(not (cider-in-string-p))
2000-
(not (cljr--in-keyword-sans-alias-p))
2001-
(not (cljr--in-number-p))
2002-
(clojure-find-ns)
2003-
(cljr--magic-requires-lookup-alias)))
2004-
(let ((short (cl-first aliases)))
2005-
(when-let (long (cljr--prompt-user-for "Require " (cl-second aliases)))
2006-
(when (and (not (cljr--in-namespace-declaration-p (concat ":as " short "\b")))
2007-
(or (not (eq :prompt cljr-magic-requires))
2008-
(not (> (length (cl-second aliases)) 1)) ; already prompted
2009-
(yes-or-no-p (format "Add %s :as %s to requires?" long short))))
2010-
(save-excursion
2011-
(cljr--insert-in-ns ":require")
2012-
(let ((libspec (format "[%s :as %s]" long short)))
2013-
(insert libspec)
2014-
(ignore-errors (cljr--maybe-eval-ns-form))
2015-
(cljr--indent-defun)
2016-
(cljr--post-command-message "Required %s" libspec))))))))
2022+
(let ((original-input (cider-symbol-at-point)))
2023+
(insert "/")
2024+
(when-let (aliases (and cljr-magic-requires
2025+
(completable-for-cljr-slash? original-input)
2026+
(not (cljr--in-map-destructuring?))
2027+
(not (cljr--in-ns-above-point-p))
2028+
(not (cljr--in-reader-literal-p))
2029+
(not (cider-in-comment-p))
2030+
(not (cider-in-string-p))
2031+
(not (cljr--in-keyword-sans-alias-p))
2032+
(not (cljr--in-number-p))
2033+
(clojure-find-ns)
2034+
(cljr--magic-requires-lookup-alias)))
2035+
(let ((short (cl-first aliases)))
2036+
(when-let (long (cljr--prompt-user-for "Require " (cl-second aliases)))
2037+
(when (and (not (cljr--in-namespace-declaration-p (concat ":as " short "\b")))
2038+
(or (not (eq :prompt cljr-magic-requires))
2039+
(not (> (length (cl-second aliases)) 1)) ; already prompted
2040+
(yes-or-no-p (format "Add %s :as %s to requires?" long short))))
2041+
(save-excursion
2042+
(cljr--insert-in-ns ":require")
2043+
(let ((libspec (format "[%s :as %s]" long short)))
2044+
(insert libspec)
2045+
(ignore-errors (cljr--maybe-eval-ns-form))
2046+
(cljr--indent-defun)
2047+
(cljr--post-command-message "Required %s" libspec)))))))))
20172048

20182049
(defun cljr--in-namespace-declaration-p (s)
20192050
(save-excursion

0 commit comments

Comments
 (0)